Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python io模块';的TextIOWrapper或BufferrPair函数不能很好地与pySerial配合使用_Python_Io_Pyserial_Python Unicode - Fatal编程技术网

Python io模块';的TextIOWrapper或BufferrPair函数不能很好地与pySerial配合使用

Python io模块';的TextIOWrapper或BufferrPair函数不能很好地与pySerial配合使用,python,io,pyserial,python-unicode,Python,Io,Pyserial,Python Unicode,我正在为一些使用UTF-8字符编码的科学硬件编写一个串行适配器。来自硬件的所有响应都以回车符(u'\r')终止。我希望能够使用pySerial的readline()函数并指定EOL字符,因此我有以下设置: 奇怪的是,第一个命令字符串(非unicode直接使用pySerial)从硬件中引出正确的行为。第二个(unicode通过Python的io模块)导致它不稳定地移动,然后挂起。为什么会这样?如果命令字符串只有两三个字符,则向硬件发送unicode命令字符串确实有效。一旦开始使用hex(ord(b

我正在为一些使用UTF-8字符编码的科学硬件编写一个串行适配器。来自硬件的所有响应都以回车符(u'\r')终止。我希望能够使用pySerial的
readline()
函数并指定EOL字符,因此我有以下设置:

奇怪的是,第一个命令字符串(非unicode直接使用pySerial)从硬件中引出正确的行为。第二个(unicode通过Python的io模块)导致它不稳定地移动,然后挂起。为什么会这样?如果命令字符串只有两三个字符,则向硬件发送unicode命令字符串确实有效。一旦开始使用
hex(ord(byte))
values>0x7F(超出ASCII范围)发送字节,那么就开始运行intro。我可以不费吹灰之力解决这个问题,但我想知道到底发生了什么。谢谢

来自:

BufferedRWPair不尝试同步对其 底层原始流您不应将其作为同一对象传递 读者和作者;改用

我猜这是你的问题,因为你传递的对象和读写器是相同的。BufferendRandom看起来也不太合适


您的
serial
问题是它挂起等待下线吗?

Derp,谢谢!我想当然地认为我所遵循的例子是有原则的解决方案。。。我猜他们一定没有发送很长的命令字符串。
import serial
import io

ser = serial.Serial(port='COM10', baudrate=128000)
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser, 1), encoding='utf-8', newline=u'\r')

ser.open()

# these commands move to coordintes (25000, 0, 25000)
cmd = 'M\x80\x1a\x06\x00\x00\x00\x00\x00\x80\x1a\x06\x00'
ucmd = u'M\x80\x1a\x06\x00\x00\x00\x00\x00\x80\x1a\x06\x00'

#this works
ser.write(cmd)
print sio.readline()

#this does not
sio.write(ucmd)
sio.flush()
print sio.readline()