Skip to content

Instantly share code, notes, and snippets.

@cwoodall
Last active December 31, 2015 07:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwoodall/7952754 to your computer and use it in GitHub Desktop.
Save cwoodall/7952754 to your computer and use it in GitHub Desktop.
Extract data from data_file which is a "binary string". Returns it in the format (Data[31 downto 8], Data[7 downto 0]) which is technically data, ADC channel... Created this after talking to Dean DeCarli.
import struct
from itertools import izip_longest
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx... From stackoverflow"
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
# Extract data from data_file which is a "binary string". Returns it in the format (Data[31 downto 8], Data[7 downto 0])
# which is technically data, ADC channel
data_file = "\x81\x82\x83\x01\x84\x85\x86\x02\x21\x30\xf1\x01"
[(h>>8, h&0xff) for i in grouper(4, data_file) for h in struct.unpack("!I",''.join(i))]
# Better solution
[struct.unpack("!IB",'\x00' + (''.join(i))) for i in grouper(4, data_file)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment