ArduinoCore-API icon indicating copy to clipboard operation
ArduinoCore-API copied to clipboard

[Feature request] Add to Serial.write() the hability of sending byte structs

Open q2dg opened this issue 10 years ago • 4 comments

I mean...Serial.write() has already the hability of sending byte arrays in the form Serial.write(myarray, sizeof(myarray));. Why not doing the same with structs like struct { byte x; byte y;} mystruct; ?? Thanks!

q2dg avatar Mar 29 '15 22:03 q2dg

I guess you already can, by just treating a struct as an array of bytes. I believe something like this should work:

struct { byte x; byte y} var;

Serial.write((uint8_t*)&var, sizeof(var));

matthijskooijman avatar Mar 30 '15 16:03 matthijskooijman

You're right, it works!! The expression is a little ugly...but I suppose it can't be written in another (more friendly) manner... Well, it's ok, anyway. Thanks a lot!!! I close the issue.

q2dg avatar Mar 31 '15 10:03 q2dg

Serial.write really should have been defined to take a "const void *". Sadly, it's probably far too late to change now. Unless someone can think of a crafty way with overloading, so old code overriding Print still works?

PaulStoffregen avatar Mar 31 '15 10:03 PaulStoffregen

I reopen the issue to give the opportunity to answer Paul's question

q2dg avatar Mar 31 '15 11:03 q2dg