0

I am trying to pass char array form c++(client) to python (server). I have made an interface using winsock I tested it by hello world string and it works. But in reality I need to pass floats from c++ side to python. (3 floats lets say x,y,z). These floats are constantly updated so the code I am talking about is in while loop and runs forever. In c++ I am processing real time data captured from IMU sensor and these needs to be further processed in Python. I tried to convert float values into char array:

char zarray[20];
sprintf(zarray, "%f", z_angle);// z_angle the float I need to pass
char *c[]={zarray};

Then send it to python:

iResult=send(sock,*c, (int)strlen(*c),0);

The output I am getting in python looks like this: 0.1152720.1152720.115272

I have no clue why is this happening and what kind of format is this. I tried to empty the char *c after:

iResult=send(sock,*c, (int)strlen(*c),0);
*c[0] = 0;

but it did not help. Does anyone know why is this happening?? Or are there any other ways I can pass floats from c++ to python using winsock??

Thank you

3 Answers 3

1

You say you are sending characters representing floats in a loop over a TCP connection. A TCP connection is a STREAM. That means that you may get more than one sent packet in a single receive, or you could need many receive for a single send.

You must use a higher level protocol to clearly declare what the sent bytes mean and to delimit them. The least you could do is to separate your floats with blanks (or even better with newlines):

sprintf(zarray, "%f\n", z_angle);

That way, different floats will come on different lines Python side.

Anyway, the way you use an array of pointers seem overcomplicated here. Just use:

iResult=send(sock,zarray, (int)strlen(zarray),0);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. It works using sprintf(zarray, "%f\n", z_angle);
Thank you. It works using sprintf(zarray, "%f\n", z_angle) .. But I need to send x_angle, y_angle too. I tried to convert these angles to strings concatenate them with "!"( so I can split the string on python side) and then convert them to char. x_angle,y_angle,z_angle are updated simultaneously and I need to send them together. This is what I am getting in python : ´õ It does not work. Is there any clever way to pass 3 floats to the server using TCP.
0

Instead of passing array you should pass JSON file

Comments

0

What do you want to do on the Python side with the value? Is it important to use it as a float type, or are you simply after the value of the float? Parsing the float value to string and sending it means you will need to parse it from string to float on the python side if you want to use the float as a float variable.

Are both the c++ and python applications running on the same platform?

When sending floats over a network you need to consider endianness as the different platforms might handle floats differently.

Also, why copy the zarray to c variable? You can simply pass the zarray variable to the send function:

iResult=send(sock,zarray, (int)strlen(zarray),0);

You might want to have a look at this link as well ->

Sending floating point number from server to client

2 Comments

OP does not send binary floats but string encoded ones. Endianness is not a problem here.
Sure, but OP does ask about other ways of sending floats over a network "Or are there any other ways I can pass floats from c++ to python using winsock??"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.