Skip to content

Timeout for ModbusUdpClient #51

@ghost

Description

If the slave stops, the UDP client realized with pymodbus hangs (on OS X). There seems to be no way to specify a timeout on the underlying socket as for the ModbusSerialClient. The following would allow to use a timeout argument using the newly passed down kwargs.

class ModbusUdpClient(BaseModbusClient):
    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 framer: The modbus framer to use (default ModbusSocketFramer)
        '''
        self.host = host
        self.port = port
        self.socket = None
        self.timeout  = kwargs.get('timeout',  Defaults.Timeout)
        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:
            family = ModbusUdpClient._get_address_family(self.host)
            self.socket = socket.socket(family, socket.SOCK_DGRAM)
            self.socket.settimeout(self.timeout)
        except socket.error, ex:
            _logger.error('Unable to create udp socket %s' % ex)
            self.close()
        return self.socket != None

An update on the python3 build would be nice too.

Thanks,
Marko

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions