Python Forum

Full Version: Filtering characters that pass through serial
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am developing a tkinter application in order to grab my arduino serial data.

This is the data that are being fed to the pc:

----------------------------------------

SENSOR COORDINATE         = 0

MEASURED RESISTANCE       = 3.70 kOhm

----------------------------------------

----------------------------------------

SENSOR COORDINATE         = 1

MEASURED RESISTANCE       = 3.70 kOhm

----------------------------------------

----------------------------------------

SENSOR COORDINATE         = 2

MEASURED RESISTANCE       = 3.69 kOhm

----------------------------------------
If you are curious, this is the arduino (C warning) code that manages all the 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");
Up till now, i have a tkinter application that grabs the data, and prints them onto a text frame.

This is the code snippet that handles this:

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)
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.

My idea on doing this is to:
1. Search the incoming stream for the characters "TE = ". When you find them grab the incoming data until the newline character, and convert them to int (save them to a temporary variable)
2. When this happens, a flag called readyForJoin will be set to True.
3. Then search incoming stream for the characters "CE = "
4. When you find them, grab the next characters until you find a space character, and cast it to float. (save them to a temporary variable)
5. If readyForJoin == True, then add the second value, to the array specified by the first value we grabbed.

I want to ask more experience people, is this a good enough algorithm? Is there a better way?
If this is a decent strategy, then how exactly do i parse the incoming data on the fly and search for specified strings, in the received data? Do i have to buffer them first?