[英]How can i use the values of serial.readline() function in python
import serial
ser=serial.Serial('/dev/ttyACMO',9600)
while 1:
ser.readline()
I am getting output of my sensors on python like 我在python上获取我的传感器的输出
X=316 O=418
can you help me to use these values 316 and 418? 您可以帮助我使用这些值316和418吗?
do you want the following? 您要以下吗?
import serial
ser=serial.Serial('/dev/ttyACMO',9600)
while 1:
x_value, o_value = ser.readline().split(" ")
x_value = x_value.split("=")[-1]
o_value = o_value.split("=")[-1]
print o_value, x_value
ValueError: empty separator
ValueError:空分隔符
Well, it usually means you're parsing a line that does not match the splits we're trying to do, you may want to try this instead: 好吧,这通常意味着您要解析的行与我们尝试的拆分不匹配,您可能需要尝试以下操作:
import serial
ser=serial.Serial('/dev/ttyACMO',9600)
while 1:
value = ser.readline().split(" ")
if " " in value:
x_value, o_value = value
if "=" in x_value and "=" in o_value:
x_value = x_value.split("=")[-1]
o_value = o_value.split("=")[-1]
print o_value, x_value
else:
print value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.