Open
Description
Problem
The function signature for the ModbusBaseSlaveContext.getValues
method is as follows:
def getValues(self, fc_as_hex: int, address: int, count: int = 1) -> Sequence[int | bool]:
However, I've found that if I return a tuple
for this method instead of a list
it causes a failure when trying to read coils but not holding registers. The workaround is easy (just return a list
instead) but I've not found the underlying cause.
More information
- python 3.13.3
- pymodbus 3.9.2
Code
server.py
import asyncio
from typing import Sequence
from pymodbus.datastore import ModbusServerContext, ModbusBaseSlaveContext
from pymodbus.server import ModbusTcpServer
class ModbusSlaveContext(ModbusBaseSlaveContext):
def getValues(self, fc_as_hex: int, address: int, count: int = 1) -> Sequence[int | bool]:
print(f"requested ({fc_as_hex=}, {address=}, {count=})")
return list([1] * count) # <-- HERE using tuple instead of list fails
slave_context = ModbusSlaveContext()
context = ModbusServerContext(slaves=slave_context, single=True)
async def main() -> None:
server = ModbusTcpServer(
context=context,
address=("127.0.0.1", 5022),
)
await server.serve_forever()
if __name__ == "__main__":
asyncio.run(main())
client.py
import asyncio
from pymodbus.client import AsyncModbusTcpClient
async def main() -> None:
client = AsyncModbusTcpClient(
host="127.0.0.1",
port=5022,
)
await client.connect()
result = await client.read_coils(address=500, count=2, slave=1)
print(result)
if __name__ == "__main__":
asyncio.run(main())
Metadata
Metadata
Assignees
Labels
No labels