Pages: [1]   Go Down
Author Topic: Sending byte streams over Serial  (Read 626 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)

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.
« Last Edit: June 04, 2009, 10:09:11 am by ArduinoBerlin » Logged

Toronto, ON
Offline Offline
Full Member
Karma: 10
Posts: 233
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You should just use:

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

This is already in the Print class.

Serial.write(mybuffer, 10);


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

Global Moderator
Offline Offline
Brattain Member
Karma: 309
Posts: 26512
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Pages: [1]   Go Up
Jump to: