Skip to content

Commit

Permalink
Modified InitialRead(), raise exception if the tag doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
dmroeder committed Feb 12, 2017
1 parent a140a04 commit c95c90d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
1 change: 1 addition & 0 deletions changelog
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
-Fixed CIP type for LINT, changed to signed
-Cleaned up some formatting issues
-Fixed crash on a read/write after a multi-packet reply
-Modified InitialRead() to raise exception if tag doesn't exist

02/06/17
-Use a different attribute in GetPLCTime()
Expand Down
70 changes: 41 additions & 29 deletions eip.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def _readTag(self, tag, elements):
if not self.SocketConnected: return None

t,b,i = TagNameParser(tag, 0)
if b not in self.KnownTags: InitialRead(self, t, b)
if not InitialRead(self, t, b): return None

if self.KnownTags[b][0] == 211:
tagData = _buildTagIOI(self, tag, isBoolArray=True)
Expand All @@ -174,7 +174,7 @@ def _writeTag(self, tag, value, elements):
if not self.SocketConnected: return None

t,b,i = TagNameParser(tag, 0)
if b not in self.KnownTags: InitialRead(self, t, b)
if not InitialRead(self, t, b): return None

dataType = self.KnownTags[b][0]

Expand All @@ -189,7 +189,7 @@ def _writeTag(self, tag, value, elements):
for i in xrange(elements):
writeData.append(int(value[i]))
else:
print "Fix this"
pass

# write a bit of a word, boolean array or everything else
if BitofWord(tag):
Expand All @@ -204,6 +204,7 @@ def _writeTag(self, tag, value, elements):

eipHeader = _buildEIPHeader(self, writeRequest)
retData = _getBytes(self, eipHeader)
return

def _multiRead(self, args):
'''
Expand All @@ -217,8 +218,8 @@ def _multiRead(self, args):

for i in xrange(tagCount):
t,b,i = TagNameParser(args[i], 0)
if b not in self.KnownTags: InitialRead(self, t, b)

if not InitialRead(self, t, b): return None
tagIOI = _buildTagIOI(self, t, isBoolArray=False)
readIOI = _addReadIOI(self, tagIOI, 1)
serviceSegments.append(readIOI)
Expand Down Expand Up @@ -1043,17 +1044,28 @@ def InitialRead(self, tag, baseTag):
Store each unique tag read in a dict so that we can retreive the
data type or data length (for STRING) later
'''
# if a tag alread exists, return True
if baseTag in self.KnownTags:
return True

tagData = _buildTagIOI(self, baseTag, isBoolArray=False)
readRequest = _addPartialReadIOI(self, tagData, 1)
eipHeader = _buildEIPHeader(self, readRequest)

# send our tag read request
self.Socket.send(eipHeader)
retData = self.Socket.recv(1024)
dataType = unpack_from('<B', retData, 50)[0]
dataLen = unpack_from('<H', retData, 2)[0] # this is really just used for STRING
self.KnownTags[baseTag] = (dataType, dataLen)
return
status = unpack_from('<b', retData, 48)[0]

# make sure it was successful
if status == 0 or status == 6:
dataType = unpack_from('<B', retData, 50)[0]
dataLen = unpack_from('<H', retData, 2)[0] # this is really just used for STRING
self.KnownTags[baseTag] = (dataType, dataLen)
return True
else:
raise Exception('Failed to read initial tag: ' + cipErrorCodes[status])


def TagNameParser(tag, offset):
'''
Expand Down Expand Up @@ -1203,26 +1215,26 @@ def _parseIdentityResponse(data):
return resp

def extractTagPacket(self, data, programName):
# the first tag in a packet starts at byte 50
packetStart = 50

while packetStart < len(data):
# get the length of the tag name
tagLen = unpack_from('<H', data, packetStart+8)[0]
# get a single tag from the packet
packet = data[packetStart:packetStart+tagLen+10]
# extract the offset
self.Offset = unpack_from('<H', packet, 0)[0]
# add the tag to our tag list
tag = parseLgxTag(packet, programName)
# filter out garbage
if "__DEFVAL_" and "Routine:" not in tag.TagName:
taglist.append(tag)
if not programName:
if 'Program:' in tag.TagName:
programNames.append(tag.TagName)
# increment ot the next tag in the packet
packetStart = packetStart+tagLen+10
# the first tag in a packet starts at byte 50
packetStart = 50

while packetStart < len(data):
# get the length of the tag name
tagLen = unpack_from('<H', data, packetStart+8)[0]
# get a single tag from the packet
packet = data[packetStart:packetStart+tagLen+10]
# extract the offset
self.Offset = unpack_from('<H', packet, 0)[0]
# add the tag to our tag list
tag = parseLgxTag(packet, programName)
# filter out garbage
if "__DEFVAL_" and "Routine:" not in tag.TagName:
taglist.append(tag)
if not programName:
if 'Program:' in tag.TagName:
programNames.append(tag.TagName)
# increment ot the next tag in the packet
packetStart = packetStart+tagLen+10

def parseLgxTag(packet, programName):
tag = LGXTag()
Expand Down

0 comments on commit c95c90d

Please sign in to comment.