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 = int(time.time() * 1000) 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 = int(time.time() * 1000) # print(1 - (end_cap - start_cap) / 1000) try: time.sleep(1 - (end_cap - start_cap) / 1000) except Exception: pass
def pseudo_live(values, window): global thread_cap global zscore_array global index_number_array thread_cap = True sample_value = int(values["live_bit_count"]) interval_value = int(values["live_time_count"]) file_name = time.strftime( f"%Y%m%d-%H%M%S_pseudo_s{sample_value}_i{interval_value}") file_name = f"1-SavedFiles/{file_name}" index_number = 0 csv_ones = [] zscore_array = [] index_number_array = [] blocksize = int(sample_value / 8) while thread_cap: start_cap = time.time() index_number += 1 with open(file_name + '.bin', "ab+") as bin_file: # save binary file try: chunk = secrets.token_bytes(blocksize) # read bytes except Exception: thread_cap = False rm.popupmsg("Warning!", "Read Failed!!!") window['live_plot'].update("Start") window["stat_live"].update(" Idle", text_color="orange") window['ac_button'].update("Start") window["stat_ac"].update(" Idle", text_color="orange") return bin_file.write(chunk) bin_hex = BitArray(chunk) # bin to hex bin_ascii = bin_hex.bin # hex to ASCII # count numbers of ones in the 2048 string num_ones_array = int(bin_ascii.count('1')) csv_ones.append(num_ones_array) sums_csv = sum(csv_ones) avrg_csv = sums_csv / index_number zscore_csv = (avrg_csv - (sample_value / 2)) / \ (((sample_value / 4) ** 0.5) / (index_number ** 0.5)) zscore_array.append(zscore_csv) index_number_array.append(index_number) # open file and append time and number of ones with open(file_name + '.csv', "a+") as write_file: write_file.write( f'{strftime("%H:%M:%S", localtime())} {num_ones_array}\n') end_cap = time.time() # print(interval_value - (end_cap - start_cap) / 1000) try: time.sleep(interval_value - (end_cap - start_cap)) except Exception: pass
def pseudo_cap(values, window): global thread_cap sample_value = int(values["ac_bit_count"]) interval_value = int(values["ac_time_count"]) blocksize = int(sample_value / 8) file_name = time.strftime( f"%Y%m%d-%H%M%S_pseudo_s{sample_value}_i{interval_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 try: x = secrets.token_bytes( 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") window['live_plot'].update("Start") window["stat_live"].update(" Idle", text_color="orange") break bin_file.write(x) bin_hex = BitArray(x) # bin to hex bin_ascii = bin_hex.bin # hex to ASCII # count numbers of ones in the string num_ones_array = bin_ascii.count('1') # open file and append time and number of ones with open(file_name + '.csv', "a+") as write_file: write_file.write( f'{strftime("%H:%M:%S", localtime())} {num_ones_array}\n') end_cap = time.time() # print(interval_value - (end_cap - start_cap)) try: time.sleep(interval_value - (end_cap - start_cap)) except Exception: pass
def trng3live(values, window): global thread_cap global zscore_array global index_number_array thread_cap = True sample_value = int(values["live_bit_count"]) interval_value = int(values["live_time_count"]) file_name = time.strftime( f"%Y%m%d-%H%M%S_trng_s{sample_value}_i{interval_value}") file_name = f"1-SavedFiles/{file_name}" index_number = 0 csv_ones = [] zscore_array = [] index_number_array = [] blocksize = int(sample_value / 8) 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_cap: start_cap = time.time() index_number += 1 with open(file_name + '.bin', "ab+") as bin_file: # save binary file try: # timeout set at 10 seconds in case the read fails ser = serial.Serial(port=rng_com_port, timeout=10) except Exception: thread_cap = 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") window['ac_button'].update("Start") window["stat_ac"].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_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['live_plot'].update("Start") window["stat_live"].update(" Idle", text_color="orange") window['ac_button'].update("Start") window["stat_ac"].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_cap = False rm.popupmsg("Warning!", "Read Failed!!!") window['live_plot'].update("Start") window["stat_live"].update(" Idle", text_color="orange") window['ac_button'].update("Start") window["stat_ac"].update(" Idle", text_color="orange") return 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 # count numbers of ones in the 2048 string num_ones_array = int(bin_ascii.count('1')) csv_ones.append(num_ones_array) sums_csv = sum(csv_ones) avrg_csv = sums_csv / index_number zscore_csv = (avrg_csv - (sample_value / 2)) / \ (((sample_value / 4) ** 0.5) / (index_number ** 0.5)) zscore_array.append(zscore_csv) index_number_array.append(index_number) # open file and append time and number of ones with open(file_name + '.csv', "a+") as write_file: write_file.write( f'{strftime("%H:%M:%S", localtime())} {num_ones_array}\n') end_cap = time.time() # print(interval_value - (end_cap - start_cap) / 1000) try: time.sleep(interval_value - (end_cap - start_cap)) except Exception: pass
def trng3_cap(values, window): global thread_cap sample_value = int(values["ac_bit_count"]) interval_value = int(values["ac_time_count"]) blocksize = int(sample_value / 8) 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( f"%Y%m%d-%H%M%S_trng_s{sample_value}_i{interval_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 try: # timeout set at 10 seconds in case the read fails ser = serial.Serial(port=rng_com_port, timeout=10) 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") window['live_plot'].update("Start") window["stat_live"].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") window['live_plot'].update("Start") window["stat_live"].update(" Idle", text_color="orange") break bin_file.write(x) ser.close() bin_hex = BitArray(x) # bin to hex bin_ascii = bin_hex.bin # hex to ASCII # count numbers of ones in the string num_ones_array = bin_ascii.count('1') # open file and append time and number of ones with open(file_name + '.csv', "a+") as write_file: write_file.write( f'{strftime("%H:%M:%S", localtime())} {num_ones_array}\n') end_cap = time.time() # print(interval_value - (end_cap - start_cap)) try: time.sleep(interval_value - (end_cap - start_cap)) except Exception: pass