-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
59 lines (50 loc) · 1.9 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# PSM API is quite simple and just has a few functions. When writing your
# own programs, avoid touching on the "__main__" function as this regulates
# how PSM works on the PLC cycle. You can write your own hardware initialization
# code on hardware_init(), and your IO handling code on update_inputs() and
# update_outputs()
#
# To manipulate IOs, just use PSM calls psm.get_var([location name]) to read
# an OpenPLC location and psm.set_var([location name], [value]) to write to
# an OpenPLC location. For example:
# psm.get_var("QX0.0")
# will read the value of %QX0.0. Also:
# psm.set_var("IX0.0", True)
# will set %IX0.0 to true.
#
# Below you will find a simple example that uses PSM to switch OpenPLC's
# first digital input (%IX0.0) every second. Also, if the first digital
# output (%QX0.0) is true, PSM will display "QX0.0 is true" on OpenPLC's
# dashboard. Feel free to reuse this skeleton to write whatever you want.
from psm import psm
import time
import yaml
with open('config.yaml', 'r') as file:
config = yaml.safe_load(file)
counter = 0
var_state = False
def hardware_init():
#Insert your hardware initialization code in here
server_settings = (config['server']['host'], config['server']['port']) # (hostname, port)
psm.start(server_settings)
def update_inputs():
#place here your code to update inputs
global counter
global var_state
psm.set_var("IX0.0", var_state)
counter += 1
if (counter == 10):
counter = 0
var_state = not var_state
def update_outputs():
#place here your code to work on outputs
a = psm.get_var("QX0.0")
if a == True:
print("QX0.0 is true")
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()