Serial.write error when sending an array

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

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

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

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

  Serial.write( (uint8_t*)serialArrayOne, sizeof(serialArrayOne) );

This defines serialArrayOne as a "buffer"...

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

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

I know this is an old post.

but does someone know how you can read it from the serial port so it will be placed in an array again.
I use xbees for the communication and I'm looking for a way to send multiple parameters in 1 massage.
So I was thinking an array would be ideal to store each parameter.

After this post I know am able to send an array over serial connection, but I still do not have a way to read it in my second arduino ( as an array) so I can use those parameters that were placed in the array at the transmitter side

but does someone know how you can read it from the serial port so it will be placed in an array again.

"it"? What is "it" that you want to read from the serial port?

In general, you'll have some code that looks like:

  array[index] = Serial.read();

The type of array depends on what "it" is. How to use the array, and how many bytes you need to wait for/read, also depend on what "it" is.

with "it" I meant the array/values.

So, the values get printed each at a time?
first "1", then "2", then "3", ......
(if we use the given array)

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

but I still do not have a way to read it in my second arduino ( as an array) so I can use those parameters that were placed in the array at the transmitter side

We still don't know how you are sending the data. As ASCII strings or as binary values.

How to read, and use, the data depends on how you send it.

Serial Input Basics - updated has a lot of information on sending and receiving serial data. If that doesn't answer all of your questions, post the sending code and the receiving code, and we can help you further.

I have switched my method of sending. I know Serial.write (which means it is an 8-bit binary value if i'm not mistaken) all the parameters behind each other.
If i am right it gets send to the serial buffer where it is saved then.
So I read the serial port and place it in an array, after that i read the port again and place it in the second spot of that array. I haven't been able to fully test the code so I'm not sure if it is functional.

I am also not a 100% sure if I correctly placed the value in the array. It would be really helpfull if you could take a quick look.

Sender:

int V1 = 1;
int V2 = 55;
int V3 = 158;

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

void loop() {

  //Serial.write("A");
  Serial.write(V1);
  Serial.write(V2);
  Serial.write(V3);
  delay(50);
}

Receiver:

char myArray[3] = {0, 0, 0,};
int sentDat1 = 0;
int sentDat2 = 0;
int sentDat3 = 0;

void setup() {
  pinMode(2, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(10, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {

    /* first byte */
    sentDat1 = Serial.read();  //gets one byte from serial buffer
    myArray[0] = sentDat1;

    /* second byte */
    sentDat2 = Serial.read();  //gets one byte from serial buffer
    myArray[1] = sentDat2;

    /* third byte */
    sentDat3 = Serial.read();  //gets one byte from serial buffer
    myArray[2] = sentDat3;

    delay(1000);

  }
}//END OF LOOP

It would be really helpfull if you could take a quick look.

It would be really helpful if YOU read the documentation for Serial.write(). The write() method does NOT expect an int. So, don't pass it an int.

  if (Serial.available() > 0) {

If there is one or more bytes in the buffer, how many can you read? THREE is the WRONG answer.

You do understand, I hope, that the transmission of serial data does NOT follow the UPS delivery model, where they guarantee to deliver your package or return it to you. It follows the USPS model, where they guarantee to try to deliver you package, undamaged. If they can't, or if gets damaged, it is simply thrown away, and you are NOT told.

Sending binary data IS faster, but you have NO way of keeping it in sync. If you are sending R, G, B values, for instance, and the G byte gets lost, you'll apply the blue component to the green LED and the next red component to the blue LED, which will cause the color to be all wrong. And there is nothing you can send that is not a valid value, so you have no way of keeping in sync or of knowing that you are out of sync.

Sending ASCII data results in more data, but with lots of ways to know that serial data got lost/corrupted, so you know whether to use the data, or not.

PaulS:
It would be really helpful if YOU read the documentation for Serial.write(). The write() method does NOT expect an int. So, don't pass it an int.

the serial communication is with Xbees (series1). i can only use Serial.write(). I have tried to use Serial.print() or Serial.println() but those don't send the appropriate value

the serial communication is with Xbees (series1). i can only use Serial.write(). I have tried to use Serial.print() or Serial.println() but those don't send the appropriate value

You CAN use Serial.print() and Serial.println() with the XBees. If you are getting "wrong values" it is your code that is wrong.