Arduino.py 2.02 KB
Newer Older
1 2
from datetime import datetime
from time import sleep, time
3 4 5 6 7 8
from serial import Serial

class Arduino(object):
    def __init__(self, logger, location, channels):
        logger("[ARDUINO]", "Opening serial port...")
        try:
9
            self.ser = Serial(location, 115200, timeout=0.1)
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
        logger("[ARDUINO]", "Starting board...")
15 16 17 18
        #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()
19
        logger("[ARDUINO]", "Board started, good luck!")
20 21 22
        today = datetime.now().strftime("%Y-%m-%d-%H:%M:%S")
        self.logFile = open("/home/phckopper/logs/{}-calib.csv".format(today), "w")
        self.logFile.write("Tempo (Unix), Calibrante (V), Pressão (bar)\n")
23 24

    def read(self):
25
        pass
26 27

    def readAll(self):
28
        print("Reading:")
29
        self.ser.flushInput()                                   # Limpeza do buffer de entrada
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
        val = []
        self.ser.write(bytes(":MEAS:VOLT:DC? A0\r\n", "utf-8"))                                     # Trigga leitura
        reading = self.ser.readline().strip()
        print(reading)
        val.append(float(reading))

        self.ser.write(bytes(":MEAS:VOLT:DC? A3\r\n", "utf-8"))                                     # Trigga leitura
        reading = self.ser.readline().strip()
        print(reading)
        val.append(float(reading) * (250.0/10.0))

        self.logFile.write("{},{},{}\n".format(time(), val[0], val[1]))
        self.logFile.flush()
        #data = self.ser.readline().split(bytes(" ", "utf-8"))   # Leitura de todos os valores em uma linha
        #return [int(x) for x in data]
        return val
46 47 48 49

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