Subversion Repositories HomeAutomation

Rev

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

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