Mar-29-2019, 12:17 PM
Trying to expand on a script for imax b6 charger which has USB HID interface.
Old code sends a list of integers to hidapi.write(). I've decoded some of the protocol and would like to generate custom USB packet instead of replaying sniffed data.
I've made a format string that gives me the correct output as a string, but the write method of the hidapi wants a list of integers as far as I can tell.
Any pointers on how to make the code more streamlined?
Old code sends a list of integers to hidapi.write(). I've decoded some of the protocol and would like to generate custom USB packet instead of replaying sniffed data.
I've made a format string that gives me the correct output as a string, but the write method of the hidapi wants a list of integers as far as I can tell.
LION = 1 DISCHARGE = 1 S1=1 def packbuff(operation, celltype=None, cellcount=None, chargecurrent=None, dischargecurrent=None, dischargecutoff=None, chargecutoff=None): #pack binary string into data packet if celltype is not None and chargecutoff is not None: strbuff = pack(">H3b4H8x", 0x0500, celltype, cellcount, operation, ((6000, chargecurrent)[chargecurrent < 6000]), ((2000, dischargecurrent)[dischargecurrent < 2000]), dischargecutoff, chargecutoff) strbuff = pack(">xH", 0x0F16) + strbuff + pack(">BH37x", sum(map(ord, strbuff))%256, 0xFFFF) #add checksum and padding #or into a short control packet else: strbuff = pack(">xHBxBH56x", 0x0F03, operation, operation, 0xFFFF) #cheksum = operation byte it seems? #then convert string to list of integers - must be a better way?! listbuff = [] for c in strbuff: listbuff.append(ord(c)) return listbuff discharge=[0x00, 0x0f, 0x16, 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0xe8, 0x03, 0xe8, 0x0c, 0x1c, 0x10, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] print discharge == packbuff(DISCHARGE, LION, S1, 1000, 1000, 3100, 4200)is the code I've come up with that generates the output that is identical to the list used by the script i used as a starting point but I'm failing to find out an easier way to pack the data into a buffer of the corret type without the inbetween step of a string.
Any pointers on how to make the code more streamlined?