在 Python 中从 RS232 读取数据
Reading data form RS232 in Python
到目前为止,这是我的代码。它旨在读取投影仪电源状态的输出。
我遇到的问题是除了“>>”
它没有输出
已经尝试修改响应变量:
- 响应 = ser.readline()
- 响应 = ser.read()
- 响应 = ser.inWaiting()
有趣的是,当我 运行 cat /dev/ttyS5
完成 Python 脚本后。它给了我输出并退出。通常,当我使用 cat /dev/ttyS5
时,它会永远持续下去,需要终止。
#!/usr/bin/python
import serial
import sys
import os
import time
pin_export = 'echo 2 > /sys/class/gpio/export'
pin_out = 'sleep 0.1 && echo out > /sys/class/gpio/gpio2/direction'
pin_high = 'sleep 0.1 && echo 1 > /sys/class/gpio/gpio2/value'
pin_low = 'sleep 0.1 && echo 0 > /sys/class/gpio/gpio2/value'
pin_unexport = 'echo 2 > /sys/class/gpio/unexport'
response = ''
os.system(pin_export)
os.system(pin_out)
os.system(pin_high)
ser = serial.Serial()
ser.port = '/dev/ttyS5'
ser.baudrate = 9600
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.xonxoff = False
ser.open()
ser.write(b'\r*pow=?#\r')
y = "1"
ser.timeout=0.5
while ser.inWaiting() > 0:
response = ser.readline()
print(">>" + response)
time.sleep(1)
os.system(pin_low)
os.system(pin_unexport)
另一方面,如果你们中的任何人对如何读取来自 /dev/ttyS5
的输入有更好的想法,我稍后可以将其保存到文本文件中,我愿意征求建议。
好的,所以我通过一些测试自己弄明白了。如果有人遇到相同或相关的问题,这是我的回答。
问题是 rs 响应的第一行包含一些不需要的值,它阻止了其他答案的端口。
#!/usr/bin/python
import serial
import sys
import os
import time
# bash commands for gpio
pin_export = 'sleep 0.05 && echo 2 > /sys/class/gpio/export'
pin_out = 'sleep 0.06 && echo out > /sys/class/gpio/gpio2/direction'
pin_high = 'sleep 0.05 && echo 1 > /sys/class/gpio/gpio2/value'
pin_low = 'sleep 0.05 && echo 0 > /sys/class/gpio/gpio2/value'
pin_unexport = 'sleep 0.05 && echo 2 > /sys/class/gpio/unexport'
# turning on pin 2
os.system(pin_export)
os.system(pin_out)
os.system(pin_high)
# serial settings
ser = serial.Serial()
ser.port = '/dev/ttyS5'
ser.baudrate = 9600
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.xonxoff = False
# port open
ser.open()
while True:
# buffor reset
ser.reset_input_buffer()
ser.reset_output_buffer()
# sending question for projector
ser.write('\r*pow=?#\r')
time.sleep(0.6)
# trashing first line
trash = ser.readline(30)
# the needed answer from rs
pwrinfo = ser.readline(9)
if pwrinfo == "*POW=OFF#":
print "Projector OFF"
elif pwrinfo == "*POW=ON#\r":
print "Projector ON"
break
else:
print "Heating or Cooling..."
# buffor clear
ser.flush()
# port close
ser.close()
# turning off pin 2
os.system(pin_low)
os.system(pin_unexport)
到目前为止,这是我的代码。它旨在读取投影仪电源状态的输出。
我遇到的问题是除了“>>”
它没有输出已经尝试修改响应变量:
- 响应 = ser.readline()
- 响应 = ser.read()
- 响应 = ser.inWaiting()
有趣的是,当我 运行 cat /dev/ttyS5
完成 Python 脚本后。它给了我输出并退出。通常,当我使用 cat /dev/ttyS5
时,它会永远持续下去,需要终止。
#!/usr/bin/python
import serial
import sys
import os
import time
pin_export = 'echo 2 > /sys/class/gpio/export'
pin_out = 'sleep 0.1 && echo out > /sys/class/gpio/gpio2/direction'
pin_high = 'sleep 0.1 && echo 1 > /sys/class/gpio/gpio2/value'
pin_low = 'sleep 0.1 && echo 0 > /sys/class/gpio/gpio2/value'
pin_unexport = 'echo 2 > /sys/class/gpio/unexport'
response = ''
os.system(pin_export)
os.system(pin_out)
os.system(pin_high)
ser = serial.Serial()
ser.port = '/dev/ttyS5'
ser.baudrate = 9600
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.xonxoff = False
ser.open()
ser.write(b'\r*pow=?#\r')
y = "1"
ser.timeout=0.5
while ser.inWaiting() > 0:
response = ser.readline()
print(">>" + response)
time.sleep(1)
os.system(pin_low)
os.system(pin_unexport)
另一方面,如果你们中的任何人对如何读取来自 /dev/ttyS5
的输入有更好的想法,我稍后可以将其保存到文本文件中,我愿意征求建议。
好的,所以我通过一些测试自己弄明白了。如果有人遇到相同或相关的问题,这是我的回答。
问题是 rs 响应的第一行包含一些不需要的值,它阻止了其他答案的端口。
#!/usr/bin/python
import serial
import sys
import os
import time
# bash commands for gpio
pin_export = 'sleep 0.05 && echo 2 > /sys/class/gpio/export'
pin_out = 'sleep 0.06 && echo out > /sys/class/gpio/gpio2/direction'
pin_high = 'sleep 0.05 && echo 1 > /sys/class/gpio/gpio2/value'
pin_low = 'sleep 0.05 && echo 0 > /sys/class/gpio/gpio2/value'
pin_unexport = 'sleep 0.05 && echo 2 > /sys/class/gpio/unexport'
# turning on pin 2
os.system(pin_export)
os.system(pin_out)
os.system(pin_high)
# serial settings
ser = serial.Serial()
ser.port = '/dev/ttyS5'
ser.baudrate = 9600
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.xonxoff = False
# port open
ser.open()
while True:
# buffor reset
ser.reset_input_buffer()
ser.reset_output_buffer()
# sending question for projector
ser.write('\r*pow=?#\r')
time.sleep(0.6)
# trashing first line
trash = ser.readline(30)
# the needed answer from rs
pwrinfo = ser.readline(9)
if pwrinfo == "*POW=OFF#":
print "Projector OFF"
elif pwrinfo == "*POW=ON#\r":
print "Projector ON"
break
else:
print "Heating or Cooling..."
# buffor clear
ser.flush()
# port close
ser.close()
# turning off pin 2
os.system(pin_low)
os.system(pin_unexport)