View unanswered posts | View active topics It is currently Thu May 23, 2013 11:57 am



Reply to topic  [ 15 posts ] 
 Debug python 
Author Message

Joined: Aug 31, 2011
Posts: 56
Location: Toulouse - FRANCE
Post Debug python
Hello,

I have a plugin with a serial communication based on the Simple Serial Plugin.
This plugin reads and treats data continuously. Unfortunately, I have a bug occurring every 6 or 7 hours (the serial connection is closed, so I think this bug raise an exception) ... So it's not easy to debug :shock:

Is there a solution to know which line of code cause the exception ?


Wed Feb 29, 2012 2:38 pm
Profile
Site Admin
User avatar

Joined: Jan 27, 2003
Posts: 11692
Location: Texas
Post Re: Debug python
What does the Event Log show around the time it fails (copy/paste what you see)?

_________________
Image


Wed Feb 29, 2012 3:29 pm
Profile WWW

Joined: Aug 31, 2011
Posts: 56
Location: Toulouse - FRANCE
Post Re: Debug python
Hello Matt,

I have a main loop to read data :
Code: Select all
      try:
         self.plugin.debugLog("Starting concurrent serial communications.")
         while True:   
            while not commandQueue.empty():
               command = commandQueue.get()
               if command == "stopconcurrentSerialThread":
                  self.plugin.debugLog("Raising exception to stop concurrent thread.")
                  raise self.StopThread
            
            validatedMessages = self.readTeleinfoMessage(serialConnection)
            self.decodeResponse(devId, validatedMessages)

      except self.StopThread:
         self.plugin.debugLog("closing connection to device ")
         pass
      except Exception, e:
         self.plugin.exceptionLog()
      except:
         self.plugin.exceptionLog()
      finally:
         self.plugin.debugLog("closed connection to device")
         serialConnection.close()
         pass

The log only shows "closed connection to device", so I think this means an exception has been raised, and it gone through the finally statement to close the serial connection.

By adding some checkpoints in my code - pluginLog("Checkpoint 1") - I identified where is the bug. It's in the readTeleinfoMessage() method, I have a loop waiting for a specific character :
Code: Select all
while '\x02' not in trame:
   trame = serialConnection.readline()

Sometimes - as said every 5 or 6 hours - this code raise an exception. I don't understand why ... I tried to add a sleep command in the loop, but the result is identical.


Fri Mar 02, 2012 12:24 am
Profile
Site Admin
User avatar

Joined: Jan 27, 2003
Posts: 11692
Location: Texas
Post Re: Debug python
It is strange that the only thing logged is "closed connection to device." If an exception was thrown then one of those to except handler cases should be catching it and then logging the exception details, so I'm not sure how it is getting past that logic (and not logging any additional details).

The pyserial source suggests setting a timeout for the serial connection when using readline().

The serial connection could also be getting dropped, especially if you have it plugged into a USB hub or your Mac goes to sleep. Making serial communication robust can be a bit tricky. You might want to glance through the plugin.py and easydaq.py source files in the EasyDAQ USB Relay Cards plugin. Whenever it detects anything that might be a serial communication failure/hiccup:

Code: Select all
      except serial.SerialException, exc:
         self.plugin.handleCommException(dev, exc)      # enters silent retry mode
      except termios.error, exc:
         self.plugin.handleCommException(dev, exc)      # enters silent retry mode
      except OSError, exc:
         self.plugin.handleCommException(dev, exc)      # enters silent retry mode

I have it start an automatic serial reconnect mechanism that tries to reestablish the serial communication every few seconds. The result is you can physically disconnect the hardware from the Mac and then when it is reconnected the plugin automatically gets everything up and working again.

_________________
Image


Sat Mar 03, 2012 12:42 pm
Profile WWW

Joined: Aug 31, 2011
Posts: 56
Location: Toulouse - FRANCE
Post Re: Debug python
I'm really annoyed, I tried a lot of things (and it's very long because I have to wait several hours to see the result), and the bug is not solved ...

As indicated Matt, I added some code in the main loop to catch the exception and start a reconnect method but it seems to go directly into the finally statement as I have nothing in the log except "closed connection to device" :
Code: Select all
      except self.StopThread:
         pass
      except serial.SerialException, exc:
         #self.plugin.handleCommException(dev, exc)      # enters silent retry mode
         self.plugin.debugLog("Exception : " + str(exc))
         pass
      except termios.error, exc:
         #self.plugin.handleCommException(dev, exc)      # enters silent retry mode
         self.plugin.debugLog("Exception : " + str(exc))
         pass
      except OSError, exc:
         #self.plugin.handleCommException(dev, exc)      # enters silent retry mode
         self.plugin.debugLog("Exception : " + str(exc))
         pass
      except Exception, exc:
         self.plugin.exceptionLog()                  # shouldn't happen; bail out
      except:
         self.plugin.exceptionLog()                  # shouldn't happen; bail out
      finally:
         self.plugin.debugLog("closed connection to device")
         serialConnection.close()


I added a timeout to work with readline(), not better.
I tried to replace readline() by something like : while serialConnection.read(1) == '\r\n', not better.

I have no more idea at this time ...


