I need to send a modified version of Airbus (a weird modified version of Modbus) hex commands over RS485 to a serial device. The command that I want to send is 0x8181521500005315. I'm able to successfully send the command as a literal in the form:
b'\x81\x81\x15\x21\x00\x00\x53\x15'
but I'd like to somehow convert an int variable into the same format.
Here's what I have so far:
def advancedWriteR(param):
command = (0x818152*0x10000000000 + param*0x100000000 + genECC('r', param))
msg =command.to_bytes(8, byteorder='big')
ser.write(msg)
The strange thing is that msg becomes:
b'\x81\x81R\x15\x00\x00S\x15'
Any help is greatly appreciated!
b'\x81\x81R\x15\x00\x00S\x15' is exactly
b'\x81\x81\52\x15\x00\x00\53\x15'
You can get them back and force with the struct module:
>>> import struct
>>> struct.pack('>Q', 0x8181521500005315)
b'\x81\x81R\x15\x00\x00S\x15'
>>> struct.unpack('>Q', b'\x81\x81R\x15\x00\x00S\x15')
(9331830153036190485,)
>>> '0x{:x}'.format(9331830153036190485)
'0x8181521500005315'