Page 1 of 1

packing a uarray

Posted: Tue Mar 23, 2021 12:05 am
by ttmetro
I'd like to efficiently (without memory allocation) send binary data over a uart.

This is what I came up with:

Code: Select all

from array import array
from struct import pack

a = array('i', range(4))

# this works - is there a way to get rid of the loop?
# i.e. have C do the looping ...
for i in a:
    print(pack('i', i))

# TypeError: can't convert array to int
print(pack('4i', a))
The for loop works fine, but the "more efficient (?)" 2nd option does not (neither in CPython). Is there a "clever" solution?

Re: packing a uarray

Posted: Tue Mar 23, 2021 1:44 am
by ttmetro
Answered the question myself:

Directly write the array to the UART (no pack). If both ends of the connection use the same binary encoding (endian), this works.