11import logging
22import asyncio
33from pymodbus .client import ModbusTcpClient , ModbusUdpClient
4- from pymodbus .transaction import ModbusRtuFramer , ModbusIOException
5- from pymodbus .pdu import ModbusRequest
4+ from pymodbus .framer .rtu import FramerRTU
5+ from pymodbus .transaction .transaction import ModbusIOException
6+ from pymodbus .pdu import ModbusPDU
7+ #from pymodbus.exceptions import ModbusException
68import threading
79
810try :
3537
3638_LOGGER = logging .getLogger (__name__ )
3739
40+ class ModbusRequest (ModbusPDU ):
41+ """Base class for a modbus request PDU."""
42+
43+ function_code = - 1
44+
45+ def __init__ (self , slave , transaction , skip_encode ):
46+ """Proxy to the lower level initializer.
47+
48+ :param slave: Modbus slave slave ID
49+ """
50+ super ().__init__ (slave , transaction , skip_encode )
51+ self .fut = None
52+
53+ def doException (self , exception ):
54+ """Build an error response based on the function.
55+
56+ :param exception: The exception to return
57+ :raises: An exception response
58+ """
59+ exc = ExceptionResponse (self .function_code , exception )
60+ Log .error ("Exception response {}" , exc )
61+ return exc
62+
3863class ModbusResetEnergyRequest (ModbusRequest ):
3964 _rtu_frame_size = 4
4065 function_code = 0x42
@@ -58,19 +83,19 @@ def __init__(self, protocol, host, port, slave):
5883 self ._client = ModbusTcpClient (
5984 host = host ,
6085 port = port ,
61- framer = ModbusRtuFramer ,
86+ framer = 'rtu' ,
6287 timeout = 2 ,
63- retry_on_empty = True ,
64- retry_on_invalid = False
88+ # retry_on_empty=True,
89+ # retry_on_invalid=False
6590 )
6691 elif protocol == "rtuoverudp" :
6792 self ._client = ModbusUdpClient (
6893 host = host ,
6994 port = port ,
70- framer = ModbusRtuFramer ,
95+ framer = 'rtu' ,
7196 timeout = 2 ,
72- retry_on_empty = False ,
73- retry_on_invalid = False
97+ # retry_on_empty=False,
98+ # retry_on_invalid=False
7499 )
75100
76101 def connect (self ):
@@ -81,27 +106,27 @@ def close(self):
81106 with self ._lock :
82107 self ._client .close ()
83108
84- # 新增同步版本,供线程池调用
85- def read_input_registers_sync (self , address , count ):
86- with self ._lock :
87- kwargs = {"slave " : self ._slave }
88- return self ._client .read_input_registers (address , count , ** kwargs )
89-
90- # 异步版本,交给线程池执行,避免阻塞事件循环
91- async def read_input_registers (self , address , count ):
92- loop = asyncio .get_running_loop ()
93- return await loop .run_in_executor (
94- None ,
95- self .read_input_registers_sync ,
96- address ,
97- count
98- )
99-
100- def reset_energy (self ):
101- with self ._lock :
102- kwargs = {"slave" : self ._slave }
103- request = ModbusResetEnergyRequest (** kwargs )
104- self ._client .execute (request )
109+ # 新增同步版本,供线程池调用
110+ def read_input_registers_sync (self , address , count ):
111+ with self ._lock :
112+ kwargs = {"device_id " : self ._slave }
113+ return self ._client .read_input_registers (address = address , count = count , ** kwargs )
114+
115+ # 异步版本,交给线程池执行,避免阻塞事件循环
116+ async def read_input_registers (self , address , count ):
117+ loop = asyncio .get_running_loop ()
118+ return await loop .run_in_executor (
119+ None ,
120+ self .read_input_registers_sync ,
121+ address ,
122+ count
123+ )
124+
125+ def reset_energy (self ):
126+ with self ._lock :
127+ kwargs = {"slave" : self ._slave }
128+ request = ModbusResetEnergyRequest (** kwargs )
129+ self ._client .execute (request )
105130
106131 # 异步版本info_gather
107132 async def info_gather (self ):
0 commit comments