pySerial 和读取二进制数据 [英] pySerial and reading binary data

查看:63
本文介绍了pySerial 和读取二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我与之通信的设备发送二进制数据时,我可以恢复大部分数据.但是,似乎总是缺少一些字节,取而代之的是非标准字符.例如,一个单独的输出如下所示:

When the device I am communicating with sends binary data, I can recover most of it. However, there always seem to be some bytes missing, replaced by non-standard characters. For instance, one individual output looks like this:

\xc4\xa5\x06\x00.\xb3\x01\x01\x02\x00\x00\x00=\xa9

句点和等号应该是十六进制格式的传统字节(我在另一个应用程序中确认了这一点).其他时候我会得到其他奇怪的字符,例如 ')' 或 's'.这些字符通常出现在完全相同的位置(随我传递给设备的命令而异).

The period and equals sign should be traditional bytes in hexadecimal format (I confirmed this in another application). Other times I get other weird characters such as ')' or 's'. These characters usually occur in the exact same spot (which varies with the command I passed to the device).

我该如何解决这个问题?

How can I fix this problem?

推荐答案

您是否使用类似的方式显示输出?:

Are you displaying the output using something like this?:

print output

如果您的某些字节碰巧与可打印字符相对应,它们将显示为字符.试试这个:

If some of your bytes happen to correspond with printable characters, they'll show up as characters. Try this:

print output.encode('hex')

查看所有字节的十六进制值.

to see hex values for all your bytes.

这篇关于pySerial 和读取二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