Receiving data from serial and adding to list

I have a function, in my python tkinter application that reads serial data:

def readSerial():
    global after_id
    while ser.in_waiting:
            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("No data received")

My program reads serial data from the arduino and prints them on a tkinter text field.

This is what i receive:













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.print("SENSOR COORDINATE         = ");
Serial.print("MEASURED RESISTANCE       = ");
double resistanse = ((period * GAIN_VALUE * 1000) / (4 * CAPACITOR_VALUE)) - R_BIAS_VALUE;
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)

Of course, you can send what you like, there is also the write() function.

That would work just fine. If you send a lot of 'cosmetic' data then you have to filter more of it out.

Thank you for your kind answer!
I definately want to get the data logged.
So my 'printing code', i don't want to change that.

But how can i add the variables, so that python instant grabs them, id's them and places in an array (without printing them - as they will already have been printed by my previous code)?

Or can i modify the


lines, so that python parses them, stores them and then prints them?
Because in python's eyes, these lines are just standard serial printing.

in other words, i want to both get the data to print, and understand tyem and store them in an array.
Printing them is straightforward, but what is the best way to understand the usuful data, i norder to store them?

Then you should let you python code parse it from there.
There is only a single stream of data. you can not exclude separately sent data from the stream, unless you do that at the receiving end.

Could you add some pointers on how i could grab the data?
For example, take a look at the lines:

Serial.print("SENSOR COORDINATE         = ");

I could search for the characters TE = and grab the next value until the endline character, and after i convert it to float, it would be my value.

But how exactly do i do that, in a stream of receiving data?

You are the one writing Python.

Check out the python .split() function. Format the serial data from the Arduino so that the line received has simple markers for the split.

You can simplify your Python <--> Arduino interface by using the compatible libraries pySerialTransfer and SerialTransfer.h. This will allow you to automatically format, packetize, and parse data transferred via USB/UART between Python and your Arduino.

Thank you. But the thing is i want all characters printed as they are to a tkinter frame. But i want some of them (the sensor coordinate and the resistance) collected - in order to be stored in a list.

How will python differentiate between the ones i want to store?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.