Python PySerial编码utf8 Windows与Linux

2024-10-01 13:27:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个使用PySerial的程序,它需要同时支持Windows和Linux。它是用python2.7forwindows和python2.7.4linux编写的。在

在Linux(Ubuntu)上,当我向串行端口写入一个字符串时

"""Write a string to the serial port and guarantee it was wrtiten out"""
serialPort.write(serialPattern)
serialPort.flush()

我得到以下错误:

^{pr2}$

同样的代码在Windows中可以完美地工作。最后,我尝试将字符串编码为utf-8,如下所示:

serialPort.write(serialPattern.encode("utf-8"))

现在看来它在两个平台上都能工作。在

所以我的问题是:为什么Python/PySerial for Windows会使用默认的ASCII编码,而pythonforlinux需要将字符串编码为UTF-8?在


Tags: 端口字符串程序编码stringubuntulinuxwindows