Python Forum

Full Version: Reading data from Serial
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to read data from serial port and save it to file but when doing so I lose 10 minutes od data every 1 hour any ideas?

Code:
#!/usr/bin/env python
import tkinter
import serial
import numpy as np
import struct
from time import gmtime, strftime
import os,sys
from datetime import datetime,timedelta
import matplotlib.pyplot as plt
plt.ion()
#start reading from serial
ser = serial.Serial('/dev/ttyUSB0', 34800)
i = 0
#os.remove("test.csv")
#f = open("data.csv","wb")
f2= open("tempV.csv","wb")
#ax1=plt.axes()
#ch1fin = [0] * 50
#line, = plt.plot(ch1fin)
while ser is not None:
      fin = np.array([])
      temp= np.array([])
      volt= np.array([])
      t2= np.array([])
      t1 = datetime.now()
      directory = os.path.basename(t1.strftime("%d-%m-%y"))
      #print(directory)
      if not os.path.exists(directory):
         os.makedirs(directory)
      #dirPath2 = os.path.join(directory+(t1.strftime("%H-%M-%S.%f")))
      f = open(directory + '/' + (t1.strftime("%H-%M-%S.%f") + '.csv'),"wb")
      last_t= datetime.now()
      delta = timedelta(seconds=.02)
      #w2 = np.array([])
      #w=np.append(w,now)
      #w2=np.append(w2,now.strftime("%H:%M:%S.%f"))
      #for n in range (0,36000):
      for i in range (0,3600):
            #print(w2) 
            now = datetime.now()                      
            data = struct.unpack('B' *160 , ser.read(160))
            l=0
            for l in range (0,160,16):
                datasync = data[l]
                if datasync == 170:
                   dataz = data[(l+1):(l+15)]
                   ch1fin = np.array([])
                   w = np.array([])
                   w=np.append(w,last_t)
                   w2 = np.array([])
                   w2=np.append(w2,last_t.strftime("%H:%M:%S.%f"))
                   m = 0
                   k = 1
                   for m in range (0,10,2):
                     ch1 = (((dataz[m])*265) + (dataz[(m+1)]))
                     ch1fin=np.append(ch1fin,ch1)
                     w1= w[(k-1)] + delta
                     w=np.append(w,w1)
                     w2=np.append(w2,w1.strftime("%H:%M:%S.%f"))
                     k=k+1
                w2=w2[0:5]
                last_t=w[5]
                fin =  np.column_stack((ch1fin, w2))
                np.savetxt(f,fin,delimiter=",",fmt="%s") 
                temp=(((dataz[10])*265) + (dataz[(11)]))      
                volt=(((dataz[12])*265) + (dataz[(13)]))
                t2=now.strftime("%H:%M:%S.%f")
                fin2 =  np.column_stack((temp, volt, t2))
                np.savetxt(f2,fin2,delimiter=",",fmt="%s")
                print(fin)
It may well be a hardware issue. Did you somehow check that serial connection works as expected and all the data you want to receive is sent?
Either way, please put your code in Python code tags. You can find help here.