In [1]:
import sys
import csv
import serial
import numpy as np
from matplotlib import pyplot as plt

In [6]:
class DataParser:
    
    # constructor
    def __init__(self, filename):
        self.filename = filename
        self.line_cnt = 0
        self.data = []
        self.content = []
        self.original_time = 8080
        
    def openfile(self):
        with open(self.filename) as f:
            reader = f.readlines()
        self.content = [x.strip() for x in reader] 
        self.line_cnt = len(self.content)
        self.original_time = int(content[1][content[1].find("...") + 3:])
        print("Successfully open the file" + str(self.filename) + " with "+ str(self.line_cnt) + " data.")
        self.read();
        
    def read(self):
        print("Start reading...")
        for i in range(self.line_cnt):
            if((i %2) == 0):
                inflow = self.content[i].find("InFlow") +  len("InFlow is:")
                outflow = self.content[i].find("OutFlow") +  len("OutFlow is:")
                f1 = self.content[i].find("F1") + len("F1 is:")
                f2 = self.content[i].find("F2") + len("F2 is:")
            else:
                row = []
                time = self.content[i].find("...") + len("...")
                row.append(float(self.content[i-1][inflow:inflow+5]))
                row.append(float(self.content[i-1][outflow:outflow+5]))
                row.append(float(self.content[i-1][f1:f1+5]))
                row.append(float(self.content[i-1][f2:f2+5]))
                row.append(int(self.content[i][time:]) - self.original_time)
                self.data.append(row)
                print("reading...%.0f%%\r" % (i*100.0//self.line_cnt))
                sys.stdout.flush()
        print("Done!")
    def save_csv(self):
        print("Save to csv")
        with open(self.filename + ".csv", "wb") as f:
            writer = csv.writer(f)
            writer.writerows(data)

Add to csv file


In [7]:
filename = "flow2"
parser = DataParser(filename)
parser.openfile()
parser.save_csv()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-602447208f14> in <module>()
      1 filename = "flow2"
      2 parser = DataParser(filename)
----> 3 parser.openfile()
      4 parser.save_csv()

<ipython-input-6-341ff9b959df> in openfile(self)
     15         self.content = [x.strip() for x in reader]
     16         self.line_cnt = len(self.content)
---> 17         self.original_time = int(content[1][content[1].find("...") + 3:])
     18         print("Successfully open the file" + str(self.filename) + " with "+ str(self.line_cnt) + " data.")
     19         self.read();

NameError: global name 'content' is not defined

In [184]:
parser.save_csv()


Save to csv

In [2]:
ser = serial.Serial('COM3', 9600)   # Your port number
 
plt.ion() # set plot to animated

ydata = [0] * 100    # deque of 100 data point
ax1=plt.axes()
# make plot
line, = plt.plot(ydata)
plt.ylim([10,40])
while True:
    data = []
    data.append(ser.readline())
    data.append(ser.readline())
    index = 0
    if data[0].find("InFlow") != -1:
        index = 1
    inflow = data[index].find("InFlow") +  len("InFlow is:")
    outflow = data[index].find("OutFlow") +  len("OutFlow is:")
    f1 = data[index].find("F1") + len("F1 is:")
    f2 = data[index].find("F2") + len("F2 is:")
    ydata.append(inflow)
    line.set_xdata(np.arange(len(ydata)))
    line.set_ydata(ydata)
    plt.draw()


---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-2-e39299e23ae3> in <module>()
     10 while True:
     11     data = []
---> 12     data.append(ser.readline())
     13     data.append(ser.readline())
     14     index = 0

C:\Python27\lib\site-packages\serial\serialwin32.pyc in read(self, size)
    288                     ctypes.byref(self._overlapped_read),
    289                     ctypes.byref(rc),
--> 290                     True)
    291                 if not result_ok:
    292                     if win32.GetLastError() != win32.ERROR_OPERATION_ABORTED:

KeyboardInterrupt: 

In [50]:
Data


Out[50]:
'Temperature InFlow is:25.06 = OutFlow is:25.12 = F1 is:16.62 = F2 is:-127.00 = Water Flow Rate is: 24 L/hour\n'

In [51]:
Timestemp


Out[51]:
'Requesting temperatures and flow rate...15950\r\n'

In [ ]: