-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial2CSV.py
executable file
·155 lines (132 loc) · 4.73 KB
/
serial2CSV.py
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------------------------------
#
# The serial reader is a stand alone python progamm, which can collect data from a serial port
# and store the content into a csv file
#
# the structure of this binary data is stricktly defined in the PODRacer FW
#
# If something is changed in FW, you have to adapt this changes in this file too (normaly adjusting columns)
# ------------------------------------------------------------------------------------------------------
import csv
import time
from datetime import datetime
import serial
import argparse
import sys
import os
# SYNC allways FEEF, if not ignore record
columns = [
# grouping information
"TIME", "TASK","GROUP","GROUPING",
# channels
"CH_R", "CH_P", "CH_Y", "CH_H", "CH_T",
"ARMING","AUX2","AUX3",
# floats & long values, used by tasks
"float0","float1", "float2", "float3", "float4", "float5", "float6", "float7",
"ldata0","ldata1","ldata2","ldata3","ldata4","ldata5","ldata6","ldata7",
# pid values used by tasks
"pidRoll","pidPitch","pidYaw","pidThrust","pidHover",
# constants.....
"HOVER_MINIMAL_HEIGHT", "HOVER_MIN_DISTANCE", "HOVER_MAX_DISTANCE",
# CRC 16Bit, PODRacer send 8Bytes 0000+4BytesCRC
"CRC"
];
fname = "livedata{TS}.csv"
parser = argparse.ArgumentParser()
parser.add_argument("--port", type=str, help="insert your serial port")
parser.add_argument("--baud", type=int, default=115200, help="Baud rate, default=115200")
parser.add_argument("--tout", type=int, default=5, help="Timout in seconds, default=5")
parser.add_argument("--opath", type=str, default="./", help="output path, default is ./")
parser.add_argument("--delimiter", type=str, default=",", help="delimiter, default is comma (,)")
parser.add_argument("--use_ts", action="store_true", help="if set, use a timestamp in file name")
parser.add_argument("-v", "--verbose", action="count", default=1, help="verbosity output -v -vv -vvv")
args = parser.parse_args()
def createSerialObj(port, baud, tout):
"""
create a pySerial object on a given port with baud and timeout
Args:
port (_type_): path to serial port
baud (_type_): baud rate
tout (_type_): timeout
Return:
serial obj
"""
try:
s = serial.Serial(port=port,baudrate=baud, timeout=tout)
s.isOpen()
print("SerialPort open");
except IOError as err:
print ("Port already in use. Try to close ...")
s.close()
print ("Port closed, try to reopen...")
s.open()
except Exception as err:
print (err)
print (f"problem to create serial object on '{port}' with '{baud}' baudrate")
return None
return s
def readSerialData(ser:serial, writer : csv.writer):
"""
read data from serial port into data structure and return this struct
Args:
ser (serial): serial object
data (dict): data structure
Returns:
dict: data structuse
"""
ser.flushInput()
raw = []
timeout = False
lines = lb = 0
print("-----------------------------")
print("waiting for serial data......")
print("-----------------------------")
while not timeout:
line = ser.readline()
l = line.decode('utf-8').splitlines()
if (len(l) > 0):
raw = l[0].split(',')
if raw[0] != "FEEF":
continue
else:
continue
writer.writerow(raw[1:])
if (lines % 100 == 0):
if (lb % 10 == 0) and (lb > 0):
print("")
lb = 0
print(f"{lines:>6} ",end="")
lb += 1
lines += 1
def getFileName(path="./"):
dt = datetime.fromtimestamp(time.time())
ts = ""
if args.use_ts:
ts = dt.strftime("_%Y%m%d_%H%M%S")
f = os.path.join(path, fname.format(TS=ts))
return f
def createNewFile():
fn = getFileName(args.opath)
with open (fn, "w") as f:
#csv_writer = csv.DictWriter(f, fieldnames=columns)
csv_writer = csv.writer(f, delimiter=args.delimiter)
csv_writer.writerow(columns)
return fn
def run():
ser = createSerialObj(args.port, args.baud, None)
if ser == None:
print("no serial port available")
sys.exit(1)
fn = createNewFile()
if args.verbose > 2:
print (f"output file '{fn}'")
count = 0
lb = 80
with open (fn, "a") as f:
cw = csv.writer(f, delimiter=args.delimiter)
print(f"writing into '{fn}'")
data = readSerialData(ser, cw)
if __name__ == "__main__":
run()