Mon Mar 12, 2012 11:45 pm
Profile
Site Admin
User avatar

Joined: Jan 27, 2003
Posts: 11692
Location: Texas
Post Re: Debug python
I wonder if:

Code: Select all
self.plugin.exceptionLog()

is failing for some reason. Try adding some more debug logging to the last 2 exception handlers to see if it is getting into one of them and failing on exceptionLog():

Code: Select all
      except Exception, exc:
         self.plugin.debugLog("inside1A")
         self.plugin.exceptionLog()                  # shouldn't happen; bail out
         self.plugin.debugLog("inside1B")
      except:
         self.plugin.debugLog("inside2A")
         self.plugin.exceptionLog()                  # shouldn't happen; bail out
         self.plugin.debugLog("inside2B")

_________________
Image


Tue Mar 13, 2012 2:35 pm
Profile WWW

Joined: Aug 31, 2011
Posts: 56
Location: Toulouse - FRANCE
Post Re: Debug python
Hey,
One good and one bad news ...

The good one : I know what is the cause of my problem - my device is in the garage, and my Mac Mini with Indigo is in the living room. Between them, I use an USB to RJ45 extender like this. This extender causes the device connection closing. Unfortunately, I can't move the Mac Mini to the garage ...

The bad news : I modified the code as you said Matt, but I have always in the log "closed connection to device" ... So it seems it goes directly to the finally statement ... I don't know how I can implement your reconnect mechanism.


Fri Mar 16, 2012 10:10 am
Profile
Site Admin
User avatar

Joined: Jan 27, 2003
Posts: 11692
Location: Texas
Post Re: Debug python
Try adding the:

Code: Select all
self.plugin.debugLog("inside _ SOME UNIQUE ID")

As the first line after all of the except: clauses (not just the 2 I mentioned above). It is possible that it is hitting one of the other ones and the debugLog() is failing because the case of str(exc) is failing first. So let's log just a simple line first to try to see which one. Once we know what type of exception is being thrown, we can dig further into what the exception handler is failing.

_________________
Image


Fri Mar 16, 2012 10:15 am
Profile WWW

Joined: Aug 31, 2011
Posts: 56
Location: Toulouse - FRANCE
Post Re: Debug python
Same result :evil:


Fri Mar 16, 2012 12:55 pm
Profile
User avatar

Joined: Nov 18, 2008
Posts: 1721
Location: Berkeley, CA
Post Re: Debug python
Juju wrote:... I know what is the cause of my problem - my device is in the garage, and my Mac Mini with Indigo is in the living room. Between them, I use an USB to RJ45 extender... ...This extender causes the device connection closing. Unfortunately, I can't move the Mac Mini to the garage...

I have had similar issues with USB over Cat-5 extenders and solved the problem by installing a USB hub between the remote device and the far end USB over Cat-5 driver.


Fri Mar 16, 2012 1:53 pm
Profile

Joined: Aug 31, 2011
Posts: 56
Location: Toulouse - FRANCE
Post Re: Debug python
Not better.
Is it a powered USB hub ? Perhaps this problem is due to a lack of power ? I will try to find a power adaptor for the hub.


Sat Mar 17, 2012 12:28 am
Profile
User avatar

Joined: Nov 18, 2008
Posts: 1721
Location: Berkeley, CA
Post Re: Debug python
Juju wrote:Not better.
Is it a powered USB hub ?
Sorry, I should have said so. Yes it needs to be powered.


Sat Mar 17, 2012 12:50 am
Profile
User avatar

Joined: Nov 18, 2008
Posts: 1721
Location: Berkeley, CA
Post Re: Debug python
Juju... You might try looking in the Console log to see if any of your plugin errors are being logged there. For some reason, it looks like messages that Indigo does not see are still recorded in the Console log.


Mon Mar 19, 2012 1:28 pm
Profile

Joined: Aug 31, 2011
Posts: 56
Location: Toulouse - FRANCE
Post Re: Debug python
I will have a look in the Console log.
Tried a powered USB hub, same result. tried another one, same result ...
As it is now clear the problem come from the extender, I'm looking at other extender types. I think I will try with this type of device : link.

Something strange : on the USB hub, I have connected my power meter (FTDI chip) and a USB/serial ports adaptor with 4 serial ports (FTDI chip too). There is no exception raised for the power meter connected directly with USB, there is a termios exception raised for a (virtual !) easydaq card connected on a serial port from the adaptor, and an exception (except Exception, exc:) for a PLCBUS (virtual too ! - another plugin I'm working on) device connected on a serial port from the adaptor ...


Tue Mar 20, 2012 12:56 am
Profile

Joined: Aug 31, 2011
Posts: 56
Location: Toulouse - FRANCE
Post Re: Debug python
Some news : works fine with this USB over Cat-5 extender ! So my problem was caused by a bad device ...

I took time to implement the automatic reconnecting process from the easydaq plugin. It is based on the detection of an exception during the main loop. Unfortunately I haven't a better result concerning this topic.
For all my others devices connected through a serial port to an USB/serial adapter, an exception is raised, but not for this one connected directly with USB...


Tue Apr 10, 2012 10:07 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 15 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.   Template designed by STSoftware.