{"id":167,"date":"2008-03-12T07:56:21","date_gmt":"2008-03-12T12:56:21","guid":{"rendered":"http:\/\/www.tigoe.net\/pcomp\/code\/category\/uncategorized\/167"},"modified":"2008-03-12T16:15:38","modified_gmt":"2008-03-12T21:15:38","slug":"reading-multiple-serial-ports-in-processing","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/misc\/167\/","title":{"rendered":"Reading Multiple Serial Ports in Processing"},"content":{"rendered":"<p>\nThis 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.\n<\/p>\n<pre>\n\/*\n   Multiple Serial Ports\n   Language: Processing\n\n   This program reads multiple serial ports and lets you know when data comes \n   from one port or the other. The two ports in this example are attached to \n   ID Innovations ID-12 RFID readers. The ID-12 readers send a string that ends \n   with a byte whose value is 0x03.\n   \n   Created 12 Mar 2008\n   by Tom Igoe\n*\/\n<\/pre>\n<p><!--more--><\/p>\n<pre>\nimport processing.serial.*;\n\nSerial portOne;    \/\/ the first serial port\nSerial portTwo;    \/\/ the second serial port\n\n\/\/ the list of names\nString[] tags = {\n  \"04157EC3CB67\",\"04157EC1E846\",\"04157EC22588\"};\n\n\/\/ the list of people\nString[] people = {\n  \"Christer\", \"Marianne\", \"no one\"};\n\nvoid setup() {\n  \/\/ list the serial ports\n  println(Serial.list());\n  \/\/ open the serial ports:\n  portOne = new Serial(this, Serial.list()[0], 9600);\n  portTwo = new Serial(this, Serial.list()[2], 9600);\n  \/\/ set both ports to buffer information until you get 0x03:\n  portOne.bufferUntil(0x03);\n  portTwo.bufferUntil(0x03);\n}\n\nvoid draw() {\n\n}\n\nvoid serialEvent(Serial thisPort) {\n  \/\/ read the incoming serial data:\n  String inString = thisPort.readStringUntil(0x03);\n\n  \/\/ if the string is not empty, do stuff with it:\n  if (inString != null) {\n    \/\/ if the string came from serial port one:\n    if (thisPort == portOne) {\n      print (\"Data from port one: \");\n    }\n    \/\/ if the string came from serial port two:\n    if (thisPort == portTwo) {\n      print (\"Data from port two: \");\n    }\n    \/\/ print the string:\n    println(inString);\n\n    \/\/ the tag ID is only bytes 1 through 13. Get it:\n    String payload = inString.substring(1, 13);\n    \/\/ match the tag against the list of tags:\n    matchTag(payload);\n  }\n}\n\nvoid matchTag(String thisTag) {\n  \/\/ iterate over the list of all known tags:\n  for (int whichTag = 0; whichTag &lt; tags.length; whichTag++) {\n    \/\/ if the tag you got matches this tag in the list:\n    if (thisTag.equals(tags[whichTag])) {\n      \/\/ get the name from the people list\n      \/\/ that corresponds to this tag's position\n      String thisName = people[whichTag];\n      \/\/ print it:\n      println(\"Here's \" + thisName);\n    }\n  }\n\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.tigoe.com\/pcomp\/code\/misc\/167\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Reading Multiple Serial Ports in Processing&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,7],"tags":[],"class_list":["post-167","post","type-post","status-publish","format-standard","hentry","category-misc","category-Processing"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=167"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/167\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}