开发者

Getting my serial port to talk to my Adobe Air app

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!

So, I got a USB to serial adapter, plugged into my computer, and used a program called Cornflake to read this from the machine:

0,1,71.0,234.2,453,31.1,72.8,161.4,118.2,30,32.7,9336

Success! So I do some googling and it seems that serproxy 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:

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 
...

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:

private function onSocketData(event:Event):void {
  var data:String = socket.readUTFBytes(socket.bytesAvailable);
  trace(data);
}

This outputs gibberish:

¬ ±¬·±.0¬²3´ .0¬´53¬3±. ±¬·².¸¬±6± .²¬±±¸.0¬3 0¬3².6¬93 3±

So I assume I am not reading the right encoding. Seems like it should be ascii, so I do this:

var data:String = socket.readMultiByte(bytes.bytesAvailable, "us-ascii");

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 by开发者_StackOverflow中文版tes to see what I'm getting out of serproxy:

trace(socket.readByte().toString());

It looks like I'm getting negitive numbers:

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

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.

Getting my serial port to talk to my Adobe Air app

[SOLUTION]

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 read all about it on Wikipedia.

Now, to fix this strange issue, we simply need to be sure that if 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 negative byte.

var text:String = "";
var tmp:int = socket.readByte();
var charCode:int code;
if(tmp < 0)  //check to see if the parity bit was flipped to 1
{
     code=tmp & 0x7F;  //reset the parity bit to 0
}
else
{
     code=tmp;  //nothing to do if parity bit is 0
}
text+=String.fromCharCode(code);

So there you have it.

[/SOLUTION]


48 decimal = 0 (zero)

so, you're one the right track; instead, print their numeric value (and spend some time on the parity bits too)

[edit]
note too that 46 decimal = . (period)
54 decimal = 6 (six)
51 decimal = 3 (three)

finally, note that 0,1,71.0,234.2,453,31.1,72.8,161.4,118.2,30,32.7,9336 has a not coincidental connection to ±.0¬²3².6¬933..

so you're actually quite a bit closer than you think.
[/edit]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