-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrngkitpsg.py
More file actions
375 lines (342 loc) · 16 KB
/
rngkitpsg.py
File metadata and controls
375 lines (342 loc) · 16 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# Default imports
import time
import threading
import subprocess
from time import localtime, strftime
# External imports
import pandas as pd
import PySimpleGUI as sg
import matplotlib.pyplot as plt
from matplotlib import style
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from bitstring import BitArray
import serial
from serial.tools import list_ports
# Internal imports
import rng_module as rm
global thread_live
thread_live = False
global thread_cap
thread_cap = False
global index_number_array
index_number_array = []
global zscore_array
zscore_array = []
def main():
# Mensagem para versão console
print("""Welcome!
Wait for the application to load!
Do not close this window!""")
with open("src/instructions.txt", "r", encoding="utf8") as f:
texto = f.read()
# THEME
# Good Ones: DarkBlue14, Dark, DarkBlue, DarkBlue3, DarkTeal1, DarkTeal10, DarkTeal9, LightGreen
sg.theme('DarkBlue')
# TAB 1 - Capture / Analyse
acquiring_data = [[sg.T("Choose RNG", size=(16, 1)), sg.T("RAW(0)/XOR(1,2...)", size=(20, 1))],
[sg.Radio('BitBabbler', "radio_graph_1", k="bit_ac", default=True, size=(19, 1)),
sg.InputCombo((0, 1, 2, 3, 4), default_value=0, size=(4, 1), k="ac_combo", enable_events=False,
readonly=True), sg.T("", size=(4, 1)), sg.B("Start", k='ac_button', size=(20, 1))],
[sg.Radio('TrueRNG', "radio_graph_1", k="true3_ac", size=(36, 1)),
sg.T(" Idle", k="stat_ac", text_color="orange", size=(10, 1), relief="sunken")],
[sg.Radio('TrueRNG + BitBabbler', "radio_graph_1", k="true3_bit_ac", size=(20, 1))]]
data_analysis = [[sg.Text('Select file:'), sg.Input(),
sg.FileBrowse(key='open_file', file_types=(('CSV and Binary', '.csv .bin'),),
initial_folder="./1-SavedFiles")],
[sg.B("Generate"), sg.B("Open Output Folder", k="out_folder")]]
tab1_layout = [[sg.Frame("Acquiring Data", layout=acquiring_data, k="acquiring_data", size=(90, 9))],
[sg.Frame("Data Analysis", layout=data_analysis, k="data_analysis", size=(90, 9))]]
# TAB 2 - Gráfico
graph_options = [[sg.T("Choose RNG", size=(20, 1)), sg.T("RAW/XOR", size=(20, 1))],
[sg.Radio('BitBabbler', "radio_graph", k="bit_live", default=True, size=(19, 1)),
sg.InputCombo((0, 1), default_value=0, size=(4, 1), k="live_combo", enable_events=False,
readonly=True), sg.T("", size=(2, 1)), sg.B("Start", k='live_plot', size=(20, 1))],
[sg.Radio('TrueRNG3', "radio_graph", k="true3_live", size=(20, 1)),
sg.T(" Idle", k="stat_live", text_color="orange", size=(10, 1), relief="sunken")]]
live_graph = [[sg.Canvas(key='-CANVAS-')]]
tab2_layout = [[sg.Frame("Options", layout=graph_options, k="graph_options", size=(90, 9))],
[sg.Frame("Live Plot", layout=live_graph, k="graph", size=(90, 9))]]
# TAB 3 - Instruções
tab3_layout = [[sg.T("Instructions", relief="raised", justification="center", size=(70, 1), font=("Calibri, 24"))],
[sg.Multiline(default_text=texto, size=(75, 19), disabled=True, enable_events=False,
font=("Calibri, 20"), pad=(5, 5))]]
# LAYOUT
layout = [[sg.TabGroup(
[[sg.Tab('Start', tab1_layout), sg.Tab('Live Plot', tab2_layout), sg.Tab('Instructions', tab3_layout)]],
tab_location="top", font="Calibri, 18")]]
# WINDOW
window = sg.Window("RngKit ver 2.0.0 - by Thiago Jung", layout, size=(1024, 720), location=(50, 50), finalize=True,
element_justification="center", font="Calibri 18", resizable=True, icon=("src/BitB.ico"))
# Setting things up!
canvas_elem = window['-CANVAS-']
canvas = canvas_elem.TKCanvas
# draw the intitial plot
style.use("ggplot")
fig, ax = plt.subplots(figsize=(10, 5), dpi=100)
fig_agg = rm.draw_figure(canvas, fig)
# LOOP
while True:
event, values = window.read(timeout=200)
if event == sg.WIN_CLOSED: # always, always give a way out!
break
elif event == 'ac_button':
global thread_cap
if not thread_cap:
thread_cap = True
threading.Thread(target=ac_data, args=(values, window), daemon=True).start()
window['ac_button'].update("Stop")
window["stat_ac"].update(" Capturing", text_color="green")
else:
thread_cap = False
window['ac_button'].update("Start")
window["stat_ac"].update(" Idle", text_color="orange")
elif event == "out_folder":
rm.open_folder()
elif event == "Generate":
rm.file_to_excel(values["open_file"])
elif event == 'live_plot':
global thread_live
if not thread_live:
thread_live = True
ax.clear()
threading.Thread(target=live_plot, args=(values, window), daemon=True).start()
window['live_plot'].update("Stop")
window["stat_live"].update(" Capturing", text_color="green")
else:
thread_live = False
window['live_plot'].update("Start")
window["stat_live"].update(" Idle", text_color="orange")
# Live Plot on Loop
ax.plot(index_number_array, zscore_array, color='orange')
ax.set_title("Live Plot")
ax.set_xlabel('Time(s)', fontsize=10)
ax.set_ylabel('Z-Score', fontsize='medium')
fig_agg.draw()
window.close()
# ---------------- Acquire Data Functions -------
def ac_data(values, window):
if values["bit_ac"]:
bit_cap(values, window)
elif values['true3_ac']:
trng3_cap(window)
elif values["true3_bit_ac"]:
threading.Thread(target=bit_cap, args=(values, window), daemon=True).start()
trng3_cap(window)
def bit_cap(values, window): # criar função para quando o botão for clicado
xor_value = values["ac_combo"]
global thread_cap
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
file_name = time.strftime("%Y%m%d-%H%M%S_bitb_f{}".format(xor_value))
file_name = f"1-SavedFiles/{file_name}"
while thread_cap:
start_cap = time.time()
with open(file_name + '.bin', "ab+") as bin_file: # save binary file
proc = subprocess.run(f'datafiles/seedd.exe --limit-max-xfer --no-qa -f{xor_value} -b 256',
stdout=subprocess.PIPE)
chunk = proc.stdout
bin_file.write(chunk)
bin_hex = BitArray(chunk) # bin to hex
bin_ascii = bin_hex.bin # hex to ASCII
if not bin_ascii:
thread_cap = False
sg.popup_non_blocking('WARNING !!!',
"Something went wrong, is the device attached? Attach it and try again!!!",
keep_on_top=True, no_titlebar=False, grab_anywhere=True, font="Calibri, 18",
icon="src/BitB.ico")
window['ac_button'].update("Start")
window["stat_ac"].update(" Idle", text_color="orange")
break
num_ones_array = bin_ascii.count('1') # count numbers of ones in the 2048 string
with open(file_name + '.csv', "a+") as write_file: # open file and append time and number of ones
write_file.write('{} {}\n'.format(strftime("%H:%M:%S", localtime()), num_ones_array))
end_cap = time.time()
# print(1 - (end_cap - start_cap))
try:
time.sleep(1 - (end_cap - start_cap))
except Exception:
pass
def trng3_cap(window):
global thread_cap
blocksize = 256
ports = dict()
ports_avaiable = list(list_ports.comports())
rng_com_port = None
for temp in ports_avaiable:
if temp[1].startswith("TrueRNG"):
if rng_com_port == None: # always chooses the 1st TrueRNG found
rng_com_port = str(temp[0])
file_name = time.strftime("%Y%m%d-%H%M%S_trng")
file_name = f"1-SavedFiles/{file_name}"
while thread_cap:
start_cap = time.time()
with open(file_name + '.bin', "ab") as bin_file: # save binary file
try:
ser = serial.Serial(port=rng_com_port, timeout=10) # timeout set at 10 seconds in case the read fails
if (ser.isOpen() == False):
ser.open()
ser.setDTR(True)
ser.flushInput()
except Exception:
rm.popupmsg("Warning!", f"Port Not Usable! Do you have permissions set to read {rng_com_port}?")
thread_cap = False
window['ac_button'].update("Start")
window["stat_ac"].update(" Idle", text_color="orange")
break
try:
x = ser.read(blocksize) # read bytes from serial port
except Exception:
rm.popupmsg("Warning!", "Read failed!")
thread_cap = False
window['ac_button'].update("Start")
window["stat_ac"].update(" Idle", text_color="orange")
break
if bin_file != 0:
bin_file.write(x)
ser.close()
if bin_file != 0:
bin_file.close()
bin_hex = BitArray(x) # bin to hex
bin_ascii = bin_hex.bin # hex to ASCII
num_ones_array = bin_ascii.count('1') # count numbers of ones in the 2048 string
with open(file_name + '.csv', "a+") as write_file: # open file and append time and number of ones
write_file.write('{} {}\n'.format(strftime("%H:%M:%S", localtime()), num_ones_array))
end_cap = time.time()
# print(1 - (end_cap - start_cap))
try:
time.sleep(1 - (end_cap - start_cap))
except Exception:
pass
# ----------------Live Plot Functions------------
def live_plot(values, window):
if values['bit_live']:
livebblaWin(values, window)
elif values['true3_live']:
trng3live(window)
def livebblaWin(values, window): # Function to take live data from bitbabbler
global thread_live
global zscore_array
global index_number_array
thread_live = True
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
live_combo_value = values['live_combo']
file_name = time.strftime("%Y%m%d-%H%M%S_bitb_f{}".format(live_combo_value))
file_name = f"1-SavedFiles/{file_name}"
index_number = 0
csv_ones = []
zscore_array = []
index_number_array = []
while thread_live:
start_cap = time.time()
index_number += 1
with open(file_name + '.bin', "ab+") as bin_file: # save binary file
proc = subprocess.run(f'datafiles/seedd.exe --limit-max-xfer --no-qa -f{live_combo_value} -b 256',
stdout=subprocess.PIPE)
chunk = proc.stdout
bin_file.write(chunk)
bin_hex = BitArray(chunk) # bin to hex
bin_ascii = bin_hex.bin # hex to ASCII
if not bin_ascii:
thread_live = False
sg.popup_non_blocking('WARNING !!!',
"Something went wrong, is the device attached? Attach it and try again!!!",
keep_on_top=True, no_titlebar=False, grab_anywhere=True, font="Calibri, 18",
icon="src/BitB.ico")
window['live_plot'].update("Start")
window["stat_live"].update(" Idle", text_color="orange")
break
num_ones_array = bin_ascii.count('1') # count numbers of ones in the 2048 string
csv_ones.append(num_ones_array)
sums_csv = sum(csv_ones)
avrg_csv = sums_csv / index_number
zscore_csv = (avrg_csv - 1024) / (22.62741699796 / (index_number ** 0.5))
zscore_array.append(zscore_csv)
index_number_array.append(index_number)
with open(file_name + '.csv', "a+") as write_file: # open file and append time and number of ones
write_file.write('{} {}\n'.format(strftime("%H:%M:%S", localtime()), num_ones_array))
end_cap = time.time()
# print(1 - (end_cap - start_cap))
try:
time.sleep(1 - (end_cap - start_cap))
except Exception:
pass
def trng3live(window):
global thread_live
global zscore_array
global index_number_array
thread_live = True
file_name = time.strftime("%Y%m%d-%H%M%S_trng")
file_name = f"1-SavedFiles/{file_name}"
index_number = 0
csv_ones = []
zscore_array = []
index_number_array = []
blocksize = 256
ports_avaiable = list(list_ports.comports())
rng_com_port = None
# Loop on all available ports to find TrueRNG
for temp in ports_avaiable:
if temp[1].startswith("TrueRNG"):
if rng_com_port == None: # always chooses the 1st TrueRNG found
rng_com_port = str(temp[0])
while thread_live:
start_cap = time.time()
index_number += 1
with open(file_name + '.bin', "ab+") as bin_file: # save binary file
try:
ser = serial.Serial(port=rng_com_port, timeout=10) # timeout set at 10 seconds in case the read fails
except Exception:
thread_live = False
rm.popupmsg("Warning!", f"Port Not Usable! Do you have permissions set to read {rng_com_port}?")
window['live_plot'].update("Start")
window["stat_live"].update(" Idle", text_color="orange")
return
# Open the serial port if it isn't open
if (ser.isOpen() == False):
try:
ser.open()
except Exception:
thread_live = False
sg.popup_non_blocking('WARNING !!!',
"Something went wrong, is the device attached? Attach it and try again!!!",
keep_on_top=True, no_titlebar=False, grab_anywhere=True, font="Calibri, 18",
icon="src/BitB.ico")
window['live_plot'].update("Start")
window["stat_live"].update(" Idle", text_color="orange")
return
# Set Data Terminal Ready to start flow
ser.setDTR(True)
# This clears the receive buffer so we aren't using buffered data
ser.flushInput()
try:
chunk = ser.read(blocksize) # read bytes from serial port
except Exception:
thread_live = False
rm.popupmsg("Warning!", "Read Failed!!!")
window['live_plot'].update("Start")
window["stat_live"].update(" Idle", text_color="orange")
return
if bin_file != 0:
bin_file.write(chunk)
# Close the serial port
ser.close()
bin_hex = BitArray(chunk) # bin to hex
bin_ascii = bin_hex.bin # hex to ASCII
num_ones_array = int(bin_ascii.count('1')) # count numbers of ones in the 2048 string
csv_ones.append(num_ones_array)
sums_csv = sum(csv_ones)
avrg_csv = sums_csv / index_number
zscore_csv = (avrg_csv - 1024) / (22.62741699796 / (index_number ** 0.5))
zscore_array.append(zscore_csv)
index_number_array.append(index_number)
with open(file_name + '.csv', "a+") as write_file: # open file and append time and number of ones
write_file.write('{} {}\n'.format(strftime("%H:%M:%S", localtime()), num_ones_array))
end_cap = time.time()
# print(1 - (end_cap - start_cap) / 1000)
try:
time.sleep(1 - (end_cap - start_cap))
except Exception:
pass
if __name__ == '__main__':
main()