Advertisement
QuantumWarpCode

Arduino Communication Classes

Oct 25th, 2014
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. """
  2. These class is used to communicate with an Arduino.
  3. It requires the pySerial library.
  4. """
  5.  
  6. import time, serial
  7.  
  8. class init(object):
  9.    
  10.     #open port
  11.    
  12.     def create (self, port, channel):
  13.        
  14.         arduino = serial.Serial(port, channel)
  15.         check = self.check(arduino)
  16.         return arduino, check
  17.        
  18.     #checks whether it worked or not
  19.    
  20.     def check (self, arduino):
  21.        
  22.         print(arduino.isOpen())
  23.         return arduino.isOpen()
  24.    
  25.     #closes the port
  26.    
  27.     def close (self, arduino):
  28.        
  29.         check = self.check(arduino)
  30.        
  31.         if check == True:
  32.             arduino.close()
  33.            
  34.         self.check(arduino)
  35.  
  36. class read_write(object):
  37.    
  38.     #readline
  39.    
  40.     def readAline(self, arduino):
  41.        
  42.         text = arduino.readline()
  43.         print(text)
  44.         return text
  45.    
  46.     #readline without /r/n, and b at the beginning
  47.    
  48.     def readASline(self, arduino):
  49.        
  50.         text = self.readAline(arduino)
  51.         text = text.decode('ascii')
  52.         text = text.replace("\n", "")
  53.         text = text.replace("\r", "")
  54.         print(text)
  55.         return text
  56.    
  57.     #write
  58.    
  59.     def write(self, arduino, text):
  60.        
  61.         arduino.write(text)
  62.    
  63.     #write with non-byte format string
  64.    
  65.     def writeASCII(self, arduino, text):
  66.        
  67.         text = text.encode()
  68.         self.write(arduino, text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement