-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyCSVlog.py
executable file
·179 lines (149 loc) · 5.32 KB
/
pyCSVlog.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
#script for Loging CSV data from Serial
import serial
import time
import os
import sys
import os
import json
################################################
#code that stops this process from running twice
pid = str(os.getpid()) #get process id
pidfile = 'pyCSVlog.pid'
if 0:#os.path.isfile(pidfile):
print("%s already exists, exiting",pidfile)
sys.exit()
open(pidfile, 'w').write(pid) #create lock file
################################################
#serial settings
port = '/dev/ttyACM0'
baud = '9600'
endline = b'\n'
delimiter = ','
#################################################
## Function Declarationss ##
def openserialport():
status = 0
while status == 0:
if os.path.isfile('serialsettings.json'): #look for settings file
#load settings file
print('settings file found');
with open("serialsettings.json") as infile:
serialsettings = json.load(infile)
try:
s = serial.Serial(serialsettings['port'], baudrate=baud, timeout=5.0)
status = 1
print('settings applied')
print('port opened')
except:
print('failed to apply saved settings')
os.remove('serialsettings.json')
time.sleep(1)
else: #there is no settings file
time.sleep(1)
#try opening serial port defined in this file
try:
s = serial.Serial(port, baudrate=baud, timeout=3.0)
print('serial port opened')
status = 1
#update settings file
serialsettings = { 'port':port }
with open("serialsettings.json", "w") as outfile:
json.dump(serialsettings, outfile, indent=4)
print('settings saved')
except serial.serialutil.SerialException:
print('cant connect to serial port')
time.sleep(30) #wait for serial to be connected
return(s)
allowedchars = [ ord(b'0'),ord(b'1'),ord(b'2'),ord(b'3'),ord(b'4'),ord(b'5'),ord(b'6'),ord(b'7'),ord(b'8'),ord(b'9'),ord(b','),ord(b'-'),ord('.')]
def readlineCR(serial_p):
rv = ""
state = 0
while state == 0:
val = serial_p.read(1)
if (val != b'\x00') & (val != b''): #no time out
val = ord(val) #convert ascii to a number
if val == ord('\n'):
state = 1 #eol detected
else:
if val in allowedchars: #check for valid characters
rv = rv + chr(val)
else: #invalid character
rv = ""
print('invalid character')
state = 0 #invalid character detected
return(rv)
def csv2dict(line,keys):
#split line
d = line.split(delimiter)
#zip into dictionary
data = dict(zip(keys,d))
return data
#################################################
## Settings ##
#csv format
keys = ['v','w','x','y','z']
#log file
filesize = 5000
#http://stackoverflow.com/questions/647769/why-cant-pythons-raw-string-literals-end-with-a-single-backslash
logfiledir = r"/home/pi/Log"
logfilename = 'log'
logfiletype = '.csv'
logfilenumber = 0
logsessionnumber = 0
lograte = 5
#display
displayrate = 1.0
#variables for timing
logtimer = 0
displaytimer = 0
#keeping track of file number
filesizecount = 0
#####################################
##### put actual code that runs here
try:
if not os.path.exists(logfiledir):
os.makedirs(logfiledir)
#scan log directory to determine number
#http://stackoverflow.com/questions/8933237/how-to-find-if-directory-exists-in-python#8933290
while os.path.isdir(os.path.join(logfiledir , str(logsessionnumber))):
logsessionnumber += 1
#update log file directory using number
logfiledir = os.path.join(logfiledir, str(logsessionnumber))
os.mkdir(logfiledir)
try:
while 1:
#open serial port
s = openserialport()
#make log file
logfile = os.path.join(logfiledir,logfilename + str(logfilenumber) + logfiletype)
f = open(logfile,'w')
while filesizecount <= filesize:
now = time.time()
line = readlineCR(s)
data = csv2dict(line,keys)
#display data
if (now - displaytimer) >= displayrate:
print(data)
displaytimer = now
#log data in csv
if (now - logtimer) >= lograte:
f.write(line)
filesizecount += 1
logtimer = now
if filesizecount == filesize:
f.close()
logfilenumber += 1
#make new log file
logfile = os.path.join(logfiledir,logfilename + str(logfilenumber) + logfiletype)
f = open(logfile,'a')
filesizecount = 0
except KeyboardInterrupt:
print('closing')
f.close()
s.close()
##################################
#this runs when the process closes
finally:
os.unlink(pidfile)
##################################