Wednesday, 12 May 2021

Find string between two substrings, in a stream of data

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: 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");


from Find string between two substrings, in a stream of data

No comments:

Post a Comment