【问题标题】:python struct unpack: Int PIN; char verified; time_t time_second; char status;python struct unpack: Int PIN;字符验证; time_t time_second;字符状态;
【发布时间】:2016-02-13 13:53:22
【问题描述】:

我正在尝试从考勤时钟设备 (DeviceName=TA8020) 通过 UDP 接收的二进制数据中解压缩以下结构:

 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;

尝试像这样解压数据:

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 ) ) )

产生以下结果:

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

uid 和时间戳是正确的,但我无法获得正确的 char verify 和 char status 值,因为您可以看到像上面那样解压结构,它们返回为空。

当尝试解压 char status 和 char 单独验证时:

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

产量:

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

每次都是不正确的值。(通过查看考勤设备的Web界面日志验证)

解包如下:

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

收益: ('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')

API 文档给出的详细信息如下:

只有读取指令可以读取所有考勤记录。考勤日志可以以长模式或短模式压缩。压缩方法(如果现在正在读取char *Buffer,则手在第一个字节)是:前2个字节用于存储用户PIN(U 16 PIN),使用第三个字节的前三位用于存储验证状态。第 4 和第 5 位存储验证方法。第 6 位存储短时和长时符号。如果是短时间格式,时间值是第三个字节和第三个字节的最后两位加上最近的长时间值(因此,时间格式存储为前一个长时间值的失准值)。然后根据时间编码方式(参考用户自定义编码方式)解码得到正确的时间。

任何帮助将不胜感激。

【问题讨论】:

  • 那是一个 sh*t API 文档;这些类型都没有标准尺寸,并且必须有填充。
  • 是的,它很难理解...不是最好的英语。关于我可以尝试的填充有什么想法吗?

标签: python struct binary-data unpack time-and-attendance


【解决方案1】:

好的,我想通了:

state = unpack('c',attendancedata[29:30])

是我需要的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 2011-02-21
    相关资源
    最近更新 更多