Comments (8)
There are three stages to a Rockwell PLC connection:
- TCP connection. This is usually pretty fast but still on the order of milliseconds.
- EIP connection. This is a simple handshake but still takes a request/response pair.
- CIP connection set up. You can do disconnected messaging but your maximum packet size is only about 500 bytes. This takes a while. Up to 30ms on older PLCs. With a modern 1756-L8x it is in the low single digit milliseconds.
The whole connection process is time and CPU consuming. I try to keep connections open as long as I can.
The PLC will drop the connection after some period of idle time (no requests). I don't remember what Pylogix sets that to. In my experience it is better to control this from your end of the connection and cleanly close the connection if you are going to have a long idle period.
from pylogix.
Pylogix makes the connection to the PLC automatically for you, whenever you call any function (read/write, etc). When you first create an instance of pylogix.PLC(), a connection is not yet made to the PLC. When you then call Read(), for example, we'll generate fresh connection parameters (serial number, sequence count), then make a connection to the PLC. Each time you call Read(), we use the same connection. If the connection suddenly fails, we'll generate new connection parameters and create a new connection.
If in your code, you end a With statement, the connection will automatically close. The next time you call Read(), a new connection will be made.
If you create a new instance of pylogix.PLC() in your code, a connection will be made.
As long as you don't close the connection yourself, (calling .Close() or ending your With), the connection does not fail or you wait too long between calling Read/Write/etc. the same connection will be used.
As for when to close the connection, that's a bit up to you. If I'm reading/writing every minute, I keep the connection open. If I can't predict when I need to read/write (some external dependency), I'll keep the connection open so I don't end up with some situation where I suddenly get a bunch of requests. If I'm reading every 10 minutes or every hour, I'll probably close the connection. I usually error towards keeping the connection open by either reading a tag periodically or requesting the PLC time.
The PLC can handle connections being opened and closed, though I wouldn't make it do extra work unnecessarily. The 1756-ENBT can be a turd, so definitely don't overburden that module.
from pylogix.
The PLC won't unless something else pokes at it. For instance, if someone puts it in program mode, I think you lose your connections. Or if someone restarts it. Etc.
from pylogix.
Connections are maintained in program mode. A PLC reboot will do it, network cable disconnected, too much time between exchanges.
In your case, the PLC won't drop the connection because you are reading frequently.
from pylogix.
I tend to close the connection sooner than later. If I am not reading more than once every ten seconds or so, I close the connection. That said, many of the environments I was using had a lot of PLCs and so had connections starting up and shutting down. Also note that some PLCs have extremely limited connection resources. If your app is critical, then you might want to keep the connection up to make sure that nothing else takes all the connection slots.
from pylogix.
@kyle-github @dmroeder Thank you for the help.
Based on above, I would then never close the connection, unless the script terminates (I imagine the connection would drop automatically without calling .Close()? ). Consider a finite state machine like the one below. If I am reading say 10-20 tags every <1 second, the connection should be left open.
from urllib import response
from pylogix import PLC
import requests
#PLC
IP_ADDRESS = "192.168.1.9"
TAG_NAME = "something"
PROCESSOR_SLOT = "2"
IS_MICRO800 = False
#request
API_END_POINT = "127.0.0.1"
# Abstract State class
class State:
def execute(self):
pass
# StateA class
class StateA(State):
def execute(self):
print("Executing State A")
# Perform State A specific actions or operations
# Transition to State B after execution
# StateB class
class StateB(State):
def execute(self):
print("Executing State B")
# Perform State B specific actions or operations
# Transition to State C after execution
# StateC class
class StateC(State):
def execute(self):
print("Executing State C")
# Perform State C specific actions or operations
# Transition to State A after execution
class connect_plc(State):
def execute(self):
with PLC() as comm:
comm.ProcessorSlot = PROCESSOR_SLOT
comm.Micro800 = IS_MICRO800
comm.IPAddress = IP_ADDRESS
ret = comm.Read(TAG_NAME)
print(ret.TagName, ret.Value, ret.Status)
class send_data(State):
def execute(self):
response=requests.post(API_END_POINT)
print(response.json())
# State Machine class
class StateMachine:
def __init__(self):
self.states = [StateA(), StateB(), StateC()]
self.current_state = self.states[0] # Start with State A
def run(self):
while True:
self.current_state.execute()
self.transition_to_next_state()
def transition_to_next_state(self):
current_state_index = self.states.index(self.current_state)
next_state_index = (current_state_index + 1) % len(self.states)
self.current_state = self.states[next_state_index]
# Usage
state_machine = StateMachine()
state_machine.run()
from pylogix.
Make sure you understand when the PLC will close the connection. I have found that if the PLC closes the connection, it can be messier to clean up and restart another one. It may depend on a number of factors including the OSes involved. I always make sure that I (or the underlying library) close the connection cleanly first.
from pylogix.
Make sure you understand when the PLC will close the connection. I have found that if the PLC closes the connection, it can be messier to clean up and restart another one. It may depend on a number of factors including the OSes involved. I always make sure that I (or the underlying library) close the connection cleanly first.
Since I am reading at a time interval which is less than the idle time - shouldn't the PLC never close the connection on its own? At this point, I just close the connection once I terminate the script by choice or if the script/host the script is running on crashes.
from pylogix.
Related Issues (20)
- "Forward open failed" when trying to read tags from Berghof PLC HOT 8
- .GetDeviceProperties always returns DeviceType of "None" HOT 5
- Issues connecting to emulate HOT 7
- micro850 connection HOT 5
- Return data gets jumbled up on a multi write when the tags don't exist. HOT 13
- The return value is different when using a multi write compared to a single write for a string. HOT 4
- cannot get a tag list HOT 3
- Is it possible to inhibit a periodic task? HOT 2
- pylogix - returning a tag list as a row of values HOT 7
- PLC REAL values HOT 5
- Pylogix - compare a previously acquired tag list with the current one HOT 5
- Discover does not work while running pylogix in docker HOT 10
- Invalid Continuation Byte HOT 5
- Abnormal code termination. HOT 8
- Do I need to use routing? HOT 20
- Forward Open Failed HOT 22
- Connection Issues HOT 9
- PLC read does not run when called from a function HOT 5
- documentation on the CIP protocol HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pylogix.