-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_to_csv
69 lines (58 loc) · 1.94 KB
/
serial_to_csv
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
64
65
66
67
import serial
import csv
import time
import os
import sys
import io
def scan():
available = []
COM=['COM1','COM2','COM3','COM3','COM4','COM5','COM6','COM7','COM8','COM9','COM10']
for i in COM:
try:
ser = serial.Serial(i)
available.append((i, ser.portstr))
ser.close()
except serial.SerialException:
pass
return available
if __name__=='__main__':
print ("Found ports:")
for ser in scan():
print (" %s" % (ser))
selection = input("Enter port number:")
try:
ser = serial.Serial(selection, 9600, timeout=1)
print("connected to: " + ser.portstr)
except serial.SerialException:
pass
if ser:
print ("Do you want to name your file")
ans = input("Y or N:")
if ans=="Y":
name = input("Give CSV file name : ")
DataFileName = name+ ".csv"
else :
name= time.strftime("%d %b %Y_%H-%M", time.localtime())
DataFileName = name + "_Data.csv"
print("Give name of variables in sequence:")
variables = []
for i in range(int(input("Number of values :"))) :
variables.append(input("Value " + str(i) + " :"))
print("Value list : " + str(variables))
Dataline = []
DataList = []
f = open(str(DataFileName), 'w+')
while (ser.isOpen()):
output = str(ser.readline().decode('cp1252'))
print (output)
Dataline.append(output)
f = open(str(DataFileName), 'w+')
Data= csv.DictWriter(f ,delimiter=',',fieldnames=variables,lineterminator='\n')
Data.writeheader()
Data = csv.writer(f ,delimiter=',',lineterminator='\n')
for item in Dataline :
Data_line = str(item[0:-1])
DataList = str(Data_line).split(",")
print(DataList)
Data.writerow(DataList) #write .csv file
ser.flush()