Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modbus Plugin
#7
I played around with the signal plugin and finally got it working so far. Before I needed to resolve some issues with the cabling and some strange interaction with the serial monitor within the simplemodbus library.

What I did:

I only added one new line to the valves section of signalling_examples.py:

Code:
### valves ###
def notify_zone_change(name, **kw):
   print "zones changed"
   print gv.srvals
   #This is the new part for modbus interaction
   #gv.srvals is an array which contains the state of the valves
   modbus_control.write_to_slave(gv.srvals)


I creaded a module named modbus_control.py:

Code:
#!/usr/bin/env python
import minimalmodbus

#The Arduino library SimpleModbusSlave only supports function codes 3 and 16, which is reading (code 3)
#and writing (code 16) to Analog Output Holding Registers (register number 40001-49999)
#Therefore we will only use these two function codes:
#Within minimalmodbus we are using method "read_register" to read from the register, it will use
#function code 3 by default: read_register(REGISTERADDRESS,DECIMALNUMBER)
#To write we are using method "write_register" which is using function code 16 by default

#Initialize the modbus interface
instrument = minimalmodbus.Instrument('/dev/ttyUSB1', 1, 'rtu') # port name, slave address (in decimal)

# The following function writes the values received from the parent script to the modbus slave
# The variable ventil is an array with the status of the valves called from the parent script (e.g. signalling_examples.py)
def write_to_slave(ventil):
   print ("Function write_to_slave triggered with value: {}".format(ventil))

   #The following loop assumes a default amount of 8 valves inside the array ventil (default)
   #If the number has been changed from the default the loop need to be changed as well
   #Either implement a detection of array length (how many values are in the ventil array)
   #or exchange the number (here 8) manually
   i=8 #number of valves (equals to the number of entries in the array "ventil")
   x=i-1 #Counting starts from 0 in the array, therefore we need this additional step
   while i!=0:
       v=i-1
       #The following function writes the values to the modbus slave
       #i equals to the register number (starting with 1),
       #ventil[v] is the value for each valve, however this array starts with 0, therefore v instead of i
       instrument.write_register(i, ventil[v], 0) # Registernumber, value, number of decimals for storage
       i=i-1 #Decrement i by 1
Reply


Messages In This Thread
Modbus Plugin - by cyberthom - 2017 Mar 07, 03:54 PM
RE: Modbus Plugin - by dan - 2017 Mar 07, 10:54 PM
RE: Modbus Plugin - by cyberthom - 2017 Mar 08, 10:30 AM
RE: Modbus Plugin - by dan - 2017 Mar 08, 04:41 PM
RE: Modbus Plugin - by cyberthom - 2017 Mar 09, 07:11 PM
RE: Modbus Plugin - by dan - 2017 Mar 10, 03:00 AM
RE: Modbus Plugin - by cyberthom - 2017 Mar 23, 09:50 PM
RE: Modbus Plugin - by dan - 2017 Mar 24, 04:38 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)