File: generator.py

package info (click to toggle)
pymodbus 3.8.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,720 kB
  • sloc: python: 14,867; makefile: 27; sh: 17
file content (45 lines) | stat: -rwxr-xr-x 1,739 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
"""Build framer encode responses."""

from pymodbus.framer import (
    FramerAscii,
    FramerRTU,
    FramerSocket,
    FramerTLS,
)
from pymodbus.pdu import DecodePDU, ExceptionResponse
from pymodbus.pdu.register_message import (
    ReadHoldingRegistersRequest,
    ReadHoldingRegistersResponse,
)


def set_calls():
    """Define calls."""
    for framer in (FramerAscii, FramerRTU, FramerSocket, FramerTLS):
        print(f"framer --> {framer}")
        for dev_id in (0, 17, 255):
            print(f"  dev_id --> {dev_id}")
            for tid in (0, 3077):
                print(f"    tid --> {tid}")
                client = framer(DecodePDU(False))
                request = ReadHoldingRegistersRequest(address=124, count=2, dev_id=dev_id)
                request.transaction_id = tid
                result = client.buildFrame(request)
                print(f"      request --> {result}")
                print(f"      request --> {result.hex()}")
                server = framer(DecodePDU(True))
                response = ReadHoldingRegistersResponse(registers=[141,142])
                response.dev_id = dev_id
                response.transaction_id = tid
                result = server.buildFrame(response)
                print(f"      response --> {result}")
                print(f"      response --> {result.hex()}")
                exception = ExceptionResponse(request.function_code, ExceptionResponse.ILLEGAL_ADDRESS)
                exception.transaction_id = tid
                exception.dev_id = dev_id
                result = server.buildFrame(exception)
                print(f"      exception --> {result}")
                print(f"      exception --> {result.hex()}")

set_calls()