mirror of
https://github.com/joBr99/nspanel-lovelace-ui.git
synced 2025-12-19 14:14:12 +01:00
added python script to craft messages
This commit is contained in:
45
test-msg-gen.py
Normal file
45
test-msg-gen.py
Normal 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))
|
||||
Reference in New Issue
Block a user