forked from swalschuler/CellTestBench
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_data_rev_2.py
More file actions
63 lines (57 loc) · 1.72 KB
/
read_data_rev_2.py
File metadata and controls
63 lines (57 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import time
import sys
import csv
import os
import threading as thr
import win32com.client
import serial
connected = False
STATE = 0
fileNum = 0
#finds COM port that the Arduino is on (assumes only one Arduino is connected)
wmi = win32com.client.GetObject("winmgmts:")
for port in wmi.InstancesOf("Win32_SerialPort"):
#print port.Name #port.DeviceID, port.Name
if "Arduino" in port.Name:
comPort = port.DeviceID
print(comPort, "is Arduino")
#sets up serial connection (make sure baud rate is correct - matches Arduino)
ser = serial.Serial(comPort, 9600)
while not connected:
serin = ser.read()
connected = True
while True:
#time.sleep(.1)
try:
data = ser.readline()
except:
print('Could not read data')
#print(data)
data = data.decode('utf-8')[:-2] # get rid of \r\n
if STATE == 0:
if data == 'START':
STATE = 1
try:
fileName = str(fileNum) +'chargeData.csv'
outputFile = open(fileName, 'w', newline='')
print('Opened file: ' + fileName)
fileNum += 1
except:
print('File not opened')
continue
out = csv.writer(outputFile)
out.writerow(['voltage(V)', 'current(A)'])
print("Started")
elif STATE == 1:
if data[0] == 'D':
STATE = 0
try:
outputFile.close()
print("Finished. File closed.")
except:
print('Finished. File NOT CLOSED')
else:
resList = [data.split()[i] for i in range(2)]
resList[0] = float(resList[0]) * .0049 * 2
print(resList)
out.writerow(resList)