Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to unpack the following struct from binary data received via UDP from a time attendance clock device(DeviceName=TA8020):

C++
typedef struct _AttLog_{
Int PIN; //U16 PIN, user number
char verified;// verifying method
time_t time_second; //time, time code is user-defined time code.
char status;// attendance state
}TAttLog, *PAttLog;


Trying to unpack the data like this:

Python
uid, state, timestamp, space = unpack( '24s1s4s11s', attendancedata.ljust(40)[:40] )
print "%s, %s, %s, %s" % (uid, state, space, decode_time( int( reverseHex( timestamp.encode('hex') ), 16 ) ) )

yields the following result:

Python
5, ,   , 2016-02-13 11:55:36


The uid and timestamp are correct but I am unable to get the correct values for char verified and char status as you can see unpacking the struct like above they are returned empty.

When trying to unpack the char status and char verified separately like this:

Python
state = unpack('>3c',attendancedata[17:20])
space = unpack('>2c',attendancedata[21:23])

yields:

Python
statem ('\x00', '\x00', '\x00')
statev 0

every time which are not the correct values.(verified by looking at the web interface logs of the time attendance device)

Unpacking like:

Python
oneSet = unpack('24s1s4s11s',attendancedata.ljust(40)[:40])

yields:
Python
('5\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00','\x01','h\x1b\xe0\x1e','\x01\x00\x00\x00\x00\x00\x00\x00\x00\xb2\x00')


The API documentation gives details as follows:

Only read command can be used to read all attendance logs. Attendance log can be compressed in long or short mode. The compressing method (if char *Buffer is being read now, the hand will be at the first byte) is: the first 2 bytes are used for storing user PIN (U 16 PIN), the front three bits of the third byte are used for storing verifying state. The fourth and fifth bits are to store verifying method. The sixth bit is to store short and long time sign. If it is short time format, time value is the last two bits of the third byte and the third byte plus the recent long time value (therefore, time format is stored as the misregistration value of the former long time value). Then decode it according to time coding mode (refer to user-defined coding mode) to get the correct time.

Any help would be much appreciated.

What I have tried:

I have tried unpacking it as posted above.
Posted
Comments
Sergey Alexandrovich Kryukov 13-Feb-16 14:28pm    
Your problem could be data alignment/padding: https://en.wikipedia.org/wiki/Data_structure_alignment.
You cannot not assume that data comes byte by byte without gaps (called "padding"). One of the way to handle that is using the same technology, language, compiler, compiler settings, etc...
Another potention problem is byte order.
With cross-plattform/cross-language development, you always face such problems.
—SA

1 solution

Thanks - I was able to figure it out:

Python
state = unpack('c',attendancedata[29:30])
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900