我试图读取一些GPIO的值。代码如下:
import serial
import codecs
import time
ser = serial.Serial(port = 'COM4', baudrate = 9600, \
parity = serial.PARITY_NONE, \
stopbits = serial.STOPBITS_ONE, \
bytesize = serial.EIGHTBITS, \
timeout = 0, \
)
print('connected to: ',ser.name)
ser.close()
def SSend(input):
ser.write(codecs.decode(input, "hex_codec")) #send as ASCII
print('sent: ', input)
def ReadIO():
#open the port
try:
ser.open()
except:
print('error opening serial port')
exit()
#flush the buffers
ser.flushInput()
ser.flushOutput()
#write data to read from GPIO
#causes SC18IM to return a byte containing each of the 8 I/O values
SSend(b'4950')
time.sleep(0.1) #allow time for the data to be received
#read the data
serialData = False
serialData = ser.readline()
ser.close()
return serialData
while 1:
print(ReadIO())
time.sleep(0.5)
这将打印以下内容:
发送时间: 4950英尺
b''
(我希望返回0x00或0x20,而不是空字节)
我知道我的硬件和我发送的一样好,因为它可以恢复我使用Realterm时所期望的,并且在其他地方也可以成功地在我的脚本中编写命令。在
我用这个很幸运
^{pr2}$然而,我真的不明白为什么它会起作用,而且似乎只是间歇性地起作用。在
谢谢你的阅读。在
readline()
假设有一些行尾符号,比如\n
或{相关问题 更多 >
编程相关推荐