Skip to content

Commit e9e0697

Browse files
sytsereitsmaSytse Reitsma
andauthored
Add support for byte strings in the device information fields (#693)
* Add support for byte strings in the device information fields Iterate over the list of identity values and append the bytes if the field is a byte string, append the encoded string otherwise. * Review comments, isinstance instead of 'type is' Co-authored-by: Sytse Reitsma <[email protected]>
1 parent bf0bc1c commit e9e0697

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

pymodbus/other_message.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,16 @@ def execute(self, context=None):
369369
reportSlaveIdData = getattr(context, 'reportSlaveIdData', None)
370370
if not reportSlaveIdData:
371371
information = DeviceInformationFactory.get(_MCB)
372-
identifier = "-".join(information.values()).encode()
372+
373+
# Support identity values as bytes data and regular str data
374+
id_data = []
375+
for v in information.values():
376+
if isinstance(v, bytes):
377+
id_data.append(v)
378+
else:
379+
id_data.append(v.encode())
380+
381+
identifier = b"-".join(id_data)
373382
identifier = identifier or b'Pymodbus'
374383
reportSlaveIdData = identifier
375384
return ReportSlaveIdResponse(reportSlaveIdData)

test/test_other_messages.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,45 @@ def testGetCommEventLogWithEvents(self):
8787
self.assertEqual(response.event_count, 0x12)
8888
self.assertEqual(response.events, [0x12,0x34,0x56])
8989

90+
def testReportSlaveIdRequest(self):
91+
with mock.patch("pymodbus.other_message.DeviceInformationFactory") as dif:
92+
# First test regular identity strings
93+
identity = {
94+
0x00: 'VN', # VendorName
95+
0x01: 'PC', # ProductCode
96+
0x02: 'REV', # MajorMinorRevision
97+
0x03: 'VU', # VendorUrl
98+
0x04: 'PN', # ProductName
99+
0x05: 'MN', # ModelName
100+
0x06: 'UAN', # UserApplicationName
101+
0x07: 'RA', # reserved
102+
0x08: 'RB', # reserved
103+
}
104+
dif.get.return_value = identity
105+
expected_identity = "-".join(identity.values()).encode()
106+
107+
request = ReportSlaveIdRequest()
108+
response = request.execute()
109+
self.assertEqual(response.identifier, expected_identity)
110+
111+
# Change to byte strings and test again (final result should be the same)
112+
identity = {
113+
0x00: b'VN', # VendorName
114+
0x01: b'PC', # ProductCode
115+
0x02: b'REV', # MajorMinorRevision
116+
0x03: b'VU', # VendorUrl
117+
0x04: b'PN', # ProductName
118+
0x05: b'MN', # ModelName
119+
0x06: b'UAN', # UserApplicationName
120+
0x07: b'RA', # reserved
121+
0x08: b'RB', # reserved
122+
}
123+
dif.get.return_value = identity
124+
125+
request = ReportSlaveIdRequest()
126+
response = request.execute()
127+
self.assertEqual(response.identifier, expected_identity)
128+
90129
def testReportSlaveId(self):
91130
with mock.patch("pymodbus.other_message.DeviceInformationFactory") as dif:
92131
dif.get.return_value = dict()

0 commit comments

Comments
 (0)