/usr/share/pyshared/pymodbus/client/sync.py is in python-pymodbus 1.2.0-2.
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 364 365 366 367 368 | import socket
import serial
from pymodbus.constants import Defaults
from pymodbus.factory import ClientDecoder
from pymodbus.exceptions import NotImplementedException, ParameterException
from pymodbus.exceptions import ConnectionException
from pymodbus.transaction import FifoTransactionManager
from pymodbus.transaction import DictTransactionManager
from pymodbus.transaction import ModbusSocketFramer, ModbusBinaryFramer
from pymodbus.transaction import ModbusAsciiFramer, ModbusRtuFramer
from pymodbus.client.common import ModbusClientMixin
#---------------------------------------------------------------------------#
# Logging
#---------------------------------------------------------------------------#
import logging
_logger = logging.getLogger(__name__)
#---------------------------------------------------------------------------#
# The Synchronous Clients
#---------------------------------------------------------------------------#
class BaseModbusClient(ModbusClientMixin):
'''
Inteface for a modbus synchronous client. Defined here are all the
methods for performing the related request methods. Derived classes
simply need to implement the transport methods and set the correct
framer.
'''
def __init__(self, framer):
''' Initialize a client instance
:param framer: The modbus framer implementation to use
'''
self.framer = framer
if isinstance(self.framer, ModbusSocketFramer):
self.transaction = DictTransactionManager(self)
else: self.transaction = FifoTransactionManager(self)
#-----------------------------------------------------------------------#
# Client interface
#-----------------------------------------------------------------------#
def connect(self):
''' Connect to the modbus remote host
:returns: True if connection succeeded, False otherwise
'''
raise NotImplementedException("Method not implemented by derived class")
def close(self):
''' Closes the underlying socket connection
'''
pass
def _send(self, request):
''' Sends data on the underlying socket
:param request: The encoded request to send
:return: The number of bytes written
'''
raise NotImplementedException("Method not implemented by derived class")
def _recv(self, size):
''' Reads data from the underlying descriptor
:param size: The number of bytes to read
:return: The bytes read
'''
raise NotImplementedException("Method not implemented by derived class")
#-----------------------------------------------------------------------#
# Modbus client methods
#-----------------------------------------------------------------------#
def execute(self, request=None):
'''
:param request: The request to process
:returns: The result of the request execution
'''
if not self.connect():
raise ConnectionException("Failed to connect[%s]" % (self.__str__()))
return self.transaction.execute(request)
#-----------------------------------------------------------------------#
# The magic methods
#-----------------------------------------------------------------------#
def __enter__(self):
''' Implement the client with enter block
:returns: The current instance of the client
'''
if not self.connect():
raise ConnectionException("Failed to connect[%s]" % (self.__str__()))
return self
def __exit__(self, klass, value, traceback):
''' Implement the client with exit block '''
self.close()
def __str__(self):
''' Builds a string representation of the connection
:returns: The string representation
'''
return "Null Transport"
#---------------------------------------------------------------------------#
# Modbus TCP Client Transport Implementation
#---------------------------------------------------------------------------#
class ModbusTcpClient(BaseModbusClient):
''' Implementation of a modbus tcp client
'''
def __init__(self, host='127.0.0.1', port=Defaults.Port, framer=ModbusSocketFramer):
''' Initialize a client instance
:param host: The host to connect to (default 127.0.0.1)
:param port: The modbus port to connect to (default 502)
:param framer: The modbus framer to use (default ModbusSocketFramer)
.. note:: The host argument will accept ipv4 and ipv6 hosts
'''
self.host = host
self.port = port
self.socket = None
BaseModbusClient.__init__(self, framer(ClientDecoder()))
def connect(self):
''' Connect to the modbus tcp server
:returns: True if connection succeeded, False otherwise
'''
if self.socket: return True
try:
self.socket = socket.create_connection((self.host, self.port), Defaults.Timeout)
except socket.error, msg:
_logger.error('Connection to (%s, %s) failed: %s' % \
(self.host, self.port, msg))
self.close()
return self.socket != None
def close(self):
''' Closes the underlying socket connection
'''
if self.socket:
self.socket.close()
self.socket = None
def _send(self, request):
''' Sends data on the underlying socket
:param request: The encoded request to send
:return: The number of bytes written
'''
if not self.socket:
raise ConnectionException(self.__str__())
if request:
return self.socket.send(request)
return 0
def _recv(self, size):
''' Reads data from the underlying descriptor
:param size: The number of bytes to read
:return: The bytes read
'''
if not self.socket:
raise ConnectionException(self.__str__())
return self.socket.recv(size)
def __str__(self):
''' Builds a string representation of the connection
:returns: The string representation
'''
return "%s:%s" % (self.host, self.port)
#---------------------------------------------------------------------------#
# Modbus UDP Client Transport Implementation
#---------------------------------------------------------------------------#
class ModbusUdpClient(BaseModbusClient):
''' Implementation of a modbus udp client
'''
def __init__(self, host='127.0.0.1', port=Defaults.Port, framer=ModbusSocketFramer):
''' Initialize a client instance
:param host: The host to connect to (default 127.0.0.1)
:param port: The modbus port to connect to (default 502)
:param framer: The modbus framer to use (default ModbusSocketFramer)
'''
self.host = host
self.port = port
self.socket = None
BaseModbusClient.__init__(self, framer(ClientDecoder()))
@classmethod
def _get_address_family(cls, address):
''' A helper method to get the correct address family
for a given address.
:param address: The address to get the af for
:returns: AF_INET for ipv4 and AF_INET6 for ipv6
'''
try:
_ = socket.inet_pton(socket.AF_INET6, address)
except socket.error: # not a valid ipv6 address
return socket.AF_INET
return socket.AF_INET6
def connect(self):
''' Connect to the modbus tcp server
:returns: True if connection succeeded, False otherwise
'''
if self.socket: return True
try:
family = ModbusUdpClient._get_address_family(self.host)
self.socket = socket.socket(family, socket.SOCK_DGRAM)
except socket.error, ex:
_logger.error('Unable to create udp socket %s' % ex)
self.close()
return self.socket != None
def close(self):
''' Closes the underlying socket connection
'''
self.socket = None
def _send(self, request):
''' Sends data on the underlying socket
:param request: The encoded request to send
:return: The number of bytes written
'''
if not self.socket:
raise ConnectionException(self.__str__())
if request:
return self.socket.sendto(request, (self.host, self.port))
return 0
def _recv(self, size):
''' Reads data from the underlying descriptor
:param size: The number of bytes to read
:return: The bytes read
'''
if not self.socket:
raise ConnectionException(self.__str__())
return self.socket.recvfrom(size)[0]
def __str__(self):
''' Builds a string representation of the connection
:returns: The string representation
'''
return "%s:%s" % (self.host, self.port)
#---------------------------------------------------------------------------#
# Modbus Serial Client Transport Implementation
#---------------------------------------------------------------------------#
class ModbusSerialClient(BaseModbusClient):
''' Implementation of a modbus serial client
'''
def __init__(self, method='ascii', **kwargs):
''' Initialize a serial client instance
The methods to connect are::
- ascii
- rtu
- binary
:param method: The method to use for connection
:param port: The serial port to attach to
:param stopbits: The number of stop bits to use
:param bytesize: The bytesize of the serial messages
:param parity: Which kind of parity to use
:param baudrate: The baud rate to use for the serial device
:param timeout: The timeout between serial requests (default 3s)
'''
self.method = method
self.socket = None
BaseModbusClient.__init__(self, self.__implementation(method))
self.port = kwargs.get('port', 0)
self.stopbits = kwargs.get('stopbits', Defaults.Stopbits)
self.bytesize = kwargs.get('bytesize', Defaults.Bytesize)
self.parity = kwargs.get('parity', Defaults.Parity)
self.baudrate = kwargs.get('baudrate', Defaults.Baudrate)
self.timeout = kwargs.get('timeout', Defaults.Timeout)
@staticmethod
def __implementation(method):
''' Returns the requested framer
:method: The serial framer to instantiate
:returns: The requested serial framer
'''
method = method.lower()
if method == 'ascii': return ModbusAsciiFramer(ClientDecoder())
elif method == 'rtu': return ModbusRtuFramer(ClientDecoder())
elif method == 'binary': return ModbusBinaryFramer(ClientDecoder())
elif method == 'socket': return ModbusSocketFramer(ClientDecoder())
raise ParameterException("Invalid framer method requested")
def connect(self):
''' Connect to the modbus tcp server
:returns: True if connection succeeded, False otherwise
'''
if self.socket: return True
try:
self.socket = serial.Serial(port=self.port, timeout=self.timeout,
bytesize=self.bytesize, stopbits=self.stopbits,
baudrate=self.baudrate, parity=self.parity)
except serial.SerialException, msg:
_logger.error(msg)
self.close()
return self.socket != None
def close(self):
''' Closes the underlying socket connection
'''
if self.socket:
self.socket.close()
self.socket = None
def _send(self, request):
''' Sends data on the underlying socket
:param request: The encoded request to send
:return: The number of bytes written
'''
if not self.socket:
raise ConnectionException(self.__str__())
if request:
return self.socket.write(request)
return 0
def _recv(self, size):
''' Reads data from the underlying descriptor
:param size: The number of bytes to read
:return: The bytes read
'''
if not self.socket:
raise ConnectionException(self.__str__())
return self.socket.read(size)
def __str__(self):
''' Builds a string representation of the connection
:returns: The string representation
'''
return "%s baud[%s]" % (self.method, self.baudrate)
#---------------------------------------------------------------------------#
# Exported symbols
#---------------------------------------------------------------------------#
__all__ = [
"ModbusTcpClient", "ModbusUdpClient", "ModbusSerialClient"
]
|