ottowayi / pycomm3

A Python Ethernet/IP library for communicating with Allen-Bradley PLCs.
MIT License
377 stars 88 forks source link

Read Tag Optimization #304

Open Kelton622 opened 8 months ago

Kelton622 commented 8 months ago

Hello,

I'm working on an application that requires a tag value to be updated very frequently. Currently I'm using the following to read the tag, but it still takes about 250 [ms] to read the tag. Are there any ways to improve the read time? Ideally it would be < 50 [ms].

Thanks!

from pycomm3 import LogixDriver import time

def read_single(): with LogixDriver('192.168.0.20') as plc: return plc.read('ZoneCount')

st = time.time()
ZoneCount = read_single() et = time.time()

get the execution time

elapsed_time = et - st print('Execution time:', elapsed_time, 'seconds') print(ZoneCount)

ottowayi commented 7 months ago

In your read_single method, you're creating a new driver every time which has a lot of overhead. When the driver first connects, it uploads the tag list and all of the data types from the controller. This is so that it can optimize the reads based on tag size and allows reading full structures (like UDTs, AOIs, and builtins). You should try creating a single driver instance and reusing it each time, it will probably be much faster that way.

Another optimization would be to read multiple tags at once, since it will be able to use fewer messages by packing them together in a single request. Or if you're reading from an array or UDT/AOI, it can read the whole array/structure at once instead of individual attributes across multiple requests.