Hi,
I'm working for a while on different possibility to implement a Modbus Slave on TCP, in order to make a kind of PLC for my house
Thanks to this great lib, my work has seen some progress, but I'm not a good programmer and I'm not habitued to Python so it's hard for me ...
My Modbus Master is made with Mango from Serotonine Software.
On the slave I'm writing in Python, I've been able to reflect coils values into a register that the master can successfully read but I don't understand how to force the Data of the slave register when sending a Write Holding Register (func 06) from the master. In fact I don't know how to read this value on the slave.
I guess, this can be done with the get_values function of my object but I can't see any change.
here is my code :
#!/usr/bin/env python
import Adafruit_BBIO.GPIO as GPIO
import threading
import time
import modbus_tk
import modbus_tk.modbus_tcp as modbus_tcp
import modbus_tk.defines as mdef
class Scan_GPIO(threading.Thread):
def __init__(self, nom = ''):
threading.Thread.__init__(self)
self.nom = nom
self.Terminated = False
def run(self):
pin8_13 = 0;
register = 0
GPIO.output("P8_42", GPIO.HIGH)
slave1.set_values("a", 0, register)
while not self.Terminated:
# before reflecting local changes, I try to catch the value sent by the master ...
register2 = slave1.get_values("b", 1);
print "Register2 : ",register2
time.sleep(1)
if (GPIO.input("P8_13") and not pin8_13):
pin8_13 = 1;
#print "P8_13 Pressed !"
if GPIO.input("P8_42"):
GPIO.output("P8_42", 0)
print "GPIO P8_42 LOW"
register |= 0x01
else:
GPIO.output("P8_42", 1)
print "GPIO P8_42 HIGH"
register &= 0xfe
elif ( not GPIO.input("P8_13") and pin8_13):
pin8_13 = 0;
slave1.set_values("a", 0, register)
time.sleep(0.02)
def stop(self):
self.Terminated = True
logger = modbus_tk.utils.create_logger(name="console", record_format="%(message)s")
server = modbus_tcp.TcpServer(address='172.16.17.173')
slave1 = server.add_slave(1)
slave1.add_block("a", mdef.HOLDING_REGISTERS, 0, 1)#address 0, length 1
slave1.add_block("b", mdef.HOLDING_REGISTERS, 1, 1)#address 1, length 1
j = 0
slave1.set_values("a", 0, j)
server.set_verbose(1)
server.start()
GPIO.setup("P8_42", GPIO.OUT)
a = Scan_GPIO('GPIO Scan')
a.start()
while not GPIO.input("P8_41"):
# Do nothing
b = 1
a.stop()
server.stop()
GPIO.cleanup()
And here a en extract of the requests :
('172.16.17.250', 55628) is connected with socket 9...
-->3-192-0-0-0-6-1-3-0-0-0-1 <- Function 3, to read the data on the slave
<--3-192-0-0-0-5-1-3-2-0-0
('172.16.17.250', 55629) is connected with socket 9...
-->3-193-0-0-0-6-1-6-0-0-0-2 <- Function 6, When forcing coil2 from the master
<--3-193-0-0-0-6-1-6-0-0-0-2 <- The slave answer is good ...
Register2 : (0,) <- ... but I can't see the change on the local register
9 is disconnected
Register2 : (0,)
GPIO P8_42 LOW
Register2 : (0,)
Register2 : (0,)
('172.16.17.250', 55664) is connected with socket 10...
-->3-228-0-0-0-6-1-3-0-0-0-1 <- Function 3, to read the data on the slave
<--3-228-0-0-0-5-1-3-2-0-1<- When changing state of coil1 on the slave, the information is well sent to the master
10 is disconnected
Can anybody tell me how to read the result of the Function 6 frmae sent by the master to force local registers ?
Thanks,
Nicolas