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()