Subversion Repositories HomeAutomation

Rev

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

Rev Author Line No. Line
736 olof 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
class SerialThread(Thread):
15
 
16
    terminated = False
17
    port = None
18
    pktHandler = None
19
 
737 olof 20
    def __init__ (self, pktHandler, port):
736 olof 21
        Thread.__init__(self)
22
        self.pktHandler = pktHandler
23
        self.port = port
24
 
25
    def run(self):
737 olof 26
        try:
27
            log.debug('SerialThread.run')
28
            readBuffer = []
29
 
30
            while not self.terminated:
31
                inByte = self.port.read()
32
                if inByte == '\n':
33
                    msg = ''.join(readBuffer)
34
                    readBuffer = []
35
                    #print 'Incoming:', msg
36
                    self.pktHandler.input(msg)
37
                else:
38
                    readBuffer.append(inByte)
736 olof 39
 
738 olof 40
                time.sleep(0.001)
41
 
737 olof 42
            self.port.close()
738 olof 43
            self.ownerNotifier.notify(self.ownerNotifier.TERMINATE)
44
 
737 olof 45
        except: # sys.excepthook does not work in threads
46
            import traceback
47
            traceback.print_exc()
48
            self.terminate()
49
            self.port.close()
50
 
736 olof 51
    def terminate(self):
52
        log.debug('SerialThread.terminate')
53
        self.terminated = True
54
 
55
 
56
class NodeIfSerial(NodeIfBase):
57
 
58
    DEFAULT_CONFIG = {'serialPort':1, #com1
59
                      'baudRate':9600,
60
                      'byteSize':8,
61
                      'parity': 'N',
62
                      'stopBits':1,
63
                      'timeOut': 0, # must be 0 (non-blocking mode)
64
                      'softFlowCtl': 0,
65
                      'hardFlowCtl': 0
66
                      }
67
 
68
    serialThread = None
69
    pktHandler = None
750 olof 70
    ownerNotifier = None
746 olof 71
    config = {}
736 olof 72
 
737 olof 73
    def __init__ (self, pktHandler, cfg = None):
736 olof 74
        if cfg is None:
75
            self.config = self.DEFAULT_CONFIG
746 olof 76
        else:
77
            self.confg = cfg
736 olof 78
 
79
        self.pktHandler = pktHandler
80
        NodeIfBase.__init__(self, cfg, pktHandler)
750 olof 81
 
82
    def setPktHandler(self, pktHandler):
83
        self.pktHandler = pktHandler
84
 
85
    def setIfNotifier(self, ifNotifier):
86
        self.ownerNotifier = ifNotifier
736 olof 87
 
88
    def start(self):
89
 
90
        try:
91
            port = serial.Serial(self.config['serialPort'],
92
                                 self.config['baudRate'],
93
                                 self.config['byteSize'],
94
                                 self.config['parity'],
95
                                 self.config['stopBits'],
96
                                 self.config['timeOut'],
97
                                 self.config['softFlowCtl'],
98
                                 self.config['hardFlowCtl'])
99
        except (serial.SerialException), Error:
100
            print Error
101
            return False
102
 
737 olof 103
        self.serialThread = SerialThread(self.pktHandler, port)
736 olof 104
        self.serialThread.start()
105
        return True
106
 
107
    def stop(self):
738 olof 108
        if self.serialThread is not None and not self.stimThread.terminated:
736 olof 109
            self.serialThread.terminate()
110
        else:
111
            log.debug('Serial interface not started, or already stopped')
737 olof 112
 
113
    def running(self):
114
        if self.serialThread is not None:
739 olof 115
            return not self.serialThread.terminated
737 olof 116
        else:
117
            return False
118