1 __author__ = 'Copyright (c) 2013 Alan Yorinks All rights reserved.'
2 """
3 Created on Tue Sep 3 07:12:01 2013
4
5 @author: Alan Yorinks
6 Copyright (c) 2013-14 Alan Yorinks All rights reserved.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public
10 License as published by the Free Software Foundation; either
11 version 3 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 """
22
23 import threading
24 import serial
25
26
28 """
29 This class manages the serial port for Arduino serial communications
30 """
31
32
33 arduino = serial.Serial()
34
35 port_id = ""
36 baud_rate = 57600
37 timeout = 1
38 command_deque = None
39
40
41 - def __init__(self, port_id, command_deque):
55
58
60 return self.stop_event.is_set()
61
63 """
64 open the serial port using the configuration data
65 returns a reference to this instance
66 """
67
68 print '\nOpening Arduino Serial port %s ' % self.port_id
69
70 try:
71
72
73
74 self.arduino.close()
75 self.arduino.open()
76 return self.arduino
77
78 except Exception:
79
80 raise
81
83 """
84 Close the serial port
85 return: None
86 """
87 self.arduino.close()
88
90 """
91 write the data to the serial port
92 return: None
93 """
94 self.arduino.write(data)
95
97 """
98 This method continually runs. If an incoming character is available on the serial port
99 it is read and placed on the _command_deque
100 @return: Never Returns
101 """
102 while not self.is_stopped():
103
104
105 try:
106 if self.arduino.inWaiting():
107 c = self.arduino.read()
108 self.command_deque.append(ord(c))
109 except OSError:
110 pass
111
112 self.close()
113