Can't send varying int from arduino to OF with ofxSerial


I’m trying to send an array of int from Arduino to OF (Arduino last version, as far as OF, I’ve tried with Uno and Nano Every).
First I’m trying to send one integer.
My code for the Arduino is pretty much the Slip packet serial example of ofxSerial.

#include <PacketSerial.h>

// By default, PacketSerial automatically wraps the built-in `Serial` object.
// While it is still possible to use the Serial object directly, it is
// recommended that the user let the PacketSerial object manage all serial
// communication. Thus the user should not call Serial.write(), Serial.print(),
// etc. Additionally the user should not use the serialEvent() framework.
// By default, SLIPPacketSerial uses SLIP encoding and has a 256 byte receive
// buffer. This can be adjusted by the user by replacing `SLIPPacketSerial`
// with a variation of the `PacketSerial_<SLIP, SLIP::END, BufferSize>` template
// found in PacketSerial.h.
SLIPPacketSerial myPacketSerial;

void setup()
  // We begin communication with our PacketSerial object by setting the
  // communication speed in bits / second (baud).

  // If we want to receive packets, we must specify a packet handler function.
  // The packet handler is a custom function with a signature like the
  // onPacketReceived function below.

void loop()
  // Do your program-specific loop() work here as usual.

  // The PacketSerial::update() method attempts to read in any incoming serial
  // data and emits received and decoded packets via the packet handler
  // function specified by the user in the void setup() function.
  // The PacketSerial::update() method should be called once per loop(). Failure
  // to call the PacketSerial::update() frequently enough may result in buffer
  // serial overflows.

  // Check for a receive buffer overflow (optional).
  if (myPacketSerial.overflow())
    // Send an alert via a pin (e.g. make an overflow LED) or return a
    // user-defined packet to the sender.
    // Ultimately you may need to just increase your recieve buffer via the
    // template parameters (see the

// This is our handler callback function.
// When an encoded packet is received and decoded, it will be delivered here.
// The `buffer` is a pointer to the decoded byte array. `size` is the number of
// bytes in the `buffer`.
void onPacketReceived(const uint8_t* buffer, size_t size)
  // In this example, we will simply reverse the contents of the array and send
  // it back to the sender.

  // Make a temporary buffer.
  uint8_t tempBuffer[size];

  // Copy the packet into our temporary buffer.
  memcpy(tempBuffer, buffer, size);

  // Reverse our temporaray buffer.
  reverse(tempBuffer, size);

  // Send the reversed buffer back to the sender. The send() method will encode
  // the whole buffer as as single packet, set packet markers, etc.
  // The `tempBuffer` is a pointer to the `tempBuffer` array and `size` is the
  // number of bytes to send in the `tempBuffer`.
  //myPacketSerial.send(tempBuffer, size);
  byte sizeB = 2;
  //Envoi des données
  uint8_t tempBuffer2[sizeB];
  int data = random(1000);
  tempBuffer2[0] = highByte(1600);
  tempBuffer2[1] = lowByte(1600);
  myPacketSerial.send(tempBuffer2, sizeB);

// This function takes a byte buffer and reverses it.
void reverse(uint8_t* buffer, size_t size)
  uint8_t tmp;

  for (size_t i = 0; i < size / 2; i++)
    tmp = buffer[i];
    buffer[i] = buffer[size - i - 1];
    buffer[size - i - 1] = tmp;

When I send a constant value the example works but when I send a variable value it does not.
For example if I do

//Envoi des données
  uint8_t tempBuffer2[sizeB];
  int data = random(1600);
  tempBuffer2[0] = highByte(data);
  tempBuffer2[1] = lowByte(data);

instead of sending a constant it stops working (negative value, value beyond the max).
I have check the size of the int. It is two bytes as expected for the Arduino.
Do I miss something ?

I’ve tried to add a end message byte with this code

byte sizeB = 3;
  //Envoi des données
  uint8_t tempBuffer2[sizeB];
  int data = random(100, 150);
  tempBuffer2[0] = highByte(data);
  tempBuffer2[1] = lowByte(data);
  tempBuffer2[2] = ',';
  myPacketSerial.send(tempBuffer2, sizeB);

On the openframeworks side the code is

void ofApp::onSerialBuffer(const ofxIO::SerialBufferEventArgs& args)
	// Decoded serial packets will show up here.
	SerialMessage message;
	message.message = args.buffer().toString();
	char buf[3]; //Where the received bytes are
	buf[0] = args.buffer()[0];
	buf[1] = args.buffer()[1];
	buf[2] = args.buffer()[2];
	int number;
	if (buf[2] == ',') number = buf[1] | buf[0] << 8;
	else number = 1111;
	cout << number << endl;

The result also have errors. I expect to see growing integer in this test and suddenly I have negative numbers. But, even for the negative numbers the last byte is the end message I defined ( a ‘,’).
It seems that the byte are changed or corrupted in a way…

Ok, Found it.
The integer in Arduino world is 2 byte but not in C++. I don’t know the exact technical reason but when I change my conversion method from

number = buf[1] | buf[0] << 8;


#define bytes_to_u16(MSB,LSB) (((unsigned int) ((unsigned char) MSB)) & 255)<<8 | (((unsigned char) LSB)&255)


if (buf[2] == ',')number = bytes_to_u16(buf[0], buf[1]);

it worked