Skip to content

Commit

Permalink
Cleaned up _connect() and it's usage
Browse files Browse the repository at this point in the history
  • Loading branch information
dmroeder committed Feb 12, 2017
1 parent c95c90d commit 99725db
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
3 changes: 3 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
02/12/17
-Cleaned up _connect() and it's usage

02/11/17
-Fixed CIP type for LINT, changed to signed
-Cleaned up some formatting issues
Expand Down
34 changes: 17 additions & 17 deletions eip.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ def _readTag(self, tag, elements):
processes the read request
'''
self.Offset = 0
if not self.SocketConnected: _connect(self)
if not self.SocketConnected: return None

if not _connect(self): return None

t,b,i = TagNameParser(tag, 0)
if not InitialRead(self, t, b): return None
Expand All @@ -169,9 +169,8 @@ def _writeTag(self, tag, value, elements):
'''
self.Offset = 0
writeData = []

if not self.SocketConnected: _connect(self)
if not self.SocketConnected: return None

if not _connect(self): return None

t,b,i = TagNameParser(tag, 0)
if not InitialRead(self, t, b): return None
Expand Down Expand Up @@ -213,8 +212,8 @@ def _multiRead(self, args):
serviceSegments = []
segments = ""
tagCount = len(args)
if not self.SocketConnected: _connect(self)
if not self.SocketConnected: return None

if not _connect(self): return None

for i in xrange(tagCount):
t,b,i = TagNameParser(args[i], 0)
Expand Down Expand Up @@ -252,8 +251,7 @@ def _getPLCTime(self):
'''
Requests the PLC clock time
'''
if not self.SocketConnected: _connect(self)
if not self.SocketConnected: return None
if not _connect(self): return None

AttributeService = 0x03
AttributeSize = 0x02
Expand Down Expand Up @@ -286,8 +284,7 @@ def _setPLCTime(self):
'''
Requests the PLC clock time
'''
if not self.SocketConnected: _connect(self)
if not self.SocketConnected: return None
if not _connect(self): return None

AttributeService = 0x04
AttributeSize = 0x02
Expand Down Expand Up @@ -317,9 +314,7 @@ def _getTagList(self):
'''
Requests the controller tag list and returns a list of LgxTag type
'''

if not self.SocketConnected: _connect(self)
if not self.SocketConnected: return None
if not _connect(self): return None

self.Offset = 0

Expand Down Expand Up @@ -416,6 +411,9 @@ def _connect(self):
'''
Open a connection to the PLC
'''
if self.SocketConnected:
return True

try:
self.Socket = socket.socket()
self.Socket.settimeout(1.0)
Expand All @@ -424,15 +422,15 @@ def _connect(self):
self.SocketConnected = False
self.SequenceCounter = 1
self.Socket.close()
return
return False

retData = _getBytes(self, _buildRegisterSession(self))
if retData:
self.SessionHandle = unpack_from('<I', retData, 4)[0]
else:
self.SocketConnected = False
print "Failed to register session"
return
return False

retData = _getBytes(self, _buildForwardOpenPacket(self))
if retData:
Expand All @@ -441,7 +439,9 @@ def _connect(self):
else:
self.SocketConnected = False
print "Forward Open Failed"
return
return False

return True

def _closeConnection(self):
'''
Expand Down

0 comments on commit 99725db

Please sign in to comment.