-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcsv2vcd.py
138 lines (121 loc) · 6.4 KB
/
csv2vcd.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
import tkinter as tk
import os
from tkinter import filedialog, messagebox
class MainWindow():
def __init__(self):
self.tw = tk.Tk()
self.tw.geometry("400x300")
self.tw.title('csv2vcd - by feecat')
self.sourcefile=''
self.outputfile=''
self.separator=','
self.triggervoltage=1.8
my_font1=('times', 18, 'bold')
self.l1 = tk.Label(self.tw,text='Picoscope csv to Pulseview vcd',width=30,font=my_font1).pack()
self.b1 = tk.Button(self.tw, text='Select File', width=20,command = self.select_file).pack()
self.sourcefilevar = tk.StringVar()
self.sourcefilevar.set("Please Select Source file.")
self.w1 = tk.Label(self.tw, textvariable=self.sourcefilevar).pack()
self.b2 = tk.Button(self.tw, text='Select Output', width=20,command = self.save_file).pack()
self.outputfilevar = tk.StringVar()
self.outputfilevar.set("Please Select Output file.")
self.w2 = tk.Label(self.tw, textvariable=self.outputfilevar).pack()
self.b4 = tk.Button(self.tw, text='change separator , or ;', width=20,command = self.change_).pack()
self.separatorvar = tk.StringVar()
self.separatorvar.set("Separator for column is: " + self.separator)
self.w4 = tk.Label(self.tw, textvariable=self.separatorvar).pack()
self.b3 = tk.Button(self.tw, text='Start Converter', width=20,command = self.start_converter).pack()
self.resultvar = tk.StringVar()
self.resultvar.set("Waiting Select File.")
self.w3 = tk.Label(self.tw, textvariable=self.resultvar).pack()
self.tw.mainloop()
def change_(self):
if self.separator == ",":
self.separator = ';'
else:
self.separator = ','
self.separatorvar.set("Separator is: " + self.separator)
def check_dir(self):
if len(self.sourcefile) > 0 and len(self.outputfile) > 0:
self.resultvar.set("Waiting Start Converter")
def select_file(self):
self.sourcefile = filedialog.askopenfilename(filetypes=[("PicoScope CSV",".csv")])
self.outputfilevar.set('Source File: ')
if len(self.sourcefile) > 0:
self.sourcefilevar.set('Source File: ' + self.sourcefile)
self.outputfile = self.sourcefile[:len(self.sourcefile)-3] + 'vcd'
self.outputfilevar.set('Output File: ' + self.outputfile)
self.check_dir()
def save_file(self):
self.outputselect = filedialog.asksaveasfile(initialfile=self.outputfile, filetypes=[("PulseView VCD",".vcd")])
if hasattr(self.outputselect,'name'):
self.outputfile = self.outputselect.name
self.outputfilevar.set('Output File: ' + self.outputfile)
self.check_dir()
def start_converter(self):
print('sourcefile:',self.sourcefile)
print('savefile:',self.outputfile)
if len(self.sourcefile) > 0 and len(self.outputfile) > 0:
self.resultvar.set("In Processing...")
self.tw.update()
try:
fo = open(self.sourcefile, mode='r', encoding='UTF-8')
fw = open(self.outputfile, mode='w')
dataline = fo.readline()#first line, Multi language, we dont use it
dataline = fo.readline()#second line, we got timebase (ns, us, ms, s, min)
base = ['ns','us','ms','s']
time = dataline.split(self.separator)[0][1:-1]
for i in range(len(base)):
if time == base[i]:
multi = 1000 ** i
dataline = fo.readline()#empty line
dataline = fo.readline()#first data line
dataline = dataline.replace(",", "." )#replace , with .
print('dataline:',dataline)
channel = dataline.count(self.separator)
timebase = dataline.split(self.separator)[0]
data = dataline.split(self.separator)
data[channel]=data[channel][:len(data[channel])-1] #remove \n
for i in range(channel):
data[i+1] = data[i+1] if len(data[i+1]) > 2 else '0'
string = '#' + str(round((float(data[0])-float(timebase))*multi)) + ' r' + data[1] + ' % '
if channel == 1:
string = string + '\n'
elif channel == 2:
string = string + 'r' + data[2] + ' & ' + '\n'
fw.writelines('$timescale 1 ns $end\n')
fw.writelines('$scope module libsigrok $end\n')
fw.writelines('$var real 64 % A0 $end\n')
if channel > 1:
fw.writelines('$var real 64 & A1 $end\n')
fw.writelines('$upscope $end\n')
fw.writelines('$enddefinitions $end\n')
fw.writelines(string)
linenum = 4
for line in fo:
line = line.replace(",", "." )#replace , with .
print('line:',line)
linenum = linenum + 1
data = line.split(self.separator)
data[channel]=data[channel][:len(data[channel])-1] #remove \n
if linenum == 5:
factor = str(round((float(data[0])-float(timebase))*multi))
for i in range(channel):
data[i+1] = data[i+1] if len(data[i+1]) > 2 else '0'
string = '#' + str(round((float(data[0])-float(timebase))*multi)) + ' r' + data[1] + ' % '
if channel == 1:
string = string + '\n'
elif channel == 2:
string = string + 'r' + data[2] + ' & ' + '\n'
fw.writelines(string)
fo.close()
fw.close()
result = 'Converter Finished!' + '\n' + 'Line:' + str(linenum) + '\n' + 'Suggest Downsampling Factor:' + factor
self.resultvar.set(result)
except Exception as e:
messagebox.showerror(title='Error!', message=e)
self.resultvar.set("Converter Failed!")
else:
messagebox.showwarning(title='Warning!', message='No file selected!')
if __name__ == "__main__":
app = MainWindow()