Python Forum
Filtering characters that pass through serial
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filtering characters that pass through serial
#1
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  filtering files using 'any()" tester_V 19 1,531 May-08-2021, 12:01 PM
Last Post: snippsat
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 869 Sep-07-2020, 08:02 AM
Last Post: perfringo
  Remove escape characters / Unicode characters from string DreamingInsanity 5 3,925 May-15-2020, 01:37 PM
Last Post: snippsat
  Pass by reference vs Pass by value leodavinci1990 1 930 Nov-20-2019, 02:05 AM
Last Post: jefsummers
  Help with filtering in Python Junes786 2 1,033 Jun-05-2019, 08:10 AM
Last Post: Junes786
  csv filtering with python. KirkmanJ 12 6,184 Jun-26-2018, 11:29 AM
Last Post: KirkmanJ
  filtering a string hello_its_me 6 2,384 Nov-29-2017, 05:46 AM
Last Post: hello_its_me

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020