mbunds
The PSM has been incredibly helpful for developing MODBUS server/peer routines, but without being intimately familiar with the way its code is compiled, integrated, and serviced by the main runtime application, I don't know if there is a "tipping point" where I might load it down "too much" and interfere with the functionality of the host runtime.

So far, the 12 connections made to "remote" peers from this instance are very quick to establish and solid, and the runtime seems to have no difficulty populating python variables with MODBUS data read from all of them, without (visibly) affecting the latency of the main logic, although I have yet to write the data transfer routines moving the data from python variables into the runtime variables.
 
I am working on writing the peer connection functions into their own libraries to reduce the bulk of entries into the PSM window, but given that the PSM was created to provide a place to integrate I/O devices, and was not created as a means to expand the entire runtime system, I wonder if you may be able to give me an idea about things to avoid.

Among the more "risky" routines which could potentially cause chaos with the main runtime are some multi-threaded routines which I would employ to perform connection monitoring among a few. If done carelessly, this could introduce thread race conditions, and/or who knows what else, so these will be very carefully integrated and tested.

Another "risky" routine is causing the runtime to spawn a tkinter dialog window to allow peer configuration, which may introduce some challenges for certain platforms, depending on the method selected to spawn a window, while expecting it to appear from the OpenPLC runtime web GUI...TBD anyway.

Testing every iteration will reveal bad ideas regardless, but I'd hate to collide with a deal-breaker after x-hundred hours of work.

Below is a very short and simple sample of the primitive "first try" PSM code I used to verify connections to multiple peers.
The latest code is fragmented as I am breaking it out into modules, and testing a primitive tkinter GUI and .ini file system to store and serve peer connection and register configuration data to the OpenPLC runtime when it starts a PLC project running this PSM code:

#import all your libraries here
import psm
import time
from pymodbus.client.sync import ModbusTcpClient
 
#global variables
counter = 0
ramp_up = 0
ramp_dn = 32767
vso_timer = 1000
vsc_timer = 1000
var_state = False
q00_state = False
i00_state = False
 
# Dictionary of PLC peers with their IP addresses
plc_peers = {
      "PLC05": "192.168.0.105",
      "PLC06": "192.168.0.106",
      "PLC07": "192.168.0.107",
      "PLC08": "192.168.0.108",
      "PLC09": "192.168.0.109",
      "PLC10": "192.168.0.110",
      "PLC11": "192.168.0.111",
      "PLC12": "192.168.0.112",
      "PLC13": "192.168.0.113",
      "PLC14": "192.168.0.114",
      "PLC50": "192.168.0.150",
      "PLC54": "192.168.0.154",
}
 
# Dictionary to hold Modbus TCP client connections
plc_connections = {}
 
def hardware_init():
      #Insert your hardware initialization code in here
      psm.start()
      for plc_name, plc_ip in plc_peers.items():
            client = ModbusTcpClient(plc_ip, port=502)
            if client.connect():
                  print(f"Successful connection to {plc_name} at {plc_ip}")
                  plc_connections[plc_name] = client # Store the client connection
            else:
                  print(f"Failed to connect to {plc_name} at {plc_ip}")
 
def update_inputs():
      #place here your code to update inputs
      read_coils()
      read_discrete_inputs()
      global counter
      global ramp_up
      global ramp_dn
      global vso_timer
      global vsc_timer
      global var_state
 
      psm.set_var("IX0.0", var_state)
      psm.set_var("IX0.7", not var_state)
      psm.set_var("IW0", ramp_up)
      psm.set_var("IW7", ramp_dn)
      counter += 1
      if (counter == 10):
          counter = 0
          var_state = not var_state
      ramp_up += 10
      if (ramp_up >= 32767):
          ramp_up = 0
          ramp_dn -= 5
      if (ramp_dn <= 0):
          ramp_dn = 32767

def update_outputs():
      #place here your code to work on outputs
      global q00_state
      global i00_state

      a = psm.get_var("QX0.0")
 
      if q00_state != a:
          q00_state = a

