Posts Tagged ‘Serial’

Making The Connection – Serially

July 17, 2009

Locating a Processing example program to communicate with an Arduino is not too hard to find. But making the serial connection work can be troublesome. Since Processing is supported across different operating systems your connection can work fine in Linux, for instance, but switch to Windows and it quits working. If you want to share your program with others the problem becomes even worse. The problem is usually a result of having the wrong serial port selected.

Editing your program each time you switch between operating systems or plug the Arduino into a different port is troublesome. When sharing the program with others it is not a good idea to expect them to edit the source code to get it working on their computers. What we need is a way to interactively select the serial port while the program is running.

Serial Connection Selection List

Serial Connection Selection List

Processing is a relatively easy language to learn with tutorials and a reference section on the processing.org web site. The Libraries reference includes information about the Serial library. The reference section has limited information on user input with Mouse events and Keyboard actions. Looking for common Graphical User Interface (GUI) features like text boxs, drop down lists, radio buttons are absent. That doesn’t mean these GUI features aren’t availaible.

Processing is built upon the Java Language. With very little effort many of the Java features can be used.  To add a drop down list you simply need to know what to ask for. The Java Choice component provides the desired feature. You don’t have to import any libraries because Processing is already aware of these Java components. To find more information about the Choice Class you need to look at Java’s documentation.

Choice guiChoice = new Choice(); //Choice component for GUI selections.

import processing.serial.*; //Include the Processing Serial Library in this program

Serial port; //Declare the variable ‘port’ as a member of the Serial class.

void setup()
{
int nKtr;
Choice guiChoice = new Choice(); //Choice component for GUI selections.

nKtr = Serial.list().length; //Find out how many serial connections are recognized

println(Serial.list());
println(“Serial length = ” + Serial.list().length);

for (int i=0; i< nKtr; i++) { guiChoice.addItem(Serial.list()[i]); //Add each serial port found to the selection } add(guiChoice); //Place the Choice selection box on the display area. println("selected index = " + guiChoice.getSelectedIndex()); } void draw() { } [/sourcecode] This source code simply sets up a drop down list in a Processing display showing the available serial ports. The Choice list does not select the serial connection yet.

(c) 2009 – Vince Thompson