Skip to content

Commit 7e6af33

Browse files
committed
Client/Server framer as enum.
1 parent d3aa53d commit 7e6af33

24 files changed

+189
-208
lines changed

API_changes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Versions (X.Y.Z) where Z > 0 e.g. 3.0.1 do NOT have API changes!
44

55
API changes 3.6.0 (future)
66
--------------------------
7+
- framer= is an enum: pymodbus.Framer, but still accept a framer class
78

89

910
API changes 3.5.0

examples/client_custom_msg.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,10 @@
1414
import logging
1515
import struct
1616

17+
from pymodbus import Framer
1718
from pymodbus.bit_read_message import ReadCoilsRequest
1819
from pymodbus.client import AsyncModbusTcpClient as ModbusClient
19-
20-
# --------------------------------------------------------------------------- #
21-
# import the various server implementations
22-
# --------------------------------------------------------------------------- #
2320
from pymodbus.pdu import ModbusExceptions, ModbusRequest, ModbusResponse
24-
from pymodbus.transaction import ModbusSocketFramer
2521

2622

2723
# --------------------------------------------------------------------------- #
@@ -130,7 +126,7 @@ def __init__(self, address, **kwargs):
130126

131127
async def main(host="localhost", port=5020):
132128
"""Run versions of read coil."""
133-
with ModbusClient(host=host, port=port, framer=ModbusSocketFramer) as client:
129+
with ModbusClient(host=host, port=port, framer_name=Framer.SOCKET) as client:
134130
await client.connect()
135131

136132
# new modbus function code.

examples/client_performance.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import asyncio
1717
import time
1818

19+
from pymodbus import Framer
1920
from pymodbus.client import AsyncModbusSerialClient, ModbusSerialClient
20-
from pymodbus.transaction import ModbusRtuFramer
2121

2222

2323
LOOP_COUNT = 1000
@@ -29,7 +29,7 @@ def run_sync_client_test():
2929
print("--- Testing sync client v3.4.1")
3030
client = ModbusSerialClient(
3131
"/dev/ttys007",
32-
framer=ModbusRtuFramer,
32+
framer_name=Framer.RTU,
3333
baudrate=9600,
3434
)
3535
client.connect()
@@ -56,7 +56,7 @@ async def run_async_client_test():
5656
print("--- Testing async client v3.4.1")
5757
client = AsyncModbusSerialClient(
5858
"/dev/ttys007",
59-
framer=ModbusRtuFramer,
59+
framer_name=Framer.RTU,
6060
baudrate=9600,
6161
)
6262
await client.connect()

examples/datastore_simulator.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@
2525
import asyncio
2626
import logging
2727

28-
from pymodbus import pymodbus_apply_logging_config
28+
from pymodbus import Framer, pymodbus_apply_logging_config
2929
from pymodbus.datastore import ModbusServerContext, ModbusSimulatorContext
3030
from pymodbus.device import ModbusDeviceIdentification
3131
from pymodbus.server import StartAsyncTcpServer
32-
from pymodbus.transaction import ModbusSocketFramer
3332

3433

3534
logging.basicConfig()
@@ -141,7 +140,7 @@ def setup_simulator(setup=None, actions=None, cmdline=None):
141140
args = get_commandline(cmdline=cmdline)
142141
pymodbus_apply_logging_config(args.log.upper())
143142
_logger.setLevel(args.log.upper())
144-
args.framer = ModbusSocketFramer
143+
args.framer = Framer.SOCKET
145144
args.port = int(args.port)
146145

147146
_logger.info("### Create datastore")

examples/helper.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,11 @@
99
import os
1010

1111
from pymodbus import pymodbus_apply_logging_config
12-
from pymodbus.transaction import (
13-
ModbusAsciiFramer,
14-
ModbusBinaryFramer,
15-
ModbusRtuFramer,
16-
ModbusSocketFramer,
17-
ModbusTlsFramer,
18-
)
1912

2013

2114
_logger = logging.getLogger(__file__)
2215

2316

24-
def get_framer(framer):
25-
"""Convert framer name to framer class"""
26-
framers = {
27-
"ascii": ModbusAsciiFramer,
28-
"binary": ModbusBinaryFramer,
29-
"rtu": ModbusRtuFramer,
30-
"socket": ModbusSocketFramer,
31-
"tls": ModbusTlsFramer,
32-
}
33-
return framers[framer]
34-
35-
3617
def get_commandline(server=False, description=None, extras=None, cmdline=None):
3718
"""Read and validate command line arguments"""
3819
parser = argparse.ArgumentParser(description=description)
@@ -123,7 +104,8 @@ def get_commandline(server=False, description=None, extras=None, cmdline=None):
123104
}
124105
pymodbus_apply_logging_config(args.log.upper())
125106
_logger.setLevel(args.log.upper())
126-
args.framer = get_framer(args.framer or comm_defaults[args.comm][0])
107+
if not args.framer:
108+
args.framer = comm_defaults[args.comm][0]
127109
args.port = args.port or comm_defaults[args.comm][1]
128110
if args.comm != "serial" and args.port:
129111
args.port = int(args.port)

examples/server_hook.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
import asyncio
88
import logging
99

10-
from pymodbus import pymodbus_apply_logging_config
10+
from pymodbus import Framer, pymodbus_apply_logging_config
1111
from pymodbus.datastore import (
1212
ModbusSequentialDataBlock,
1313
ModbusServerContext,
1414
ModbusSlaveContext,
1515
)
1616
from pymodbus.server import ModbusTcpServer
17-
from pymodbus.transaction import ModbusSocketFramer
1817

1918

