Skip to content
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
10 changes: 8 additions & 2 deletions pymodbus/server/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from ..logging import Log
from ..pdu import DecodePDU, ModbusPDU
from ..pdu.device import ModbusControlBlock, ModbusDeviceIdentification
from ..simulator import SimDevice
from ..simulator.simcore import SimCore
from ..transport import CommParams, ModbusProtocol
from .requesthandler import ServerRequestHandler

Expand All @@ -22,7 +24,7 @@ class ModbusBaseServer(ModbusProtocol):
def __init__( # pylint: disable=too-many-arguments
self,
params: CommParams,
context: ModbusServerContext | None,
context: ModbusServerContext | SimDevice | list[SimDevice],
ignore_missing_devices: bool,
broadcast_enable: bool,
identity: ModbusDeviceIdentification | None,
Expand All @@ -42,7 +44,11 @@ def __init__( # pylint: disable=too-many-arguments
if custom_pdu:
for func in custom_pdu:
self.decoder.register(func)
self.context = context or ModbusServerContext()
self.context: ModbusServerContext | SimCore
if isinstance(context, ModbusServerContext):
self.context = context
else:
self.context = SimCore(context)
self.control = ModbusControlBlock()
self.ignore_missing_devices = ignore_missing_devices
self.broadcast_enable = broadcast_enable
Expand Down
9 changes: 5 additions & 4 deletions pymodbus/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ..framer import FramerType
from ..pdu import ModbusPDU
from ..pdu.device import ModbusDeviceIdentification
from ..simulator import SimDevice
from ..transport import CommParams, CommType
from .base import ModbusBaseServer

Expand All @@ -20,7 +21,7 @@ class ModbusTcpServer(ModbusBaseServer):

def __init__( # pylint: disable=too-many-arguments
self,
context: ModbusServerContext,
context: ModbusServerContext | SimDevice | list[SimDevice],
*,
framer=FramerType.SOCKET,
identity: ModbusDeviceIdentification | None = None,
Expand Down Expand Up @@ -84,7 +85,7 @@ class ModbusTlsServer(ModbusTcpServer):

def __init__( # pylint: disable=too-many-arguments
self,
context: ModbusServerContext,
context: ModbusServerContext | SimDevice | list[SimDevice],
*,
framer=FramerType.TLS,
identity: ModbusDeviceIdentification | None = None,
Expand Down Expand Up @@ -155,7 +156,7 @@ class ModbusUdpServer(ModbusBaseServer):

def __init__( # pylint: disable=too-many-arguments
self,
context: ModbusServerContext,
context: ModbusServerContext | SimDevice | list[SimDevice],
*,
framer=FramerType.SOCKET,
identity: ModbusDeviceIdentification | None = None,
Expand Down Expand Up @@ -216,7 +217,7 @@ class ModbusSerialServer(ModbusBaseServer):

def __init__(
self,
context: ModbusServerContext,
context: ModbusServerContext | SimDevice | list[SimDevice],
*,
framer: FramerType = FramerType.RTU,
ignore_missing_devices: bool = False,
Expand Down
17 changes: 9 additions & 8 deletions pymodbus/server/startstop.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from time import sleep

from ..datastore import ModbusServerContext
from ..simulator import SimDevice
from .base import ModbusBaseServer
from .server import (
ModbusSerialServer,
Expand All @@ -15,7 +16,7 @@


async def StartAsyncTcpServer(
context: ModbusServerContext,
context: ModbusServerContext | SimDevice | list[SimDevice],
**kwargs,
) -> None:
"""Start and run a tcp modbus server.
Expand All @@ -32,7 +33,7 @@ async def StartAsyncTcpServer(


def StartTcpServer(
context: ModbusServerContext,
context: ModbusServerContext | SimDevice | list[SimDevice],
**kwargs
) -> None:
"""Start and run a modbus TCP server.
Expand All @@ -49,7 +50,7 @@ def StartTcpServer(


async def StartAsyncTlsServer(
context: ModbusServerContext,
context: ModbusServerContext | SimDevice | list[SimDevice],
**kwargs,
) -> None:
"""Start and run a tls modbus server.
Expand All @@ -66,7 +67,7 @@ async def StartAsyncTlsServer(


def StartTlsServer(
context: ModbusServerContext,
context: ModbusServerContext | SimDevice | list[SimDevice],
**kwargs
) -> None:
"""Start and run a modbus TLS server.
Expand All @@ -83,7 +84,7 @@ def StartTlsServer(


async def StartAsyncUdpServer(
context: ModbusServerContext,
context: ModbusServerContext | SimDevice | list[SimDevice],
**kwargs,
) -> None:
"""Start and run a udp modbus server.
Expand All @@ -100,7 +101,7 @@ async def StartAsyncUdpServer(


def StartUdpServer(
context: ModbusServerContext,
context: ModbusServerContext | SimDevice | list[SimDevice],
**kwargs
) -> None:
"""Start and run a modbus UDP server.
Expand All @@ -117,7 +118,7 @@ def StartUdpServer(


async def StartAsyncSerialServer(
context: ModbusServerContext,
context: ModbusServerContext | SimDevice | list[SimDevice],
**kwargs,
) -> None:
"""Start and run a serial modbus server.
Expand All @@ -134,7 +135,7 @@ async def StartAsyncSerialServer(


def StartSerialServer(
context: ModbusServerContext,
context: ModbusServerContext | SimDevice | list[SimDevice],
**kwargs
) -> None:
"""Start and run a modbus serial server.
Expand Down
16 changes: 11 additions & 5 deletions pymodbus/simulator/simcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import annotations

from ..constants import ExcCodes
from ..pdu.pdu import unpack_bitstring
from .simdevice import SimDevice, SimRegs


Expand All @@ -22,10 +23,15 @@ class Runtime:
for i in (1, 5, 15)])


def __convert_to_bit(self, block: SimRegs):
@classmethod
def convert_to_bit(cls, block: SimRegs) -> SimRegs:
"""Convert registers to bits."""
new_flags = block[1]
new_registers = block[2]
new_registers: list[int] = []
for entry in block[2]:
bool_list = unpack_bitstring(entry.to_bytes(2, byteorder="big"))
for x in bool_list:
new_registers.append(1 if x else 0)
new_flags: list[int] = [block[1][0]]*len(new_registers)
return (block[0], new_flags, new_registers)

def __init__(self, device: SimDevice):
Expand All @@ -39,8 +45,8 @@ def __init__(self, device: SimDevice):
return
self.shared = False
self.block: dict[str, SimRegs] = {
"c": self.__convert_to_bit(build["c"]),
"d": self.__convert_to_bit(build["d"]),
"c": self.convert_to_bit(build["c"]),
"d": self.convert_to_bit(build["d"]),
"h": build["h"],
"i": build["i"],
}
Expand Down
4 changes: 3 additions & 1 deletion test/client/test_client_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ class TestSyncClientUdp:

def test_basic_syn_udp_bind(self):
"""Test the basic methods for the udp sync client."""
ModbusUdpClient("127.0.0.1", source_address=('', 4096))
client = ModbusUdpClient("127.0.0.1", source_address=('', 4096))
client.connect()


def test_basic_syn_udp_client(self):
"""Test the basic methods for the udp sync client."""
Expand Down
25 changes: 24 additions & 1 deletion test/server/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

import pytest

from pymodbus.datastore import ModbusServerContext
from pymodbus.pdu import ReadHoldingRegistersRequest
from pymodbus.server import ModbusBaseServer
from pymodbus.simulator import SimData, SimDevice
from pymodbus.transport import CommParams, CommType


Expand All @@ -22,7 +24,7 @@ async def baseserver(self):
reconnect_delay_max=0.0,
timeout_connect=0.0,
),
None,
ModbusServerContext(),
False,
False,
None,
Expand All @@ -37,6 +39,27 @@ async def baseserver(self):
async def test_base(self, baseserver):
"""Test __init__."""

async def test_base_simcore(self):
"""Test __init___ with SimCore."""
ModbusBaseServer(
CommParams(
comm_type=CommType.TCP,
comm_name="server_listener",
reconnect_delay=0.0,
reconnect_delay_max=0.0,
timeout_connect=0.0,
),
SimDevice(0, SimData(0)),
False,
False,
None,
"socket",
None,
None,
None,
[ReadHoldingRegistersRequest],
)

async def test_base_serve_forever1(self, baseserver):
"""Test serve_forever."""
baseserver.listen = mock.AsyncMock(return_value=None)
Expand Down
10 changes: 10 additions & 0 deletions test/simulator/test_simcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,13 @@ async def test_simdcore_set(self, kwargs):
"""Test that simdata can be objects."""
core = SimCore(devices=SimDevice(0, simdata=self.simdata2))
await core.async_setValues(**kwargs)

@pytest.mark.parametrize(("block", "expect"), [
((3, [1], [0xffff]), (3, [1]*16, [1]*16)),
((3, [1], [0x0000]), (3, [1]*16, [0]*16)),
((3, [1], [0xffff, 0xffff]), (3, [1]*32, [1]*32)),
])
async def test_simdcore_convert_bit(self, block, expect):
"""Test that simdata can be objects."""
result = SimCore.Runtime.convert_to_bit(block)
assert result == expect