Typeerror: Unicode strings are not supported, please encode to bytes: 'ÿ'
Typeerror: Unicode strings are not supported, please encode to bytes: 'ÿ'
我发现这段代码 (https://drive.google.com/open?id=1TGuTPjfiV4r7ra_5eLMkbXBnesYlvTyT) 由 Adafruit 编写(并由我修改以添加一些在 python 2.7 中工作的额外功能,当我尝试 运行 python 3 中的代码使其更易于开发(因为我之前没有看过太多 python 2)它给了我错误:
Traceback (most recent call last):
File "/home/pi/Forecast-Printer/forecast.py", line 55, in <module>
printer = Adafruit_Thermal("/dev/serial0", 19200, timeout=5)
File "/home/pi/Forecast-Printer/Adafruit_Thermal.py", line 95, in __init__
self.wake()
File "/home/pi/Forecast-Printer/Adafruit_Thermal.py", line 605, in wake
self.writeBytes(255)
File "/home/pi/Forecast-Printer/Adafruit_Thermal.py", line 189, in writeBytes
super(Adafruit_Thermal, self).write(chr(arg))
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 518, in write
d = to_bytes(data)
File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 63, in to_bytes
raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq))
TypeError: unicode strings are not supported, please encode to bytes: 'ÿ'
代码是 运行 on a raspberry pi 运行ning raspbian (GUI version) 需要 运行 with sudo
因为它通过串口与迷你热敏打印机通信。
提前感谢您的帮助:)
编辑:
下面是一些代码,当 运行 in python 3:
时会触发相同的错误
from Adafruit_Thermal import *
printer = Adafruit_Thermal("/dev/serial0", 19200, timeout=5)
从堆栈跟踪来看,打印机初始化时,Adafruit 库通过串行写入整数 255
来唤醒打印机。它通过在第 189 行调用 Serial
class 的 write()
方法(在它自己的 write_bytes()
方法中)来实现:
super(Adafruit_Thermal, self).write(chr(arg))
它能够做到这一点,因为 Adafruit_Thermal
class 继承自 Serial
,如您在 class 定义中所见。
正如@abarnert 指出的那样,chr()
returns 是一个字符串,但听起来 write()
需要二进制数据。因此,我会尝试对您的问题发表评论中的建议。首先尝试将第 189 行更改为:
super(Adafruit_Thermal, self).write(arg)
如果不行试试:
super(Adafruit_Thermal, self).write(bytes([arg]))
我发现这段代码 (https://drive.google.com/open?id=1TGuTPjfiV4r7ra_5eLMkbXBnesYlvTyT) 由 Adafruit 编写(并由我修改以添加一些在 python 2.7 中工作的额外功能,当我尝试 运行 python 3 中的代码使其更易于开发(因为我之前没有看过太多 python 2)它给了我错误:
Traceback (most recent call last):
File "/home/pi/Forecast-Printer/forecast.py", line 55, in <module>
printer = Adafruit_Thermal("/dev/serial0", 19200, timeout=5)
File "/home/pi/Forecast-Printer/Adafruit_Thermal.py", line 95, in __init__
self.wake()
File "/home/pi/Forecast-Printer/Adafruit_Thermal.py", line 605, in wake
self.writeBytes(255)
File "/home/pi/Forecast-Printer/Adafruit_Thermal.py", line 189, in writeBytes
super(Adafruit_Thermal, self).write(chr(arg))
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 518, in write
d = to_bytes(data)
File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 63, in to_bytes
raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq))
TypeError: unicode strings are not supported, please encode to bytes: 'ÿ'
代码是 运行 on a raspberry pi 运行ning raspbian (GUI version) 需要 运行 with sudo
因为它通过串口与迷你热敏打印机通信。
提前感谢您的帮助:)
编辑: 下面是一些代码,当 运行 in python 3:
时会触发相同的错误from Adafruit_Thermal import *
printer = Adafruit_Thermal("/dev/serial0", 19200, timeout=5)
从堆栈跟踪来看,打印机初始化时,Adafruit 库通过串行写入整数 255
来唤醒打印机。它通过在第 189 行调用 Serial
class 的 write()
方法(在它自己的 write_bytes()
方法中)来实现:
super(Adafruit_Thermal, self).write(chr(arg))
它能够做到这一点,因为 Adafruit_Thermal
class 继承自 Serial
,如您在 class 定义中所见。
正如@abarnert 指出的那样,chr()
returns 是一个字符串,但听起来 write()
需要二进制数据。因此,我会尝试对您的问题发表评论中的建议。首先尝试将第 189 行更改为:
super(Adafruit_Thermal, self).write(arg)
如果不行试试:
super(Adafruit_Thermal, self).write(bytes([arg]))