Hi list,
I've been using modbus_tk successfully for a few years, for communicating with various systems in power generation and distribution sector. Usually the PC contacts the equipment to fetch data or send commands; the equipment acts as a normal modbus slave, it works fine.
Recently, I've been asked to develop software for some modbus slave devices that can call the Scada on alarm condition. When the device calls, the PC is supposed to send a bogus modbus request, then the device sends his slave address; then a normal modbus dialog can proceed. I've been able to implement a working script for modbus_rtu by reusing the rs232 object. Here is some test code:
rs232 = serial.Serial(port='/dev/ttyS0', baudrate=9600, bytesize=8,
parity='N', stopbits=1, xonxoff=False, timeout=30)
...
# Réponse à une alarme : superviseur envoie en diffusion (adresse 0) un message
# invalide (code de fonction 0). Les 2 derniers octets sont le CRC 16 bits.
# modbus_tk ne permet pas d'envoyer cette trame, écriture directe sur le port.
rs232.write('\x00\x00\x01\xB0')
# Lecture réponse (5 octets), par exemple :
# 01 80 00 41 C0
# \ \ \ CRC 16 bits
# \ \ code exception -> sous-adresse
# \ fonction reçue (0) avec bit de poids for à 1
# adresse modbus
x = rs232.read(size=5)
esclave = ord(x[0])
sous_adr = ord(x[2])
# Dialogue modbus normal : lecture évènements en un seul bloc
# (mot échange + 4 évènements, chacun sur 8 mots)
print u'Lecture évènements...'
res = None
try:
mb = modbus_rtu.RtuMaster(rs232)
mb.open()
res = mb.execute(esclave, cst.READ_INPUT_REGISTERS, 0x000F, 33)...
This is working fine.
Now I need to do something similar for modbus_tcp... but that seems much more difficult, and this is where I'm looking for help. The idea I had was to use
SocketServer.TCPServer or socket.socket to create a server waiting for incoming connections, then reuse that connection for modbus_tcp (like above for modbus_rtu). I have naively tried to modify modbus_tcp.
TcpMaster so that it can take an existing socket as argument, but then modbus_tk fails (in hooks if I remember correctly). Is it possible to make it work with minimal changes to modbus_tk?
Thanks,
--
Jean-Denis Girard
SysNux Systèmes Linux en Polynésie française
http://www.sysnux.pf/ Tél: +689 50 10 40 / GSM: +689 79 75 27