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


Messages In This Thread
Filtering characters that pass through serial - by xbit - May-06-2021, 11:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 3,292 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  How to pass encrypted pass to pyodbc script tester_V 0 800 Jul-27-2023, 12:40 AM
Last Post: tester_V
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,513 Sep-07-2020, 08:02 AM
Last Post: perfringo
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,410 May-15-2020, 01:37 PM
Last Post: snippsat
  Pass by reference vs Pass by value leodavinci1990 1 2,162 Nov-20-2019, 02:05 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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