Python Forum
Pack binary data to list of integers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pack binary data to list of integers
#1
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.

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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 317 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,181 Mar-24-2023, 08:34 AM
Last Post: fullytotal
Question How to append integers from file to list? Milan 8 1,358 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  Error "list indices must be integers or slices, not str" dee 2 1,393 Dec-30-2022, 05:38 PM
Last Post: dee
  read a text file, find all integers, append to list oldtrafford 12 3,368 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,499 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  Why changing data in a copied list changes the original list? plumberpy 3 2,190 Aug-14-2021, 02:26 AM
Last Post: plumberpy
  How to convert binary data into text? ZYSIA 3 2,585 Jul-16-2021, 04:18 PM
Last Post: deanhystad
  JS Buffer.from VS struct.pack DreamingInsanity 3 2,407 Apr-05-2021, 06:27 PM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020