Arduino.py 1.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
from time import sleep
from serial import Serial

class Arduino(object):
    def __init__(self, logger, location, channels):
        logger("[ARDUINO]", "Opening serial port...")
        try:
            self.ser = Serial(location, 115200)
        except:
            logger("[ARDUINO]", "ERROR: Could not open serial port")
        logger("[ARDUINO]", "Starting board...")
        self._reset()                              # Reset do arduino para reinicio da leitura
        self.ser.readline()                        # Espera o Arduino inicializar
        self.ser.write(bytes("press",'utf-8'))
        self.ser.readline()
        logger("[ARDUINO]", "Board started, good luck!")

    def read(self):
        return 10

    def readAll(self):
        self.ser.flushInput()                                   # Limpeza do buffer de entrada
        data = self.ser.readline().split(bytes(" ", "utf-8"))   # Leitura de todos os valores em uma linha
        return [int(x) for x in data]

    def _reset(self):
        self.ser.setDTR(False)
        sleep(0.05)
        self.ser.setDTR(True)