Skip to content

Commit 80bfa78

Browse files
committed
new rtu.
1 parent 503804c commit 80bfa78

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

pymodbus/framer/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
"""
77
from __future__ import annotations
88

9+
from abc import abstractmethod
10+
911

1012
class FramerBase:
1113
"""Intern base."""
@@ -21,6 +23,7 @@ def set_dev_ids(self, _dev_ids: list[int]):
2123
def set_fc_calc(self, _fc: int, _msg_size: int, _count_pos: int):
2224
"""Set/Update function code information."""
2325

26+
@abstractmethod
2427
def decode(self, data: bytes) -> tuple[int, int, int, bytes]:
2528
"""Decode ADU.
2629
@@ -31,6 +34,7 @@ def decode(self, data: bytes) -> tuple[int, int, int, bytes]:
3134
modbus request/response (bytes)
3235
"""
3336

37+
@abstractmethod
3438
def encode(self, pdu: bytes, dev_id: int, tid: int) -> bytes:
3539
"""Encode ADU.
3640

pymodbus/framer/framer.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,19 @@ def __init__(self,
6666
super().__init__(params, is_server)
6767
self.device_ids = device_ids
6868
self.broadcast: bool = (0 in device_ids)
69+
self.handle1 = FramerRaw()
70+
self.handle2 = FramerAscii()
71+
self.handle3 = FramerRTU()
72+
self.handle4 = FramerSocket()
73+
self.handle5 = FramerTLS()
74+
6975
self.handle = {
70-
FramerType.RAW: FramerRaw,
71-
FramerType.ASCII: FramerAscii,
72-
FramerType.RTU: FramerRTU,
73-
FramerType.SOCKET: FramerSocket,
74-
FramerType.TLS: FramerTLS,
75-
}[framer_type]()
76+
FramerType.RAW: FramerRaw(),
77+
FramerType.ASCII: FramerAscii(),
78+
FramerType.RTU: FramerRTU(),
79+
FramerType.SOCKET: FramerSocket(),
80+
FramerType.TLS: FramerTLS(),
81+
}[framer_type]
7682

7783

7884
def callback_data(self, data: bytes, addr: tuple | None = None) -> int:

pymodbus/framer/raw.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ def decode(self, data: bytes) -> tuple[int, int, int, bytes]:
2727
tid = int(data[1])
2828
return len(data), dev_id, tid, data[2:]
2929

30-
def encode(self, pdu: bytes, device_id: int, tid: int) -> bytes:
30+
def encode(self, pdu: bytes, dev_id: int, tid: int) -> bytes:
3131
"""Encode ADU."""
32-
return device_id.to_bytes(1, 'big') + tid.to_bytes(1, 'big') + pdu
32+
return dev_id.to_bytes(1, 'big') + tid.to_bytes(1, 'big') + pdu

pymodbus/framer/rtu.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Modbus RTU frame implementation."""
22
from __future__ import annotations
33

4+
from collections import namedtuple
5+
46
from pymodbus.framer.base import FramerBase
57
from pymodbus.logging import Log
68

@@ -15,7 +17,7 @@ class FramerRTU(FramerBase):
1517
neither when receiving nor when sending.
1618
1719
Decoding is a complicated process because the RTU frame does not have a fixed prefix
18-
only suffix, therefore it is nessecary to decode the content (PDU) to get length etc.
20+
only suffix, therefore it is necessary to decode the content (PDU) to get length etc.
1921
2022
There are some restraints however that help the detection.
2123
@@ -43,16 +45,19 @@ class FramerRTU(FramerBase):
4345
- data in frame garbled (wrong CRC)
4446
decoding assumes the frame is sound, and if not enters a hunting mode.
4547
46-
The 3.5 byte wait betwen frames is 31ms at 1.200Bps and 1ms at 38.600bps,
47-
so the decoder will wait 50ms for more data if not the transmission is
48+
The 3.5 byte wait 31ms at 1.200Bps and 1ms at 38.600bps,
49+
so the decoder will wait for more data a number of milliseconds, if not the transmission is
4850
considered complete
4951
"""
5052

5153
MIN_SIZE = 5
5254

55+
FC_LEN = namedtuple("FC_LEN", "req_len req_bytepos resp_len resp_bytepos")
56+
5357
def __init__(self) -> None:
5458
"""Initialize a ADU instance."""
5559
super().__init__()
60+
self.fc_len: dict[int, FramerRTU.FC_LEN] = {}
5661

5762

5863
@classmethod
@@ -74,6 +79,14 @@ def generate_crc16_table(cls) -> list[int]:
7479
return result
7580
crc16_table: list[int] = [0]
7681

82+
83+
def setup_fc_len(self, _fc: int,
84+
_req_len: int, _req_byte_pos: int,
85+
_resp_len: int, _resp_byte_pos: int
86+
):
87+
"""Define request/response lengths pr function code."""
88+
return
89+
7790
def decode(self, data: bytes) -> tuple[int, int, int, bytes]:
7891
"""Decode ADU."""
7992
if (buf_len := len(data)) < self.MIN_SIZE:

0 commit comments

Comments
 (0)