# How do I write a (uint8\_t) data array via serial

**URL:** <https://discourse.processing.org/t/how-do-i-write-a-uint8-t-data-array-via-serial/30543>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [June 6, 2021, 10:03pm UTC](https://discourse.processing.org/t/how-do-i-write-a-uint8-t-data-array-via-serial/30543 "2021-06-06T22:03:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gerrikoio](https://avatars.discourse-cdn.com/v4/letter/g/b2d939/32.png) [@gerrikoio](https://discourse.processing.org/u/gerrikoio)\
**Post date:** [June 6, 2021, 10:03pm UTC](https://discourse.processing.org/t/how-do-i-write-a-uint8-t-data-array-via-serial/30543/1 "2021-06-06T22:03:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 6, 2021, 11:32pm UTC](https://discourse.processing.org/t/how-do-i-write-a-uint8-t-data-array-via-serial/30543/2 "2021-06-06T23:32:05Z")

</div>

> [@gerrikoio](#):
>
> 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.

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?

> [@gerrikoio](#):
>
> 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.

Take another look at the description and parameters:  
_[write() / Libraries / Processing.org](https://processing.org/reference/libraries/serial/Serial_write_.html)_

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**
>
> ```auto
> 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**
>
> ```auto
> 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:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/75ed2d443612b9cf841c94ef0a82f64b64fc9659.png)

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:

```auto
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:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/57d0da6ab3742286ca3be58841c76864b445f4b1.png)

`:)`

---

<div class="post-metadata">

**Author:** ![gerrikoio](https://avatars.discourse-cdn.com/v4/letter/g/b2d939/32.png) [@gerrikoio](https://discourse.processing.org/u/gerrikoio)\
**Post date:** [June 7, 2021, 9:49am UTC](https://discourse.processing.org/t/how-do-i-write-a-uint8-t-data-array-via-serial/30543/3 "2021-06-07T09:49:50Z")

</div>

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.

```auto
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);

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 7, 2021, 10:05am UTC](https://discourse.processing.org/t/how-do-i-write-a-uint8-t-data-array-via-serial/30543/4 "2021-06-07T10:05:32Z")

</div>

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.

`:)`

---

<div class="post-metadata">

**Author:** ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)\
**Post date:** [June 7, 2021, 6:40pm UTC](https://discourse.processing.org/t/how-do-i-write-a-uint8-t-data-array-via-serial/30543/5 "2021-06-07T18:40:01Z")

</div>

> [@gerrikoio](#):
>
> and that if dealing with unsigned data types like “uint8\_t” I need to use the signed integer data type.

That sentence seams weird.
