componentDidUpdate
我在这里尝试从arduino获取数据,但是它的格式为import serial
import numpy
import matplotlib.pyplot as plt
from drawnow import *
data = serial.Serial('com3',115200)
while True:
while (data.inWaiting() == 0):
pass
ardstr = data.readline()
print (ardstr)
。我想使用b'29.20\r\n'
格式的数据,以便进行绘制。
我尝试了"29.20"
和ardstr = str(ardstr).strip('\r\n')
但他们都不在工作。我的python版本是3.4.3。
我怎样做才能得到ardstr.decode('UTF-8')
而不是"29.40"
的结果?
答案 0 :(得分:3)
我尝试了
ardstr = str(ardstr).strip('\r\n')
和ardstr.decode('UTF-8')
您接近了!与.strip()
调用一样,使用.decode()
方法返回新值。
ardstr = ardstr.strip()
ardstr = ardstr.decode('UTF-8')
答案 1 :(得分:2)
如果要单行执行,可以尝试:
ardstr = ardstr.decode('UTF-8').rstrip()
rstrip()
将返回删除后缀字符的字符串副本。