Python io functions TextIOWrapper or BuffereRWPair do not work well with pySerial

I am writing a serial adapter for some scientific equipment whose set uses UTF-8 character encodings. All responses from the hardware end with a carriage return (u '\ r'). I would like to use the pySerial readline() function with the specified EOL character, so I have this setting, 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() 

Oddly, the first command line (non-Unicode using pySerial directly) causes the correct hardware behavior. The second (unicode via the Python io module) makes it move randomly and then freeze. Why should it be? Sending Unicode command lines to the hardware runs IF , and the command line only has a few characters. After you start sending bytes with hex(ord(byte)) values> 0x7F (outside the ASCII range), you will start to run intro problems. I can get around this problem without much trouble, but would like to know what is going on. Thanks!

+1
source share
1 answer

From io docs :

BufferedRWPair does not attempt to synchronize access to its main raw streams. You should not give him the same object as the reader and writer ; use BufferedRandom instead.

I assume that your problem is because you are passing the same ser object as a reader and a writer. BufferendRandom is unlikely to be a good fit for counting.

So, your problem with serial is that it expects EOL expectations?

0
source

Source: https://habr.com/ru/post/944659/


All Articles