arduino - Python doesn't parse Serial data to an array -


i working on project in have collect voltages teensy 2.0(which programming arduino) , send voltages python. have send microsecond data taken on. using pyserial communicate teensy. first read in data array of length 3998. have 2 other arrays, timedata array, keeps track of microseconds, , radardata array, keeps track of voltages. each array carries half data, or 1999 points.

sample portion of serialdata:

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

this repeated 1999 times. python code takes these inputs , writes them , array "serialdata". after done reading data separates points 2 arrays so:

for 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 , sample1 counter variables.

from printing out float(str(serialdata[i], 'utf-8')), know parsing string float works, whenever print out radardata[samples] or timedata[samples], see 0. doing wrong? appreciated.

thanks!

i suspect have false premise on how lists work messing up. works:

serialdata = [ b'468095384\r\n', b'0.01\r\n'] * 10                                radardata = []                                                                   timedata = []                                                                     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 mean when ask mcve)

i changed code append empty lists , removed sample indexes.

it seems your code work serialdata needs allocated list len(serialdata) (or more) items.

if literally running serialdata[samples] when script exits, looking @ pre-initialized item of sort in list. value samples have when look?

here 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 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)       

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -