{"id":52,"date":"2004-05-24T09:49:03","date_gmt":"2004-05-24T14:49:03","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/uncategorized\/52"},"modified":"2007-08-07T13:29:07","modified_gmt":"2007-08-07T18:29:07","slug":"serial-call-and-response-directorlingo","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/Lingo\/52\/","title":{"rendered":"Serial Call-and-Response (Director\/Lingo)"},"content":{"rendered":"<p>Here&#8217;s an example where a desktop computer running Director receives data from a BX-24. In this case, the BX-24 is sending 3 bytes, one for each of 3 analog sensors. the Director program checks to see how many bytes there are in the serial port buffer. If there are none, it asks for data. If there are five, it reads the data into five variables. It runs this checking routine repeatedly (in the exitframe or idle), so that if there are some bytes, but not a full three, it skips the whole routine and goes about its other business.<\/p>\n<p>This example uses <a href=\"http:\/\/www.physicalbits.com\/\" target=\"_other\">Geoff Smith&#8217;s SerialXtra version 1.0<\/a> for Director. <\/p>\n<p><!--more--><\/p>\n<p>Lingo side:<\/p>\n<pre>on exitFrame\n      doSerial\n      go the frame\nend \n\non doSerial\n  -- see if the port's been opened:\n  If serialObject.isPortOpen() then\n    If serialObject.charsavailable() = 0 then\n      --send a byte to ask for data\n      SerialObject.writeChar(\"A\")    \n    End if\n    \n    If SerialObject.charsAvailable() >=3 then      \n      --first byte sent by microcontroller:\n      MyVar = SerialObject.readNumber()    \n      --next byte sent by microcontroller:\n      MyVar2 = SerialObject.readNumber()    \n      --third one sent by microcontroller:\n      MyVar3 = SerialObject.readNumber()    \n    end if\n  end if  \nend \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 three sensor readings. If it doesn&#8217;t, it goes about its other business. Examples for PicBasic Pro, BX-24, and Arduino\/Wiring can be found at <a href=\"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/arduinowiring\/49\">this link<\/a>.\n<\/p>\n<p>\nHere&#8217;s the full Lingo code, the text for the main movie script:\n<\/p>\n<pre>\n-- simpleserial.dir\n-- by Tom Igoe, 2000\n-- based on Geoff Smith's SerialXtra\n\nglobal serialObject\nglobal myVar1, myVar2, myVar3\n\non startMovie\n  \tclearglobals\n  -- variables for setting up the xtra.\n  -- Fill in your own values from the email\n  -- that you get from the licence registrar:\n    \n  -- fill in your email address below (see physicalbits.com for details)\n  programmerName = \"youremail@some.com\"\n  serialnum  =  \"xxxx-xxxx-xxxx-xxxx\"   -- physicalbits.com will mail you this number\n  licenseLength = 90\n  \n    -- fill in the name of your serial port below (e.g. COM1 on Windows):\n  mySerialPort = \"\/dev\/cu.USA19QW191P1.1\"\n\n  \n  \n  \t-- make a new instance of the xtra (mac OSX)\n  \topenxlib the pathname & \"serialXtra.osx\"\n\n  \t-- use this line instead for Windows:\n  \t-- openxlib the pathname & \"serialXtra.xtr\"\n  \t\n  \t\n  \tserialObject =  new (xtra \"SerialXtra\", programmerName, serialnum, licenseLength ) \n  \n  \t-- check that it has been created correctly     \n  \tif objectP( serialObject ) then\n    \tput serialObject \n  \telse\n    \talert(\"Instance not valid\")\n  \tend if\n  \n--\topen the serial port:\n  \tserialObject.openPort(mySerialPort)\n\n  \n  \t-- set the data rate, start bits, etc:\n  \tserialObject.setProtocol(9600, \"n\", 8, 1)\nend\n\non stopMovie  \n  -- dispose of the xtra and close the xtra file\n  serialObject.closePort()\n  set serialObject to 0\n  closeXlib \nend \n\non exitFrame\n  \tdoSerial\n  \tgo the frame\nend \n\non doSerial\n  -- see if the port's been opened:\n  If serialObject.isPortOpen() then\n    If serialObject.charsavailable() = 0 then\n      --send a byte to ask for data\n      SerialObject.writeChar(\"A\")\t\n    End if\n    \n    If SerialObject.charsAvailable() >=3 then      \n      --first byte sent by microcontroller:\n      MyVar = SerialObject.readNumber()\t\n      --next byte sent by microcontroller:\n      MyVar2 = SerialObject.readNumber()\t\n      --third one sent by microcontroller:\n      MyVar3 = SerialObject.readNumber()\t\n    end if\n  end if  \nend \n\n<\/pre>\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>It runs this checking routine repeatedly (in the exitframe or idle), so that if there are some bytes, but not a full three, it skips the whole routine and goes about its other business.This example uses Geoff Smith&#8217;s SerialXtra version 1.0 for Director.  The full version of the Director code can be downloaded as a Director .dir file here, or as a text file (the main movie script) here.Lingo side:on exitFrame doSerial go the frameend on doSerial &#8212; see if the port&#8217;s been opened: If serialObject.isPortOpen() then If serialObject.charsavailable() = 0 then &#8211;send a byte to ask for data SerialObject.writeChar(&#8220;A&#8221;) End if If SerialObject.charsAvailable() >=3 then &#8211;first byte sent by microcontroller: MyVar = SerialObject.readNumber() &#8211;next byte sent by microcontroller: MyVar2 = SerialObject.readNumber() &#8211;third one sent by microcontroller: MyVar3 = SerialObject.readNumber() end if end if end 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":[23],"tags":[],"class_list":["post-52","post","type-post","status-publish","format-standard","hentry","category-Lingo"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/52","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=52"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}