Sending Float value through socket

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Sending Float value through socket

1,866 Views
prashantdev
Contributor III

Hi,

I'm trying to send a structured data from my Source system to Target system through UDP protocol. Structured data contains some int and some float values. I am able to receive int value properly but I am receiving float values as 0 (instead of -0.323114). Source and Target system details are as below :

Source System :

Controller : MK61FX512VMJ12

Protocol   : UDP

Target System :

Processor : VMPC7 (Power PC 750GX ) from Kontron

Operating System : Lynx OS 4.0

Please help.

3 Replies

1,348 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

Can you use sprintf() to format the structure and copy each variable to the string S, then transfer the string s.

for example :

char str[];

sprinf(&str[0],"%d,%d,%f\n",intVar0,IntVar1,float f);

then transfer the char array str[] with udp protocol.

Another option,I think you can define another structure with the structure like:

typedef struct

{

unsigned int intVar0,

unsugned int intVar1,

char floatVar[]

}intFloatSVar;

struct intFloatSVar;

sprintf(&intFloatSVar.floatVar1[0],"%f\n", float variable);

This is a workaround, hope it can help you.

BR

XiangJun Rong

1,348 Views
prashantdev
Contributor III

@ XiangJun Rong

Hi,

Thank  u for the quick reply.

But I for some reason I have to send float value without conversion to any other format (not even int ). Plz suggest if it can be done this way.

0 Kudos

1,348 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi

This is my idea that you have to convert the structure to an char array, then transfer the char array into structure again.

Can you accept the following solution?

typedef struct

{

unsigned int intVar0,

float f1;

}intFloatSVar;

struct intFloatSVar p1;

char array[100];

p1.intVar0=10;

p1.f1=1.23;

memcpy(array,&p1,sizeof(intFloatSVar));

sendto(socket, array,sizeof(intFloatSVa),...);

On the receiver side,

recvfrom(socket,temp,sizeof(intFloatSVar),...);

memcpy(&p1,temp,sizeof(intFloatSVar));

you can recover the structure.

maybe the other people have good idea.

Hope it can help you.

BR

Xiangjun Rong

0 Kudos