PySerial - 无法发送 STX

发布于 2024-10-08 04:52:42 字数 359 浏览 0 评论 0原文

好吧,所以我对 pySerial 完全是菜鸟。我正在尝试与一台实验室设备进行通信,但在发送 STX(文本开始)命令时遇到问题。到目前为止,我的基本代码如下所示:

ser = serial.Serial(0, 19200, timeout=1,parity=serial.PARITY_ODD, rtscts=0)
ser.write(0x02) #ASCII STX is 0x2 in hex

但是当我查看示波器上的 232 数据时,我尝试发送的 STX 看起来与 Hperterminal 中发送的 STX 命令完全不同。

有什么想法吗?我确信这是非常简单的,我只是忽略了一些微不足道的事情。

谢谢!

Ok, so I am a complete noob to pySerial. I am trying to communicate to a piece of lab equipment, but am having trouble just sending the STX (Start of Text) command. So far, my basic code looks like:

ser = serial.Serial(0, 19200, timeout=1,parity=serial.PARITY_ODD, rtscts=0)
ser.write(0x02) #ASCII STX is 0x2 in hex

But when I look at the 232 data on my scope, the STX I'm trying to send, doesn't look anything like a STX command sent in Hperterminal.

Any ideas? I am sure this is incredibly straight forward and I am just overlooking something trivial.

Thanks!

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

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

发布评论

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

评论(2

不可一世的女人 2024-10-15 04:52:42

Serial 类中的 write 函数接受字节或字符串。您正在传递一个整数,因此结果未知,可能会将其转换为 str,以便您发送字符“2”。

正确的做法是:

ser = serial.Serial(0, 19200, timeout=1,parity=serial.PARITY_ODD, rtscts=0)
ser.write(chr(0x02)) #ASCII STX is 0x2 in hex

The write function in serial class accepts bytes or strings. You are passing an integer so the result is unknown, maybe is casting it to str so you are sending the char '2'.

The correct way to do it is :

ser = serial.Serial(0, 19200, timeout=1,parity=serial.PARITY_ODD, rtscts=0)
ser.write(chr(0x02)) #ASCII STX is 0x2 in hex
聊慰 2024-10-15 04:52:42

您确定超级终端和 PySerial 使用相同的配置吗?您应该确保以下各项相同:

  • 波特率(您使用的是 19200)
  • 奇偶校验(您使用的是 PARITY_ODD)
  • 数据位数(pySerial 默认为 8)
  • 停止位(pySerial 默认为 1)

Are you sure that the same configuration is used for HyperTerminal and PySerial. You should make sure that the following are same:

  • Baudrate (you are using 19200)
  • Parity (you are using PARITY_ODD)
  • Number of data bits (pySerial default 8)
  • Stop bits (pySerial default 1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文