I have a function, in my python tkinter application that reads 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)
My program reads serial data from the arduino and prints them on a tkinter text field.
This is what i receive:
----------------------------------------
SENSOR COORDINATE = 0
MEASURED RESISTANCE = 3.70 kOhm
----------------------------------------
----------------------------------------
SENSOR COORDINATE = 1
MEASURED RESISTANCE = 3.70 kOhm
----------------------------------------
----------------------------------------
SENSOR COORDINATE = 2
MEASURED RESISTANCE = 3.69 kOhm
----------------------------------------
I want to be able to parse the sensor coodrinate [0-7] and then parse the value for the specific coordinate and place it in the appropriate list.
For example, the first value (3.70), will be placed in the sensor0 list, the second in the sensor1 list and so on.
This is the arduino code thatr handles the serial printing:
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");
As you can see, its basic serial printing.
Does anyone have any idea on how this could be achieved?
What is the best approach to grab data?
Should my python code parse the strings and if it finds something, log it?
Should i change my arduino code to add some hidden characters, so that when python sees it, it will know that the next character should be for parsing?
Can i send data directly? (not printline)