/usr/lib/python2.7/dist-packages/pymodbus/register_read_message.py is in python-pymodbus 1.3.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | '''
Register Reading Request/Response
---------------------------------
'''
import struct
from pymodbus.pdu import ModbusRequest
from pymodbus.pdu import ModbusResponse
from pymodbus.pdu import ModbusExceptions as merror
from pymodbus.compat import int2byte, byte2int
class ReadRegistersRequestBase(ModbusRequest):
'''
Base class for reading a modbus register
'''
_rtu_frame_size = 8
def __init__(self, address, count, **kwargs):
''' Initializes a new instance
:param address: The address to start the read from
:param count: The number of registers to read
'''
ModbusRequest.__init__(self, **kwargs)
self.address = address
self.count = count
def encode(self):
''' Encodes the request packet
:return: The encoded packet
'''
return struct.pack('>HH', self.address, self.count)
def decode(self, data):
''' Decode a register request packet
:param data: The request to decode
'''
self.address, self.count = struct.unpack('>HH', data)
def get_response_pdu_size(self):
"""
Func_code (1 byte) + Byte Count(1 byte) + 2 * Quantity of Coils (n Bytes)
:return:
"""
return 1 + 1 + 2 * self.count
def __str__(self):
''' Returns a string representation of the instance
:returns: A string representation of the instance
'''
return "ReadRegisterRequest (%d,%d)" % (self.address, self.count)
class ReadRegistersResponseBase(ModbusResponse):
'''
Base class for responsing to a modbus register read
'''
_rtu_byte_count_pos = 2
def __init__(self, values, **kwargs):
''' Initializes a new instance
:param values: The values to write to
'''
ModbusResponse.__init__(self, **kwargs)
self.registers = values or []
def encode(self):
''' Encodes the response packet
:returns: The encoded packet
'''
result = int2byte(len(self.registers) * 2)
for register in self.registers:
result += struct.pack('>H', register)
return result
def decode(self, data):
''' Decode a register response packet
:param data: The request to decode
'''
byte_count = byte2int(data[0])
self.registers = []
for i in range(1, byte_count + 1, 2):
self.registers.append(struct.unpack('>H', data[i:i + 2])[0])
def getRegister(self, index):
''' Get the requested register
:param index: The indexed register to retrieve
:returns: The request register
'''
return self.registers[index]
def __str__(self):
''' Returns a string representation of the instance
:returns: A string representation of the instance
'''
return "ReadRegisterResponse (%d)" % len(self.registers)
class ReadHoldingRegistersRequest(ReadRegistersRequestBase):
'''
This function code is used to read the contents of a contiguous block
of holding registers in a remote device. The Request PDU specifies the
starting register address and the number of registers. In the PDU
Registers are addressed starting at zero. Therefore registers numbered
1-16 are addressed as 0-15.
'''
function_code = 3
def __init__(self, address=None, count=None, **kwargs):
''' Initializes a new instance of the request
:param address: The starting address to read from
:param count: The number of registers to read from address
'''
ReadRegistersRequestBase.__init__(self, address, count, **kwargs)
def execute(self, context):
''' Run a read holding request against a datastore
:param context: The datastore to request from
:returns: An initialized response, exception message otherwise
'''
if not (1 <= self.count <= 0x7d):
return self.doException(merror.IllegalValue)
if not context.validate(self.function_code, self.address, self.count):
return self.doException(merror.IllegalAddress)
values = context.getValues(self.function_code, self.address, self.count)
return ReadHoldingRegistersResponse(values)
class ReadHoldingRegistersResponse(ReadRegistersResponseBase):
'''
This function code is used to read the contents of a contiguous block
of holding registers in a remote device. The Request PDU specifies the
starting register address and the number of registers. In the PDU
Registers are addressed starting at zero. Therefore registers numbered
1-16 are addressed as 0-15.
'''
function_code = 3
def __init__(self, values=None, **kwargs):
''' Initializes a new response instance
:param values: The resulting register values
'''
ReadRegistersResponseBase.__init__(self, values, **kwargs)
class ReadInputRegistersRequest(ReadRegistersRequestBase):
'''
This function code is used to read from 1 to approx. 125 contiguous
input registers in a remote device. The Request PDU specifies the
starting register address and the number of registers. In the PDU
Registers are addressed starting at zero. Therefore input registers
numbered 1-16 are addressed as 0-15.
'''
function_code = 4
def __init__(self, address=None, count=None, **kwargs):
''' Initializes a new instance of the request
:param address: The starting address to read from
:param count: The number of registers to read from address
'''
ReadRegistersRequestBase.__init__(self, address, count, **kwargs)
def execute(self, context):
''' Run a read input request against a datastore
:param context: The datastore to request from
:returns: An initialized response, exception message otherwise
'''
if not (1 <= self.count <= 0x7d):
return self.doException(merror.IllegalValue)
if not context.validate(self.function_code, self.address, self.count):
return self.doException(merror.IllegalAddress)
values = context.getValues(self.function_code, self.address, self.count)
return ReadInputRegistersResponse(values)
class ReadInputRegistersResponse(ReadRegistersResponseBase):
'''
This function code is used to read from 1 to approx. 125 contiguous
input registers in a remote device. The Request PDU specifies the
starting register address and the number of registers. In the PDU
Registers are addressed starting at zero. Therefore input registers
numbered 1-16 are addressed as 0-15.
'''
function_code = 4
def __init__(self, values=None, **kwargs):
''' Initializes a new response instance
:param values: The resulting register values
'''
ReadRegistersResponseBase.__init__(self, values, **kwargs)
class ReadWriteMultipleRegistersRequest(ModbusRequest):
'''
This function code performs a combination of one read operation and one
write operation in a single MODBUS transaction. The write
operation is performed before the read.
Holding registers are addressed starting at zero. Therefore holding
registers 1-16 are addressed in the PDU as 0-15.
The request specifies the starting address and number of holding
registers to be read as well as the starting address, number of holding
registers, and the data to be written. The byte count specifies the
number of bytes to follow in the write data field."
'''
function_code = 23
_rtu_byte_count_pos = 10
def __init__(self, **kwargs):
''' Initializes a new request message
:param read_address: The address to start reading from
:param read_count: The number of registers to read from address
:param write_address: The address to start writing to
:param write_registers: The registers to write to the specified address
'''
ModbusRequest.__init__(self, **kwargs)
self.read_address = kwargs.get('read_address', 0x00)
self.read_count = kwargs.get('read_count', 0)
self.write_address = kwargs.get('write_address', 0x00)
self.write_registers = kwargs.get('write_registers', None)
if not hasattr(self.write_registers, '__iter__'):
self.write_registers = [self.write_registers]
self.write_count = len(self.write_registers)
self.write_byte_count = self.write_count * 2
def encode(self):
''' Encodes the request packet
:returns: The encoded packet
'''
result = struct.pack('>HHHHB',
self.read_address, self.read_count, \
self.write_address, self.write_count, self.write_byte_count)
for register in self.write_registers:
result += struct.pack('>H', register)
return result
def decode(self, data):
''' Decode the register request packet
:param data: The request to decode
'''
self.read_address, self.read_count, \
self.write_address, self.write_count, \
self.write_byte_count = struct.unpack('>HHHHB', data[:9])
self.write_registers = []
for i in range(9, self.write_byte_count + 9, 2):
register = struct.unpack('>H', data[i:i + 2])[0]
self.write_registers.append(register)
def execute(self, context):
''' Run a write single register request against a datastore
:param context: The datastore to request from
:returns: An initialized response, exception message otherwise
'''
if not (1 <= self.read_count <= 0x07d):
return self.doException(merror.IllegalValue)
if not (1 <= self.write_count <= 0x079):
return self.doException(merror.IllegalValue)
if (self.write_byte_count != self.write_count * 2):
return self.doException(merror.IllegalValue)
if not context.validate(self.function_code, self.write_address,
self.write_count):
return self.doException(merror.IllegalAddress)
if not context.validate(self.function_code, self.read_address,
self.read_count):
return self.doException(merror.IllegalAddress)
context.setValues(self.function_code, self.write_address,
self.write_registers)
registers = context.getValues(self.function_code, self.read_address,
self.read_count)
return ReadWriteMultipleRegistersResponse(registers)
def get_response_pdu_size(self):
"""
Func_code (1 byte) + Byte Count(1 byte) + 2 * Quantity of Coils (n Bytes)
:return:
"""
return 1 + 1 + 2 * self.read_count
def __str__(self):
''' Returns a string representation of the instance
:returns: A string representation of the instance
'''
params = (self.read_address, self.read_count, self.write_address,
self.write_count)
return "ReadWriteNRegisterRequest R(%d,%d) W(%d,%d)" % params
class ReadWriteMultipleRegistersResponse(ModbusResponse):
'''
The normal response contains the data from the group of registers that
were read. The byte count field specifies the quantity of bytes to
follow in the read data field.
'''
function_code = 23
_rtu_byte_count_pos = 2
def __init__(self, values=None, **kwargs):
''' Initializes a new instance
:param values: The register values to write
'''
ModbusResponse.__init__(self, **kwargs)
self.registers = values or []
def encode(self):
''' Encodes the response packet
:returns: The encoded packet
'''
result = int2byte(len(self.registers) * 2)
for register in self.registers:
result += struct.pack('>H', register)
return result
def decode(self, data):
''' Decode the register response packet
:param data: The response to decode
'''
bytecount = byte2int(data[0])
for i in range(1, bytecount, 2):
self.registers.append(struct.unpack('>H', data[i:i + 2])[0])
def __str__(self):
''' Returns a string representation of the instance
:returns: A string representation of the instance
'''
return "ReadWriteNRegisterResponse (%d)" % len(self.registers)
#---------------------------------------------------------------------------#
# Exported symbols
#---------------------------------------------------------------------------#
__all__ = [
"ReadHoldingRegistersRequest", "ReadHoldingRegistersResponse",
"ReadInputRegistersRequest", "ReadInputRegistersResponse",
"ReadWriteMultipleRegistersRequest", "ReadWriteMultipleRegistersResponse",
]
|