我在GitHub上发现了pylogix,并一直在AB L71 CPU上读写标签。我在读/写部分很成功,但是我想做的是基于plc值大于0触发GPIO引脚输出。
我似乎无法弄清楚将不断更新的值输入输出函数所需要做的事情。
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)
答案 0 :(得分:1)
很高兴看到我不是这个论坛上唯一对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