Last active
August 15, 2016 20:43
-
-
Save kwilcox/02d4695420143dba3ca134d7eea04c7f to your computer and use it in GitHub Desktop.
Campbell Scientific 18-bit binary format decode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jSO@_`@Be@@T@@qB\P@@@@Ao@B@@@@@RpjSK@Be@]lLPE@_`@BeTWUDsIC$l@BD@@C@@C@DX@En@BS@I~$~z@@@@ND@@F@@|LPE@_`@BeSWGBhG}cqLPE@_`@BeSXMBhG}cqLPE@_`@BeSYRBhG}cqLPE@_`@BeSZXBhG}cqLPE@_`@BeS[]BhG}cqLPE@_`@BeS\cBhG}cqLPE@_`@BeS]hBhG}cqLPE@_`@BeS^nBhG}cqLPE@_`@BeS_sBhG}cqLPE@_`@BeS`xBhG}cqLPE@_`@BeSa~BhG}cqLPE@_`@BeScCBhG}cqLPE@_`@BeSdIBhG}cqLPE@_`@BeSeNBhG}cqLPE@_`@BeSfTBhG}cqLPE@_`@BeSgYBhG}cqLPE@_`@BeSh_BhG}cqLPE@_`@BeSidBhG}cqLPE@_`@BeSjiBhG}cqLPE@_`@BeSkoBhG}cqLPE@_`@BeSltBhG}cqLPE@_`@BeSmzBhG}cqLPE@_`@BeSn$BhG}cqLPE@_`@BeSpEBhG}cqLPE@_`@BeSqJBhG}cqLPE@_`@BeSrPBhG}cqLPE@_`@BeSsUBhG}cqLPE@_`@BeStZBhG}cqLPE@_`@BeSu`BhG}cqLPE@_`@BeSveBhG}cqLPE@_`@BeSwkBhG}cqLPE@_`@BeSxpBhG}cqLPE@_`@BeSyvBhG}cqLPE@_`@BeSz{BhG}cqLPE@_`@BeS|ABhG}cqLPE@_`@BeS}FBhG}cqLPE@_`@BeS~KBhG}cqLPE@_`@BeS$QBhG}cqLPE@_`@BeT@VBhG}cqLPE@_`@BeTA\BhG}cqLPE@_`@BeTBaBhG}cqLPE@_`@BeTCgBhG}cqLPE@_`@BeTDlBhG}c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q@uX@t_@Cg@AqB\$@DF@BRBWM@@d@_V@@A@@@Ar[@@@@qJAm~B^a}cq}cq@u$@t~@C~@ALChT@DP@AbCNQ@@d@_P@@A@@@Ara@@@@qeAhLB^c}cq}cq@va@uX@Bx@ALDqO@B|@AcDqo@@d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def decode_binary_message(bites): | |
""" | |
Takes Cambell Scientific High Resolution 18-Bit Binary Format and converts | |
it to a CSV row. | |
""" | |
row = [] | |
while True: | |
# Number are three bytes long | |
byte_s = bites.read(3) | |
if not byte_s: | |
break | |
# logger.debug('Bytes: {}'.format(byte_s, type(byte_s))) | |
members = [] | |
for i, b in enumerate(byte_s): | |
# binary = '{0:08b}'.format(b) | |
# logger.debug('Binary {}: {}'.format(i, binary)) | |
# Apply mask to remove first two bits | |
mask = 0b00111111 | |
masked = mask & b | |
masked_bin = '{0:08b}'.format(masked)[2:] | |
# logger.debug('Masked {}: {}'.format(i, masked_bin)) | |
members.append(masked_bin.encode()) | |
sofar = b''.join(members) | |
# logger.debug('Combined mask: {}'.format(sofar)) | |
integ = int(sofar, 2) | |
# This correct subtracts one and takes the complement if | |
# the value should be negative per the Campbell Scientific docs | |
# It's a typical two's complement | |
# http://stackoverflow.com/a/9147327 | |
bits = 18 | |
if (integ & (1 << (bits - 1))) != 0: | |
integ = integ - (1 << bits) | |
row.append(str(integ)) | |
return ','.join(row) | |
with open('binary1.dat', 'rb') as f: | |
print(decode_binary_message(f)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment