Search code examples
python-3.xarduinoteensy

Python doesn't parse Serial data to an array


I am working on a project in which I have to collect voltages from a teensy 2.0(which I am programming with arduino) and send those voltages to Python. I also have to send the microsecond the data was taken on. I am using PySerial to communicate with the teensy. I first read in all my data to an array of length 3998. I have two other arrays, a timeData array, which keeps track of the microseconds, and a radarData array, which keeps track of the voltages. Each array carries half the data, or 1999 points.

Sample portion of SerialData:

b'1468095384\r\n'
b'0.01\r\n'

This will get repeated 1999 times. The Python code takes these inputs and writes them into and array "SerialData". After it is done reading all the data it separates all the points into the two arrays like so:

for i in range (0,3998):
    if(i % 2 == 0):
        radarData[samples] = float(str(SerialData[i], 'utf-8'))
        samples = samples + 1
    else:
        timeData[samples1] = float(str(SerialData[i], 'utf-8'))
        samples1 = samples1 + 1

Sample and Sample1 are counter variables.

From printing out float(str(SerialData[i], 'utf-8')), I know that parsing the string as a float works, but whenever I print out radarData[samples] or timeData[samples], I just see 0. What am I doing wrong? Any help is appreciated.

Thanks!


Solution

  • I suspect you have a false premise on how lists work that is messing you up. This works:

    SerialData = [ b'468095384\r\n', b'0.01\r\n'] * 10                              
    
    radarData = []                                                                  
    timeData = []                                                                   
    
    for i in range(0,len(SerialData)):                                              
        if(i % 2 == 0):                                                             
            radarData.append(float(str(SerialData[i], 'utf-8')))                    
        else:                                                                       
            timeData.append(float(str(SerialData[i], 'utf-8')))                     
    
    print(radarData)                                                                
    print(timeData)       
    

    (This btw is what we mean when we ask for an MCVE)

    I changed your code to append to empty lists and removed the sample indexes.

    It seems that for your code to work SerialData needs to already be an allocated list with len(SerialData) (or more) items.

    If you are literally running SerialData[samples] when your script exits, then you are looking at a pre-initialized item of some sort in your list. What value does samples have when you look?

    Here is my output:

    [468095384.0, 468095384.0, 468095384.0, 468095384.0, 468095384.0, 468095384.0, 468095384.0, 468095384.0, 468095384.0, 468095384.0]
    [0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01]
    

    And for fun:

    serialData = [ b'468095384\r\n', b'0.01\r\n'] * 10                              
    tmp = list(map(lambda d: float(d), serialData))                   
    radarData = tmp[0::2]                                                           
    timedata = tmp[1::2]                                                            
    print(radarData)                                                                
    print(timeData)