2019
class Manipulator:
@@ -64,7 +63,7 @@ async def setup(self):
6463
)
6564
self.server = ModbusTcpServer(
6665
context,
67-
ModbusSocketFramer,
66+
Framer.SOCKET,
6867
None,
6968
("127.0.0.1", 5020),
7069
request_tracer=self.server_request_tracer,

examples/simple_async_client.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@
1111
"""
1212
import asyncio
1313

14-
from pymodbus import pymodbus_apply_logging_config
15-
16-
# --------------------------------------------------------------------------- #
17-
# import the various client implementations
18-
# --------------------------------------------------------------------------- #
14+
from pymodbus import Framer, pymodbus_apply_logging_config
1915
from pymodbus.client import (
2016
AsyncModbusSerialClient,
2117
AsyncModbusTcpClient,
@@ -24,16 +20,9 @@
2420
)
2521
from pymodbus.exceptions import ModbusException
2622
from pymodbus.pdu import ExceptionResponse
27-
from pymodbus.transaction import (
28-
# ModbusAsciiFramer,
29-
# ModbusBinaryFramer,
30-
ModbusRtuFramer,
31-
ModbusSocketFramer,
32-
ModbusTlsFramer,
33-
)
3423

3524

36-
async def run_async_simple_client(comm, host, port, framer=ModbusSocketFramer):
25+
async def run_async_simple_client(comm, host, port, framer=Framer.SOCKET):
3726
"""Run async client."""
3827

3928
# activate debugging
@@ -56,7 +45,7 @@ async def run_async_simple_client(comm, host, port, framer=ModbusSocketFramer):
5645
client = AsyncModbusUdpClient(
5746
host,
5847
port=port,
59-
framer=ModbusSocketFramer,
48+
framer=framer,
6049
# timeout=10,
6150
# retries=3,
6251
# retry_on_empty=False,
@@ -67,7 +56,7 @@ async def run_async_simple_client(comm, host, port, framer=ModbusSocketFramer):
6756
elif comm == "serial":
6857
client = AsyncModbusSerialClient(
6958
port,
70-
framer=ModbusRtuFramer,
59+
framer=framer,
7160
# timeout=10,
7261
# retries=3,
7362
# retry_on_empty=False,
@@ -83,7 +72,7 @@ async def run_async_simple_client(comm, host, port, framer=ModbusSocketFramer):
8372
client = AsyncModbusTlsClient(
8473
host,
8574
port=port,
86-
framer=ModbusTlsFramer,
75+
framer=Framer.TLS,
8776
# timeout=10,
8877
# retries=3,
8978
# retry_on_empty=False,

examples/simple_sync_client.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# --------------------------------------------------------------------------- #
1414
# import the various client implementations
1515
# --------------------------------------------------------------------------- #
16-
from pymodbus import pymodbus_apply_logging_config
16+
from pymodbus import Framer, pymodbus_apply_logging_config
1717
from pymodbus.client import (
1818
ModbusSerialClient,
1919
ModbusTcpClient,
@@ -22,16 +22,9 @@
2222
)
2323
from pymodbus.exceptions import ModbusException
2424
from pymodbus.pdu import ExceptionResponse
25-
from pymodbus.transaction import (
26-
# ModbusAsciiFramer,
27-
# ModbusBinaryFramer,
28-
ModbusRtuFramer,
29-
ModbusSocketFramer,
30-
ModbusTlsFramer,
31-
)
3225

3326

34-
def run_sync_simple_client(comm, host, port, framer=ModbusSocketFramer):
27+
def run_sync_simple_client(comm, host, port, framer=Framer.SOCKET):
3528
"""Run sync client."""
3629

3730
# activate debugging
@@ -54,7 +47,7 @@ def run_sync_simple_client(comm, host, port, framer=ModbusSocketFramer):
5447
client = ModbusUdpClient(
5548
host,
5649
port=port,
57-
framer=ModbusSocketFramer,
50+
framer=framer,
5851
# timeout=10,
5952
# retries=3,
6053
# retry_on_empty=False,
@@ -65,7 +58,7 @@ def run_sync_simple_client(comm, host, port, framer=ModbusSocketFramer):
6558
elif comm == "serial":
6659
client = ModbusSerialClient(
6760
port,
68-
framer=ModbusRtuFramer,
61+
framer=framer,
6962
# timeout=10,
7063
# retries=3,
7164
# retry_on_empty=False,
@@ -81,7 +74,7 @@ def run_sync_simple_client(comm, host, port, framer=ModbusSocketFramer):
8174
client = ModbusTlsClient(
8275
host,
8376
port=port,
84-
framer=ModbusTlsFramer,
77+
framer=Framer.TLS,
8578
# timeout=10,
8679
# retries=3,
8780
# retry_on_empty=False,

examples/simulator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
import asyncio
1111
import logging
1212

13+
from pymodbus import Framer
1314
from pymodbus.client import AsyncModbusTcpClient
1415
from pymodbus.datastore import ModbusSimulatorContext
1516
from pymodbus.server import ModbusSimulatorServer, get_simulator_commandline
16-
from pymodbus.transaction import ModbusSocketFramer
1717

1818

1919
logging.basicConfig()
@@ -75,7 +75,7 @@ async def run_simulator():
7575
client = AsyncModbusTcpClient(
7676
"127.0.0.1",
7777
port=5020,
78-
framer=ModbusSocketFramer,
78+
framer=Framer.SOCKET,
7979
)
8080
await client.connect()
8181
assert client.connected

pymodbus/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
"""
55

66
__all__ = [
7+
"Framer",
78
"pymodbus_apply_logging_config",
89
"__version__",
910
"__version_full__",
1011
]
1112

13+
from pymodbus.framer import Framer
1214
from pymodbus.logging import pymodbus_apply_logging_config
1315

1416

0 commit comments

Comments
 (0)