{"id":50,"date":"2004-05-25T12:06:51","date_gmt":"2004-05-25T17:06:51","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/processing\/50"},"modified":"2007-08-07T10:21:44","modified_gmt":"2007-08-07T15:21:44","slug":"serial-call-and-response-processing","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/Processing\/50\/","title":{"rendered":"Serial Call-and-Response (Processing)"},"content":{"rendered":"<p> This example for Processing shows how to take in a multi-byte string of serial data and process it byte by byte.  In this example, the computer sends an &#8220;A&#8221; to the microcontroller, and the microcontroller sends three bytes in response. Each byte represents a sensor value.<\/p>\n<p>After sending the &#8220;A&#8221;, the computer reads each byte in, adding it to a string until it has three bytes in the string. It then parses each byte of the string out into an int, and assigns values to three variables from those bytes.<\/p>\n<p>This example is written for  <a href=\"http:\/\/www.processing.org\/\">Processing,<\/a>by Casey Reas and Ben Fry. It was last updated for beta version 115.<\/p>\n<p><!--more--><\/p>\n<p>Processing code:<\/p>\n<pre>\r\n\/*\r\n  Serial call-and-response for v.85 and beyond\r\n by Tom Igoe\r\n\r\n Sends a byte out the serial port, and reads 3 bytes in.\r\n Sets foregound color, xpos, and ypos of a circle onstage\r\n using the values returned from the serial port.\r\n Thanks to Daniel Shiffman for the improvements.\r\n Thanks to Casey Reas for reminding me to use SerialEvent() properly.\r\n\r\n Updated 30 May 2006\r\n *\/\r\n\r\nimport processing.serial.*;\r\n\r\nint bgcolor;\t\t\t     \/\/ Background color\r\nint fgcolor;\t\t\t     \/\/ Fill color\r\nSerial port;                         \/\/ The serial port\r\nint[] serialInArray = new int[3];    \/\/ Where we'll put what we receive\r\nint serialCount = 0;                 \/\/ A count of how many bytes we receive\r\nint xpos, ypos;\t\t     \/\/ Starting position of the ball\r\nboolean firstContact = false;        \/\/ Whether we've heard from the microcontroller\r\n\r\nvoid setup() {\r\n  size(256, 256);  \/\/ Stage size\r\n  noStroke();      \/\/ No border on the next thing drawn\r\n\r\n  \/\/ Set the starting position of the ball (middle of the stage)\r\n  xpos = width\/2;\r\n  ypos = height\/2;\r\n\r\n  \/\/ Print a list of the serial ports, for debugging purposes:\r\n  println(Serial.list());\r\n\r\n  \/\/ I know that the first port in the serial list on my mac\r\n  \/\/ is always my  Keyspan adaptor, so I open Serial.list()[0].\r\n  \/\/ On Windows machines, this generally opens COM1.\r\n  \/\/ Open whatever port is the one you're using.\r\n  port = new Serial(this, Serial.list()[0], 9600);\r\n  port.write(65);    \/\/ Send a capital A to start the microcontroller sending\r\n}\r\n\r\nvoid draw() {\r\n  background(bgcolor);\r\n  fill(fgcolor);\r\n  \/\/ Draw the shape\r\n  ellipse(xpos, ypos, 20, 20);\r\n\r\n  \/\/ If no serial data has beeen received, send again until we get some.\r\n  \/\/ (in case you tend to start Processing before you start your\r\n  \/\/ external device):\r\n  if (firstContact == false) {\r\n    delay(300);\r\n    port.write(65);\r\n  }\r\n}\r\n\r\nvoid serialEvent(Serial port) {\r\n  \/\/ if this is the first byte received,\r\n  \/\/ take note of that fact:\r\n  if (firstContact == false) {\r\n    firstContact = true;\r\n  }\r\n  \/\/ Add the latest byte from the serial port to array:\r\n  serialInArray[serialCount] = port.read();\r\n  serialCount++;\r\n\r\n  \/\/ If we have 3 bytes:\r\n  if (serialCount &gt; 2 ) {\r\n    xpos = serialInArray[0];\r\n    ypos = serialInArray[1];\r\n    fgcolor = serialInArray[2];\r\n\r\n    \/\/ print the values (for debugging purposes only):\r\n    println(xpos + \"t\" + ypos + \"t\" + fgcolor);\r\n    \/\/ Send a capital A to request new sensor readings:\r\n    port.write(65);\r\n    \/\/ Reset serialCount:\r\n    serialCount = 0;\r\n  }\r\n}<\/pre>\n<p>The microprocessor is reading its sensors, then listening for a byte of data from the Director program. If it gets a byte, it sends out its five sensor readings. If it doesn&#8217;t, it goes about its other business.  Examples for for PicBasic Pro, the BX-24, and  Wiring\/Arduino can be found at <a href=\"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/arduinowiring\/49\">this link<\/a> as well.<\/p>\n<p>These methods won&#8217;t work in every situation, and they will need to be modified to fit most situations, but they are a starting point.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ Starting position of the ballboolean firstContact = false; \/\/ Whether we&#8217;ve heard from the microcontrollervoid setup() { size(256, 256); \/\/ Stage size noStroke(); \/\/ No border on the next thing drawn \/\/ Set the starting position of the ball (middle of the stage) xpos = width\/2; ypos = height\/2; \/\/ Print a list of the serial ports, for debugging purposes: println(Serial.list()); \/\/ I know that the first port in the serial list on my mac \/\/ is always my Keyspan adaptor, so I open Serial.list()[0]&#8230;.  \/\/ (in case you tend to start Processing before you start your \/\/ external device): if (firstContact == false) { delay(300); port.write(65); }}void serialEvent(Serial port) { \/\/ if this is the first byte received, \/\/ take note of that fact: if (firstContact == false) { firstContact = true; } \/\/ Add the latest byte from the serial port to array: serialInArray[serialCount] = port.read(); serialCount++; \/\/ If we have 3 bytes: if (serialCount > 2 ) { xpos = serialInArray[0]; ypos = serialInArray[1]; fgcolor = serialInArray[2]; \/\/ print the values (for debugging purposes only): println(xpos + &#8220;\\t&#8221; + ypos + &#8220;\\t&#8221; + fgcolor); \/\/ Send a capital A to request new sensor readings: port.write(65); \/\/ Reset serialCount: serialCount = 0; }}The microprocessor is reading its sensors, then listening for a byte of data from the Director program.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-50","post","type-post","status-publish","format-standard","hentry","category-Processing"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/50","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=50"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/50\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}