Sending an 8-bit Unsigned Char in Java using the Processing Language

In a Processing (which is basically Java) application that I am writing, I need to send a packet of data over the serial port that consists of unsigned bytes that are 8 bits long. In C programming, it would just be a matter of using unsigned char data types. Java or Processing does not have an unsigned char data type.

The requirements for the application are to send a packet of data from an array over the serial port. The first two bytes in the packet are header bytes, followed by various data and command bytes with a checksum at the end. The code already runs on an Arduino with the data packet stored in an array as follows:

byte tx_cmd[12] = { 0x55, 0xAA,             // header
                    0x01, 0x00,             // device ID
                    0x01, 0x00, 0x00, 0x00, // input parameter
                    0x01, 0x00,             // command code
                    0x02, 0x01              // checksum
                  };

In this Arduino code the byte type is an unsigned 8-bit byte  — equivalent of an unsigned char in C, as Arduino programming is basically C / C++.

Processing / Java Data Types Equivalent to C Unsigned Char

Two obvious candidates that look like they should be the equivalent of a C unsigned char are the Java char and Java byte data types. These two data types have the following problems:

Java Char

The Java char data type is 16 bits long. Trying to put the data packet to send over the serial port into a char array would send two 8-bit bytes for every char variable sent. The Java char is also intended for characters, so trying to pack two bytes into a char would be bad programming practice.

Processing char reference.

Java Byte

The Java byte data type is 8 bits long, however it is a signed data type, not unsigned. The range of the byte is from 127 to -128. This differs from the Arduino byte data type which is 8 bits long and unsigned with a range from 0 to 255 (same as a C unsigned char).

Processing byte reference.

Attempting to Use the Byte Data Type

The following Processing code shows the problem when using a data packet array of the byte data type.

void setup() {
  byte[] tx_cmd = new byte[12];
  tx_cmd[0] = 0x55;
  tx_cmd[1] = 0xAA;
  tx_cmd[2] = 0x01;
  tx_cmd[3] = 0x00;
  tx_cmd[4] = 0x01;
  tx_cmd[5] = 0x00;
  tx_cmd[6] = 0x00;
  tx_cmd[7] = 0x00;
  tx_cmd[8] = 0x12;
  tx_cmd[9] = 0x00;
  tx_cmd[10] = 0x13;
  tx_cmd[11] = 0x01;
  
  for (int i = 0; i < 12; i++) {
    println(hex(tx_cmd[i]));
  }
}

When trying to run the code in the Processing IDE, it gives the following error:

cannot convert from int to byte

Which refers to the code in the line:

tx_cmd[1] = 0xAA;

The problem is that the the hexadecimal value of AA which is the decimal value of 170 is too big for the positive range of the byte data type which can have a maximum value of 127. None of the other numbers assigned to the array elements give an error because they are all less than 127 decimal.

 Solution to Sending an Unsigned Byte Using Processing / Java

Although the byte data type is signed, it still stores binary values from 0000 0000b to 1111 1111b (or 0x00 to 0xFF hexadecimal) with the most significant bit (MSB) being the sign bit which is set when the number is negative. The code only treats the byte as negative because the data type of the language being used says that it is a signed number.

When data is sent over the serial port, it is sent as bytes. The device receiving the data bytes on the other side can choose whether to treat the bytes as signed or unsigned, so the solution to sending the bytes is to convert the data bytes with values above 127 to their negative equivalents.

This is actually done very simply by casting the input integer to the byte data type, which cuts off the top bytes of the integer and puts the least significant byte of the integer into the byte variable.

void setup() {
  byte[] tx_cmd = new byte[12];
  tx_cmd[0] = (byte)0x55;
  tx_cmd[1] = (byte)0xAA;
  tx_cmd[2] = (byte)0x01;
  tx_cmd[3] = (byte)0x00;
  tx_cmd[4] = (byte)0x01;
  tx_cmd[5] = (byte)0x00;
  tx_cmd[6] = (byte)0x00;
  tx_cmd[7] = (byte)0x00;
  tx_cmd[8] = (byte)0x12;
  tx_cmd[9] = (byte)0x00;
  tx_cmd[10] = (byte)0x13;
  tx_cmd[11] = (byte)0x01;
  
  // print the array in hexadecimal and decimal
  for (int i = 0; i < 12; i++) {
    print("0x");
    print(hex(tx_cmd[i]));
    print(" = ");
    println(tx_cmd[i]);
    
  }
  
}

