title: Getting my serial port to talk to my Adobe Air app
body: <p>I have run into this issue where I needed a piece of simple medical equipment to output a string of comma separated numbers to my Adobe Air app.  I'm just thrilled to be talking to something besides a web service!</p>
  
  <p>So, I got a USB to serial adapter, plugged into my computer, and used a program called <a href="http://tomgerhardt.com/Cornflake/" rel="nofollow noreferrer">Cornflake</a> to read this from the machine:  </p>
  
  <blockquote>
    <p>0,1,71.0,234.2,453,31.1,72.8,161.4,118.2,30,32.7,9336  </p>
  </blockquote>
  
  <p>Success!  So I do some googling and it seems that <a href="http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1140282379/5" rel="nofollow noreferrer"> serproxy</a> is a handy tool that Arduino buffs are using to do exactly what I need:  serial to flash.  So I download, install, and config like so:</p>
  
  <pre><code>newlines_to_nils=false
  
  serial_device1=/dev/cu.usbserial
  
  
  comm_ports=1,2,3,4
  
  comm_baud=2400
  comm_databits=7
  comm_stopbits=1
  comm_parity=even
  
  timeout=300
  
  net_port1=5331 
  ...
  </code></pre>
  
  <p>These are the same settings that I used in Cornflake.  I plug in some simple actionscript to listen for an event, and when I catch the data event, I do this:</p>
  
  <pre><code>private function onSocketData(event:Event):void {
    var data:String = socket.readUTFBytes(socket.bytesAvailable);
    trace(data);
  }
  </code></pre>
  
  <p>This outputs gibberish:</p>
  
  <blockquote>
    <p>¬ ±¬·±.0¬²3´ .0¬´53¬3±. ±¬·².¸¬±6± .²¬±±¸.0¬3 0¬3².6¬93 3±</p>
  </blockquote>
  
  <p>So I assume I am not reading the right encoding.  Seems like it should be ascii, so I do this:</p>
  
  <pre><code>var data:String = socket.readMultiByte(bytes.bytesAvailable, "us-ascii");
  </code></pre>
  
  <p>No dice.  So I start trying a bunch of different encodings and get the same gibberish.  I change the serial port to tty.usbserial, try flipping the newlines flag, all with the same results.  So, frustrated, I read the first couple of bytes to see what I'm getting out of serproxy:</p>
  
  <pre><code>trace(socket.readByte().toString());
  </code></pre>
  
  <p>It looks like I'm getting negitive numbers:</p>
  
  <blockquote>
    <p>48 -84 -79 -84 -73 -79 46 48 -84 -78 51 -76 46 -78 -84 -76 53 48 -84 51 48 46 57 -84 -73 -78 46 -76 -84 -79 54 -79 46 -72 -84 -79 -79 -72 46 -76 -84 51 48 -84 51 -78 46 -73 -84 57 51 51 54 -115 10</p>
  </blockquote>
  
  <p>Having no idea what this means, I hang my head in shame, and meander over to stackoverflow in the hopes that someone out there can teach me what I clearly never learned in serialports101.</p>
  
  <p><a href="http://www.flickr.com/photos/55348866@N08/5146511780/" rel="nofollow noreferrer" title="data by stephanothegreat, on Flickr"><img src="https://farm2.static.flickr.com/1123/5146511780_61980b919d.jpg" width="421" height="154" alt="data" /></a></p>
  
  <p><strong>[SOLUTION]</strong></p>
  
  <p>The issue I was having was that ActionScript has no way to read parity bits.  So, when it saw 10101100 (44 with even parity bit flipped), it read that as -84.  Now that may seem strange, as it did to me, so you'll want to <a href="http://en.wikipedia.org/wiki/Two&#39;s_complement" rel="nofollow noreferrer">read all about it</a> on Wikipedia.</p>
  
  <p>Now, to fix this strange issue, we simply need to be sure that <strong>if</strong> the parity bit is flipped to 1, we flip it back to zero.  Doing this is simple, because ActionScript reads the parity bit as a <em>negative</em> byte.</p>
  
  <pre><code>var text:String = "";
  var tmp:int = socket.readByte();
  var charCode:int code;
  if(tmp &lt; 0)  //check to see if the parity bit was flipped to 1
  {
       code=tmp &amp; 0x7F;  //reset the parity bit to 0
  }
  else
  {
       code=tmp;  //nothing to do if parity bit is 0
  }
  text+=String.fromCharCode(code);
  </code></pre>
  
  <p>So there you have it.  </p>
  
  <p><strong>[/SOLUTION]</strong></p>