Advertisement
Guest User

Reconnect Serialport

a guest
Aug 1st, 2011
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. class JNWSNProtocol(basic.LineReceiver):
  2. .
  3. .
  4. .
  5.     def connectionMade(self):
  6.         log.msg("Connected...")
  7.        
  8.     def connectionLost(self, reason):
  9.         log.msg("Lost connection (%s)" % reason)
  10.         log.msg("Reconnecting in 5 seconds...")
  11.         self.retry = self.wrapper.reactor.callLater(5, self.reconnect)
  12.  
  13.     def reconnect(self):
  14.         try:
  15.             SerialPort(self, self.wrapper.port, self.wrapper.reactor)
  16.             log.msg("RECONNECTED")
  17.            
  18.         except:
  19.             log.msg("Error opening serial port %s (%s)" % (self.wrapper.port, sys.exc_info()[1]))
  20.             log.msg("Reconnecting in 5 seconds...")
  21.             self.retry = self.wrapper.reactor.callLater(5, self.reconnect)
  22. .
  23. .
  24. .
  25.  
  26. class JNWSNWrapper():
  27. .
  28. .
  29. .
  30.        
  31.     def start(self):
  32.         '''
  33.        Function that starts the JeeNode WSN plug-in. It handles the creation
  34.        of the plugin connection and connects to the specified serial port.
  35.        '''
  36.        
  37.         try:
  38.             self.myserial = SerialPort (self.protocol, self.port, reactor)
  39.             self.myserial.setBaudRate(57600)      
  40.             self.reactor = reactor
  41.  
  42.             reactor.run(installSignalHandlers=0)
  43.            
  44.             log.msg("The main loop has terminated")
  45.                        
  46.             return(True)
  47.        
  48.         except:
  49.             log.msg("Error opening serial port %s (%s)" % (self.port, sys.exc_info()[1]))
  50.            
  51.             return(False)
  52.  
  53.  
  54.            
  55. if __name__ == '__main__':
  56.     jnwsn = JNWSNWrapper()
  57.     jnwsn.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement