# Python Libraries pymodbus

**URL:** <https://forum.inductiveautomation.com/t/python-libraries-pymodbus/10473>\
**Category:** Ignition\
**Created:** [September 16, 2015, 9:19am UTC](https://forum.inductiveautomation.com/t/python-libraries-pymodbus/10473 "2015-09-16T09:19:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Taylor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chris_taylor/32/3315_2.png) [@Chris\_Taylor](https://forum.inductiveautomation.com/u/Chris_Taylor)\
**Post date:** [September 16, 2015, 9:19am UTC](https://forum.inductiveautomation.com/t/python-libraries-pymodbus/10473/1 "2015-09-16T09:19:23Z")

</div>

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[https://github.com/bashwork/pymodbus](https://github.com/bashwork/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('192.168.2.21')
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\common.py”, 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\sync.py”, 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\sync.py”, line 143, in connect

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

```

File “C:\Users\Chris.ignition\cache\gw127.0.0.1\_8088\_443\_main\C0\pylib\pymodbus\client\sync.py”, line 143, in connect

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

```

TypeError: create\_connection() got an unexpected keyword argument ‘source\_address’

[/quote]

now part of the ModbusTcpClient class looks like this:

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

```
def __init__ (self, host='127.0.0.1', port=Defaults.Port,
    framer=ModbusSocketFramer, **kwargs):
    ''' 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 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
    '''
    self.host = 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
    try:
        address = (self.host, self.port)
      self.socket = socket.create_connection((self.host, self.port),
            timeout=Defaults.Timeout, source_address=self.source_address)
    except socket.error, msg:
        _logger.error('Connection to (%s, %s) failed: %s' % \
            (self.host, self.port, msg))
        self.close()
    return self.socket != None[/code]

```

line 143 is

`self.socket = socket.create_connection((self.host, 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 😃

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?

---

<div class="post-metadata">

**Author:** ![pdibenedetto](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pdibenedetto/32/6364_2.png) [@pdibenedetto](https://forum.inductiveautomation.com/u/pdibenedetto)\
**Post date:** [September 16, 2015, 3:02pm UTC](https://forum.inductiveautomation.com/t/python-libraries-pymodbus/10473/2 "2015-09-16T15:02:25Z")

</div>

The error is basically tells you that there not are a “source\_address” keyword argument for the function “create\_connection”.

From [here](https://github.com/bashwork/pymodbus/blob/master/pymodbus/client/sync.py) in the first line, there the following line: `import socket` which means that use the socket module of the python standard library.

From [here](https://docs.python.org/2/library/socket.html), 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.

Regards,

---

<div class="post-metadata">

**Author:** ![Chris\_Taylor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chris_taylor/32/3315_2.png) [@Chris\_Taylor](https://forum.inductiveautomation.com/u/Chris_Taylor)\
**Post date:** [September 16, 2015, 3:20pm UTC](https://forum.inductiveautomation.com/t/python-libraries-pymodbus/10473/3 "2015-09-16T15:20:43Z")

</div>

pdibenedetto

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

---

<div class="post-metadata">

**Author:** ![Chris\_Taylor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chris_taylor/32/3315_2.png) [@Chris\_Taylor](https://forum.inductiveautomation.com/u/Chris_Taylor)\
**Post date:** [September 16, 2015, 4:38pm UTC](https://forum.inductiveautomation.com/t/python-libraries-pymodbus/10473/4 "2015-09-16T16:38:59Z")

</div>

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.
