def writePackets(self, pkts): """ write all pkt in pkts into the log @param pkts: items to write @return: None """ for i in pkts:#i是单个字典形式的要发送的数据包, pkts是从队列里取出来的字典的列表[{},{},{}] p = self.cmdPack.pack(i)#p是经过处理打包过后的十六进制字符流 logging.debug(_("Write Packet to Serial Port; [%s] with Binary Data [%s]"), i, str2hex(p)) if self.device: print repr(p)#打印十六进制流 self.device.write(p) else: logging.error(_("Fail to Write Packet to Serial port; Device=%s, which is closed or None"), self.device)
def unpackField(self, msg, packet, rules): """ internal function, used for unpack all fields from packet according to a rule @param msg: python dict @param packet: octet packet @param rules: dict, like self.commonField """ """ ========fc.commonFieldOnly============== [{'begin': 5, 'end': None, 'name': u'Length', 'format': '', 'title': u'Length', 'type': u'uint8'}, {'begin': 6, 'end': None, 'name': u'Cluster', 'format': '', 'title': u'Cluster', 'type': u'uint8'}, {'begin': 7, 'end': None, 'name': u'Type', 'format': '', 'title': u'Type', 'type': u'uint8'}, {'begin': 8, 'end': None, 'name': u'SubType', 'format': '', 'title': u'SubType', 'type': u'uint8'}, {'begin': 9, 'end': None, 'name': u'Data', 'format': u'hex', 'title': u'Data', 'type': u'binary'}] """ print rules for i in rules: name = i["name"] fbegin = i["begin"] fend = i["end"] ftype = i["type"] try: s = self.structParser.unpack(ftype, packet[fbegin:fend] ) except: logging.error("Unpack Error for Packet=[%s] with Format=%s @ field name=%s", str2hex(packet), rules, name) ## raise msg[name] = s