Pages: [1]   Go Down
Author Topic: Serial.write error when sending an array  (Read 876 times)
0 Members and 1 Guest are viewing this topic.
NZ
Offline Offline
Jr. Member
**
Karma: 1
Posts: 91
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.write(serialArrayOne,8);
}


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

Cheers
Kim
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 197
Posts: 12740
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Code:
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:
 Serial.write( (uint8_t*)serialArrayOne, sizeof(serialArrayOne) );

This defines serialArrayOne as a "buffer"...

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

NZ
Offline Offline
Jr. Member
**
Karma: 1
Posts: 91
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
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.).
Logged

Pages: [1]   Go Up
Jump to: