PySerial 写入错误(波特率敏感) [英] PySerial write errors (baudrate sensitive)

查看:108
本文介绍了PySerial 写入错误(波特率敏感)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 pyserial 和 windows 时遇到了一些问题(在 linux 上使用相同的代码可以正常工作);我似乎在写错误的字节.

I'm having some issues with pyserial and windows (on linux it works fine with the same code); I seem to be writing erroneous bytes.

该代码甚至可以在 debian VM 内的同一台机器上正常运行,所以我很确定它是一个软件.

The code even works fine on the same machine inside a debian VM, So Im pretty sure its a software thing.

我使用的是 Python 2.7.6 32 位和 pyserial 2.7,但我尝试过其他版本(2.5 和 2.4)都出现相同的错误.

I'm using Python 2.7.6 32bit, pyserial 2.7 but have tried others (2.5 and 2.4) all exibit the same errors.

有趣的是,当波特率增加到 115200 时,错误就会减少很多.

Interestingly enough, the errors are much less frequent when the baudrate is increased to 115200.

这是我的代码的相关部分:

This is the relavant bit of my code:

ser = serial.Serial()
ser.timeout = 0.1
ser.port = "COM74" 
ser.baudrate = 9600
ser.bytesize = 8
ser.parity = "N"
ser.stopbits = 1
ser.open()

ser.write("AT+CMGR\r")
search("OK", 5, rec, ser)
ser.write("AT+CGSN\r")
search("OK", 5, rec, ser)
ser.write("AT+CSQ\r")
search("OK", 5, rec, ser)
ser.write("AT+CREG?\r")
search("OK", 5, rec, ser)

ser.close()

测试时给出以下输出:(写入带有回显的自动波特率设备):

Which when tested gave the following output: (writing to an autobauding device with echo on):

***Timeout***, expected: "OK" received: "A\xa8+CMGR\r"
***Timeout***, expected: "OK" received: "AT+C\x87SN\r\r\nERROR\r\n"
***Timeout***, expected: "OK" received: "AT+CS\xd1\r"
***Timeout***, expected: "OK" received: "A\xa8+CREG?\r"

我可以确认received:"之后的内容(没有引号,除了ERROR"和设备生成的一些 CR NL 字符)是设备实际收到的内容:

I can confirm that the stuff after "received:" (sans quotes and with the exception of the "ERROR" and some CR NL chars which are generated by the device) is what is actually received by the device:

A\xa8+CMGR\r
AT+C\x87SN\r
AT+CS\xd1\r
A\xa8+CREG?\r

如果有人可以提出修复建议,我会是葡萄柚

If anyone can suggest a fix I would be grapefruit

推荐答案

我在 windows 和 Serial 中使用 AT 命令,我没有这些问题,我猜它与编码有关.

I'm using the AT commands with windows and Serial and i don't have these problems, my guess is that it's has something to do with encoding.

你能试试这个AT指挥官

尝试连接您的设备,看看是否得到了正确的响应,如果相同,则问题出在您的设备上.

Try to connect your device and to see whether you get the right response if it's the same then the problem is at your device.

这篇关于PySerial 写入错误(波特率敏感)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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