added python script to craft messages

This commit is contained in:
joBr99
2022-01-15 17:19:04 +01:00
parent 763ed45f8a
commit 1814ce999b

45
test-msg-gen.py Normal file
View File

@@ -0,0 +1,45 @@
import binascii
import struct
import codecs
print("Calculate NSPanel hex command from JSON payload\n")
#value = input("Enter JSON:\n")
value = "entityUpdHeading,Heading1234"
def crc16(data:bytes, poly:hex=0xA001) -> str:
'''
CRC-16 MODBUS HASHING ALGORITHM
'''
crc = 0xFFFF
for b in data:
crc ^= b
for _ in range(8):
crc = ((crc >> 1) ^ poly
if (crc & 0x0001)
else crc >> 1)
return crc
payload = bytes(value, 'ascii')
header = binascii.unhexlify('55BB')
print("length:", len(value))
length = len(value).to_bytes(1, 'little')
bytes_payload = header + length + payload
#print("bytes_payload:", bytes_payload)
msg_crc = crc16(bytes_payload)
#print('{:04x}'.format(msg_crc))
crc=struct.pack('H', msg_crc)
command=binascii.hexlify(bytes_payload + crc)
print("\n\n")
print(bytes.decode(command))