Go Down

Topic: Serial.write error when sending an array (Read 1 time) previous topic - next topic


the following code has a compiler error, which I don't quite understand

Code: [Select]

int serialArrayOne[]  = {1,2,3,4,5,6,7,8};

void setup()

void loop()

error is:
sketch_jan15a.cpp: In function 'void loop()':
sketch_jan15a.cpp:19:32: error: no matching function for call to 'HardwareSerial::write(int [8], int)'
/usr/share/arduino/hardware/arduino/cores/arduino/HardwareSerial.h:58:20: note: candidates are: virtual size_t HardwareSerial::write(uint8_t)
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:50:20: note:                 virtual size_t Print::write(const uint8_t*, size_t)
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:49:12: note:                 size_t Print::write(const char*)

The manual says
Serial.write(buf, len)
buf: an array to send as a series of bytes
len: the length of the buffer

Anyone care to enlighten me


Coding Badly

From the perspective of Serial.write, a "buffer" is an array of bytes (uint8_t or byte).  This...

Code: [Select]
int serialArrayOne[]  = {1,2,3,4,5,6,7,8};

...is an array of integers (int).

This sends the array as you have defined it...

Code: [Select]
 Serial.write( (uint8_t*)serialArrayOne, sizeof(serialArrayOne) );

This defines serialArrayOne as a "buffer"...

Code: [Select]
byte serialArrayOne[]  = {1,2,3,4,5,6,7,8};


Cheers for that.
Coming from a 8bit assembly world, C can be a bit hard to to interpret at time
int used to be a byte for me

getting there

Nick Gammon

Code: [Select]
Serial.write( (uint8_t*)serialArrayOne, sizeof(serialArrayOne) );

That, however, is unlikely to send your ints in a way you expect. It is "casting" them to a different data type.

If you want to send those ints (16-bit signed numbers) as such, it would be better in your case to set up a loop, and in that loop do a Serial.print of each element, with something in-between (like a space, comma, newline, etc.).
Please post technical questions on the forum, not by personal message. Thanks!

More info:

Go Up