Python Libraries pymodbus

Morning all

I sometimes struggle with importing Python Libraries, right now I need to do some weird ModbusTCP stuff, retrieving stored data tables, I know what to do but I can’t do it with the Modbus driver or Kepware etc.

The python library pyModbus looks just the job and claims it will work with any python version above 2.3 without any third party dependencies (I don’t need serial).

So the question is: has anyone got it to work?
I’m just trying the the basic example

client = ModbusTcpClient('') client.write_coil(1, True) result = client.read_coils(1,1) print result.bits[0] client.close()

The client is a working modbus device that will respond monitored with wireshark.
I keep getting the same error

[quote]Traceback (most recent call last):

File “”, line 3, in

File “C:\Users\Chris.ignition\cache\gw127.0.0.1_8088_443_main\C0\pylib\pymodbus\client\”, line 65, in write_coil

return self.execute(request)

File “C:\Users\Chris.ignition\cache\gw127.0.0.1_8088_443_main\C0\pylib\pymodbus\client\”, line 81, in execute

if not self.connect():

File “C:\Users\Chris.ignition\cache\gw127.0.0.1_8088_443_main\C0\pylib\pymodbus\client\”, line 143, in connect

self.socket = socket.create_connection((, self.port),

File “C:\Users\Chris.ignition\cache\gw127.0.0.1_8088_443_main\C0\pylib\pymodbus\client\”, line 143, in connect

self.socket = socket.create_connection((, self.port),

TypeError: create_connection() got an unexpected keyword argument ‘source_address’


now part of the ModbusTcpClient class looks like this:

[code]class ModbusTcpClient(BaseModbusClient):
‘’’ Implementation of a modbus tcp client

def __init__(self, host='', port=Defaults.Port,
    framer=ModbusSocketFramer, **kwargs):
    ''' Initialize a client instance

    :param host: The host to connect to (default
    :param port: The modbus port to connect to (default 502)
    :param source_address: The source address tuple to bind to (default ('', 0))
    :param framer: The modbus framer to use (default ModbusSocketFramer)

    .. note:: The host argument will accept ipv4 and ipv6 hosts
    ''' = host
    self.port = port
    self.source_address = kwargs.get('source_address', ('', 0))
    self.socket = None
    BaseModbusClient.__init__(self, framer(ClientDecoder()), **kwargs)

def connect(self):
    ''' Connect to the modbus tcp server

    :returns: True if connection succeeded, False otherwise
    if self.socket: return True
        address = (, self.port)
      self.socket = socket.create_connection((, self.port),
            timeout=Defaults.Timeout, source_address=self.source_address)
    except socket.error, msg:
        _logger.error('Connection to (%s, %s) failed: %s' % \
            (, self.port, msg))
    return self.socket != None[/code]

line 143 is

self.socket = socket.create_connection((, self.port), timeout=Defaults.Timeout, source_address=self.source_address)

I can’t see the error, its driving me nuts! I guess its back to school, again :smiley:

anyone feeling bored and want to tackle it, any pointers?

BTW, should I suggest a fourth topic on the 3rd Party Add-ons section of the forum for this type of post?

The error is basically tells you that there not are a “source_address” keyword argument for the function “create_connection”.

From here in the first line, there the following line: import socket which means that use the socket module of the python standard library.

From here, if you find the “create_connection” function descriptions, it’s has the following “signature”: socket.create_connection(address[, timeout[, source_address]]) but on the bottom says the next thing: [quote]Changed in version 2.7: source_address was added.[/quote]

So, maybe it’s not working with any version above 2.3 or so it seems.

Hope this helps.



Thanks, that’s very useful information, I might be able to get around that.

So deleting the keyword “source_address” for socket.create_connection solves the problem and makes it compatible, the basic read and writes now work

thank you very much.