Arduino.py 1.14 KB
Newer Older
1 2 3
from time import sleep
from serial import Serial

4

5 6 7 8 9
class Arduino(object):
    def __init__(self, logger, location, channels):
        logger("[ARDUINO]", "Opening serial port...")
        try:
            self.ser = Serial(location, 115200)
10
        except Exception as e:
11
            logger("[ARDUINO]", "ERROR: Could not open serial port")
12 13
            raise FileNotFoundError("Serial port not found") from e

14 15 16 17 18 19 20 21
        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):
22
        pass
23 24 25 26 27 28 29 30 31 32

    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)