Raspberry Pi到AB controllogix:如何在GPIO中基于连续读取plc标记值触发输出

2024-09-27 18:06:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我在GitHub上发现了pylogix,并在abl71 CPU上玩读/写标签。我在读/写部分是成功的,但我想做的是根据大于0的plc值触发一个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)

Tags: 函数fromimportgithubledgpio标签cpu
1条回答
网友
1楼 · 发布于 2024-09-27 18:06:40

很高兴在这个论坛上我不是唯一对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

相关问题 更多 >

    热门问题