Hi all,
Great software and forum here! 🙂
I'm pretty new to PLC programming, so please bear with me asking a beginner question:
I'm running OpenPLC on Raspberry PI 4, with a RGB light sensor attached. This light sensor can be easily read with the help of Python libraries, so I have set up OpenPLC with Python / PSM as "hardware driver" (love it so far!).
In my PSM code, I am (probably?) writing sensor output to %IX0.0 and %IX0.1:
psm.set_var("IX0.0", int(Light.Get_Lux()))
psm.set_var("IX0.1", int(Light.Get_ColorTemp()))
I'm checking that the program runs well by printing Light.Get_Lux() for every loop, which lets me successfully monitor the sensor values on OpenPLC's dashboard.
I'm running the whole thing with a blank PLC program if that matters. My understanding is that it should still work since the python script is directly writing to the registers IX0.0 and IX0.1, right?
Where I struggle: I want to read those IX0.0 and IX0.1 registers via Modbus, from an industrial edge software. The connection with OpenPLC works perfectly, and I see incoming data (heartbeat?) but can't see any tags.
Why is this? What do I need to do differently?
Thank you very much in advance - any help highly appreciated!
Best regards
---
Full PSM code below if of interest
#import all your libraries here
import psm
import time
import RPi.GPIO as GPIO
from TCS34725 import TCS34725
#global variables
counter = 0
var_state = False
Light=TCS34725(0X29, debug=False)
def hardware_init():
#Insert your hardware initialization code in here
if(Light.TCS34725_init() == 1):
print("TCS34725 init error!")
else:
print("TCS34725 init success!")
time.sleep(1)
psm.start()
def update_inputs():
#place here your code to update inputs
global counter
Light.Get_RGBData()
psm.set_var("IX0.0", int(Light.Get_Lux()))
psm.set_var("IX0.1", int(Light.Get_ColorTemp()))
counter += 1
print("\nUpdated inputs: Lux = ", Light.Get_Lux(), " | Color Temp = ", Light.Get_ColorTemp(), "K - #", counter)
# 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()