Subversion Repositories HomeAutomation

Rev

Rev 739 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 739 Rev 746
1
###########################################################
1
###########################################################
2
#
2
#
3
# Serial interface, connects with can bus over a serial
3
# Serial interface, connects with can bus over a serial
4
# line
4
# line
5
#
5
#
6
###########################################################
6
###########################################################
7
import logging as log
7
import logging as log
8
from NodeIfBase import NodeIfBase
8
from NodeIfBase import NodeIfBase
9
from threading import Thread
9
from threading import Thread
10
import serial
10
import serial
11
import sys
11
import sys
12
import time
12
import time
13
 
13
 
14
class SerialThread(Thread):
14
class SerialThread(Thread):
15
   
15
   
16
    terminated = False
16
    terminated = False
17
    port = None
17
    port = None
18
    pktHandler = None
18
    pktHandler = None
19
   
19
   
20
    def __init__ (self, pktHandler, port):
20
    def __init__ (self, pktHandler, port):
21
        Thread.__init__(self)
21
        Thread.__init__(self)
22
        self.pktHandler = pktHandler
22
        self.pktHandler = pktHandler
23
        self.port = port
23
        self.port = port
24
 
24
 
25
    def run(self):
25
    def run(self):
26
        try:
26
        try:
27
            log.debug('SerialThread.run')
27
            log.debug('SerialThread.run')
28
            readBuffer = []
28
            readBuffer = []
29
                   
29
                   
30
            while not self.terminated:
30
            while not self.terminated:
31
                inByte = self.port.read()
31
                inByte = self.port.read()
32
                if inByte == '\n':
32
                if inByte == '\n':
33
                    msg = ''.join(readBuffer)
33
                    msg = ''.join(readBuffer)
34
                    readBuffer = []
34
                    readBuffer = []
35
                    #print 'Incoming:', msg
35
                    #print 'Incoming:', msg
36
                    self.pktHandler.input(msg)
36
                    self.pktHandler.input(msg)
37
                else:
37
                else:
38
                    readBuffer.append(inByte)
38
                    readBuffer.append(inByte)
39
   
39
   
40
                time.sleep(0.001)
40
                time.sleep(0.001)
41
               
41
               
42
            self.port.close()
42
            self.port.close()
43
            self.ownerNotifier.notify(self.ownerNotifier.TERMINATE)
43
            self.ownerNotifier.notify(self.ownerNotifier.TERMINATE)
44
           
44
           
45
        except: # sys.excepthook does not work in threads
45
        except: # sys.excepthook does not work in threads
46
            import traceback
46
            import traceback
47
            traceback.print_exc()
47
            traceback.print_exc()
48
            self.terminate()
48
            self.terminate()
49
            self.port.close()
49
            self.port.close()
50
   
50
   
51
    def terminate(self):
51
    def terminate(self):
52
        log.debug('SerialThread.terminate')
52
        log.debug('SerialThread.terminate')
53
        self.terminated = True
53
        self.terminated = True
54
       
54
       
55
 
55
 
56
class NodeIfSerial(NodeIfBase):
56
class NodeIfSerial(NodeIfBase):
57
   
57
   
58
    DEFAULT_CONFIG = {'serialPort':1, #com1
58
    DEFAULT_CONFIG = {'serialPort':1, #com1
59
                      'baudRate':9600,
59
                      'baudRate':9600,
60
                      'byteSize':8,
60
                      'byteSize':8,
61
                      'parity': 'N',
61
                      'parity': 'N',
62
                      'stopBits':1,
62
                      'stopBits':1,
63
                      'timeOut': 0, # must be 0 (non-blocking mode)
63
                      'timeOut': 0, # must be 0 (non-blocking mode)
64
                      'softFlowCtl': 0,
64
                      'softFlowCtl': 0,
65
                      'hardFlowCtl': 0
65
                      'hardFlowCtl': 0
66
                      }
66
                      }
67
   
67
   
68
    serialThread = None
68
    serialThread = None
69
    pktHandler = None
69
    pktHandler = None
-
 
70
    config = {}
70
   
71
   
71
    def __init__ (self, pktHandler, cfg = None):
72
    def __init__ (self, pktHandler, cfg = None):
72
        if cfg is None:
73
        if cfg is None:
73
            self.config = self.DEFAULT_CONFIG
74
            self.config = self.DEFAULT_CONFIG
-
 
75
        else:
-
 
76
            self.confg = cfg
74
       
77
       
75
        self.pktHandler = pktHandler
78
        self.pktHandler = pktHandler
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:
81
            port = serial.Serial(self.config['serialPort'],
84
            port = serial.Serial(self.config['serialPort'],
82
                                 self.config['baudRate'],
85
                                 self.config['baudRate'],
83
                                 self.config['byteSize'],
86
                                 self.config['byteSize'],
84
                                 self.config['parity'],
87
                                 self.config['parity'],
85
                                 self.config['stopBits'],
88
                                 self.config['stopBits'],
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)
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 and not self.stimThread.terminated:
101
        if self.serialThread is not None and not self.stimThread.terminated:
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')
102
           
105
           
103
    def running(self):
106
    def running(self):
104
        if self.serialThread is not None:
107
        if self.serialThread is not None:
105
            return not self.serialThread.terminated
108
            return not self.serialThread.terminated
106
        else:
109
        else:
107
            return False
110
            return False
108
   
111