|
| 1 | +import csv |
| 2 | +import queue |
| 3 | +import re |
| 4 | +import threading |
| 5 | +from time import perf_counter |
| 6 | + |
| 7 | +import PySimpleGUI as sg |
| 8 | + |
| 9 | +import serial_comm as my_serial |
| 10 | + |
| 11 | + |
| 12 | +class Application: |
| 13 | + |
| 14 | + def __init__(self, *args, **kwargs): |
| 15 | + super(Application, self).__init__(*args, **kwargs) |
| 16 | + baud_rate = 115200 |
| 17 | + gui_queue = queue.Queue() |
| 18 | + serial_connector = my_serial.SerialObj(baud_rate) |
| 19 | + |
| 20 | + headerFont = ('Helvetica', 16) |
| 21 | + middleFont = ('Helvetica', 14) |
| 22 | + contextFont = ('Helvetica', 12) |
| 23 | + smallFont = ('Helvetica', 10) |
| 24 | + sg.theme('DarkBlue') |
| 25 | + |
| 26 | + layout = [[sg.Text('GET ACCELEROMETER GYROSCOPE\nSAMPLING DATA VIA SERIAL', font=headerFont)], |
| 27 | + [sg.Text('Select your serial port', font=contextFont), |
| 28 | + sg.Button('Serial Port Reload', size=(20, 1), font=smallFont)], |
| 29 | + [sg.Listbox(values=[x[0] for x in my_serial.SerialObj.get_ports()], |
| 30 | + size=(40, 6), |
| 31 | + key='_SERIAL_PORT_LIST_', |
| 32 | + font=contextFont, |
| 33 | + enable_events=True)], |
| 34 | + [sg.Text('', key='_SERIAL_PORT_CONFIRM_', size=(40, 1), font=middleFont, ), ], |
| 35 | + [sg.Text('Buad Rate: {} bps'.format(baud_rate), size=(40, 1), font=middleFont, ), ], |
| 36 | + [sg.Text('How many samples?', font=contextFont, ), sg.VerticalSeparator(), |
| 37 | + sg.Input(do_not_clear=True, enable_events=True, key='_SAMPLE_IN_', font=contextFont, )], |
| 38 | + |
| 39 | + [sg.HorizontalSeparator()], |
| 40 | + [sg.Text('Serial Comm Status', font=contextFont, pad=((6, 0), (20, 0))), ], |
| 41 | + [sg.Text('', key='_OUTPUT_', size=(40, 2), font=middleFont, ), ], |
| 42 | + [sg.Button('Start', key='_ACT_BUTTON_', font=middleFont, size=(40, 1), pad=((0, 0), (0, 0)))], |
| 43 | + [sg.Button('Exit', font=middleFont, size=(40, 1), pad=((0, 0), (20, 0)))], |
| 44 | + [sg.Text('ThatProject - Version: 0.1', justification='right', size=(50, 1), font=smallFont, ), ]] |
| 45 | + |
| 46 | + self.window = sg.Window('Simple Serial Application', layout, size=(320, 440), keep_on_top=True) |
| 47 | + |
| 48 | + while True: |
| 49 | + event, values = self.window.Read(timeout=100) |
| 50 | + |
| 51 | + if event is None or event == 'Exit': |
| 52 | + break |
| 53 | + |
| 54 | + if event == 'Serial Port Reload': |
| 55 | + self.get_ports() |
| 56 | + |
| 57 | + if event == '_SERIAL_PORT_LIST_': |
| 58 | + self.window['_SERIAL_PORT_CONFIRM_'].update(value=self.window['_SERIAL_PORT_LIST_'].get()[0]) |
| 59 | + |
| 60 | + if event == '_SAMPLE_IN_' and values['_SAMPLE_IN_'] and values['_SAMPLE_IN_'][-1] not in ('0123456789'): |
| 61 | + self.window['_SAMPLE_IN_'].update(values['_SAMPLE_IN_'][:-1]) |
| 62 | + |
| 63 | + if event == '_ACT_BUTTON_': |
| 64 | + print(self.window[event].get_text()) |
| 65 | + if self.window[event].get_text() == 'Start': |
| 66 | + |
| 67 | + if len(self.window['_SERIAL_PORT_LIST_'].get()) == 0: |
| 68 | + self.popup_dialog('Serial Port is not selected yet!', 'Serial Port', contextFont) |
| 69 | + |
| 70 | + elif len(self.window['_SAMPLE_IN_'].get()) == 0: |
| 71 | + self.popup_dialog('Set Sampling Count', 'Sampling Number Error', contextFont) |
| 72 | + |
| 73 | + else: |
| 74 | + self.stop_thread_trigger = False |
| 75 | + self.thread_serial = threading.Thread(target=self.start_serial_comm, |
| 76 | + args=(serial_connector, |
| 77 | + self.window[ |
| 78 | + '_SERIAL_PORT_LIST_'].get()[ |
| 79 | + 0], |
| 80 | + int(self.window[ |
| 81 | + '_SAMPLE_IN_'].get()), |
| 82 | + gui_queue, lambda: self.stop_thread_trigger), |
| 83 | + daemon=True) |
| 84 | + self.thread_serial.start() |
| 85 | + self.window['_ACT_BUTTON_'].update('Stop') |
| 86 | + |
| 87 | + else: |
| 88 | + self.stop_thread_trigger = True |
| 89 | + self.thread_serial.join() |
| 90 | + self.window['_ACT_BUTTON_'].update('Start') |
| 91 | + |
| 92 | + try: |
| 93 | + message = gui_queue.get_nowait() |
| 94 | + except queue.Empty: |
| 95 | + message = None |
| 96 | + if message is not None: |
| 97 | + self.window['_OUTPUT_'].Update(message) |
| 98 | + if 'Done' in message: |
| 99 | + self.window['_ACT_BUTTON_'].update('Start') |
| 100 | + self.popup_dialog(message, 'Success', contextFont) |
| 101 | + |
| 102 | + self.window.Close() |
| 103 | + |
| 104 | + def popup_dialog(self, contents, title, font): |
| 105 | + sg.Popup(contents, title=title, keep_on_top=True, font=font) |
| 106 | + |
| 107 | + def get_ports(self): |
| 108 | + self.window['_SERIAL_PORT_LIST_'].Update(values=[x[0] for x in my_serial.SerialObj.get_ports()]) |
| 109 | + |
| 110 | + def start_serial_comm(self, serial_connector, serialport, sample_num, gui_queue, stop_thread_trigger): |
| 111 | + |
| 112 | + start_time = 0 |
| 113 | + |
| 114 | + serial_connector.connect(serialport) |
| 115 | + if serial_connector.is_connect(): |
| 116 | + |
| 117 | + gui_queue.put('Serial Connected!!') |
| 118 | + |
| 119 | + n = 0 |
| 120 | + while n < sample_num: |
| 121 | + |
| 122 | + try: |
| 123 | + if stop_thread_trigger(): |
| 124 | + break |
| 125 | + |
| 126 | + data = serial_connector.get_data() |
| 127 | + if data is not None: |
| 128 | + |
| 129 | + if n == 0: |
| 130 | + gui_queue.put(' - Data Transmitting ::: Wait! ') |
| 131 | + start_time = perf_counter() |
| 132 | + |
| 133 | + decode_string = data.decode('utf-8') |
| 134 | + print(decode_string) |
| 135 | + if len(decode_string.split(',')) == 6: |
| 136 | + n += 1 |
| 137 | + percent = n / sample_num * 100 |
| 138 | + self.csv_writer('LoggedData_CalInertialAndMag.csv', n, decode_string) |
| 139 | + |
| 140 | + if percent % 10 == 0: |
| 141 | + gui_queue.put('Saving to CSV File: {}% complete'.format(int(percent))) |
| 142 | + |
| 143 | + except OSError as error: |
| 144 | + print(error) |
| 145 | + |
| 146 | + except UnicodeDecodeError as error: |
| 147 | + print(error) |
| 148 | + |
| 149 | + serial_connector.disconnect() |
| 150 | + time_taken = (perf_counter() - start_time) |
| 151 | + sampling_rate = sample_num / time_taken |
| 152 | + gui_queue.put('Sampling Rate: {} hz ::: Done!'.format(int(sampling_rate))) |
| 153 | + return |
| 154 | + |
| 155 | + def csv_writer(self, filename, index, data): |
| 156 | + with open(filename, 'a') as f: |
| 157 | + writer = csv.writer(f, delimiter=",", quoting=csv.QUOTE_NONE, escapechar=' ') |
| 158 | + writer.writerow([index, re.sub(r"\s+", "", data), 0, 0, |
| 159 | + 0]) # Dummy data for magnetometers, it doesn't use magnetometer in matlab. |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == '__main__': |
| 163 | + Application() |
0 commit comments