Sending byte streams over Serial

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:

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

Added to Print.cpp:

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.

You should just use:

void Print::write(const uint8_t *buffer, size_t size)

This is already in the Print class.

e.g.

Serial.write(mybuffer, 10);

b

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

)I missed this one first time too)

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.