dmroeder / pylogix

Read/Write data from Allen Bradley Compact/Control Logix PLC's
Apache License 2.0
599 stars 182 forks source link

PLC read does not run when called from a function #251

Closed Issa-Bqain closed 3 months ago

Issa-Bqain commented 3 months ago

I took the code in the examples and it runs just fine:

Code from example:

PLC_TAGS_LIST = ['v1','v2']

with PLC() as comm:
    comm.IPAddress = PLC_IP
    ret = comm.Read(PLC_TAGS_LIST)
    for r in ret:
        print(r.Value)

Code from example output (works as expected):

some number 1 some number 2

Code from example as a function:

PLC_TAGS_LIST = ['v1','v2']

def get_plc_data():

  with PLC() as comm:
    comm.IPAddress = PLC_IP
    ret = comm.Read(PLC_TAGS_LIST)
    for r in ret:
        print(r.Value)

Code from example in as a function:

there is no output

I am wondering if anyone knows why this might be? thanks

Versions:

dmroeder commented 3 months ago

Hmm, I'm not seeing this.

with pylogix.PLC("192.168.1.10") as comm:
    print(pylogix.__version__)
    tags = ["BaseDINT", "BaseSTRING"]
    ret = comm.Read(tags)
    for r in ret:
        print(r.Value)

Output:

1.0.2
-776185857
XaoOinvQTSuRFlYTPCqerSCIeNXthMjTyAvxQLBwTpTzwKTuUhopoLIKjDrn
TheFern2 commented 3 months ago

Are you calling get_plc_data function somewhere? My guess is that you're not.

Issa-Bqain commented 3 months ago

Hi, Thank you for checking this, I appreciate the help @TheFern2 @dmroeder Fern: I had another print statement on each line of the loop to debug which executed and meant the function did run.

This is rather odd but I believe the issue might actually be a networking problem on my end and nothing to do with pylogix. Apologies.

I was accessing a private network (which the PLC is connected to ) remotely through a firewall when I ran the function get_plc_data and it seems that this was causing some issues. I hardwired into the network and everything ran as expected.

I am still curious as to what caused this response. I just tried it again now and am not able to reproduce this (it ran as expected).

I will close the issue for now but if I run into this again I will need to dig deeper into the firewall setup as I imagine this is what may have been causing this.

Thank you

TheFern2 commented 3 months ago

Would be much valuable to print(ret) object since is the Response object, and you should always get it back, it probably would have tell you the exact error, a list of errors is here. If there is a connection issue as you think, then ret.Value would be None, and it should still print None afaik, that's just the way python works has nothing to do with pylogix.

Did you have a print statement inside the with? For me still prints None

from pylogix import PLC
from pylogix import __version__

with PLC("192.168.1.10") as comm:
    print(__version__)
    tags = ["BaseDINT", "BaseSTRING"]
    ret = comm.Read(tags)
    for r in ret:
        print(r.Value)
1.0.2
None
None

# if I print the response object
BaseDINT None Register session failed
BaseSTRING None Register session failed
Issa-Bqain commented 3 months ago

Would be much valuable to print(ret) object since is the Response object, and you should always get it back, it probably would have tell you the exact error, a list of errors is here. If there is a connection issue as you think, then ret.Value would be None, and it should still print None afaik, that's just the way python works has nothing to do with pylogix.

Did you have a print statement inside the with? For me still prints None

from pylogix import PLC
from pylogix import __version__

with PLC("192.168.1.10") as comm:
    print(__version__)
    tags = ["BaseDINT", "BaseSTRING"]
    ret = comm.Read(tags)
    for r in ret:
        print(r.Value)
1.0.2
None
None

# if I print the response object
BaseDINT None Register session failed
BaseSTRING None Register session failed

Good note.

I did have multiple print statements to indicate entering the loop but nothing specifically to display ret.Value or the like, this is useful for future reference. Thank you.