问题
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.
The code even works fine on the same machine inside a debian VM, So Im pretty sure its a software thing.
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.
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"
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
回答1:
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.
Can you please try this AT Commander
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.
来源:https://stackoverflow.com/questions/20524294/pyserial-write-errors-baudrate-sensitive