Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1. ###########################################################
  2. #
  3. # Serial interface, connects with can bus over a serial
  4. # line
  5. #
  6. ###########################################################
  7. import logging as log
  8. from NodeIfBase import NodeIfBase
  9. from threading import Thread
  10. import serial
  11. import sys
  12. import time
  13.  
  14. UART_START_BYTE = 253
  15. UART_END_BYTE = 250
  16. PACKET_LENGTH = 17
  17. readBufferSize = 8192
  18. UART_CONN_BYTE = 252
  19.  
  20. class SerialThread(Thread):
  21.    
  22.     terminated = False
  23.     port = None
  24.     pktHandler = None
  25.    
  26.     def __init__ (self, pktHandler, port, exceptHook):
  27.         Thread.__init__(self)
  28.         self.pktHandler = pktHandler
  29.         self.port = port
  30.         sys.excepthook = exceptHook
  31.  
  32.     def run(self):
  33.         log.debug('SerialThread.run')
  34.         readBuffer = []
  35.         while not self.terminated:
  36.            
  37.             inByte = self.port.read()
  38.             if inByte == '\n':
  39.                 msg = ''.join(readBuffer)
  40.                 readBuffer = []
  41.                 #print 'Incoming:', msg
  42.                 self.pktHandler.input(msg)
  43.             else:
  44.                 readBuffer.append(inByte)
  45.  
  46.             time.sleep(0.1)
  47.         self.port.close()
  48.    
  49.     def terminate(self):
  50.         log.debug('SerialThread.terminate')
  51.         self.terminated = True
  52.        
  53.  
  54. class NodeIfSerial(NodeIfBase):
  55.    
  56.     DEFAULT_CONFIG = {'serialPort':1, #com1
  57.                       'baudRate':9600,
  58.                       'byteSize':8,
  59.                       'parity': 'N',
  60.                       'stopBits':1,
  61.                       'timeOut': 0, # must be 0 (non-blocking mode)
  62.                       'softFlowCtl': 0,
  63.                       'hardFlowCtl': 0
  64.                       }
  65.    
  66.     serialThread = None
  67.     pktHandler = None
  68.     exceptHook = None
  69.    
  70.     def __init__ (self, pktHandler, exceptHook, cfg = None):
  71.         if cfg is None:
  72.             self.config = self.DEFAULT_CONFIG
  73.        
  74.         self.pktHandler = pktHandler
  75.         self.exceptHook = exceptHook
  76.         NodeIfBase.__init__(self, cfg, pktHandler)
  77.    
  78.     def start(self):
  79.        
  80.         try:
  81.             port = serial.Serial(self.config['serialPort'],
  82.                                  self.config['baudRate'],
  83.                                  self.config['byteSize'],
  84.                                  self.config['parity'],
  85.                                  self.config['stopBits'],
  86.                                  self.config['timeOut'],
  87.                                  self.config['softFlowCtl'],
  88.                                  self.config['hardFlowCtl'])
  89.         except (serial.SerialException), Error:
  90.             print Error
  91.             return False
  92.        
  93.         self.serialThread = SerialThread(self.pktHandler, port, self.exceptHook)
  94.         self.serialThread.start()
  95.         return True
  96.  
  97.     def stop(self):
  98.         if self.serialThread is not None:
  99.             self.serialThread.terminate()
  100.         else:
  101.             log.debug('Serial interface not started, or already stopped')
  102.