/usr/share/pyshared/pymodbus/register_write_message.py is in python-pymodbus 0.9.0+r175-3.
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 | '''
Register Writing Request/Response Messages
-------------------------------------------
'''
import struct
from pymodbus.pdu import ModbusRequest
from pymodbus.pdu import ModbusResponse
from pymodbus.pdu import ModbusExceptions as merror
class WriteSingleRegisterRequest(ModbusRequest):
'''
This function code is used to write a single holding register in a
remote device.
The Request PDU specifies the address of the register to
be written. Registers are addressed starting at zero. Therefore register
numbered 1 is addressed as 0.
'''
function_code = 6
_rtu_frame_size = 8
def __init__(self, address=None, value=None, **kwargs):
''' Initializes a new instance
:param address: The address to start writing add
:param value: The values to write
'''
ModbusRequest.__init__(self, **kwargs)
self.address = address
self.value = value
def encode(self):
''' Encode a write single register packet packet request
:returns: The encoded packet
'''
return struct.pack('>HH', self.address, self.value)
def decode(self, data):
''' Decode a write single register packet packet request
:param data: The request to decode
'''
self.address, self.value = struct.unpack('>HH', data)
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 (0 <= self.value <= 0xffff):
return self.doException(merror.IllegalValue)
if not context.validate(self.function_code, self.address, 1):
return self.doException(merror.IllegalAddress)
context.setValues(self.function_code, self.address, [self.value])
values = context.getValues(self.function_code, self.address, 1)
return WriteSingleRegisterResponse(self.address, values[0])
def __str__(self):
''' Returns a string representation of the instance
:returns: A string representation of the instance
'''
return "WriteRegisterRequest %d => %d" % (self.address, self.value)
class WriteSingleRegisterResponse(ModbusResponse):
'''
The normal response is an echo of the request, returned after the
register contents have been written.
'''
function_code = 6
_rtu_frame_size = 8
def __init__(self, address=None, value=None, **kwargs):
''' Initializes a new instance
:param address: The address to start writing add
:param value: The values to write
'''
ModbusResponse.__init__(self, **kwargs)
self.address = address
self.value = value
def encode(self):
''' Encode a write single register packet packet request
:returns: The encoded packet
'''
return struct.pack('>HH', self.address, self.value)
def decode(self, data):
''' Decode a write single register packet packet request
:param data: The request to decode
'''
self.address, self.value = struct.unpack('>HH', data)
def __str__(self):
''' Returns a string representation of the instance
:returns: A string representation of the instance
'''
params = (self.address, self.value)
return "WriteRegisterResponse %d => %d" % params
#---------------------------------------------------------------------------#
# Write Multiple Registers
#---------------------------------------------------------------------------#
class WriteMultipleRegistersRequest(ModbusRequest):
'''
This function code is used to write a block of contiguous registers (1
to approx. 120 registers) in a remote device.
The requested written values are specified in the request data field.
Data is packed as two bytes per register.
'''
function_code = 16
_rtu_byte_count_pos = 6
def __init__(self, address=None, values=None, **kwargs):
''' Initializes a new instance
:param address: The address to start writing to
:param values: The values to write
'''
ModbusRequest.__init__(self, **kwargs)
self.address = address
if not values: values = []
elif not hasattr(values, '__iter__'): values = [values]
self.values = values
self.count = len(self.values)
self.byte_count = self.count * 2
def encode(self):
''' Encode a write single register packet packet request
:returns: The encoded packet
'''
packet = struct.pack('>HHB', self.address, self.count, self.byte_count)
for value in self.values:
packet += struct.pack('>H', value)
return packet
def decode(self, data):
''' Decode a write single register packet packet request
:param data: The request to decode
'''
self.address, self.count, \
self.byte_count = struct.unpack('>HHB', data[:5])
self.values = [] # reset
for idx in range(5, (self.count * 2) + 5, 2):
self.values.append(struct.unpack('>H', data[idx:idx + 2])[0])
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.count <= 0x07b):
return self.doException(merror.IllegalValue)
if (self.byte_count != self.count * 2):
return self.doException(merror.IllegalValue)
if not context.validate(self.function_code, self.address, self.count):
return self.doException(merror.IllegalAddress)
context.setValues(self.function_code, self.address, self.values)
return WriteMultipleRegistersResponse(self.address, self.count)
def __str__(self):
''' Returns a string representation of the instance
:returns: A string representation of the instance
'''
params = (self.address, self.count)
return "WriteMultipleRegisterRequest %d => %d" % params
class WriteMultipleRegistersResponse(ModbusResponse):
'''
"The normal response returns the function code, starting address, and
quantity of registers written.
'''
function_code = 16
_rtu_frame_size = 8
def __init__(self, address=None, count=None, **kwargs):
''' Initializes a new instance
:param address: The address to start writing to
:param count: The number of registers to write to
'''
ModbusResponse.__init__(self, **kwargs)
self.address = address
self.count = count
def encode(self):
''' Encode a write single register packet packet request
:returns: The encoded packet
'''
return struct.pack('>HH', self.address, self.count)
def decode(self, data):
''' Decode a write single register packet packet request
:param data: The request to decode
'''
self.address, self.count = struct.unpack('>HH', data)
def __str__(self):
''' Returns a string representation of the instance
:returns: A string representation of the instance
'''
params = (self.address, self.count)
return "WriteMultipleRegisterResponse (%d,%d)" % params
#---------------------------------------------------------------------------#
# Exported symbols
#---------------------------------------------------------------------------#
__all__ = [
"WriteSingleRegisterRequest", "WriteSingleRegisterResponse",
"WriteMultipleRegistersRequest", "WriteMultipleRegistersResponse",
]
|