Sunny
Junior Member
Hello,
I wrote a simple demo project. My PSM code is like this:
#import all your libraries here
import psm
import time
#global variables
counter = 0
var_state = False
def hardware_init():
#Insert your hardware initialization code in here
psm.start()
f = open('GPIO.txt', 'w')
f.write('0')
f.close()
f = open('Axis.txt', 'w')
f.write('0\n0')
def execute_cmd(cmd: int):
print("call success")
if(cmd == 0):
print("call properly")
f = open('GPIO.txt', 'r')
values = f.readline()
port = psm.get_var("IW2")
value = psm.get_var("IX0.0")
if(value == False):
mask = -1 - (1 << port)
values = int(values) & mask
print("False")
print(values)
else:
mask = 1 << port
values = int(values) | mask
print("True");
print(values);
f.close()
f.open('GPIO.txt', 'w')
f.write(values)
f.close()
if __name__ == "__main__":
hardware_init()
print(psm.get_var("IW0"))
while (not psm.should_quit()):
print("psm looping")
while(psm.get_var("IW0") != 1):
time.sleep(1)
print("psm waiting")
cmd = psm.get_var("IW1")
execute_cmd(cmd)
time.sleep(1) #You can adjust the psm cycle time here
psm.stop()
PSM server will wait until the "IW0" is set to 1. Then it uses file operations to simulate IO control. My PLC program is like this:
(“本地” means "local")
But When I uploaded this program to OpenPLC Runtime, it didn't work as I expected. PSM server just waited and did nothing. In Monitor Tag, the result is like this:
Only the variable state switched back and forth between zero and one. the function "execute_cmd" was not called properly.
I have no idea about how to solve this problem. Thanks for any help.