Serial print, how to print full array?

Hello,

I'm trying to print the 6 bytes from buffer over to the serial monitor of Arduino, but I'm only getting one byte.

Tried searching the web, but only came up with Serial.write(buffer,6);
With this nothing shows in the serial monitor.

Can somebody give me some hints?
Mega2560 btw...

byte buffer[6];
byte MINUS1[6] =  {0x44,0x06,0x02,0x00,0x02,0xFF}; //example data
byte in[6];

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

void loop() {
  Serial1.readBytesUntil(0xFF,buffer,6);
  Serial.println(buffer[6], HEX);
  //Serial.write(MINUS1,6);
  Serial1.flush();
}

You need a loop for that:

for (int i = 0; i < 6; i++) Serial.print(buffer[i], HEX);
Serial.println();

EDIT: “i<=6” should be “i<6” or “i<sizeof(buffer)” :slight_smile:

buffer[6]

that is a pointer to the 7th element of your 6 byte array, or put another way, outside the bounds of your array (which can be addressed at element zero through five).

“i<sizeof(buffer)”

That gives the number of bytes in the array. If the elements are bytes that works. More robust is “i<sizeof(buffer)/sizeof(buffer[0])”. That will return the number of elements independent of the data type,

groundFungus:
That give the number of bytes in the array. If the elements are bytes that works. More robust is “i<sizeof(buffer)/sizeof(buffer[0])”. That will return the number of elements independent of the data type,

Yep, but even better would be a constant / define which declares the size of the array :slight_smile:

If you later change the size of the array you must remember to change the constant.

groundFungus:
If you later change the size of the array you must remember to change the constant.

If you are dynamically allocating / reallocating an array, you most likely will have a variable with the size of the array - otherwise you would be a nutter! :wink:

Serial1.readBytesUntil(0xFF,buffer,6);

The readBytesUntil function blocks until all the read is complete. Serial data comes in slowly so the processor is tied up while waiting. A non-blocking approach to serial receive is shown in Robin2’s serial input basics thread.

Danois90:
You need a loop for that:

for (int i = 0; i < 6; i++) Serial.print(buffer[i], HEX);

Serial.println();




EDIT: "i<=6" should be "i<6" or "i<sizeof(buffer)" :)

Thankyou! :slight_smile:

BulldogLowell:

buffer[6]

that is a pointer to the 7th element of your 6 byte array, or put another way, outside the bounds of your array (which can be addressed at element zero through five).

WOW, you just blew my mind! I didn’t think of it that way! I thought that it was one continuous string of bytes.

Can I successfully compare buffer with MINUS1? like:

if(buffer == PLUS1){
    digitalWrite(13, HIGH);
  }

It is a continuous array. But the first index is 0 and the last one is 5 (for array size 6).

Snake____1:
Can I successfully compare buffer with MINUS1? like:

if(buffer == PLUS1){

digitalWrite(13, HIGH);
 }

No, since that would compare the memory location, not the contents of the arrays. You need a for loop or memcmp for such a compare.

Danois90:
No, since that would compare the memory location, not the contents of the arrays. You need a for loop or memcmp for such a compare.

or the standard c string library

strcmp()
strstr()

Danois90:
No, since that would compare the memory location, not the contents of the arrays. You need a for loop or memcmp for such a compare.

You guys are magicians! Works like a charm now :slight_smile:

byte buffer[6];
byte MINUS1[6] =  {0x44,0x06,0x02,0x00,0x02,0xFF}; //example data
byte PLUS1[6] =   {0x44,0x06,0x04,0x00,0x02,0xFF};
byte in[6];

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(13, OUTPUT);
}

void loop() {
  Serial1.readBytesUntil(0xFF,buffer,6);
  Serial.print(buffer[0], HEX);
  Serial.print(buffer[1], HEX);
  Serial.print(buffer[2], HEX);
  Serial.print(buffer[3], HEX);
  Serial.print(buffer[4], HEX);
  Serial.println(buffer[5], HEX);
  
  if(memcmp (buffer, PLUS1, sizeof(buffer)) == 0){
    digitalWrite(13, HIGH);
  }
  else if(memcmp (buffer, MINUS1, sizeof(buffer)) == 0){
    digitalWrite(13, LOW);
  }

  Serial1.flush();
  buffer[0] = 0x00;
  buffer[1] = 0x00;
  buffer[2] = 0x00;
  buffer[3] = 0x00;
  buffer[4] = 0x00;
  buffer[5] = 0x00;
}

groundFungus:
The readBytesUntil function blocks until all the read is complete. Serial data comes in slowly so the processor is tied up while waiting. A non-blocking approach to serial receive is shown in Robin2's serial input basics thread.

Might take the serial receive function into consideration later, thanks!

BulldogLowell:
or the standard c string library

strcmp()

strstr()

Useless in this scenario; No null termination and a buffer which may contain null/zero as a value :slight_smile: