Processing Serial Port Number Programming and Selecting

Processing language applications that use the serial port to connect to serial devices such as Arduino need to use the correct serial port number that the serial device is connected to. Here are three methods for selecting and connecting to a serial device from a Processing application, namely, hard-coding, auto-detection and user selected Processing serial port.




Hard-coding a Processing Serial Port Number

Hard-coding the serial port number in the Processing application is the default way used in most Processing examples. In the library reference documentation for the Serial library, the hard-coded method is used.

Listing the Serial Ports and Connecting

Hard-coding examples usually print the list of serial ports on the computer to the text area at the bottom of the Processing IDE using the following line of code:

println(Serial.list());

This allows the programmer to determine which number the desired port is in the list of ports. The ports listed in the text area of the IDE are numbered from 0 in the list, so in the list of serial ports in the image below from a Linux computer, /dev/ttyACM0 would be 0 in the list, /dev/ttyS0 would be 1 in the list, etc.

Processing Serial Port List

Processing Serial Port List

 

 

 

 

 

On a Windows computer, these would typically be COM1, COM2, COM3, etc. which are also numbered in the list starting from 0.

After the correct serial port name from the list is found and the number of the serial port in the list has been determined, the serial port can be connected to in the Processing application code.

// Example by Tom Igoe

import processing.serial.*;

// The serial port:
Serial myPort;       

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

// Open the port you are using at the rate you want:
myPort = new Serial(this, Serial.list()[0], 9600);
  
// Send a capital A out the serial port:
myPort.write(65);

The above code is taken from the Processing Serial library documentation and first lists the available serial ports on the computer, then connects to the first serial port in the list. The following line of code is where the serial port number from the list is hard-coded in the application:

myPort = new Serial(this, Serial.list()[0], 9600);

The parameter Serial.list()[0] connects to the first serial port in the list of serial ports. In the above image, this would be /dev/ttyACM0. On my Windows PC, there is a default COM port, COM3. When an Arduino is plugged into the PC, it appears as COM4 or COM5, depending on which USB port the Arduino is plugged into. Processing code for the Windows PC must use Serial.list()[1] to connect to the second serial port on the PC which would be either COM4 or COM5 in this particular case.

Disadvantages of Hard-coding the Serial Port Number

If a Processing application that connects to a serial port is run on a different computer, or more than one serial device is connected to a computer, the desired port to connect to may change position in the list of ports. It would then be necessary to modify the Processing code to select the correct serial port. This means that the Processing IDE would need to be loaded onto every computer that the application is run on, in case the serial port number changes.

Auto-detecting a Processing Serial Port Number

A USB serial port can be auto-detected by a Processing application by starting with the USB serial device unplugged and then starting the Processing application. The Processing application can be programmed to store the list of serial ports before the desired USB serial device is plugged into the computer. When the device is plugged in, the Processing application can detect it and find it’s serial port name and number.

Code from the Processing application that auto-detects which serial port number an Arduino is plugged into uses the USB serial port auto-detect method and can be used as a starting point for any Processing project that is to use serial port auto-detect.

User Selected Processing Serial Port Number

Window controls in a Processing application can be used so that a user can scroll through the list of available serial ports and connect to the desired port. This method has the advantage that if the application is to run on different computers, the correct serial port can be selected by the user at run-time.

The image below shows controls in a Processing application window used to select and connect to a serial port.

Processing Serial Port Select Using Window Controls

Processing Serial Port Select Using Window Controls

Up and down buttons allow the list of serial ports to be scrolled through to select the desired port. When the Connect button is clicked, the application will connect to the selected (currently displayed) serial port.

A Disconnect button allows disconnecting from the serial port, so that the application can connect to a different serial port.

The Refresh button updates the list of available serial ports, which is useful in cases where a serial device is connected to the PC after the application is started. The new device will be added to the list when the refresh button is clicked.

Leave a Reply

Your email address will not be published. Required fields are marked *