读取python中的arduino串行连接:未知字符

时间:2022-01-27 10:09:52

I'm using Python to talk to my Arduino using the pyserial module.

我正在使用Python使用pyserial模块与我的Arduino交谈。

import serial
ser=serial.Serial('/dev/ttyUSB0', 115200)

I get the following results:

我得到以下结果:

ser.read(20)
'y8\t0.16\t0.16\t-0.65\r\n'

That's what I need.

这就是我需要的。

But if I close port and open it again, I get the following:

但如果我关闭端口并再次打开它,我会得到以下结果:

ser.close()
ser=serial.Serial('/dev/ttyUSB0', 115200)
ser.read(20)
'4\xfa\x9a\xf9t\xff\x1a\xffA\xff<\xf2x\xf68\xf6A\xffA\xff'

What are these symbols? How do I get the correct characters?

这些符号是什么?我如何获得正确的字符?

My OS is Ubuntu

我的操作系统是Ubuntu

1 个解决方案

#1


These are unicode characters that are "backslash replaced". Basically, ASCII, which is a list of characters (like A or %), cannot represent all of the characters in Unicode (a longer list of characters than ASCII). I would suggest that you look at the codecs library to deal with unicode.

这些是“反斜杠替换”的unicode字符。基本上,ASCII是一个字符列表(如A或%),不能代表Unicode中的所有字符(比ASCII更长的字符列表)。我建议您查看编解码器库来处理unicode。

#1


These are unicode characters that are "backslash replaced". Basically, ASCII, which is a list of characters (like A or %), cannot represent all of the characters in Unicode (a longer list of characters than ASCII). I would suggest that you look at the codecs library to deal with unicode.

这些是“反斜杠替换”的unicode字符。基本上,ASCII是一个字符列表(如A或%),不能代表Unicode中的所有字符(比ASCII更长的字符列表)。我建议您查看编解码器库来处理unicode。