client.sync — Twisted Synchronous Modbus Client

API Documentation

class pymodbus.client.sync.BaseModbusClient(framer, **kwargs)

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.

__enter__()

Implement the client with enter block

Returns:The current instance of the client
__exit__(klass, value, traceback)

Implement the client with exit block

__init__(framer, **kwargs)

Initialize a client instance

Parameters:framer – The modbus framer implementation to use
__str__()

Builds a string representation of the connection

Returns:The string representation
_recv(size)

Reads data from the underlying descriptor

Parameters:size – The number of bytes to read
Returns:The bytes read
_send(request)

Sends data on the underlying socket

Parameters:request – The encoded request to send
Returns:The number of bytes written
close()

Closes the underlying socket connection

connect()

Connect to the modbus remote host

Returns:True if connection succeeded, False otherwise
execute(request=None)
Parameters:request – The request to process
Returns:The result of the request execution
class pymodbus.client.sync.ModbusTcpClient(host='127.0.0.1', port=502, framer=<class 'pymodbus.transaction.ModbusSocketFramer'>, **kwargs)

Implementation of a modbus tcp client

__init__(host='127.0.0.1', port=502, framer=<class 'pymodbus.transaction.ModbusSocketFramer'>, **kwargs)

Initialize a client instance

Parameters:
  • host – The host to connect to (default 127.0.0.1)
  • port – The modbus port to connect to (default 502)
  • source_address – The source address tuple to bind to (default (‘’, 0))
  • timeout – The timeout to use for this socket (default Defaults.Timeout)
  • framer – The modbus framer to use (default ModbusSocketFramer)

Note

The host argument will accept ipv4 and ipv6 hosts

__str__()

Builds a string representation of the connection

Returns:The string representation
_recv(size)

Reads data from the underlying descriptor

Parameters:size – The number of bytes to read
Returns:The bytes read
_send(request)

Sends data on the underlying socket

Parameters:request – The encoded request to send
Returns:The number of bytes written
close()

Closes the underlying socket connection

connect()

Connect to the modbus tcp server

Returns:True if connection succeeded, False otherwise
class pymodbus.client.sync.ModbusUdpClient(host='127.0.0.1', port=502, framer=<class 'pymodbus.transaction.ModbusSocketFramer'>, **kwargs)

Implementation of a modbus udp client

__init__(host='127.0.0.1', port=502, framer=<class 'pymodbus.transaction.ModbusSocketFramer'>, **kwargs)

Initialize a client instance

Parameters:
  • host – The host to connect to (default 127.0.0.1)
  • port – The modbus port to connect to (default 502)
  • framer – The modbus framer to use (default ModbusSocketFramer)
  • timeout – The timeout to use for this socket (default None)
__str__()

Builds a string representation of the connection

Returns:The string representation
classmethod _get_address_family(address)

A helper method to get the correct address family for a given address.

Parameters:address – The address to get the af for
Returns:AF_INET for ipv4 and AF_INET6 for ipv6
_recv(size)

Reads data from the underlying descriptor

Parameters:size – The number of bytes to read
Returns:The bytes read
_send(request)

Sends data on the underlying socket

Parameters:request – The encoded request to send
Returns:The number of bytes written
close()

Closes the underlying socket connection

connect()

Connect to the modbus tcp server

Returns:True if connection succeeded, False otherwise
class pymodbus.client.sync.ModbusSerialClient(method='ascii', **kwargs)

Implementation of a modbus serial client

static _ModbusSerialClient__implementation(method)

Returns the requested framer

Method:The serial framer to instantiate
Returns:The requested serial framer
__init__(method='ascii', **kwargs)

Initialize a serial client instance

The methods to connect are:

- ascii
- rtu
- binary
Parameters:
  • method – The method to use for connection
  • port – The serial port to attach to
  • stopbits – The number of stop bits to use
  • bytesize – The bytesize of the serial messages
  • parity – Which kind of parity to use
  • baudrate – The baud rate to use for the serial device
  • timeout – The timeout between serial requests (default 3s)
__str__()

Builds a string representation of the connection

Returns:The string representation
_recv(size)

Reads data from the underlying descriptor

Parameters:size – The number of bytes to read
Returns:The bytes read
_send(request)

Sends data on the underlying socket

If receive buffer still holds some data then flush it.

Sleep if last send finished less than 3.5 character times ago.

Parameters:request – The encoded request to send
Returns:The number of bytes written
close()

Closes the underlying socket connection

connect()

Connect to the modbus serial server

Returns:True if connection succeeded, False otherwise