var myVal = 20000;var bytes = ;bytes = (myVal & 0xFF00) >> 8;bytes = (myVal & 0x00FF);
I have thought of the following approach: If I send it in raw form:red = reading 1, green = reading 2... [HiByte] [LoByte] [HiByte] [Lobyte] [HiByte] [LoByte] [HiByte] [Lobyte] ...The computer might interpret this as:... [HiByte] [LoByte] [HiByte] [LoByte] [HiByte] [LoByte] [HiByte] [LoByte] ...Am I right?
Why would there be interleaving of the bytes?.
Have a look at the 3rd example in Serial Input Basics. With minor modification it should work with your data.
My suggestion is that you reduce the range to 0 to 0xFD and use FE as a start marker and FF as an end marker.
// Sync byte. Should be anything over 0x7F buff = 0xFF; // Convert first reading to byte array. [MSB] [LSB] buff = (byte) ((reading0 & 0x3F80) >> 7); buff = (byte) (reading0 & 0x007F); // Convert second reading to byte array [MSB] [LSB] buff = (byte) ((reading1 & 0x3F80) >> 7); buff = (byte) (reading1 & 0x007F);
Because serial data transmission is NOT guaranteed to happen without corruption now and then.
I have to admit, my understanding of low level computer communication protocols is very bad. Though I don't know why this would not happen. Secondly, I guess this enables the use of a variable amount of sensors, without prior knowledge of the receiving device.
Well, I am stuck at the point on how I should calculate the least significant byte with the reduced range and the carry over to the most significant byte. I guess this should in principle be a simple "base converter", right? Instead of using base 256, you use base 253. My attempts to write a code that does this in c weren't successfull so far..