On Wed, 15 May 2019 08:17:50 -0700 (PDT), Sean Landerkin
<derkinmail.me@gmail.com> declaimed the
following:
NOTE: if you KNOW the data will be in "records" of 7 values (and you
never have an "odd" record) you could...
CHNK_SAMPLES = 64
Change that to 7 (it will mean more reads in the main loop, but...)
rdr = struct.Struct("H")
Change to "HHHHHHH" -- will interpret all 7 values in one call to the
unpack and...
i = 0 #sets up the index for looping over our array
Probably don't need this phase
while True:
chnk = fin.read(2*CHNK_SAMPLES) #gathers data from
the output file in chunks
if not chnk: break
With above changes, "chnk" should contain exactly 7 samples (14 bytes),
and
smpls = rdr.unpack(chnk)
should be a tuple or list (I haven't opened the help file to check) of all
seven.
datarr[i] = smpl[0]*1.8/4095 #converts the
raw data into voltge form and stores it in the ith entry
datarr = [smpl * 1.8 / 4095 for smpl in smpls]
should do the conversion (you probably don't need the array initializer
either since this flat out creates a regular list of values).
if(i == 6): #if we have filled the array
And since we know it is 7 values at a time, no need to test...
csvwriter.writerow([datarr[0],datarr[1],datarr[2],datarr[3],datarr[4],datarr[5],datarr[6]])
I think just
csvwrite.writerow(datarr)
would then suffice -- why index each element of the list, only to wrap them
back into a list.
#writes the data values to the csv
i = 0 #resets the index and the
array values to 0
datarr[0]=0
datarr[1]=0
datarr[2]=0
datarr[3]=0
datarr[4]=0
datarr[5]=0
datarr[6]=0
else:
i= i+1
... and not needed since the read/chunk and unpack is now working 7 values
at a time.