Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pySerial and reading binary data

Tags:

pyserial

When the device I am communicating with sends binary data, I can recover most of it. However, there always seem to be some bytes missing, replaced by non-standard characters. For instance, one individual output looks like this:

\xc4\xa5\x06\x00.\xb3\x01\x01\x02\x00\x00\x00=\xa9

The period and equals sign should be traditional bytes in hexadecimal format (I confirmed this in another application). Other times I get other weird characters such as ')' or 's'. These characters usually occur in the exact same spot (which varies with the command I passed to the device).

How can I fix this problem?

like image 611
dataman Avatar asked Oct 03 '11 21:10

dataman


People also ask

What is PySerial used for?

PySerial is a library which provides support for serial connections ("RS-232") over a variety of different devices: old-style serial ports, Bluetooth dongles, infra-red ports, and so on. It also supports remote serial ports via RFC 2217 (since V2. 5).

What is PySerial API?

pySerial is a Python API module to access the serial port. pySerial provides a uniform API across multiple operating systems, including Windows, Linux, and BSD.

Is PySerial full duplex?

Yes serial port hardware is full duplex. Yes, you can use threads to do Rx and Tx at the same time. Alternatively, you can use a single thread loop that does reads with a short timeout and alternates between reading and writing.


1 Answers

Are you displaying the output using something like this?:

print output

If some of your bytes happen to correspond with printable characters, they'll show up as characters. Try this:

print output.encode('hex')

to see hex values for all your bytes.

like image 123
RichieHindle Avatar answered Oct 13 '22 04:10

RichieHindle