Package PyMata :: Module pymata_serial
[hide private]
[frames] | no frames]

Source Code for Module PyMata.pymata_serial

  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   
27 -class PyMataSerial(threading.Thread):
28 """ 29 This class manages the serial port for Arduino serial communications 30 """ 31 32 # class variables 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):
42 """ 43 Constructor: 44 @param command_deque: A reference to the deque shared with the _command_handler 45 """ 46 self.port_id = port_id 47 self.command_deque = command_deque 48 49 threading.Thread.__init__(self) 50 self.daemon = True 51 self.arduino = serial.Serial(self.port_id, self.baud_rate, 52 timeout=int(self.timeout)) 53 54 self.stop_event = threading.Event()
55
56 - def stop( self ):
57 self.stop_event.set()
58
59 - def is_stopped( self ):
60 return self.stop_event.is_set()
61
62 - def open(self):
63 """ 64 open the serial port using the configuration data 65 returns a reference to this instance 66 """ 67 # open a serial port 68 print '\nOpening Arduino Serial port %s ' % self.port_id 69 70 try: 71 72 # in case the port is already open, let's close it and then 73 #reopen it 74 self.arduino.close() 75 self.arduino.open() 76 return self.arduino 77 78 except Exception: 79 # opened failed - will report back to caller 80 raise
81
82 - def close(self):
83 """ 84 Close the serial port 85 return: None 86 """ 87 self.arduino.close()
88
89 - def write(self, data):
90 """ 91 write the data to the serial port 92 return: None 93 """ 94 self.arduino.write(data)
95
96 - def run(self):
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 # we can get an OSError: [Errno9] Bad file descriptor when shutting down 104 # just ignore it 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