How do I write a (uint8_t) data array via serial

I am learning the hard way that in Processing a “byte” data type is -128 to 127 and that if dealing with unsigned data types like “uint8_t” I need to use the signed integer data type.

Now I am sure this question has been asked countless times but I could not find anything following a search on Google.

I need to sent an array of RGB values (128 to 255 range) via the serial port. I see that the Serial library only offers byte[] arrays and it’s asking me to cast the integer array to byte in order to write the data via serial.

I tried sending a Serial.write for each RGB integer value individually but that is causing problems/errors with the device on the other end of the serial data port.

That is how Java handles a byte. Once you send the byte out the serial port it is just data and the receiving end then has to deal with it.

What is the device that is receiving?

Take another look at the description and parameters:
write() / Libraries / Processing.org

You can convert your int to a byte and send the byte or you can send each element of your int individually in a loop.

If you are sending an integer it will only send the LSB (least significant byte which is 0 to 255):

Sending integers
import processing.serial.*;

void setup()
  {
// The serial port:
Serial myPort;

// List all the available serial ports:
printArray(Serial.list());

// Open the port you are using at the rate you want:
myPort = new Serial(this, Serial.list()[2], 9600);

// Send a capital "A" out the serial port

for(int i=0; i< 300; i++)
  {
  myPort.write(i);
  }
  
  myPort.stop();  
  }  
Sending byte array
import processing.serial.*;

byte [] b = new byte [300];

void setup()
  {
// The serial port:
Serial myPort;

// List all the available serial ports:
printArray(Serial.list());

// Open the port you are using at the rate you want:
myPort = new Serial(this, Serial.list()[2], 9600);

// Send a capital "A" out the serial port

for(int i=0; i< 300; i++)
  {
  //myPort.write(i);
  b[i] = byte(i);
  }
  
  myPort.write(b);
  
  myPort.stop();  
  } 

This is the same data received on a terminal for both of the code examples:

Take a look at the last few rows and numbers over 255 = 0xFF !

I suggest exploring this a bit more and it will make sense at some point.

Take a look at this:

void setup()
  {
  for(int i=0; i<256; i++)
    {
    byte b = byte(i);
    println(b, '\t', char(b),'\t', (int(b)), '\t', hex(b), '\t', byte(b), '\t', int(b));
    }
  }   

The output:

:)

Wow thanks.

What device is receiving?
The device is an MCU from Silicon Labs which uses a binary serial transfer protocol. There is limited insight as to how that API interprets data received. There’s some C libraries available for use but not much use here with Java based Processing.

Your examples were very helpful. After a bit of thought (and then reading up about Java - and it’s oddities) I decided to store my values in a char array instead of an int array (just because it uses 16bits). Then I save in a temporary byte array by casting each char value to signed byte values.

e.g.

char[] NewArray = {200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210};
// Then convert to byte array to send via Serial port
byte[] tempArray = new byte[NewArray.length];
for (int x = 0; x < NewArray.length; x++) tempArray[x] = byte(NewArray[x]);
MySerialPort.write(tempArray);

Hello @gerrikoio,

Have fun!

I have had many adventures getting devices and software to talk to each other.

It can be a challenge at times.

:)

That sentence seams weird.