Pages: [1]   Go Down
Author Topic: Sending byte streams over Serial  (Read 536 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
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:
Code:
void printRaw(uint8_t b[], int len);

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

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

You should just use:

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

This is already in the Print class.

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

b

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


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

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

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