我正在写一些代码从串行接口读取并返回接收到的数据的整数值。在
似乎我需要从中删除"\r\n"
。我试过分开,但没用。在
我的代码是:
import time
import serial
import string
ser = serial.Serial(
port='/dev/ttyACM1',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
counter = 0
while 1:
x = ser.readline()
if "t" in x:
print x
x = int(x)
print x
print "temp"
elif "h" in x:
print "hum "
elif "g" in x:
print "gas "
else:
pass
time.sleep(1)
那么我有一个错误:
^{pr2}$有人能帮忙吗?在
试着这样做:
第一个更改是^{} 我们从串行接口读取的行。这将删除字符串末尾的
"\r\n"
字符或空白。第二个更改是将行拆分为“type”字母(line[0]
)和数据(行的其余部分)。在相关问题 更多 >
编程相关推荐