2014-02-11 67 views
0

我有以下簡單的協議框架。爲了測試,我假設一個固定的CRC字段,但現在我需要添加從幀中先前字節計算出的實際CRC。怎麼做?也許通過嵌套結構?使用Construct添加CRC字段python庫

MyFrame = Struct("MyFrame", 
       ULInt8("type"), 
       ULInt8("IDMsg"), 
       ULInt8("totalPackets"), 
       ULInt8("numPacket"), 
       ULInt8("day"), 
       ULInt8("month"), 
       ULInt8("year"), 
       ULInt8("hour"), 
       ULInt8("minute"), 
       ULInt8("second"), 
       ULInt16("length"), 
       Bytes("payload", lambda ctx: (ctx.length - 14)), 
       ULInt16("crc") 
      ) 

回答

0

如果要檢查CRC字段是否正確,請查看Validator Adapter。覆蓋_Validate喜歡的東西:

_validate(self, obj, context): 
    return obj.crc == crc(context.type, context.idmsg, . . .) 

凡CRC是您的CRC功能,你在使用方面的參數,其餘爲食。

如果要將其他字段添加到最終容器,請使用「值」。

Value('calculated_crc', lambda ctx: crc(ctx.type, ctx.idmsg, . . .))