I have this continuous serial data stream:
----------------------------------------
SENSOR COORDINATE = 0
MEASURED RESISTANCE = 3.70 kOhm
----------------------------------------
----------------------------------------
SENSOR COORDINATE = 1
MEASURED RESISTANCE = 3.70 kOhm
----------------------------------------
----------------------------------------
SENSOR COORDINATE = 2
MEASURED RESISTANCE = 3.69 kOhm
----------------------------------------
For each iteration, i want to be able to grab the values. The sensor coordinate value, and the resistance value.
I found solutions using .split()
and with using regular expressions (
Find string between two substrings), but the problem is that in my case, there is not one string that i want to filter, but a continuous stream.
For example, .split()
will find my string, but it will split the stream in half. This does not work, in a continuous stream, for more than one time.
NOTE: After the sensor coordinate value, i have a carriage return character.
EDIT 1/3: This is the snippet of code that grabs the serial data:
def readSerial():
global after_id
while ser.in_waiting:
try:
ser_bytes = ser.readline() #read data from the serial line
ser_bytes = ser_bytes.decode("utf-8")
text.insert("end", ser_bytes)
except UnicodeDecodeError:
print("UnicodeDecodeError")
else:
print("No data received")
after_id=root.after(50,readSerial)
And if someone wants, to know, this is the C code on the arduino side, that sends the data:
Serial.println("----------------------------------------");
Serial.print("SENSOR COORDINATE = ");
Serial.println(sensor_coord);
Serial.print("MEASURED RESISTANCE = ");
double resistanse = ((period * GAIN_VALUE * 1000) / (4 * CAPACITOR_VALUE)) - R_BIAS_VALUE;
Serial.print(resistanse);
Serial.println(" kOhm");
EDIT 2/3: This is a previous approach:
def readSerial():
global after_id
while ser.in_waiting:
try:
ser_bytes = ser.readline() #read data from the serial line
ser_bytes = ser_bytes.decode("utf-8")
text.insert("end", ser_bytes)
result = re.search.(, ser_bytes)
print(result)
except UnicodeDecodeError:
print("UnicodeDecodeError")
else:
print("No data received")
after_id=root.after(50,readSerial)
And in another attempt, i changed this line result = re.search.(, ser_bytes)
to result =ser_bytes.split("TE = ")
.
This is a picture of the data i receive (this is a tkinter text frame).
EDIT 3/3: This is my code implementing dracarys algorithm:
def readSerial():
global after_id
while ser.in_waiting:
try:
ser_bytes = ser.readline()
print(ser_bytes)
ser_bytes = ser_bytes.decode("utf-8")
print(ser_bytes)
text.insert("end", ser_bytes)
if "SENSOR COORDINATE" in ser_bytes:
found_coordinate = True
coordinate = int(ser_bytes.split("=")[1].strip())
print("Coordinate",coordinate)
if "MEASURED RESISTANCE" in ser_bytes and found_coordinate:
found_coordinate = False
resistance = float(ser_bytes.split("=")[1].split("kOhm")[0].strip())
print("Resistance",resistance)
except UnicodeDecodeError:
print("UnicodeDecodeError")
else:
print("No data received")
after_id=root.after(50,readSerial)
This is the error i get, after the code runs for about ten seconds succesfully (i have included normal operation output as well for reference):
No data received
b'SENSOR COORDINATE = 2\r\n'
SENSOR COORDINATE = 2
Coordinate 2
b'MEASURED RESISTANCE = 3.67 kOhm\r\n'
MEASURED RESISTANCE = 3.67 kOhm
Resistance 3.67
b'----------------------------------------\r\n'
----------------------------------------
b'----------------------------------------\r\n'
----------------------------------------
b'SENSOR COORDINATE = 3\r\n'
SENSOR COORDINATE = 3
Coordinate 3
No data received
b'MEASURED RESISTANCE = 3.78 kOhm\r\n'
MEASURED RESISTANCE = 3.78 kOhm
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\User1\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__i
nit__.py", line 1883, in __call__
return self.func(*args)
File "C:\Users\User1\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__i
nit__.py", line 804, in callit
func(*args)
File "tkinterWithPortsExperiment.py", line 73, in readSerial
if "MEASURED RESISTANCE" in ser_bytes and found_coordinate:
UnboundLocalError: local variable 'found_coordinate' referenced before assignment