/usr/lib/python2.7/dist-packages/pymodbus/payload.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 360 361 362 363 | '''
Modbus Payload Builders
------------------------
A collection of utilities for building and decoding
modbus messages payloads.
'''
from struct import pack, unpack
from pymodbus.interfaces import IPayloadBuilder
from pymodbus.constants import Endian
from pymodbus.utilities import pack_bitstring
from pymodbus.utilities import unpack_bitstring
from pymodbus.utilities import make_byte_string
from pymodbus.exceptions import ParameterException
class BinaryPayloadBuilder(IPayloadBuilder):
'''
A utility that helps build payload messages to be
written with the various modbus messages. It really is just
a simple wrapper around the struct module, however it saves
time looking up the format strings. What follows is a simple
example::
builder = BinaryPayloadBuilder(endian=Endian.Little)
builder.add_8bit_uint(1)
builder.add_16bit_uint(2)
payload = builder.build()
'''
def __init__(self, payload=None, endian=Endian.Little):
''' Initialize a new instance of the payload builder
:param payload: Raw binary payload data to initialize with
:param endian: The endianess of the payload
'''
self._payload = payload or []
self._endian = endian
def to_string(self):
''' Return the payload buffer as a string
:returns: The payload buffer as a string
'''
return b''.join(self._payload)
def __str__(self):
''' Return the payload buffer as a string
:returns: The payload buffer as a string
'''
return self.to_string().decode('utf-8')
def reset(self):
''' Reset the payload buffer
'''
self._payload = []
def to_registers(self):
''' Convert the payload buffer into a register
layout that can be used as a context block.
:returns: The register layout to use as a block
'''
fstring = self._endian + 'H'
payload = self.build()
return [unpack(fstring, value)[0] for value in payload]
def build(self):
''' Return the payload buffer as a list
This list is two bytes per element and can
thus be treated as a list of registers.
:returns: The payload buffer as a list
'''
string = self.to_string()
length = len(string)
string = string + (b'\x00' * (length % 2))
return [string[i:i+2] for i in range(0, length, 2)]
def add_bits(self, values):
''' Adds a collection of bits to be encoded
If these are less than a multiple of eight,
they will be left padded with 0 bits to make
it so.
:param value: The value to add to the buffer
'''
value = pack_bitstring(values)
self._payload.append(value)
def add_8bit_uint(self, value):
''' Adds a 8 bit unsigned int to the buffer
:param value: The value to add to the buffer
'''
fstring = self._endian + 'B'
self._payload.append(pack(fstring, value))
def add_16bit_uint(self, value):
''' Adds a 16 bit unsigned int to the buffer
:param value: The value to add to the buffer
'''
fstring = self._endian + 'H'
self._payload.append(pack(fstring, value))
def add_32bit_uint(self, value):
''' Adds a 32 bit unsigned int to the buffer
:param value: The value to add to the buffer
'''
fstring = self._endian + 'I'
self._payload.append(pack(fstring, value))
def add_64bit_uint(self, value):
''' Adds a 64 bit unsigned int to the buffer
:param value: The value to add to the buffer
'''
fstring = self._endian + 'Q'
self._payload.append(pack(fstring, value))
def add_8bit_int(self, value):
''' Adds a 8 bit signed int to the buffer
:param value: The value to add to the buffer
'''
fstring = self._endian + 'b'
self._payload.append(pack(fstring, value))
def add_16bit_int(self, value):
''' Adds a 16 bit signed int to the buffer
:param value: The value to add to the buffer
'''
fstring = self._endian + 'h'
self._payload.append(pack(fstring, value))
def add_32bit_int(self, value):
''' Adds a 32 bit signed int to the buffer
:param value: The value to add to the buffer
'''
fstring = self._endian + 'i'
self._payload.append(pack(fstring, value))
def add_64bit_int(self, value):
''' Adds a 64 bit signed int to the buffer
:param value: The value to add to the buffer
'''
fstring = self._endian + 'q'
self._payload.append(pack(fstring, value))
def add_32bit_float(self, value):
''' Adds a 32 bit float to the buffer
:param value: The value to add to the buffer
'''
fstring = self._endian + 'f'
self._payload.append(pack(fstring, value))
def add_64bit_float(self, value):
''' Adds a 64 bit float(double) to the buffer
:param value: The value to add to the buffer
'''
fstring = self._endian + 'd'
self._payload.append(pack(fstring, value))
def add_string(self, value):
''' Adds a string to the buffer
:param value: The value to add to the buffer
'''
value = make_byte_string(value)
fstring = self._endian + str(len(value)) + 's'
self._payload.append(pack(fstring, value))
class BinaryPayloadDecoder(object):
'''
A utility that helps decode payload messages from a modbus
reponse message. It really is just a simple wrapper around
the struct module, however it saves time looking up the format
strings. What follows is a simple example::
decoder = BinaryPayloadDecoder(payload)
first = decoder.decode_8bit_uint()
second = decoder.decode_16bit_uint()
'''
def __init__(self, payload, endian=Endian.Little):
''' Initialize a new payload decoder
:param payload: The payload to decode with
:param endian: The endianess of the payload
'''
self._payload = payload
self._pointer = 0x00
self._endian = endian
@classmethod
def fromRegisters(klass, registers, endian=Endian.Little):
''' Initialize a payload decoder with the result of
reading a collection of registers from a modbus device.
The registers are treated as a list of 2 byte values.
We have to do this because of how the data has already
been decoded by the rest of the library.
:param registers: The register results to initialize with
:param endian: The endianess of the payload
:returns: An initialized PayloadDecoder
'''
if isinstance(registers, list): # repack into flat binary
payload = b''.join(pack(endian + 'H', x) for x in registers)
return klass(payload, endian)
raise ParameterException('Invalid collection of registers supplied')
@classmethod
def fromCoils(klass, coils, endian=Endian.Little):
''' Initialize a payload decoder with the result of
reading a collection of coils from a modbus device.
The coils are treated as a list of bit(boolean) values.
:param coils: The coil results to initialize with
:param endian: The endianess of the payload
:returns: An initialized PayloadDecoder
'''
if isinstance(coils, list):
payload = pack_bitstring(coils)
return klass(payload, endian)
raise ParameterException('Invalid collection of coils supplied')
def reset(self):
''' Reset the decoder pointer back to the start
'''
self._pointer = 0x00
def decode_8bit_uint(self):
''' Decodes a 8 bit unsigned int from the buffer
'''
self._pointer += 1
fstring = self._endian + 'B'
handle = self._payload[self._pointer - 1:self._pointer]
handle = make_byte_string(handle)
return unpack(fstring, handle)[0]
def decode_bits(self):
''' Decodes a byte worth of bits from the buffer
'''
self._pointer += 1
# fstring = self._endian + 'B'
handle = self._payload[self._pointer - 1:self._pointer]
handle = make_byte_string(handle)
return unpack_bitstring(handle)
def decode_16bit_uint(self):
''' Decodes a 16 bit unsigned int from the buffer
'''
self._pointer += 2
fstring = self._endian + 'H'
handle = self._payload[self._pointer - 2:self._pointer]
handle = make_byte_string(handle)
return unpack(fstring, handle)[0]
def decode_32bit_uint(self):
''' Decodes a 32 bit unsigned int from the buffer
'''
self._pointer += 4
fstring = self._endian + 'I'
handle = self._payload[self._pointer - 4:self._pointer]
handle = make_byte_string(handle)
return unpack(fstring, handle)[0]
def decode_64bit_uint(self):
''' Decodes a 64 bit unsigned int from the buffer
'''
self._pointer += 8
fstring = self._endian + 'Q'
handle = self._payload[self._pointer - 8:self._pointer]
handle = make_byte_string(handle)
return unpack(fstring, handle)[0]
def decode_8bit_int(self):
''' Decodes a 8 bit signed int from the buffer
'''
self._pointer += 1
fstring = self._endian + 'b'
handle = self._payload[self._pointer - 1:self._pointer]
handle = make_byte_string(handle)
return unpack(fstring, handle)[0]
def decode_16bit_int(self):
''' Decodes a 16 bit signed int from the buffer
'''
self._pointer += 2
fstring = self._endian + 'h'
handle = self._payload[self._pointer - 2:self._pointer]
handle = make_byte_string(handle)
return unpack(fstring, handle)[0]
def decode_32bit_int(self):
''' Decodes a 32 bit signed int from the buffer
'''
self._pointer += 4
fstring = self._endian + 'i'
handle = self._payload[self._pointer - 4:self._pointer]
handle = make_byte_string(handle)
return unpack(fstring, handle)[0]
def decode_64bit_int(self):
''' Decodes a 64 bit signed int from the buffer
'''
self._pointer += 8
fstring = self._endian + 'q'
handle = self._payload[self._pointer - 8:self._pointer]
handle = make_byte_string(handle)
return unpack(fstring, handle)[0]
def decode_32bit_float(self):
''' Decodes a 32 bit float from the buffer
'''
self._pointer += 4
fstring = self._endian + 'f'
handle = self._payload[self._pointer - 4:self._pointer]
handle = make_byte_string(handle)
return unpack(fstring, handle)[0]
def decode_64bit_float(self):
''' Decodes a 64 bit float(double) from the buffer
'''
self._pointer += 8
fstring = self._endian + 'd'
handle = self._payload[self._pointer - 8:self._pointer]
handle = make_byte_string(handle)
return unpack(fstring, handle)[0]
def decode_string(self, size=1):
''' Decodes a string from the buffer
:param size: The size of the string to decode
'''
self._pointer += size
return self._payload[self._pointer - size:self._pointer]
def skip_bytes(self, nbytes):
''' Skip n bytes in the buffer
:param nbytes: The number of bytes to skip
'''
self._pointer += nbytes
return None
#---------------------------------------------------------------------------#
# Exported Identifiers
#---------------------------------------------------------------------------#
__all__ = ["BinaryPayloadBuilder", "BinaryPayloadDecoder"]
|