This is my first time using stack overflow, if I did something wrong, forgive me, please I have to control a thermal chamber by Modbus and got a sample code in python 2.7
import socket
session = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
session.connect((HOST,PORT))
array = [1, 6, 0, 2, 11, 184, 47, 72]
#ID,Write,Register,how many registers->2,data,data,CRC,CRC
binstring = ''
for item in array:
binstring += chr(item)
session.send(binstring)
the binstring in debug mode is '\\x01\\x06\\x00\\x02\\x0b\\xb8/H'
I try to modify the for loop as below (python3.6)
for i in range(len(array)):
array[i] = bytes([array[i]]) #method1
array[i]=str(array[i]).encode('utf-8') #method2
binstring= array[0]+array[1]+array[2]+array[3]+array[1]+array[4]+array[5]+array[6]+array[7]
binstring in method1 debug mode: b'\x01\x06\x00\x02\x06\x0b\xb8/H'
binstring in method2 debug mode: b'16026111844772'
Both 2 methods not work... Also try to modify the send function
#session.send(binstring)
session.sendall(binstring)
I need more suggestions
Thank you