Send array of float with TcpClient

As per title, I am trying to send an array of float using TcpClient. Is this possible? should I convert it into bytes before? if so, how?
i’ve tried both

client.write(&packet, sizeof(count));

but neither works.

any help would be very much appreciated :slight_smile:

Hi @marcodat

Have you looked into using a union in C? That is way for floats and bytes to be accessed in the memory locations.

A simple client.write((const uint8_t*)array, sizeof(array)) should do too.

But when you say “neither works”, the resulting error message is the first help you get.
And posting the exact error message(s) would be good practice too :wink:

1 Like

Thanks for replying.
@bko I don’t understand how unions could help? I use the array of float because ultimately I want to send a Quaternion.

@ScruffR from the error line

test2.cpp:125:37: error: no matching function for call to 'TCPClient::write(float (*)[5], unsigned int)'

test2.cpp:125:37: note: candidates are:
In file included from ./inc/application.h:56:0,
                 from test2.cpp:2:
../wiring/inc/spark_wiring_tcpclient.h:47:17: note: virtual size_t TCPClient::write(uint8_t)

I should have understood unit8_t was the way to go.
is memcpy() a good way to convert floats into unit8_t? I

You could use memcpy(), but there is no need since you already have a byte array filled with your data in memory, just tell the function the pointer you provide is pointing to a byte array.
That’s exactly what my suggested type cast does.

For example, you have a float myFArray[5] and by doing this (uint8_t*)myFArray you “fool” the compiler into beliefing the pointer points to an uint8_t[]. And since sizeof(myFArray) returns the number of bytes occupied by your float array (5 elements with 4 byte each = 20), you’ll pass all the bytes making up all your floats into the function.

1 Like

amazing, thanks a lot. will try it now

@ScruffR it did the job, thank you.

Would you know how I have to deal with photon’s endianness?

There are multiple ways on the Photons side and on the remote side, but before you bother with that, first check if both sides don’t already agree there :wink:

If you really have to convert, do it on the side you are most comfortable with.

1 Like