Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion pymodbus/pdu/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def __getitem__(self, key):

:param key: The register to read
"""
return self.stat_data.setdefault(key, "")
return self.stat_data.get(key, "")

def __str__(self):
"""Build a representation of the device.
Expand Down
4 changes: 4 additions & 0 deletions pymodbus/pdu/mei_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ async def datastore_update(
return ExceptionResponse(self.function_code, ExcCodes.ILLEGAL_VALUE)

information = DeviceInformationFactory.get(_MCB, self.read_code, self.object_id)
if self.read_code == DeviceInformation.SPECIFIC and (
0x07 <= self.object_id < 0x80 or not information.get(self.object_id)
):
return ExceptionResponse(self.function_code, ExcCodes.ILLEGAL_ADDRESS)
return ReadDeviceInformationResponse(
read_code=self.read_code,
information=information,
Expand Down
1 change: 1 addition & 0 deletions test/pdu/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def test_modbus_device_identification_get(self):
assert self.ident[0x08] != "x"
assert self.ident[0x10] != "reserved"
assert not self.ident[0x54]
assert 0x54 not in dict(self.ident)

def test_modbus_device_identification_summary(self):
"""Test device identification summary creation."""
Expand Down
21 changes: 20 additions & 1 deletion test/pdu/test_mei_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import pytest

from pymodbus.constants import DeviceInformation
from pymodbus.constants import DeviceInformation, ExcCodes
from pymodbus.pdu.device import ModbusControlBlock
from pymodbus.pdu.mei_message import (
ReadDeviceInformationRequest,
Expand Down Expand Up @@ -60,6 +60,14 @@ async def test_read_device_information_request(self, mock_server_context):
with pytest.raises(KeyError):
_ = result.information[0x81]

handle = ReadDeviceInformationRequest(
read_code=DeviceInformation.SPECIFIC, object_id=0x00
)
result = await handle.datastore_update(context, 0)
assert cast(ReadDeviceInformationResponse, result).information == {
0x00: "Company"
}

handle = ReadDeviceInformationRequest(
read_code=DeviceInformation.EXTENDED, object_id=0x80
)
Expand All @@ -81,6 +89,17 @@ async def test_read_device_information_request_error(self, mock_server_context):
assert (await handle.datastore_update(context, 0)).function_code == 0xAB
handle.object_id = 0x100
assert (await handle.datastore_update(context, 0)).function_code == 0xAB
handle = ReadDeviceInformationRequest(
read_code=DeviceInformation.SPECIFIC, object_id=0x54
)
result = await handle.datastore_update(context, 0)
assert result.exception_code == ExcCodes.ILLEGAL_ADDRESS
ModbusControlBlock().Identity[0xFE] = ""
handle = ReadDeviceInformationRequest(
read_code=DeviceInformation.SPECIFIC, object_id=0xFE
)
result = await handle.datastore_update(context, 0)
assert result.exception_code == ExcCodes.ILLEGAL_ADDRESS

def test_read_device_information_calc1(self):
"""Test calculateRtuFrameSize, short buffer."""
Expand Down