o模块的TextIOWrapper或BuffereRWPair函

o模块的TextIOWrapper或BuffereRWPair函

本文介绍了Python io模块的TextIOWrapper或BuffereRWPair函数与pySerial不能很好地兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing a serial adapter for some scientific hardware whose command set uses UTF-8 character encodings. All responses from the hardware are terminated with a carriage return (u'\r'). I would like to able to use pySerial's readline() function with an EOL character specified, so I have this setup, ala this thread:

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()

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

Strangely, the first command string (non-unicode using pySerial directly) elicits the correct behavior from the hardware. The second (unicode via Python's io module) causes it to move erratically and then hang. Why would this be? Sending unicode command strings to the hardware does work IF the command string is only a couple of a characters. Once you start sending bytes with hex(ord(byte)) values > 0x7F (outside ASCII range), then you start running intro trouble. I can work around this problem without too much trouble, but would like to know what is going on. Thanks!

推荐答案

来自:

我猜这是你的问题,因为你将同一个对象 ser 作为读者和作者传递。 BufferendRandom看起来也不像账单。

I'm guessing that's your problem, as you are passing same object ser as reader and writer. BufferendRandom doesn't look like it quite fits the bill either.

你挂起的问题是 serial 等待EOL?

So is your problem with serial that it hangs waiting for the EOL?

这篇关于Python io模块的TextIOWrapper或BuffereRWPair函数与pySerial不能很好地兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 07:21