简体   繁体   中英

Python Serial Read Not Working

I am attempting to control a projector using RS232 from python. This link has the needed information about the port settings and expected responses. http://www.audiogeneral.com/Optoma/w501_rs232.pdf

To summerise it baud = 9600, Data bits = 8, No parity, 1 stop bit, no flow control.

when the command "~00124 1\\r" is sent the projector should respond okn where n is the power state.

when the command "~0000 1\\r" is sent the projector should power on

From Putty I am able to send the power on command and other commands and see that the projector does what it is supposed to. I can also send the read command and get the appropriate okn response back to putty.

From python I can send the power on command and see the projector power on. however when I send the power state command I never see any character come into the read buffer.

Here is the code for a test script i wrote trying to debug this.

import serial
ser = serial.Serial("/dev/ttyUSB0")

print ser.baudrate
print ser.bytesize
print ser.parity
print ser.stopbits
print ser.xonxoff
print ser.rtscts
print ser.dsrdtr 
print ser.name

print "Power State"
ser.write("~00124 1")

while ser.inWaiting() > 0:
  response = ser.read(3)
  print response


output:
9600
8
N
1
False
False
False
/dev/ttyUSB0
True
Power State

I expect an okn after the power state line but it does not show up

Putty emulate a serial terminal, like minicom in Linux/Unix or HyperTerminal on Windows. Try adding \\n\\r at the end of the string to act as a real serial terminal.

I suggest you to try reading your data byte per byte instead of 3 bytes at time. Better if you use readline method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM