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.
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 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).
/*-------------------------------------------------------------- Program: port_find Description: Date: 1 November 2012 Author: W.A. Smith, http://startingelectronics.org --------------------------------------------------------------*/ import processing.serial.*; Serial ser_port; // for serial port PFont fnt; // for font int num_ports; boolean device_detected = false; String[] port_list; String detected_port = ""; void setup() { size(400, 200); // size of application window background(0); // black background fnt = createFont("Arial", 16, true); // font displayed in window println(Serial.list()); // get the number of detected serial ports num_ports = Serial.list().length; // save the current list of serial ports port_list = new String[num_ports]; for (int i = 0; i < num_ports; i++) { port_list[i] = Serial.list()[i]; } } void draw() { background(0); // display instructions to user textFont(fnt, 14); text("1. Arduino or serial device must be unplugged.", 20, 30); text(" (unplug device and restart this application if not)", 20, 50); text("2. Plug the Arduino or serial device into a USB port.", 20, 80); // see if Arduino or serial device was plugged in if ((Serial.list().length > num_ports) && !device_detected) { device_detected = true; // determine which port the device was plugge into boolean str_match = false; if (num_ports == 0) { detected_port = Serial.list()[0]; } else { // go through the current port list for (int i = 0; i < Serial.list().length; i++) { // go through the saved port list for (int j = 0; j < num_ports; j++) { if (Serial.list()[i].equals(port_list[j])) { break; } if (j == (num_ports - 1)) { str_match = true; detected_port = Serial.list()[i]; } } } } } // calculate and display serial port name if (device_detected) { text("Device detected:", 20, 110); textFont(fnt, 18); text(detected_port, 20, 150); } }