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.
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.
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.
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.
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
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
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.