Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for byte strings in the device information fields #693

Merged
merged 2 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pymodbus/other_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,16 @@ def execute(self, context=None):
reportSlaveIdData = getattr(context, 'reportSlaveIdData', None)
if not reportSlaveIdData:
information = DeviceInformationFactory.get(_MCB)
identifier = "-".join(information.values()).encode()

# Support identity values as bytes data and regular str data
id_data = []
for v in information.values():
if isinstance(v, bytes):
id_data.append(v)
else:
id_data.append(v.encode())

identifier = b"-".join(id_data)
identifier = identifier or b'Pymodbus'
reportSlaveIdData = identifier
return ReportSlaveIdResponse(reportSlaveIdData)
Expand Down
39 changes: 39 additions & 0 deletions test/test_other_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,45 @@ def testGetCommEventLogWithEvents(self):
self.assertEqual(response.event_count, 0x12)
self.assertEqual(response.events, [0x12,0x34,0x56])

def testReportSlaveIdRequest(self):
with mock.patch("pymodbus.other_message.DeviceInformationFactory") as dif:
# First test regular identity strings
identity = {
0x00: 'VN', # VendorName
0x01: 'PC', # ProductCode
0x02: 'REV', # MajorMinorRevision
0x03: 'VU', # VendorUrl
0x04: 'PN', # ProductName
0x05: 'MN', # ModelName
0x06: 'UAN', # UserApplicationName
0x07: 'RA', # reserved
0x08: 'RB', # reserved
}
dif.get.return_value = identity
expected_identity = "-".join(identity.values()).encode()

request = ReportSlaveIdRequest()
response = request.execute()
self.assertEqual(response.identifier, expected_identity)

# Change to byte strings and test again (final result should be the same)
identity = {
0x00: b'VN', # VendorName
0x01: b'PC', # ProductCode
0x02: b'REV', # MajorMinorRevision
0x03: b'VU', # VendorUrl
0x04: b'PN', # ProductName
0x05: b'MN', # ModelName
0x06: b'UAN', # UserApplicationName
0x07: b'RA', # reserved
0x08: b'RB', # reserved
}
dif.get.return_value = identity

request = ReportSlaveIdRequest()
response = request.execute()
self.assertEqual(response.identifier, expected_identity)

def testReportSlaveId(self):
with mock.patch("pymodbus.other_message.DeviceInformationFactory") as dif:
dif.get.return_value = dict()
Expand Down