Skip to content

Commit 042458b

Browse files
committed
other: reportSlaveId: allow slaves to provide custom responses
At present, function code 17, "Report Slave ID" is hardcoded to reply with a string joined form of the "device identification" fields from function code 43, subcode 14 (0x2b, 0x0e) There's nothing that actually implies these should be related. This change allows slave contexts to provide custom data to return via the ModbusServerContext property reportSlaveIdData If this is not provided, the existing fallback to the device identification fields is used. Signed-off-by: Karl Palsson <karlp@etactica.com>
1 parent 93306bc commit 042458b

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

pymodbus/other_message.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -360,21 +360,26 @@ def decode(self, data):
360360
pass
361361

362362
def execute(self, context=None):
363-
''' Run a read exeception status request against the store
363+
''' Run a report slave id request against the store
364364
365365
:returns: The populated response
366366
'''
367-
information = DeviceInformationFactory.get(_MCB)
368-
identifier = "-".join(information.values()).encode()
369-
identifier = identifier or b'Pymodbus'
370-
return ReportSlaveIdResponse(identifier)
367+
reportSlaveIdData = None
368+
if context:
369+
reportSlaveIdData = context.reportSlaveIdData
370+
if not reportSlaveIdData:
371+
information = DeviceInformationFactory.get(_MCB)
372+
identifier = "-".join(information.values()).encode()
373+
identifier = identifier or b'Pymodbus'
374+
reportSlaveIdData = identifier
375+
return ReportSlaveIdResponse(reportSlaveIdData)
371376

372377
def __str__(self):
373378
''' Builds a representation of the request
374379
375380
:returns: The string representation of the request
376381
'''
377-
return "ResportSlaveIdRequest(%d)" % self.function_code
382+
return "ReportSlaveIdRequest(%d)" % self.function_code
378383

379384

380385
class ReportSlaveIdResponse(ModbusResponse):
@@ -430,7 +435,7 @@ def __str__(self):
430435
:returns: The string representation of the response
431436
'''
432437
arguments = (self.function_code, self.identifier, self.status)
433-
return "ResportSlaveIdResponse(%s, %s, %s)" % arguments
438+
return "ReportSlaveIdResponse(%s, %s, %s)" % arguments
434439

435440
#---------------------------------------------------------------------------#
436441
# TODO Make these only work on serial

0 commit comments

Comments
 (0)