This commit is contained in:
david rice
2026-01-19 16:00:19 +00:00
parent e1423fc4c3
commit 4490dd803a
5 changed files with 136 additions and 67 deletions

96
main.py
View File

@@ -12,7 +12,7 @@ AUTHOR: D. RICE 06/01/2026
# Imports
import sys
from PySide6.QtWidgets import (QApplication, QDialog, QMainWindow)
from PySide6.QtCore import (QIODevice, QByteArray)
from PySide6.QtCore import (QIODevice, QByteArray, QTimer)
from PySide6.QtSerialPort import (QSerialPort, QSerialPortInfo)
import time
@@ -65,14 +65,16 @@ class MainWindow(QMainWindow):
# Hold process reference.
self.p = None
# Thread and Worker initialisation
self.worker_thread = None
self.worker = None
# Add start button action
self.ui.connButton.setCheckable(True)
self.ui.connButton.clicked.connect(self.conn_button_press)
# Hide other controls
self.ui.sn.setHidden(True)
self.ui.snLabel.setHidden(True)
self.ui.fw.setHidden(True)
self.ui.fwLabel.setHidden(True)
# Set initial size
initial_width = 1000
initial_height = 600
@@ -95,6 +97,20 @@ class MainWindow(QMainWindow):
# Create serial rx buffer
self.receive_buffer = bytearray()
# Create store buffer
self.store_buffer = bytearray()
# Create a timeout counter flag
self.timeoutflag = False
# Create a window response variable
self.windowresponse = False
# Setup QT timers
self.timeouttimer = QTimer()
self.timeouttimer.setInterval(5000)
self.timeouttimer.timeout.connect(self.time_out_timer)
self.show()
def conn_button_press (self):
@@ -122,12 +138,40 @@ class MainWindow(QMainWindow):
self.serial.write(QByteArray(byte_data))
while (self.serial_rx_flag == False):
QApplication.processEvents()
self.timeouttimer.start()
while ((self.serial_rx_flag == False) and (self.timeoutflag == False)):
QApplication.processEvents()
w.close()
if self.timeoutflag == True:
self.windowresponse = NoDeviceWindow.get_response(self)
self.serial.close()
self.ui.connButton.setText("CONNECT")
self.ui.sn.setHidden(True)
self.ui.snLabel.setHidden(True)
self.ui.fw.setHidden(True)
self.ui.fwLabel.setHidden(True)
self.ui.sn.setText("---")
self.ui.fw.setText("---")
else:
self.process_serial_number()
self.serial_rx_flag = False
self.timeoutflag = False
else:
self.serial.close()
self.ui.connButton.setText("CONNECT")
self.ui.connButton.setText("CONNECT")
self.ui.sn.setHidden(True)
self.ui.snLabel.setHidden(True)
self.ui.fw.setHidden(True)
self.ui.fwLabel.setHidden(True)
self.ui.sn.setText("---")
self.ui.fw.setText("---")
QApplication.processEvents()
@@ -167,21 +211,42 @@ class MainWindow(QMainWindow):
def handle_valid_packet(self, packet):
# Checksum validity check
try:
print (packet)
data_bytes = list(packet[3 : -2])
received_checksum = (packet[-2] << 8) | packet[-1]
if((~sum(data_bytes)) & 0xFFFF) == received_checksum:
print(f"RX DATA: {data_bytes}")
self.serial_rx_flag = True
self.store_buffer = data_bytes
else:
pass
except Exception as e:
pass
pass
def process_serial_number(self):
# Get and update serial number
sub_sn = self.store_buffer[0:19]
sn_chars = [chr(c) for c in sub_sn if 0 <= c <= 127]
sn_str = "".join(sn_chars)
self.ui.sn.setText(sn_str)
# Get and update FW Rev
sub_fw = self.store_buffer[19:22]
major = sub_fw[1:2]
sep = sub_fw[0:1]
minor = sub_fw[2:3]
full = major + sep + minor
fw_chars = [chr(c) for c in full if 0 <= c <= 127]
fw_str = "".join(fw_chars)
self.ui.fw.setText(fw_str)
self.ui.sn.setHidden(False)
self.ui.snLabel.setHidden(False)
self.ui.fw.setHidden(False)
self.ui.fwLabel.setHidden(False)
def pack_integers_to_bytes(self, *integers: int) -> bytes:
# Setup header
@@ -211,11 +276,10 @@ class MainWindow(QMainWindow):
# We need as many 'B's as there are elements in the full packet
format_string = f"!{len(full_packet)}B"
return struct.pack(format_string, *full_packet)
def run_scope_setup (self):
self.worker.run_setup()
return struct.pack(format_string, *full_packet)
def time_out_timer(self):
self.timeoutflag = True
# Run main
if __name__ == '__main__':