-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial2csv.py
More file actions
112 lines (92 loc) · 3.78 KB
/
serial2csv.py
File metadata and controls
112 lines (92 loc) · 3.78 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import argparse
from colorama import init, Fore
import serial
import csv
import time
init(autoreset=True)
connect_state = False
file_name="Serial2CSV_data.csv"
logo = """
╭━━━╮╱╱╱╱╱╱╱╱╭╮╭━━━┳━━━┳━━━┳╮╱╱╭╮
┃╭━╮┃╱╱╱╱╱╱╱╱┃┃┃╭━╮┃╭━╮┃╭━╮┃╰╮╭╯┃
┃╰━━┳━━┳━┳┳━━┫┃╰╯╭╯┃┃╱╰┫╰━━╋╮┃┃╭╯
╰━━╮┃┃━┫╭╋┫╭╮┃┃╭━╯╭┫┃╱╭╋━━╮┃┃╰╯┃
┃╰━╯┃┃━┫┃┃┃╭╮┃╰┫┃╰━┫╰━╯┃╰━╯┃╰╮╭╯
╰━━━┻━━┻╯╰┻╯╰┻━┻━━━┻━━━┻━━━╯╱╰╯
"""
def main():
global file_name
print(Fore.YELLOW + logo)
print(Fore.BLUE + "Created By :" + Fore.GREEN + " Sukarna Jana")
print(Fore.BLUE + "Version :" + Fore.GREEN + " 0.0.1V")
print()
parser = argparse.ArgumentParser(description="Serial Data Logger")
parser.add_argument("-p", "--port", type=str, help="Serial port (e.g., COM3)")
parser.add_argument("-b", "--baud", type=int, help="Baud rate (e.g., 9600)")
parser.add_argument("-f", "--filename", type=str, help="Output CSV filename")
args = parser.parse_args()
if not args.port or not args.baud or not args.filename:
parser.error(Fore.RED + "Please provide the required arguments: -p, -b, and -f\n" + Fore.CYAN + "example: python3 serial2csv.py -p COM3 -b 9600 -f output.csv" )
print(Fore.CYAN + "Selected Serial Port: " + Fore.YELLOW + str(args.port))
print(Fore.CYAN + "Baud Rate : " + Fore.YELLOW + str(args.baud))
print(Fore.CYAN + "Output Filename : " + Fore.YELLOW + str(args.filename))
print(Fore.YELLOW + "To Terminate/Stop u can press [Ctrl+C]")
print()
file_name = args.filename
time.sleep(2)
connect(str(args.port),int(args.baud))
while isConnect():
try:
#printData()
saveData()
except KeyboardInterrupt:
break
def connect(portNo,baud):
global connect_state, device
try:
device = serial.Serial(port=portNo, baudrate=baud, timeout=0.1)
connect_state = True
print(Fore.GREEN + "Connection Establish Successfully")
except:
connect_state = False
print(Fore.RED + "Connection Establish Failed")
def isConnect():
if(connect_state):
return True
else:
return False
def printData():
try:
if(isConnect()):
ser_bytes = device.readline()
data = (ser_bytes[0:len(ser_bytes)-2]).decode("utf-8")
#print(data)
if(data != ""):
print(Fore.GREEN + data)
value_list = [eval(value) for value in data.split(",")]
print(value_list)
else:
print(Fore.RED + "[ERROR] Something Sent Wrong")
exit()
except:
device.close()
print(Fore.RED + "[ERROR] Something Sent Wrong")
exit()
def saveData():
try:
if isConnect():
ser_bytes = device.readline()
data = (ser_bytes[0:len(ser_bytes) - 2]).decode("utf-8")
#print(data)
if data != "":
value_list = [eval(value) for value in data.split(",")]
# Open the CSV file in append mode and write the data
with open(file_name, mode='a', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(value_list)
print(Fore.CYAN + "Data saved to CSV:", value_list)
except Exception as e:
device.close()
print(Fore.RED + "[ERROR] Something Went Wrong While Saving Data:", str(e))
if __name__ == "__main__":
main()