-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgui.py
608 lines (509 loc) · 24.7 KB
/
gui.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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
from PyQt6.QtWidgets import *
from PyQt6.QtCore import *
from PyQt6 import QtGui
import sys
from sys import platform
from writeWindow import *
import serial
from serial.tools.list_ports import comports
import time
import csv
from tools import *
from reader import *
class CustomComboBox(QComboBox):
# Define a custom signal
clicked = pyqtSignal()
def showPopup(self):
# Emit the custom signal before showing the dropdown
self.clicked.emit()
super().showPopup()
class Main(QWidget):
def __init__(self):
super().__init__()
self.debug = True
self.setWindowTitle('RFID Reader')
self.setGeometry(100, 100, 950, 500)
# create grid layout
self.layout = QGridLayout()
# set layout on window
self.setLayout(self.layout)
# timer stuff
self.available_update_rates = ["10", "25", "50", "100", "200", "300", "400", "500", "750", "1000", "1500", "2000"]
self.update_rate = 10
# Transmit power level stuff
self.available_power_levels = []
for i in range(0, 0x1C):
self.available_power_levels.append("{}dB".format(i-2))
self.selected_tx_power_level = -2
self.pwr_lvl_change = True
# Data table stuff
# TID
self.table_headers = ["ISO/IEC 15963 Tag", "XTID", "S bit", "F bit", "MDID", "Model", "XTID header", "38-Bit SN", "Times Read"]
self.xtid_labels = ["XTID Length", "Optional cmd Support", "block W/Erase Support", "User Data/Perma Lock Support",
"Lock Bit Support", "RFU Data", "TDS 2.0 Compliant XTID Header"]
self.table_headers_with_xtid = self.table_headers + self.xtid_labels
# EPC
self.epc_table_headers = ["Tag Data", "Times Read", "CRC From Tag", "Calculated CRC"]
self.current_table_headers = self.table_headers_with_xtid
# possible types are "B" for binary, "I" for int, and "D" for decoded
self.table_display_type = "D"
self.display_XTID_details = True
# serial stuff
# get list of devices (works for all platforms)
self.available_serial_devices = list(map(lambda com_device: com_device.name, comports()))
self.selected_device = None
self.baudrate = 38400
self.ser = None
# tag reading stuff
self.reader = None
self.unique_ids = []
self.tag_database = {}
# note: tag_database format for TID mode: {"bin_string":read_count}
# for EPC-multi mode: {"bin_string":[read_count, read_crc, calculated_crc]}
self.available_modes = ["TID","EPC-multi"]
self.selected_mode = "TID"
# bool to keep track of whether or not a read function is in process
# used to prevent overlap when spawning a new thread
self.reading = False
# init UI
self.initUI()
self.create_toolbar()
# Initialize timer
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_loop)
self.available_update_rates = ["10", "25", "50", "100", "200", "300", "400", "500", "750", "1000", "1500", "2000"]
def initUI(self):
######################################### First Row ############################
# grid width
width = 4
# adjust first row offset to account for menu bar (different for different OS)
if platform == "win32":
# windows
row = 1
elif platform == "darwin":
# osx
row = 0
elif platform == "linux":
row = 1
# ############## label for select serial device ##############
label = QLabel(self)
label.setFont(QtGui.QFont('Arial', 15))
label.setText("Select Serial Device:")
label.setAlignment(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
label.setMinimumWidth(150)
self.layout.addWidget(label, row,0)
# ############## select device dropdown ##############
self.device_select_box = CustomComboBox()
self.device_select_box.addItems(self.available_serial_devices)
self.device_select_box.activated.connect(self.update_selected_serial_device)
self.device_select_box.clicked.connect(self.refresh_serial_devices)
self.device_select_box.setMinimumWidth(220)
self.layout.addWidget(self.device_select_box, row, 1)
# ############## label for mode select ##############
label = QLabel(self)
label.setFont(QtGui.QFont('Arial', 15))
label.setText("Select Read Mode:")
label.setAlignment(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
label.setMinimumWidth(150)
self.layout.addWidget(label, row,2)
# mode select between TID, EPC single, EPC multi, and multi segment
# ############## Mode Selection ##############
self.read_mode_box = CustomComboBox()
self.read_mode_box.addItems(self.available_modes)
self.read_mode_box.activated.connect(self.update_selected_mode)
self.read_mode_box.setMinimumWidth(150)
self.layout.addWidget(self.read_mode_box, row, 3)
######################################### Second Row ############################
row += 1
# ############## label for output power select ##############
label = QLabel(self)
label.setFont(QtGui.QFont('Arial', 15))
label.setText("TX Power:")
label.setAlignment(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
label.setMinimumWidth(150)
self.layout.addWidget(label, row,0)
# mode select between TID, EPC single, EPC multi, and multi segment
# ############## Mode Selection ##############
self.tx_power_box = CustomComboBox()
self.tx_power_box.addItems(self.available_power_levels)
self.tx_power_box.activated.connect(self.update_tx_power_level)
self.tx_power_box.setMinimumWidth(220)
self.tx_power_box.setCurrentIndex(27) # set default value
self.layout.addWidget(self.tx_power_box, row, 1)
# ############## label for mode select ##############
label = QLabel(self)
label.setFont(QtGui.QFont('Arial', 15))
label.setText("Read Rate (ms):")
label.setAlignment(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
label.setMinimumWidth(150)
self.layout.addWidget(label, row,2)
# mode select between TID, EPC single, EPC multi, and multi segment
# ############## Mode Selection ##############
self.update_rate_box = CustomComboBox()
self.update_rate_box.addItems(self.available_update_rates)
self.update_rate_box.activated.connect(self.update_read_rate)
self.update_rate_box.setMinimumWidth(150)
self.update_rate_box.setCurrentIndex(3) # set default value
self.layout.addWidget(self.update_rate_box, row, 3)
######################################### Third Row ############################
# ############## section label ##############
row += 1
section_one_label = QLabel(self)
section_one_label.setMaximumHeight(30)
section_one_label.setText("Unique Tags")
section_one_label.setFont(QtGui.QFont('Arial', 20))
self.layout.addWidget(section_one_label, row,0)
# ############## export log button ##############
self.export_log_button = QPushButton(self, text='Export Log')
self.export_log_button.setStyleSheet(blue_button_style_shet)
self.export_log_button.clicked.connect(self.export_log)
self.layout.addWidget(self.export_log_button,row,1)
# ############## reset log button ##############
self.reset_log_button = QPushButton(self, text='Clear Log')
self.reset_log_button.setStyleSheet(red_button_style_shet)
self.reset_log_button.clicked.connect(self.clear_log)
self.layout.addWidget(self.reset_log_button,row,2)
# ############## button to start logging ##############
self.start_logging_button = QPushButton(self, text='Start Logging')
self.start_logging_button.setStyleSheet(green_button_style_shet)
self.start_logging_button.clicked.connect(self.start_log)
self.layout.addWidget(self.start_logging_button,row,3)
# ############## data table ##############
row += 1
self.data_table = QTableWidget()
self.layout.addWidget(self.data_table, row, 0, 1, width)
######################################### END block ############################
# Add vertical spacer at the end
# row += 1
# spacer = QSpacerItem(20, 40, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Expanding)
# self.layout.addItem(spacer, row, 0, 1, width)
# # Set row stretch for the last row
# self.layout.setRowStretch(1, 1)
def create_toolbar(self):
self.menubar = QMenuBar()
self.layout.addWidget(self.menubar, 0, 0)
# Create File menu
self.file_menu = self.menubar.addMenu("File")
# Add menu actions
open_action = QtGui.QAction("Open", self)
save_action = QtGui.QAction("Save", self)
self.file_menu.addAction(open_action)
self.file_menu.addAction(save_action)
self.file_menu.addSeparator()
self.file_read_submenu = self.file_menu.addMenu("Read Specific Tag")
monza_r6_read_action = QtGui.QAction("Monza R6", self)
self.file_read_submenu.addAction(monza_r6_read_action)
self.file_menu.addSeparator()
self.file_write_submenu = self.file_menu.addMenu("Write")
write_action = QtGui.QAction("Write", self)
self.file_write_submenu.addAction(write_action)
# connect actions to functions
write_action.triggered.connect(self.openWriteWindow)
def start_log(self):
print("logging started")
# start logging
try:
if platform == "darwin":
self.ser = serial.Serial("/dev/"+self.selected_device, self.baudrate, timeout=1)
elif platform == "linux":
self.ser = serial.Serial("/dev/"+self.selected_device, self.baudrate, timeout=1)
else:
self.ser = serial.Serial(self.selected_device, self.baudrate, timeout=1)
print("Serial interface opened")
print("device: {}".format(self.selected_device))
print("clearing input buffer")
self.ser.reset_input_buffer()
print("clearing output buffer")
self.ser.reset_output_buffer()
except Exception as error:
print("Failed to open {}".format(self.selected_device))
print("error message: {}".format(error))
return False
# change button to stop configuration
self.start_logging_button.setText('Stop Logging')
self.start_logging_button.setStyleSheet(red_button_style_shet)
# disconnect old connections
self.start_logging_button.clicked.disconnect()
self.start_logging_button.clicked.connect(self.stop_log)
self.start_logging_button.update()
# instantiate reader object
self.reader = Reader(self.ser)
self.reading = True
# start data collection
# Start timer to call update_label every Nms
self.timer.start(self.update_rate)
def update_loop(self):
"""
Read data from the reader every N seconds and then update the data
"""
############ Update Reader Settings If There Are Changes ##########
# update tx power level
if self.pwr_lvl_change:
if self.debug: print("updating power level")
success = self.reader.set_tx_power_level(self.selected_tx_power_level)
self.pwr_lvl_change = False
time.sleep(1) # this sleep is required. won't work othrwise
if self.debug: print("succes: {}".format(success))
print("Power level set to: {}".format(self.selected_tx_power_level))
############### if TID ###############
# collect new data
if self.selected_mode == "TID":
# read single TID section
tid_bank_data = self.reader.read_TID_bank()
# break if no tag was read
if tid_bank_data == False:
if self.debug: print("No Tag Detected")
return False
# convert int word list into binary string
bin_data = self.reader.convert_to_raw(tid_bank_data)
# update databsee
if bin_data in self.tag_database:
# tag already exists
if self.debug: print("Duplicate Tag Detected... Ignoring")
if self.debug: print("TID Data: {}".format(bin_data))
#increment tag read counter
self.tag_database[bin_data] += 1
else:
# tag is new
if self.debug: print("New tag detected... Adding to database")
if self.debug: print("TID Data: {}".format(bin_data))
# add new tag to db and set count to 1
self.tag_database[bin_data] = 1
# ############### if EPC-multi ###############
elif self.selected_mode == "EPC-multi":
if self.debug: print("Reading in EPC-Multi mode")
# read tags
epc_data = self.reader.multi_tag_EPC_read()
print(epc_data)
# break if no tag was read
if epc_data == False:
if self.debug: print("No Tag Detected")
return False
# convert the raw portion of the data to a binary string
for i in range(len(epc_data)):
epc_data[i][0] = self.reader.convert_to_raw(epc_data[i][0])
# update database for each tag that was read
for tag in epc_data:
# update databsee
if tag[0] in self.tag_database:
# tag already exists
if self.debug: print("Duplicate Tag Detected... Ignoring")
if self.debug: print("EPC Data: {}".format(tag))
#increment tag read counter
self.tag_database[tag[0]][0] += 1
else:
# tag is new
if self.debug: print("New tag detected... Adding to database")
if self.debug: print("EPC Data: {}".format(tag))
# add new tag to db and set count to 1
self.tag_database[tag[0]] = [1, tag[1], tag[2]]
self.update_data_table()
def stop_log(self):
print("logging stopped")
self.reading = False
# stop timer
self.timer.stop()
# change button to start configuration
self.start_logging_button.setText('Start Logging')
self.start_logging_button.setStyleSheet(green_button_style_shet)
# disconnect old connections
self.start_logging_button.clicked.disconnect()
self.start_logging_button.clicked.connect(self.start_log)
self.start_logging_button.update()
# closer serial interface
self.ser.close()
print("serial interface closed")
def clear_log(self):
"""
clear the log
"""
self.tag_database = {}
self.update_data_table()
def export_log(self):
"""
Save log to file
"""
print("Opening file save dialog")
file_name, _ = QFileDialog.getSaveFileName(self, "Save File", "/Users/taylor/Desktop", ".csv")
if file_name:
file_name += ".csv"
print("saving tag data to {}".format(file_name))
try:
with open(file_name, "x", newline="") as file:
writer = csv.writer(file)
# write the header
if self.selected_mode == "TID":
writer.writerow("TID_DATA")
print("file saved")
for element in self.tag_database:
writer.writerow(element)
file.close()
print("file closed")
except Exception as error:
print('file save failed')
print("Error: {}".format(error))
def update_data_table(self):
"""
This function takes the tag database and updates the displayed table to reflect the
tags in the database
"""
# do nothing if there is nothing in database
if len(self.tag_database) > 0:
if self.debug: print("database length: {}".format(len(self.tag_database)))
# Update table column count and headers
self.data_table.setColumnCount(len(self.current_table_headers))
self.data_table.setHorizontalHeaderLabels(self.current_table_headers)
# update number of rows
self.data_table.setRowCount(len(self.tag_database))
self.data_table.resizeColumnsToContents()
# update table based on current read mode
if self.selected_mode == "TID":
self.update_table_TID_mode()
elif self.selected_mode == "EPC-multi":
self.update_table_EPC_multi_mode()
# fit coumns to data
self.data_table.resizeColumnsToContents()
else:
# display empty table
self.data_table.setRowCount(0)
def update_table_TID_mode(self):
# for each element (tag) in self.tag_database, add that row and it's elements to the table as is (binary)
for row_index, row in enumerate(self.tag_database):
# ======= Display basic 48 bit TID header information ========
# convert the raw binary in the tag database to partitioned binary strings
segmented_TID_binary_data = segment_TID_data(True, row)
# decode row to human readable
interpreted_TID = interpret_lower_48_TID(segmented_TID_binary_data)
if self.table_display_type == "D":
segmented_TID_binary_data = interpreted_TID
for column_index, value in enumerate(segmented_TID_binary_data):
# updat table based on the correct display type
if self.table_display_type == "B":
self.data_table.setItem(row_index, column_index, QTableWidgetItem(value))
elif self.table_display_type == "I":
# convert to int (must be flipped since the string representation is LSB in elementn 0 and
# we need to have LSB in highest element)
int_value = str(int(value[::-1],2))
self.data_table.setItem(row_index, column_index, QTableWidgetItem(int_value))
elif self.table_display_type == "D":
item = QTableWidgetItem(str(value))
if value == "False":
item.setForeground(QtGui.QBrush(QtGui.QColor(224, 61, 61))) # Red
if value == "True":
item.setForeground(QtGui.QBrush(QtGui.QColor(42, 173, 48))) # Green
self.data_table.setItem(row_index, column_index, item)
# display serial number
column_index += 1
sn_bin = extract_serial_num(interpreted_TID, row)
sn = "Unknown Tag"
if type(sn_bin) is not type(None):
sn = str(int(sn_bin, 2))
item = QTableWidgetItem(str(sn))
item.setForeground(QtGui.QBrush(QtGui.QColor(66, 227, 245)))
self.data_table.setItem(row_index, column_index, item)
# display times read counter
column_index += 1
read_count = self.tag_database[row]
item = QTableWidgetItem(str(read_count))
self.data_table.setItem(row_index, column_index, item)
# ===== add additional columns as needed ======
if self.current_table_headers == self.table_headers_with_xtid:
column_index += 1
# get interpteted XTID data
interp_xtid_dat = interpret_XTID_header(segmented_TID_binary_data[6])
if self.debug: print("XTID segment data: {}".format(interp_xtid_dat))
for value in interp_xtid_dat:
item = QTableWidgetItem(str(value))
if value == "False":
item.setForeground(QtGui.QBrush(QtGui.QColor(224, 61, 61))) # Red
if value == "True":
item.setForeground(QtGui.QBrush(QtGui.QColor(42, 173, 48))) # Green
self.data_table.setItem(row_index, column_index, item)
column_index += 1
def update_table_EPC_multi_mode(self):
if self.debug: print("updating tabel from database")
# for each element (tag) in self.tag_database, add that row and it's elements to the table as is (binary)
for row_index, row in enumerate(self.tag_database):
print("Updating row {}".format(row_index))
# print("Row: {}".format(row))
# interpret_epc_data
raw = row
# print(row)
read_num = self.tag_database[row][0]
crc_read = self.tag_database[row][1]
crc_calc = self.tag_database[row][2]
# display raw and read count data
self.data_table.setItem(row_index, 0, QTableWidgetItem(str(hex(int(raw,2)).upper().replace('X', 'x'))))
self.data_table.setItem(row_index, 1, QTableWidgetItem(str(read_num)))
# display CRC data
if crc_calc == crc_read:
item_crc_read = QTableWidgetItem(str(hex(crc_read).upper().replace('X', 'x')))
item_crc_calc = QTableWidgetItem(str(hex(crc_calc).upper().replace('X', 'x')))
# green text
item_crc_read.setForeground(QtGui.QBrush(QtGui.QColor(42, 173, 48)))
item_crc_calc.setForeground(QtGui.QBrush(QtGui.QColor(42, 173, 48)))
# place on table
self.data_table.setItem(row_index, 2, item_crc_read)
self.data_table.setItem(row_index, 3, item_crc_calc)
else:
item_crc_read = QTableWidgetItem(str(hex(crc_read).upper().replace('X', 'x')))
item_crc_calc = QTableWidgetItem(str(hex(crc_calc).upper().replace('X', 'x')))
# red text
item_crc_read.setForeground(QtGui.QBrush(QtGui.QColor(224, 61, 61)))
item_crc_calc.setForeground(QtGui.QBrush(QtGui.QColor(224, 61, 61)))
# place on table
self.data_table.setItem(row_index, 2, item_crc_read)
self.data_table.setItem(row_index, 3, item_crc_calc)
def refresh_serial_devices(self):
# refresh the available serial devices
self.available_serial_devices = list(map(lambda com_device: com_device.name, comports()))
# update dropdown options
self.device_select_box.clear()
self.device_select_box.addItems(self.available_serial_devices)
print("updated device list")
def update_selected_serial_device(self):
# log selected device
self.selected_device = self.device_select_box.currentText()
print("selected device: {}".format(self.selected_device))
def update_selected_mode(self):
self.selected_mode = self.read_mode_box.currentText()
print("Selected mode: {}".format(self.selected_mode))
# update table headers
if self.selected_mode == "TID":
self.current_table_headers = self.table_headers_with_xtid
elif self.selected_mode == "EPC-multi":
self.current_table_headers = self.epc_table_headers
# clear table and database
self.clear_log()
def update_read_rate(self):
# update read rate interval
self.update_rate = int(self.update_rate_box.currentText())
self.timer.setInterval(self.update_rate)
print("Read Rate (ms): {}".format(self.update_rate))
def update_tx_power_level(self):
# update transmit power levels
self.selected_tx_power_level = int(self.tx_power_box.currentText().replace("dB", ""))
print("Changing TX power level to {}dB".format(self.selected_tx_power_level))
self.pwr_lvl_change = True
def openWriteWindow(self):
# stop logging if needed
if self.reading:
self.stop_log()
# Create an instance of the writer window
self.read_window = writeWindow()
self.read_window.show()
# window that has analysis options
class multi_analysis_popup(QWidget):
def __init__(self):
self.a = True
def initUI(self):
# spectrum view button
# mix at some frequency
# display error bars
return True
if __name__ == "__main__":
app = QApplication([])
app.setWindowIcon(QtGui.QIcon('deep_fried_burch.png'))
mainWindow = Main()
mainWindow.show()
sys.exit(app.exec())