i am working on a function that reads a line from an arduino serial monitor the line outputs: Licht: 870 Temp: 19.01 the first time the function works but after i call the function again it reads an empty line
here is my code:
import serial
import time
class Serializer:
def __init__(self, port, baudrate=9600, timeout=2):
self.port = serial.Serial(port = port, baudrate=baudrate,
timeout=timeout)
def open(self):
''' Open the serial port.'''
self.port.open()
def close(self):
''' Close the serial port.'''
self.port.close()
def write(self, msg):
time.sleep(1.6)
self.port.write(msg.encode())
def recv(self):
return self.port.readline()
here is my code to get the temp or the lux:
def getLux(self):
lux = int(self.getTempLight()[1])
print(lux)
def getTemp(self):
temp = float(self.getTempLight()[3])
print(temp)
def getTempLight(self):
data =self.recv()
data = str(data)
list = data.split()
return list
After i call the function getTemp i want to call the function getLux() to get lux value from the same line as the temp value.
for example from the line: Licht: 870 Temp: 19.01
i want the values 870 and 19.01 with the functions getTemp and getLux
If you preform readline()
on the port-object it will consume one line. I.e. if you perform a second readline()
it will return the next line your arduino has sent or an empty string if no 2nd line is available.
So either your arduino sends the values continuously, or you store the read line in your Serializer
-object.