hafidz1977 / pymodbus

Automatically exported from code.google.com/p/pymodbus
0 stars 0 forks source link

connect return True but don't work #65

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Please look at the function on sync client:

    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)
            self.transaction = ModbusTransactionManager(self)
        except socket.error, msg:
            _logger.error('Connection to (%s, %s) failed: %s' % \
                (self.host, self.port, msg))
            self.close()
        return self.socket != None

Sometime "self.socket != None" return True but I cannot make any read on socket.
On my ubuntu 12.04 I have a python daemon that read some registers from a 
remote I/O device (moxa, ndr). About one or two time per week I lose my 
connection and when I try to reconnect the function return True but I be unable 
to read.
I have to restart the daemon or reboot the client.
During this problem tcpdump say me that the connection is on TIME_WAIT state.

Do someone help me?

Thanks, Nicola.

Original issue reported on code.google.com by nicola.q...@gmail.com on 1 Dec 2012 at 10:39

GoogleCodeExporter commented 9 years ago
When you reconnect, do you close the socket first?

client.close()
client.connect()

Original comment by Bashw...@gmail.com on 2 Dec 2012 at 12:03

GoogleCodeExporter commented 9 years ago
When you reconnect, do you close the socket first?
Yes.

I also tried to recreate the class:

client.close()
client = ModbusClient(self.host)
client.connect()

Could be that socket.connect returns an invalid value and other than none?
Or "self.socket" is True even before my reconnection?

Nicola.

Original comment by nicola.q...@gmail.com on 2 Dec 2012 at 6:26

GoogleCodeExporter commented 9 years ago
Any chance you could add some logging and see what causes this?  I will make 
sure to set the socket to None in the exception handler and try and remove any 
edge cases, but I would also like to know how you are getting into that 
situation.

Original comment by Bashw...@gmail.com on 17 Dec 2012 at 3:13

GoogleCodeExporter commented 9 years ago
Now I set the "standard" logger to warning. Before I used only my personal 
logger so the logger of your library was unable to operate.

I found a similar problem after upgrading to Ubuntu 12.04 with an old python 
program that uses the library modlib.py of Hynek Petrak.
Probably the problem occurred with the transition from python 2.6 to python 2.7.

I tried to add shutdown(socket.SHUT_RDWR) before close().
Last night I lost the connection at 02:56:58 and resumed at 03:12:41.
Before adding the shutdown() the connection never restored.

Now I waiting for the next few days to see the new logs.

Nicola.

Original comment by nicola.q...@gmail.com on 18 Dec 2012 at 9:55

GoogleCodeExporter commented 9 years ago
I have set log level to warning:
log.setLevel(logging.WARNING)

I lost again the connection for 4 days. None on log. Now I tried to increase 
log verbosity to INFO. :(

Nicola.

Original comment by nicola.q...@gmail.com on 9 Jan 2013 at 11:18

GoogleCodeExporter commented 9 years ago
You might want to use DEBUG instead of INFO as that will capture everything. 

Original comment by Bashw...@gmail.com on 13 Jan 2013 at 7:37

GoogleCodeExporter commented 9 years ago
Nicola, where you able to find any causes of your error or anything repeatable 
that I can work with? I am still glad to add more logging code to the client to 
help find a solution.

Original comment by Bashw...@gmail.com on 19 Jun 2013 at 1:46

GoogleCodeExporter commented 9 years ago

Original comment by Bashw...@gmail.com on 15 Jul 2013 at 5:10