Skip to content

Commit

Permalink
Fix for reading bool array tags
Browse files Browse the repository at this point in the history
  • Loading branch information
dmroeder committed Jan 8, 2019
1 parent 0cdd5e9 commit db7629e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
3 changes: 3 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
01/07/19
-Another fix for dealing with bool arrays

12/29/18
-Make sure ProductName is str type
-Removed example.py, added examples directory
Expand Down
24 changes: 16 additions & 8 deletions eip.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
programNames = []
taglist = []


class PLC:

def __init__(self):
Expand All @@ -52,6 +53,7 @@ def __init__(self):
self.Offset = 0
self.KnownTags = {}
self.StructIdentifier = 0x0fCE
self.Version = '0.1.0'
self.CIPTypes = {160:(88 ,"STRUCT", 'B'),
193:(1, "BOOL", '?'),
194:(1, "SINT", 'b'),
Expand Down Expand Up @@ -198,7 +200,7 @@ def _readTag(self, tag, elements, dt):
if datatype == 211:
# bool array
tagData = _buildTagIOI(self, tag, isBoolArray=True)
words = _getWordCount(elements, bitCount)
words = _getWordCount(i, elements, bitCount)
readRequest = _addReadIOI(self, tagData, words)
elif BitofWord(t):
# bits of word
Expand All @@ -207,7 +209,7 @@ def _readTag(self, tag, elements, dt):
bitPos = int(bitPos)

tagData = _buildTagIOI(self, tag, isBoolArray=False)
words = _getWordCount(elements, bitCount)
words = _getWordCount(bitPos, elements, bitCount)

readRequest = _addReadIOI(self, tagData, words)
else:
Expand Down Expand Up @@ -1150,11 +1152,11 @@ def _parseReply(self, tag, elements, data):
bitPos = split_tag[len(split_tag)-1]
bitPos = int(bitPos)

wordCount = _getWordCount(elements, bitCount)
wordCount = _getWordCount(bitPos, elements, bitCount)
words = _getReplyValues(self, tag, wordCount, data)
vals = _wordsToBits(self, tag, words, count=elements)
elif datatype == 211:
wordCount = _getWordCount(elements, bitCount)
wordCount = _getWordCount(index, elements, bitCount)
words = _getReplyValues(self, tag, wordCount, data)
vals = _wordsToBits(self, tag, words, count=elements)
else:
Expand Down Expand Up @@ -1271,12 +1273,18 @@ def _wordsToBits(self, tag, value, count=0):

return ret[bitPos:bitPos+count]

def _getWordCount(length, bits):
def _getWordCount(start, length, bits):
'''
Returns the number of words reqired to accomodate
all the bits requested.
Get the number of words that the requested
bits would occupy. We have to take into account
how many bits are in a word and the fact that the
number of requested bits can span multipe words.
'''
return int(length/bits)+1
newStart = start % bits
newEnd = newStart + length

totalWords = (newEnd-1) / bits
return totalWords + 1

def InitialRead(self, tag, baseTag, dt):
'''
Expand Down

0 comments on commit db7629e

Please sign in to comment.