For our setup method in Processing, we're going to find the serial port our Arduino is connected to and set up our Serial object to listen to that port. Language:java void setup // I know that the first port in the serial list on my mac // is Serial.list0. The Processing serial library allows for easily reading and writing data to and from external machines. It allows two computers to send and receive data and gives you the flexibility to communicate with custom microcontroller devices, using them as the input or output to Processing programs.
Created on: 6 November 2012
In this article, the serial port name / number is determined programmatically using the Processing language. This means that an application written in Processing is used to find and display the computer port name that Arduino or other USB serial device is plugged into.
This application has been tested on both Windows and Linux. Processing is available for Linux, Windows and Mac.
What the Application Does
Start with the Arduino unplugged. The application written in Processing first finds all the serial ports and USB serial ports on the system and stores them. It then waits for the Arduino to be plugged in.
When the Arduino is plugged in, it finds all the serial and USB ports on the computer again. It now determines what the serial port number is by finding the difference between the initial serial ports and the new set of serial ports.
Because the Arduino has been plugged in, there will now be a new serial port on the computer which the Processing application will find.
The port_find Processing Application Waiting for Arduino to be Plugged In
Arduino Port Found on a Linux System
Amazon.co.uk
The Source Code
The listing for the Processing port_find sketch is shown below. It can be copied and pasted into the Processing IDE and run from there.
Alternatively, download the processing code (port_find.zip).
Op deze diskette vindt u de volgende macro's: 1. Wordperfect 5.1 deutsches.
The port_find Sketch in the Processing IDE
Please enable JavaScript to view the comments powered by Disqus.
Processing Software
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:
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.
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.
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:
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
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.