This program reads multiple serial ports and lets you know when data comes from one port or the other. The two ports in this example are attached to ID Innovations ID-12 RFID readers. The ID-12 readers send a string that ends with a byte whose value is 0x03.
/* Multiple Serial Ports Language: Processing This program reads multiple serial ports and lets you know when data comes from one port or the other. The two ports in this example are attached to ID Innovations ID-12 RFID readers. The ID-12 readers send a string that ends with a byte whose value is 0x03. Created 12 Mar 2008 by Tom Igoe */
import processing.serial.*; Serial portOne; // the first serial port Serial portTwo; // the second serial port // the list of names String[] tags = { "04157EC3CB67","04157EC1E846","04157EC22588"}; // the list of people String[] people = { "Christer", "Marianne", "no one"}; void setup() { // list the serial ports println(Serial.list()); // open the serial ports: portOne = new Serial(this, Serial.list()[0], 9600); portTwo = new Serial(this, Serial.list()[2], 9600); // set both ports to buffer information until you get 0x03: portOne.bufferUntil(0x03); portTwo.bufferUntil(0x03); } void draw() { } void serialEvent(Serial thisPort) { // read the incoming serial data: String inString = thisPort.readStringUntil(0x03); // if the string is not empty, do stuff with it: if (inString != null) { // if the string came from serial port one: if (thisPort == portOne) { print ("Data from port one: "); } // if the string came from serial port two: if (thisPort == portTwo) { print ("Data from port two: "); } // print the string: println(inString); // the tag ID is only bytes 1 through 13. Get it: String payload = inString.substring(1, 13); // match the tag against the list of tags: matchTag(payload); } } void matchTag(String thisTag) { // iterate over the list of all known tags: for (int whichTag = 0; whichTag < tags.length; whichTag++) { // if the tag you got matches this tag in the list: if (thisTag.equals(tags[whichTag])) { // get the name from the people list // that corresponds to this tag's position String thisName = people[whichTag]; // print it: println("Here's " + thisName); } } }