让我的串行端口与我的 Adob​​e Air 应用程序对话

发布于 2024-09-30 15:30:59 字数 2698 浏览 1 评论 0原文

我遇到了这个问题,我需要一台简单的医疗设备将一串逗号分隔的数字输出到我的 Adob​​e Air 应用程序。我很高兴能够与网络服务之外的其他东西交谈!

因此,我有一个 USB 转串口适配器,插入我的计算机,并使用一个名为 Cornflake 的程序来读取这个来自机器:

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

成功!所以我做了一些谷歌搜索,似乎 serproxy 是一个方便的工具,Arduino 爱好者可以用它来完成我所需要的工作:串行到闪存。所以我像这样下载、安装和配置:

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

这些与我在 Cornflake 中使用的设置相同。我插入一些简单的动作脚本来监听事件,当我捕获数据事件时,我这样做:

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

这会输出乱码:

Ø ±Ø·±.0ز3´ .0Ø´53Ø3±. ±Ø·2.¸Ø±6± .2ر±¸.0Ø3 0Ø3².6Ø93 3±

所以我认为我没有读取正确的编码。看起来应该是 ascii,所以我这样做:

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

没有骰子。所以我开始尝试一堆不同的编码并得到相同的乱码。我将串行端口更改为 tty.usbserial,尝试翻转换行符标志,所有结果都相同。所以,沮丧的是,我读了前几个字节,看看我从 serproxy 中得到了什么:

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

看起来我得到了负数:

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

不知道这意味着什么,我羞愧地低下了头,并漫步到 stackoverflow,希望有人能教我一些我在 Serialports101 中显然从未学到的东西。

data

[解决方案]

我遇到的问题是ActionScript 无法读取奇偶校验位。因此,当它看到 10101100(偶校验位翻转的 44)时,它将读取为 -84。现在这可能看起来很奇怪,就像对我来说一样,所以你需要阅读所有相关内容维基百科上的

现在,要解决这个奇怪的问题,我们只需确保如果奇偶校验位翻转为 1,我们会将其翻转回零。这样做很简单,因为 ActionScript 将奇偶校验位读取为字节。

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);

这样你就得到了。

[/解决方案]

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

data

[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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巡山小妖精 2024-10-07 15:30:59

48 小数 = 0(零)

所以,你是正确的选择;相反,打印它们的数值(并在奇偶校验位上花费一些时间)

[编辑]
另请注意,十进制 46 = . (期间)
54 十进制 = 6(六)
51 十进制 = 3(三)

最后,请注意 0,1,71.0,234.2,453,31.1,72.8,161.4,118.2,30,32.7,9336±.0‐²3².6‐933..

所以你实际上比你想象的要近一些。
[/编辑]

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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文