Subversion Repositories HomeAutomation

Rev

Rev 736 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 736 Rev 737
Line 21... Line 21...
21
   
21
   
22
    terminated = False
22
    terminated = False
23
    port = None
23
    port = None
24
    pktHandler = None
24
    pktHandler = None
25
   
25
   
26
    def __init__ (self, pktHandler, port, exceptHook):
26
    def __init__ (self, pktHandler, port):
27
        Thread.__init__(self)
27
        Thread.__init__(self)
28
        self.pktHandler = pktHandler
28
        self.pktHandler = pktHandler
29
        self.port = port
29
        self.port = port
30
        sys.excepthook = exceptHook
-
 
31
 
30
 
32
    def run(self):
31
    def run(self):
-
 
32
        try:
33
        log.debug('SerialThread.run')
33
            log.debug('SerialThread.run')
34
        readBuffer = []
34
            readBuffer = []
35
        while not self.terminated:
-
 
36
           
35
                   
-
 
36
            while not self.terminated:
37
            inByte = self.port.read()
37
                inByte = self.port.read()
38
            if inByte == '\n':
38
                if inByte == '\n':
39
                msg = ''.join(readBuffer)
39
                    msg = ''.join(readBuffer)
40
                readBuffer = []
40
                    readBuffer = []
41
                #print 'Incoming:', msg
41
                    #print 'Incoming:', msg
42
                self.pktHandler.input(msg)
42
                    self.pktHandler.input(msg)
43
            else:
43
                else:
44
                readBuffer.append(inByte)
44
                    readBuffer.append(inByte)
45
 
45
   
46
            time.sleep(0.1)
46
                time.sleep(0.1)
-
 
47
            self.port.close()
-
 
48
        except: # sys.excepthook does not work in threads
-
 
49
            import traceback
-
 
50
            traceback.print_exc()
-
 
51
            self.terminate()
47
        self.port.close()
52
            self.port.close()
48
   
53
   
49
    def terminate(self):
54
    def terminate(self):
50
        log.debug('SerialThread.terminate')
55
        log.debug('SerialThread.terminate')
51
        self.terminated = True
56
        self.terminated = True
Line 60... Line 65...
60
                      'stopBits':1,
65
                      'stopBits':1,
61
                      'timeOut': 0, # must be 0 (non-blocking mode)
66
                      'timeOut': 0, # must be 0 (non-blocking mode)
62
                      'softFlowCtl': 0,
67
                      'softFlowCtl': 0,
63
                      'hardFlowCtl': 0
68
                      'hardFlowCtl': 0
64
                      }
69
                      }
65
   
70
   
66
    serialThread = None
71
    serialThread = None
67
    pktHandler = None
72
    pktHandler = None
68
    exceptHook = None
-
 
69
   
73
   
70
    def __init__ (self, pktHandler, exceptHook, cfg = None):
74
    def __init__ (self, pktHandler, cfg = None):
71
        if cfg is None:
75
        if cfg is None:
72
            self.config = self.DEFAULT_CONFIG
76
            self.config = self.DEFAULT_CONFIG
73
       
77
       
74
        self.pktHandler = pktHandler
78
        self.pktHandler = pktHandler
75
        self.exceptHook = exceptHook
-
 
76
        NodeIfBase.__init__(self, cfg, pktHandler)
79
        NodeIfBase.__init__(self, cfg, pktHandler)
77
   
80
   
78
    def start(self):
81
    def start(self):
79
       
82
       
80
        try:
83
        try:
Line 86... Line 89...
86
                                 self.config['timeOut'],
89
                                 self.config['timeOut'],
87
                                 self.config['softFlowCtl'],
90
                                 self.config['softFlowCtl'],
88
                                 self.config['hardFlowCtl'])
91
                                 self.config['hardFlowCtl'])
89
        except (serial.SerialException), Error:
92
        except (serial.SerialException), Error:
90
            print Error
93
            print Error
91
            return False
94
            return False
92
       
95
       
93
        self.serialThread = SerialThread(self.pktHandler, port, self.exceptHook)
96
        self.serialThread = SerialThread(self.pktHandler, port)
94
        self.serialThread.start()
97
        self.serialThread.start()
95
        return True
98
        return True
96
 
99
 
97
    def stop(self):
100
    def stop(self):
98
        if self.serialThread is not None:
101
        if self.serialThread is not None:
99
            self.serialThread.terminate()
102
            self.serialThread.terminate()
100
        else:
103
        else:
101
            log.debug('Serial interface not started, or already stopped')
104
            log.debug('Serial interface not started, or already stopped')
-
 
105
           
-
 
106
    def running(self):
-
 
107
        if self.serialThread is not None:
-
 
108
            return self.serialThread.terminated
-
 
109
        else:
-
 
110
            return False
-
 
111