Member Avatar for tobycatlin

Hello everybody,
I am after a little help using python to read in data from a binary file. This is what i got so far:

import struct

infile = "friday.bin"

FH = open(infile, 'rb')
line = FH.read(32)

data= struct.unpack("<H", line[0:2])
print data

Which produces an output: 38288
I have a data format structure (listed below) and i wanted to know the best way to define this structure and pass it to unpack. The documentation is a little light on this area. I thought it would be a good idea to retrive the year as i know this will be 2008

Thanks for any help or suggestions on how i can acheive this.

toby

The binary data format.
Record-Length = 32 bytes; Data are in little endium (Lower byte = LSB).
-----------------------------------------------------------------------------------
Field Offset Length Description
-----------------------------------------------------------------------------------
Fix 0 1 Unsigned 8 bit data, The fix status; 0,2,3(D) 8 + if DGPS used.
Su 1 1 Unsigned 8 bit data, Satellite No used in fix.
Hdop 2 2 Unsigned 16 bit data, = (HDOP value)*10.
Year 4 2 Unsigned 16 bit data, The year, (2002) for example.
Month 6 1 Unsigned 8 bit data, The month, 1-12.
Day 7 1 Unsigned 8 bit data, The day, 1-31.
Hour 8 1 Unsigned 8 bit data, The hour, 0-23.
Minute 9 1 Unsigned 8 bit data, The minute, 0-59.
Ms 10 2 Unsigned 16 bit data, The millisecond, 0-59999
Lat 12 4 Signed 32 bit data, The latitude, =(DDMM.mmmm)*10000
>0 : North, <0 : South.
Lon 16 4 Signed 32 bit data, The longitude, =(DDDMM.mmmm)*10000
>0 : East, <0 : West.
Signed 32 bit data, The altitude, =(Altitude: meter)*10
Alt 20 4
Vel 24 2 Unsigned 16 bit data, The Velocity, =(VEL:Km/hr)*10
Cog 26 2 Unsigned 16 bit data, The course over ground, =(COG:degree)*10
Type 28 2 Unsigned 16 bit data, Record Type, NOT Used currently.
Rev 30 1 Unsigned 8 bit data, Reserved, NOT Used currently.
Cks 31 1 Unsigned 8 bit data, Internal check, NOT Used by User.

Recommended Answers

All 3 Replies

Member Avatar for tobycatlin

I can kinda see where your going with that but i am sure it can be done in a cleaner way along the lines of this:

import struct

infile = "friday.bin"

FH = open(infile, 'rb')
line = FH.read(28)
Magic = struct.unpack("<2B2H4BH3f2H", line)
print Magic

Will output:
(144, 149, 8336, 16435, 178, 2, 10, 55, 97, 0.0048828125, 2.3611832414348226e+21, 2.4493528796034062e-19, 16441, 690)

But these values are not correct, should be something like
(3, 5, 6.2, 2007, 2, 11, 12, 23, 34059, 5132.1926, 3.7167, 72, 5, 90)

I have listed the records what type they are and expected data. Can anyone see where i am going wrong?

The data fields are little endian seem to go:
fix - 1 byte Unsigned - type of fix, none, 2d, 3d - expected 0,2,3
su - 1 byte Unsigned - Number of sats used in the fix - expect 0-12
Hdop - 2 byte Unsigned - Quality of signal (lower is better) - expect 0.0 - 50.0
Year - 2 byte Unsigned - expect 2008
Month - 1 byte Unsigned - expect 1-12
Day - 1 byte Unsigned - 1-31
Hour - 1 byte Unsigned - 0-23
Minute - 1 byte Unsigned - 0-59
Ms - 2 byte Unsigned - 0-59999
Lat - 4 byte Signed
Lon - 4 byte Signed
Alt - 4 byte Signed - altitude in meters from sea level
Vel - 2 byte Unsigned - Speed in Km/hr
Cog - 2 byte Unsigned - Course over ground - expects 0 - 360
type - 2 byte Unsigned - not used
rev - 1 byte Unsigned - reserved - not used
cks - 1 byte Unsigned - internal checksum

Types from the struct docs:
Format C Type Python Notes
x pad byte no value
c char string of length 1
b signed char integer
B unsigned char integer 1 byte
h short integer (2 bytes)
H unsigned short integer (2 bytes)
i int integer 4 bytes
I unsigned int long 4 bytes
l long integer
L unsigned long long
q long long long (1)
Q unsigned long long long (1)
f float float 4 bytes
d double float 8 bytes
s char[] string
p char[] string
P void * integer

hi,

How do i convert binary format to string format in python.

Thanks,
Narendra.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.