python - Raspberry Pi to AB controllogix: how to trigger output in GPIO based on continuosly reading plc tag value
问题描述
I've discovered pylogix on GitHub and have been playing around with reading/writing tags on an AB L71 CPU. I am successful at the read/write part, but what I want to do is trigger a GPIO pin output based on plc value being larger than 0.
I can't seem to figure out what I need to do to get the constantly updated value into the output function.
import threading
from pylogix.eip import PLC
from gpiozero import LED
from time import sleep
comm = PLC()
comm.IPAddress = '10.201.191.177'
def readdata():
threading.Timer(1.0, readdata).start()
x = comm.Read('parts')
print (x)
readdata()
if x > 0:
relay = LED(2)
解决方案
很高兴看到我不是这个论坛上唯一对 PLC 感兴趣的人。我可能会为你推荐这个:
编辑:我阅读了您模块的文档。在文档下面尝试这个新代码可以找到https://gpiozero.readthedocs.io/en/stable/
import threading # I don't think this is necessity for your application
import time
from pylogix.eip import PLC
from gpiozero import LED
from time import sleep
with PLC() as comm #small edit here to control the closing of sockets upon exit
comm.IPAddress = '10.201.191.177'
running=True
relay = LED(2) #I believe the previous version of your code was constantly overwriting your 'relay' variable with a new instance of class LED
while running==True:
x=comm.read('parts')
if x > 0:
relay.on()
else: relay.off()
time.sleep(.5)
#this will run forever updating your LED every 500ms, I would recommend writing code to exit this loop
推荐阅读
- node.js - 尝试在 Node.js 中加载所有 Firestore 文档
- r - auto.arima() function suggesting ARIMA(2,0,0) when time series is non-stationary
- javascript - How can i make a page refresh in a certain time all days?
- google-cloud-platform - How do I register Google Classroom API for push notifications?
- ios - 当应用程序进入后台或用户退出应用程序时如何停止计时器
- javascript - jquery无法更改隐藏输入
- node.js - 续集同一类的两个关系
- python - How to install Polynote on Windows?
- php - Laravel Cashier 10 - 条纹支付元素为空
- node.js - Feathers services usage and eslint