Go Down

Topic: Sending byte streams over Serial (Read 645 times) previous topic - next topic

ArduinoBerlin

Jun 04, 2009, 05:08 pm Last Edit: Jun 04, 2009, 05:09 pm by ArduinoBerlin Reason: 1
Hi, I have an external device connected via serial (hardware serial on tx pin) to the Arduino and want to send c structs as byte streams over the connection. The built-in Serial.print() function fails at this as it expects arrays of ASCII-formatted chars. It won't print an array of uint8_t.

In order for this to work, I hat to add the following to Print.cpp in the Arduino Core:

Added to Print.h:
Code: [Select]

void printRaw(uint8_t b[], int len);


Added to Print.cpp:
Code: [Select]

void Print::printRaw(uint8_t  b[], int len)
{
while(len--)
      this->write(*b++);
     
}


Is there any way this could be included in the nex Arduino release? I think a feature for sending byte arrays is sorely missed in the Serial.print() function.

bhagman

You should just use:

Code: [Select]
void Print::write(const uint8_t *buffer, size_t size)

This is already in the Print class.

e.g.
Code: [Select]
Serial.write(mybuffer, 10);

b

(yes, this topic is almost 2 months old, but no one answered)

AWOL

)I missed this one first time too)

Quote
want to send c structs as byte streams over the connection.


Be aware that this may not always work as you expect for two main reasons:
1) Different architecture machines may have different endianness, and
2) even similarly endian machines may have different alignment requirements resulting in unseen packing bytes in structures, so always check that the "sizeof" operator returns the same value on both machines.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up