Using Pyserial to Send Binary Data

Binary data with pyserial(python serial port)

You need to convert your data to a string

"\xc0\x04\x00"

Null characters are not a problem in Python -- strings are not null-terminated the zero byte behaves just like another byte "\x00".

One way to do this:

>>> import array
>>> array.array('B', [0xc0, 0x04, 0x00]).tostring()
'\xc0\x04\x00'

Sending binary data over a serial connection with pyserial

Something like

port.write(struct.pack('<cii', 'd', address, length)
d = port.read(1) #'d' expected
data = port.read(length) #length bytes of data expected
if d != 'd' or len(data) < length:
raise Exception("Bad response received")

where < specifies little-endian byte order, c is a single char and i is a 32-bit int.

pySerial and reading binary data

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.

How to write and read binary (not ASCII encoded) to Arduino with pyserial

usb.write(command) expects command to be of type bytes not int.

It seems the write method internally calls bytes(), since it sends the number of zero bytes that you called it with.

Calling bytes() with an integer, creates a zero filled byte array, with the specified number of zeroes.

If you want to send a single byte, you need to call it with a list with a single element.

You should do:

command = bytes([int(input("Enter command: "))])

Read complete binary message using pySerial

I think you're looking for either the serial.readline() or
serial.read_until() methods:

  • https://pyserial.readthedocs.io/en/latest/pyserial_api.html#serial.Serial.readline

  • https://pyserial.readthedocs.io/en/latest/pyserial_api.html#serial.Serial.read_until

Both allow variable length messages as long as the same ending delimiter is found. Both work well but the newline character can occasionally be a normal part of the binary data being transmitted which causes difficulty for serial.readline(). If you can detect the message frame's ending character, or delimiter then serial.read_until() would be useful.

Using an encoding method like COBS that guarantees removal of a delimiter character (the zero byte, b'\x00') will help find the end of each message. If you cannot change the sending protocol, you may need to search for the delimiter and packet structure it's using.

Reading binary data with PySerial from serial port

It seems that PySerial (or a library that Pyserial depends on) is translating a single "0x0a" (\n) character into two characters "0x0d 0x0a"(\r\n). Both communication end-points are running on Linux, so I am not sure why someone would like to even translate those line endings at all...

Here strace indicates that sender sends only \n to ttyS0:

write(1, "M\n", 2)                      = 2
write(1, "\n", 1) = 1
write(1, "M\n", 2) = 2
write(1, "\n", 1) = 1

While debugging PySerial output I saw that each \n is prefixed with a \r.

Before claiming that this as a Bug I will do further investigation to find out who and why adds this carriage return...

Send Byte serially using PySerial in Python 3

>>> struct.pack('<I', 1234567)
b'\x87\xd6\x12\x00'
>>> struct.pack('>I', 1234567)
b'\x00\x12\xd6\x87'

Send one of those, and then read 4 bytes on the other side into a long.



Related Topics



Leave a reply



Submit