I have the following code running in the script console as a rough test, it is a Modbus broadcast using function 16 and it works OK except any values above INT127 or HEX 7F causes a failure, I really need to be-able to write up to 255 or FF.
its easiest to change the “data” bytes and it needs an open port on the slave
import socket import struct #write Modbus Function 16 # Slave IP address IP = "192.168.2.106" #Slave Port No PORT = 502 #Set UP Modbus headers (Four Byte) Modbus = (0,1,0,0,) # Length Total bytes after this (Two Bytes) Length = (0,11,) #Modbus Unit ID (one Byte) unit = (0,) #Modbus Function (one Byte) Function = (16,) # Modbus register start (Two Bytes) Ref = (0,2,) #total Word count (two Bytes) WCount = (0,2,) #Total byte Count (one Byte) BCount = (4,) #Data to write (See Byte Count) data = (0,123,0,127) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) MESSAGE = (Modbus+Length+unit+Function+Ref+WCount+BCount+data) print MESSAGE sock.connect((IP, PORT)) sock.send(MESSAGE) sock.close()
What have I done wrong? its driving me silly.