G
We have used the following packet format for Modbus(master) TCP/RTU implementation:
MBAP header:
TransId(2bytes),
protocol Id(2bytes)(0 for modbus),
MessageLength(2 bytes),
UnitId(1 bytes)
MB PDU :
FunctionCode(1byte),
Address(2 bytes),
Qty(qty of data to be read)
The python implementation for modbus-tcp read coil request is:
import time
import socket
import struct
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost",502))
#--ReadCoil - request
data = struct.pack('>HHHBBHH',
01, 00, 06, 00,
01, 01, 01)
#data sent will be :
'\x00\x01\x00\x00\x00\x06\x00\x01\x00\x01\x00\x01'
client_socket.send(data)
time.sleep(2)
print client_socket.recv(2056)
Here the connection is made to a software PLC which is configured as TCP server. We could get some response for this.
We tried the same for UDP just by making the connection to a Modbus UDP Slave at ('localhost',5001) as
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket.connect(("localhost",5001))
It sets the error count in the PLC (which indicated that the data has been sent and there is some error) and hangs while recieving the response, at sock.recv(2056) statement. We couldn't guess the cause for the error.
Can anyone help me in making out the differences between Modbus UDP and TCP implementation?
Is the difference only in transportation of packets (taking as STREAM or DGRAM option) or is it in the packet format also? Any checksum related differences? How should we format it to get a proper response?
Any help in this regard will be greatly appreciated.
Thanks in Advance,
Gayatri
MBAP header:
TransId(2bytes),
protocol Id(2bytes)(0 for modbus),
MessageLength(2 bytes),
UnitId(1 bytes)
MB PDU :
FunctionCode(1byte),
Address(2 bytes),
Qty(qty of data to be read)
The python implementation for modbus-tcp read coil request is:
import time
import socket
import struct
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost",502))
#--ReadCoil - request
data = struct.pack('>HHHBBHH',
01, 00, 06, 00,
01, 01, 01)
#data sent will be :
'\x00\x01\x00\x00\x00\x06\x00\x01\x00\x01\x00\x01'
client_socket.send(data)
time.sleep(2)
print client_socket.recv(2056)
Here the connection is made to a software PLC which is configured as TCP server. We could get some response for this.
We tried the same for UDP just by making the connection to a Modbus UDP Slave at ('localhost',5001) as
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket.connect(("localhost",5001))
It sets the error count in the PLC (which indicated that the data has been sent and there is some error) and hangs while recieving the response, at sock.recv(2056) statement. We couldn't guess the cause for the error.
Can anyone help me in making out the differences between Modbus UDP and TCP implementation?
Is the difference only in transportation of packets (taking as STREAM or DGRAM option) or is it in the packet format also? Any checksum related differences? How should we format it to get a proper response?
Any help in this regard will be greatly appreciated.
Thanks in Advance,
Gayatri