# if a == True: # BLOCK COMMENTED OUT TO REDUCE LOG ACTIVITY
# print("QX0.0 is TRUE")
# if a == False:
# print("QX0.0 is FALSE")
 
      a = psm.get_var("IX0.0")
 
      if i00_state != a:
          i00_state = a

# if a == True: # BLOCK COMMENTED OUT TO REDUCE LOG ACTIVITY
# print("IX0.0 is TRUE")
# if a == False:
# print("IX0.0 is FALSE")
 
# (Assuming plc54_client is already connected)
def read_coils():
     response = plc_connections["PLC54"].read_coils(0, 7) # Reading 7 coils starting at address 0
     if not response.isError():
          # Process the response
          coils_status = response.bits
 
# print("Coils Status:", coils_status) # BLOCK COMMENTED OUT TO REDUCE LOG ACTIVITY
 
     else:
          print("Error reading coils from PLC54")
 
     def read_discrete_inputs():
           response = plc_connections["PLC54"].read_discrete_inputs(0, 7) # Reading 7 discrete inputs starting at address 0
           if not response.isError():
           # Process the response
           inputs_status = response.bits

# print("Discrete Inputs Status:", inputs_status) # BLOCK COMMENTED OUT TO REDUCE LOG ACTIVITY

     else:
          print("Error reading discrete inputs from PLC54")

# Make sure to handle disconnection when the application is closing or when needed
def disconnect_plcs():
     for plc_name, client in plc_connections.items():
          client.close()
          print(f"Disconnected from {plc_name}")
 
if __name__ == "__main__":
     hardware_init()
     while (not psm.should_quit()):
          update_inputs()
          update_outputs()
          time.sleep(0.1) #You can adjust the psm cycle time here
     psm.stop()
 


Reply 0 0
thiagoralves
Haven’t you considered the built in functionality of slave devices to talk to other peers? Slave devices talk through Modbus, so you could essentially just add your peers as slave devices and their data will automatically be available for the master
Reply 0 0
mbunds
Haven’t you considered the built in functionality of slave devices to talk to other peers? Slave devices talk through Modbus, so you could essentially just add your peers as slave devices and their data will automatically be available for the master
Thak you!

That's most of the functionality I'm looking for; how long has the "generic MODBUS TCP" device been an option?

Our plant uses a somewhat complicated and piecemeal arrangement of registers it lays-in MODBUS data requested from each peer, so calling in a large group of addresses just to be able to parse-out logically separated groups is possible using this.

But it suddenly occurred to me that there actually may be no reason to have OpenPLC instances communicate among their peers in the first place, since the entire simulated plant is designed to test a real PLC, which CAN connect to multiple OpenPLC runtimes which would supply "dummy" data anyway.

The only reason to have OpenPLC runtimes communicate with each other in the plant simulator would be to create more "accurate" dummy data if a simulation depended on output from another simulated process.

Connecting "slave devices" using the built-in interface is perfect for that!


[EDIT] The "remapping" of the addresses called from the slave devices to addresses 100 onward was the deal-breaker for the slave device interface, although logic could be written to move these values into their correctly addressed registers.
Reply 0 0
thiagoralves
Thak you!

That's most of the functionality I'm looking for; how long has the "generic MODBUS TCP" device been an option?

Our plant uses a somewhat complicated and piecemeal arrangement of registers it lays-in MODBUS data requested from each peer, so calling in a large group of addresses just to be able to parse-out logically separated groups is possible using this.

But it suddenly occurred to me that there actually may be no reason to have OpenPLC instances communicate among their peers in the first place, since the entire simulated plant is designed to test a real PLC, which CAN connect to multiple OpenPLC runtimes which would supply "dummy" data anyway.

The only reason to have OpenPLC runtimes communicate with each other in the plant simulator would be to create more "accurate" dummy data if a simulation depended on output from another simulated process.

Connecting "slave devices" using the built-in interface is perfect for that!


[EDIT] The "remapping" of the addresses called from the slave devices to addresses 100 onward was the deal-breaker for the slave device interface, although logic could be written to move these values into their correctly addressed registers.
"generic Modbus TCP" is an option since forever (actually since 2017/2018 when the slave devices functionality was built)...
Reply 0 0
Reply