I have int arr[50][2]; and I need to send it through Serial. But I don't know how to do it. is it possible?

  • In what format? binary data or readable ascii? seperated with comma's?
    – Jot
    Jun 1 '18 at 20:38
  • @Jot Is it possible to get the same array on another side?
    – JVic
    Jun 1 '18 at 20:41
  • 3
    No, unless you make code that can do that. The most common way is to use Serial.print and transmit each element of the array (that is 100 times the Serial.print). You could transmit a single binary block of data, but the computer might have the MSB at the lower byte and the Arduino at the higher byte or vice versa. You need a very accurate description of the data which is transferred. It can be defined as a single block of 200 bytes consisting of 2 byte signed integers with MSB first with start byte and end byte and a crc checksum.
    – Jot
    Jun 1 '18 at 20:45
  • 1
    I forgot to mention to select a high baudrate. Not 9600 but 115200 or higher. The output tx buffer is 64 bytes, and if the tx buffer is filled, the Serial.print will wait and that will delay the sketch.
    – Jot
    Jun 1 '18 at 21:35
  • Send it to what through serial? A PC, another arduino, a printer, something else?
    – Craig
    Jun 1 '18 at 21:46

In the comment you write that the receiving end is an Arduino too. Then you can send the data in binary form.

To send the array:

Serial.write((byte*) arr, sizeof(arr));
  • (byte*) is cast of the memory location of arr to a byte array
  • sizeof returns the size of the array in bytes

To receive the binary data you must know the size. Here we read 100 16-bit integers:

Serial.readBytes((byte*) arr, 100 * sizeof(short));

Function readBytes is one of the 'timed' functions of Stream. It waits for the next byte (until timeout).

If one Arduino is 8-bit and the second is 32-bit, then use the type short instead of int, because the size of type int is different, but the type short is 16 bit long on all Arduinos.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.