The output from the above code:

0x55 = 85
0xAA = -86
0x01 = 1
0x00 = 0
0x01 = 1
0x00 = 0
0x00 = 0
0x00 = 0
0x12 = 18
0x00 = 0
0x13 = 19
0x01 = 1

The number stored in the byte array will be displayed as the desired value when printed as a hexadecimal number. When printed as an integer, it will be displayed as a negative value if it contains a number greater than 127.

As the code shows, it is much easier and more efficient to cast the input integer to a byte and truncate the integer, than to try and calculate the equivalent negative value for the input byte.

Improved Arduino Live Cricket Score Ticker

The new Arduino live cricket score ticker code improves on the previous version of the ticker by saving the XML live cricket results to a file on an SD card. This allows the cricket score data to be randomly accessed for the desired data. A micro SD card inserted into the micro SD card socket on the Arduino Ethernet shield is used for saving the XML file.

Parsing XML using Arduino

XML parsing functions used in this project can be used with similar Arduino projects that need to parse XML files. The functions are used to search for specified tags in the XML file and if found can then be used to find child tags in the parent tags. The functions are used to drill down to the desired child tag in order to extract the required data from these tags.

With the XML file saved to SD card, it is possible for a sketch to reliably parse the file even if the order of the tag attributes is changed. The previous ticker code tried to parse the XML file on the fly as it gets sent to the Arduino and therefore relies on the tags and attributes being in a specific order.

Memory Limitations

An Arduino MEGA is needed for the new ticker because the Arduino Uno does not have enough memory to run the sketch. The blog post that compares the amount of memory used by various sketches shows some example sketches and how much of the Arduino Uno memory they use. From this comparison, it can be seen that the Arduino Uno memory can get used up fairly quickly as sketches start to use more libraries and get slightly more complex than some of the example sketches.

Go to the Improved Arduino Cricket Score Ticker Code →

How to Save a File from the Internet to an Arduino

The article on saving a web file to an Arduino shows how to get any file from the Internet using an Arduino and Ethernet shield and save the file to an SD card inserted into the Ethernet shield.

Two sketches are provided in the article — the first sketch saves the incoming HTTP header and the requested file. If a project requires further information from the HTTP header, it can be obtained from the file instead of trying to process the incoming header on-the-fly.

The second sketch in the article strips off the incoming HTTP header and saves only the requested file to the SD card.

Saving a web file to SD card first before accessing data from it allows the data to be randomly accessed in the file instead of trying to find data from the file in the incoming stream of bytes. This is useful for retrieving data from an XML file such as from a live sports or weather feed. Specific data fields can be randomly accessed from the saved file and then used by the Arduino.

Go to the Arduino Web File Article →

Using Arduino as a Sports Score Ticker to get Live Cricket Results

An Arduino with Ethernet shield can be configured as an Ethernet client to fetch live sports scores from the Internet. The scores can the be displayed on any display device connected to the Arduino or sent through the serial port.

In the Arduino live cricket score project, the Arduino fetches the live cricket scores from cricbuzz.com and displays them in the Arduino IDE serial monitor window. It gets the scores by accessing the XML scores feed and then extracting the desired information from the XML file.

Arduino cricket score ticker

Arduino Cricket Score Ticker

This is a very early version of cricket score code and will be improved on in the future. The code is susceptible to breaking if there are any unexpected changes to the XML file, but the code will be made more robust and will be updated to handle and recover from errors in the future.

Go to the Arduino live cricket score project →

Basic Multi-file Arduino Web Server

This basic Arduino web server hosts HTML web pages and various other files such as CSS, JavaScript, text and image files.

Web pages and other resource files are stored on the SD card inserted into the SD card socket on  Arduino Ethernet shield or Arduino Ethernet board.

The code for the web server can be copied from the project’s page which contains a form that allows parameters such as the MAC, IP and port numbers to be changed. After changes have been made to the settings, a button is clicked to update the Arduino web server sketch. The sketch can then be copied and pasted into the Arduino IDE for loading to the Arduino board with Ethernet shield. Some sample HTML files, JavaScript, CSS, text and image files are also available for download and demonstrate the web server capabilities.

This web server is a supplement to the Arduino Ethernet shield web server tutorial and provides a starting place to design your own web server.

Get the Arduino web server code →