Skip to content

Commit 5c2998c

Browse files
authored
fixed DB class not allowing to write BYTE values to rows (#377)
previously, the DB_Row.set_value() method did not support setting BYTE values. this commit introduces this functionality, as well as unit tests.
1 parent 10f50bc commit 5c2998c

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

snap7/util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,7 @@ def set_value(self, byte_index: Union[str, int], type_: str, value: Union[bool,
16131613
'UINT': set_uint,
16141614
'INT': set_int,
16151615
'WORD': set_word,
1616+
'BYTE': set_byte,
16161617
'USINT': set_usint,
16171618
'SINT': set_sint,
16181619
}

test/test_util.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,31 @@
113113

114114
class TestS7util(unittest.TestCase):
115115

116-
def test_get_byte(self):
116+
def test_get_byte_new(self):
117117
test_array = bytearray(_new_bytearray)
118118
byte_ = util.get_byte(test_array, 41)
119119
self.assertEqual(byte_, 128)
120120
byte_ = util.get_byte(test_array, 42)
121121
self.assertEqual(byte_, 255)
122122

123-
def test_set_byte(self):
123+
def test_set_byte_new(self):
124124
test_array = bytearray(_new_bytearray)
125125
util.set_byte(test_array, 41, 127)
126126
byte_ = util.get_byte(test_array, 41)
127127
self.assertEqual(byte_, 127)
128128

129+
def test_get_byte(self):
130+
test_array = bytearray(_bytearray)
131+
row = util.DB_Row(test_array, test_spec, layout_offset=4)
132+
value = row.get_value(50, 'BYTE') # get value
133+
self.assertEqual(value, 254)
134+
135+
def test_set_byte(self):
136+
test_array = bytearray(_bytearray)
137+
row = util.DB_Row(test_array, test_spec, layout_offset=4)
138+
row['testByte'] = 255
139+
self.assertEqual(row['testByte'], 255)
140+
129141
def test_get_s5time(self):
130142
"""
131143
S5TIME extraction from bytearray

0 commit comments

Comments
 (0)