Serial.write Binary Values for Faster Communication

Hi,
I have been using arduino since 2 years, To communicate with arduino using serial communication I was using Serial.println("X:0;Y:1;Z:10") kind of strings so far (this is just for example, my actual string has many more diff types of parameters).

Recently I came across Serial.write binary concept using array and bitwise shift operators (as in example below) , which is very new to me as I have never been a c/c++/ java programmer.

int16_t sensorValue = analogRead(A0);
Serial.write('$');
Serial.write((int8_t)(sensorValue >> 8)); Serial.write((int8_t)(sensorValue & 0xFF));
Serial.write('\n');

I found this type of communicaton very fast and structured, I can decode out put of above code in my vb.net application as well.

Now the Actual Problem.
This code is converting "int16_t", what I want is to convert "int/Double/float/int8_t,etc." type of value in similar fashion and write to serial port and read it from my vb.net program.

So It would be great help if some one give me example of both arduino and vb.net / c#.
I tried few documents but I was unable to understand this, so if any simple explanation is welcome too.

Please Help me out.

Sending (e.g.) integers as a 2-byte binary value clearly reduces the amount of data compared to sending (e.g.) 12345 as a a string of 5 characters.

However all serial communication takes place as a series of bytes, and the order of the two bytes in an int matters. The Arduino uses the little-endian system.

A simple way to send values as binary data is to use a struct and an union. A struct can create a data element with several variables, each with a different type if necessary. A union is a method of viewin the same data in two different ways. Suppose the struct occupies 23 bytes, then the union could be a byte array with 23 bytes. Use Serial.write() to send the byte array.

And when binary data is received in the Arduino write the bytes into a byte array (perhaps a different one) and it can be accessed as the variables in the struct with which it is a union.

This Python Binary Data demo illustrates what I am saying.

...R

Robin2:
Sending (e.g.) integers as a 2-byte binary value clearly reduces the amount of data compared to sending (e.g.) 12345 as a a string of 5 characters.

However all serial communication takes place as a series of bytes, and the order of the two bytes in an int matters. The Arduino uses the little-endian system.

A simple way to send values as binary data is to use a struct and an union. A struct can create a data element with several variables, each with a different type if necessary. A union is a method of viewin the same data in two different ways. Suppose the struct occupies 23 bytes, then the union could be a byte array with 23 bytes. Use Serial.write() to send the byte array.

And when binary data is received in the Arduino write the bytes into a byte array (perhaps a different one) and it can be accessed as the variables in the struct with which it is a union.

This Python Binary Data demo illustrates what I am saying.

...R

Thank you sir,
Your example showed me the whole new (more structured, advanced way) of PC -> Arduino communication.
But in my scenerio / problem, I want to send data efficiently from ARduino -> PC (vb.net / C# client),
Which I can't figure out so far from your example...., still trying to get some clue from it if I can accomplish my task

Thanks again. Please correct me If I am wrong.

You can use the technique in either direction.

In my Python code it imports the struct library which includes the pack function. There is also an unpack function

I have not used Windows for years but I'm sure there are equivalent capabilities in VB and C#

...R

Hello and welcome,

Serial.write(buf, len) allows to send any data types:

Serial.write( (uint8_t *) &x, sizeof( x ) );

Where x is of type int, float, whatever... It could even be an array, no problem :smiley_cat:

guix:
Hello and welcome,

Serial.write(buf, len) allows to send any data types:

Serial.write( (uint8_t *) &x, sizeof( x ) );

Where x is of type int, float, whatever... It could even be an array, no problem.

This assumes the sizeof() operator works for every x which is often the case but not e.g. if x points to dynamic memory or is the address of an array.

1 Like

Well you are right Rob, but in my opinion pointers aren't considered a data type :stuck_out_tongue:

guix:
Hello and welcome,

Serial.write(buf, len) allows to send any data types:

Serial.write( (uint8_t *) &x, sizeof( x ) );

Where x is of type int, float, whatever... It could even be an array, no problem :smiley_cat:

Hi,
Thanks a lot.....
It seems Your Single line of code has solved my many problems :-),
It is working fine,
I Can't find any option to mark Answer, I have added one "karma", I hope its a +ve indicator.

Robin2:
You can use the technique in either direction.

In my Python code it imports the struct library which includes the pack function. There is also an unpack function

I have not used Windows for years but I'm sure there are equivalent capabilities in VB and C#

...R

I will definetly try and check in my languages.
As of now my problem has been solved by the suggestion from the above post of "guix".
Thanks you.