LCD在打印输出上显示'b

时间:2018-11-20 15:06:16

标签: python byte

我创建此代码的目的是从ttyUSB0上的串行设备中读取数据,然后在已连接到pi的LCD显示器上进行打印。我在某种程度上可以正常工作,但是在我的LCD显示屏上它正在打印字节符号“ b”,然后阅读。他们有办法忽略“ b”吗?

我的代码:

import time
import serial
import I2C_LCD_driver


mylcd = I2C_LCD_driver.lcd()

print ("Starting Program")
ser = serial.Serial("/dev/ttyUSB0", baudrate=9600,
                    parity=serial.PARITY_NONE,
                    stopbits=serial.STOPBITS_ONE,
                    bytesize=serial.EIGHTBITS
                    )
time.sleep(1)
try:
    ser.write("12345".encode('utf-8'))
    print ("data echo mode enabled")
    while True:
        if ser.inWaiting() > 0:
            data = ser.read(size=7)
            print ("Weight", data, "kg")
            mylcd.lcd_display_string("Weight" + str(data), 1)


except KeyboardInterrupt:
    print ("Exiting Program")

except:
    print ("Error Occurs, Exiting Program")

finally:
    ser.close()
    pass

1 个答案:

答案 0 :(得分:1)

在bytes对象上调用str将返回包含引号和b前缀的字符串,例如:

>>> foo = b"hello"
>>> str(foo)
"b'hello'"

相反,请尝试使用decode

>>> foo.decode()
'hello'

因此,对于您的代码,该名称为mylcd.lcd_display_string("Weight" + data.decode(), 1)

相关问题