Skip to content

dev_id=0 no response expected (returns ExceptionResponse(0xff)). #2567

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

Merged
merged 1 commit into from
Feb 9, 2025
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
3 changes: 2 additions & 1 deletion examples/client_async_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ async def async_execute_diagnostic_requests(client):
assert not rr.isError() # test that call was OK
rr = await client.diag_getclear_modbus_response(slave=SLAVE)
assert not rr.isError() # test that call was OK
assert not await client.diag_force_listen_only(slave=SLAVE, no_response_expected=True)
rr = await client.diag_force_listen_only(slave=SLAVE, no_response_expected=True)
assert rr.isError() # test that call was OK, error indicate no response


# ------------------------
Expand Down
8 changes: 4 additions & 4 deletions pymodbus/transaction/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def sync_execute(self, no_response_expected: bool, request: ModbusPDU) -> Modbus
count_retries = 0
while count_retries <= self.retries:
self.pdu_send(request)
if no_response_expected:
if not request.dev_id or no_response_expected:
return ExceptionResponse(0xff)
try:
return self.sync_get_response()
Expand All @@ -118,7 +118,7 @@ def sync_execute(self, no_response_expected: bool, request: ModbusPDU) -> Modbus
Log.error(txt)
raise ModbusIOException(txt)

async def execute(self, no_response_expected: bool, request: ModbusPDU) -> ModbusPDU | None:
async def execute(self, no_response_expected: bool, request: ModbusPDU) -> ModbusPDU:
"""Execute requests asynchronously.

REMARK: this method is identical to sync_execute, apart from the lock and try/except.
Expand All @@ -134,8 +134,8 @@ async def execute(self, no_response_expected: bool, request: ModbusPDU) -> Modbu
while count_retries <= self.retries:
self.response_future = asyncio.Future()
self.pdu_send(request)
if no_response_expected:
return None
if not request.dev_id or no_response_expected:
return ExceptionResponse(0xff)
try:
response = await asyncio.wait_for(
self.response_future, timeout=self.comm_params.timeout_connect
Expand Down
29 changes: 15 additions & 14 deletions test/transaction/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pymodbus.client import ModbusBaseSyncClient
from pymodbus.exceptions import ConnectionException, ModbusIOException
from pymodbus.framer import FramerRTU, FramerSocket, FramerType
from pymodbus.pdu import DecodePDU
from pymodbus.pdu import DecodePDU, ExceptionResponse
from pymodbus.pdu.bit_message import ReadCoilsRequest, ReadCoilsResponse
from pymodbus.transaction import TransactionManager

Expand Down Expand Up @@ -134,8 +134,8 @@ async def test_transaction_execute(self, use_clc, scenario):
None,
)
transact.send = mock.Mock()
request = ReadCoilsRequest(address=117, count=5)
response = ReadCoilsResponse(bits=[True, False, True, True, False])
request = ReadCoilsRequest(address=117, count=5, dev_id=1)
response = ReadCoilsResponse(bits=[True, False, True, True, False], dev_id=1)
transact.retries = 0
transact.connection_made(mock.AsyncMock())
transact.transport.write = mock.Mock()
Expand Down Expand Up @@ -206,7 +206,7 @@ async def test_client_protocol_execute_outside(self, use_clc, no_resp):
None,
)
transact.send = mock.Mock()
request = ReadCoilsRequest(address=117, count=5)
request = ReadCoilsRequest(address=117, count=5, dev_id=1)
transact.retries = 0
transact.connection_made(mock.AsyncMock())
transact.transport.write = mock.Mock()
Expand All @@ -216,7 +216,8 @@ async def test_client_protocol_execute_outside(self, use_clc, no_resp):
transact.data_received(data)
result = await resp
if no_resp:
assert not result
assert result.isError()
assert isinstance(result, ExceptionResponse)
else:
assert not result.isError()
assert isinstance(result, ReadCoilsResponse)
Expand Down Expand Up @@ -281,8 +282,8 @@ async def test_sync_transaction_execute(self, use_clc, scenario):
)
transact.send = mock.Mock()
transact.sync_client.connect = mock.Mock(return_value=True)
request = ReadCoilsRequest(address=117, count=5)
response = ReadCoilsResponse(bits=[True, False, True, True, False, False, False, False])
request = ReadCoilsRequest(address=117, count=5, dev_id=1)
response = ReadCoilsResponse(bits=[True, False, True, True, False, False, False, False], dev_id=1)
transact.retries = 0
if scenario == 0: # transport not ok and no connect
transact.transport = None
Expand All @@ -298,7 +299,7 @@ async def test_sync_transaction_execute(self, use_clc, scenario):
transact.trace_packet = mock.Mock(return_value=b'123')
transact.sync_execute(True, request)
transact.trace_pdu.assert_called_once_with(True, request)
transact.trace_packet.assert_called_once_with(True, b'\x00\x01\x00u\x00\x05\xec\x02')
transact.trace_packet.assert_called_once_with(True, b'\x01\x01\x00u\x00\x05\xed\xd3')
elif scenario == 3: # wait receive,timeout, no_responses
transact.comm_params.timeout_connect = 0.1
transact.count_no_responses = 10
Expand Down Expand Up @@ -339,8 +340,8 @@ def test_sync_transaction_receiver(self, use_clc):
)
transact.sync_client.connect = mock.Mock(return_value=True)
transact.sync_client.send = mock.Mock()
request = ReadCoilsRequest(address=117, count=5)
response = ReadCoilsResponse(bits=[True, False, True, True, False, False, False, False])
request = ReadCoilsRequest(address=117, count=5, dev_id=1)
response = ReadCoilsResponse(bits=[True, False, True, True, False, False, False, False], dev_id=1)
transact.retries = 0
transact.transport = 1
resp_bytes = transact.framer.buildFrame(response)
Expand Down Expand Up @@ -372,8 +373,8 @@ def test_sync_client_protocol_execute_outside(self, use_clc, no_resp):
sync_client=client,
)
transact.sync_client.connect = mock.Mock(return_value=True)
request = ReadCoilsRequest(address=117, count=5)
response = ReadCoilsResponse(bits=[True, False, True, True, False, False, False, False])
request = ReadCoilsRequest(address=117, count=5, dev_id=1)
response = ReadCoilsResponse(bits=[True, False, True, True, False, False, False, False], dev_id=1)
transact.retries = 0
transact.transport = 1
resp_bytes = transact.framer.buildFrame(response)
Expand Down Expand Up @@ -407,8 +408,8 @@ def test_sync_client_protocol_execute_no_pdu(self, use_clc):
sync_client=client,
)
transact.sync_client.connect = mock.Mock(return_value=True)
request = ReadCoilsRequest(address=117, count=5)
response = ReadCoilsResponse(bits=[True, False, True, True, False, False, False, False])
request = ReadCoilsRequest(address=117, count=5, dev_id=1)
response = ReadCoilsResponse(bits=[True, False, True, True, False, False, False, False], dev_id=1)
transact.retries = 0
transact.transport = 1
resp_bytes = transact.framer.buildFrame(response)[:-1]
Expand Down