17

serial.write() method in pyserial seems to only send string data. I have arrays like [0xc0,0x04,0x00] and want to be able to send/receive them via the serial port? Are there any separate methods for raw I/O?

I think I might need to change the arrays to ['\xc0','\x04','\x00'], still, null character might pose a problem.

asked Jan 23, 2009 at 14:02

3 Answers 3

12

An alternative method, without using the array module:

def a2s(arr):
 """ Array of integer byte values --> binary string
 """
 return ''.join(chr(b) for b in arr)
answered Aug 15, 2009 at 14:19
Sign up to request clarification or add additional context in comments.

1 Comment

map may be even quicker: def a2s(arr): return ''.join(map(chr,arr))
11

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'
answered Jan 23, 2009 at 14:25

Comments

1

I faced a similar (but arguably worse) issue, having to send control bits through a UART from a python script to test an embedded device. My data definition was "field1: 8 bits , field2: 3 bits, field3 7 bits", etc. It turns out you can build a robust and clean interface for this using the BitArray library. Here's a snippet (minus the serial set-up)

from bitstring import BitArray
cmdbuf = BitArray(length = 50) # 50 byte BitArray
cmdbuf.overwrite('0xAA', 0) # Init the marker byte at the head

Here's where it gets flexible. The command below replaces the 4 bits at bit position 23 with the 4 bits passed. Note that it took a binary bit value, given in string form. I can set/clear any bits at any location in the buffer this way, without having to worry about stepping on values in adjacent bytes or bits.

cmdbuf.overwrite('0b0110', 23)
# To send on the (previously opened) serial port 
ser.write( cmdbuf )
answered Nov 16, 2019 at 3:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.