Working Serial RX
This commit is contained in:
109
main.py
109
main.py
@@ -12,8 +12,8 @@ AUTHOR: D. RICE 06/01/2026
|
|||||||
# Imports
|
# Imports
|
||||||
import sys
|
import sys
|
||||||
from PySide6.QtWidgets import (QApplication, QDialog, QMainWindow)
|
from PySide6.QtWidgets import (QApplication, QDialog, QMainWindow)
|
||||||
from PySide6.QtCore import (QThread, QObject, Signal, Slot)
|
from PySide6.QtCore import (QIODevice, QByteArray)
|
||||||
from PySide6.QtSerialPort import (QserialPort, QSerialPortInfo)
|
from PySide6.QtSerialPort import (QSerialPort, QSerialPortInfo)
|
||||||
|
|
||||||
import time
|
import time
|
||||||
import struct
|
import struct
|
||||||
@@ -22,7 +22,6 @@ from ui_mainwindow import Ui_MainWindow
|
|||||||
from ui_scanning import Ui_scanningDialog
|
from ui_scanning import Ui_scanningDialog
|
||||||
from ui_connerror import Ui_connerrorDialog
|
from ui_connerror import Ui_connerrorDialog
|
||||||
|
|
||||||
|
|
||||||
# Scanning Window Class
|
# Scanning Window Class
|
||||||
class ScanningWindow(QDialog):
|
class ScanningWindow(QDialog):
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
@@ -86,12 +85,19 @@ class MainWindow(QMainWindow):
|
|||||||
|
|
||||||
self.setFixedSize(1000, 600)
|
self.setFixedSize(1000, 600)
|
||||||
|
|
||||||
|
# Setup serial port
|
||||||
|
self.serial = QSerialPort(self)
|
||||||
|
self.serial.readyRead.connect(self.on_data_received)
|
||||||
|
|
||||||
|
# Create serial rx flag
|
||||||
|
self.serial_rx_flag = False
|
||||||
|
|
||||||
|
# Create serial rx buffer
|
||||||
|
self.receive_buffer = bytearray()
|
||||||
|
|
||||||
self.show()
|
self.show()
|
||||||
|
|
||||||
def conn_button_press (self):
|
def conn_button_press (self):
|
||||||
global IDN_Response_flag
|
|
||||||
global Error_flag
|
|
||||||
|
|
||||||
text_conn = self.ui.connButton.text()
|
text_conn = self.ui.connButton.text()
|
||||||
|
|
||||||
if text_conn == "CONNECT":
|
if text_conn == "CONNECT":
|
||||||
@@ -102,52 +108,81 @@ class MainWindow(QMainWindow):
|
|||||||
|
|
||||||
QApplication.processEvents()
|
QApplication.processEvents()
|
||||||
|
|
||||||
# Setup serial port
|
# Specify serial port and baud
|
||||||
|
self.serial.setPortName("/dev/ttyACM0")
|
||||||
ser = self.serial.Serial(port="/dev/ttyACM0", baudrate=921600)
|
self.serial.setBaudRate(115200)
|
||||||
|
|
||||||
|
if not self.serial.open(QIODevice.ReadWrite):
|
||||||
|
print(f"Failed to open port: {self.serial.errorString()}")
|
||||||
|
|
||||||
command = 73 #0x49 'I' ASCII
|
command = 73 #0x49 'I' ASCII
|
||||||
state = 0
|
state = 0
|
||||||
data = (command, state)
|
data = (command, state)
|
||||||
byte_data = self.pack_integers_to_bytes(*data)
|
byte_data = self.pack_integers_to_bytes(*data)
|
||||||
|
|
||||||
|
self.serial.write(QByteArray(byte_data))
|
||||||
|
|
||||||
while ((IDN_Response_flag == False) and (Error_flag == False)):
|
while (self.serial_rx_flag == False):
|
||||||
QApplication.processEvents()
|
|
||||||
|
|
||||||
if IDN_Response_flag == True:
|
|
||||||
|
|
||||||
w.close()
|
|
||||||
IDN_Response_flag = False
|
|
||||||
Error_flag = False
|
|
||||||
self.run_scope_setup()
|
|
||||||
|
|
||||||
if Error_flag == True:
|
|
||||||
response = NoDeviceWindow.get_response(self)
|
|
||||||
self.ui.connButton.setText("CONNECT")
|
|
||||||
self.closeEvent()
|
|
||||||
|
|
||||||
self.ui.manu.setText("---")
|
|
||||||
self.ui.model.setText("---")
|
|
||||||
self.ui.sn.setText("---")
|
|
||||||
self.ui.fw.setText("---")
|
|
||||||
|
|
||||||
IDN_Response_flag = False
|
|
||||||
Error_flag = False
|
|
||||||
|
|
||||||
QApplication.processEvents()
|
QApplication.processEvents()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
self.serial.close()
|
||||||
self.ui.connButton.setText("CONNECT")
|
self.ui.connButton.setText("CONNECT")
|
||||||
self.closeEvent()
|
|
||||||
self.ui.manu.setText("---")
|
|
||||||
self.ui.model.setText("---")
|
|
||||||
self.ui.sn.setText("---")
|
|
||||||
self.ui.fw.setText("---")
|
|
||||||
|
|
||||||
QApplication.processEvents()
|
QApplication.processEvents()
|
||||||
|
|
||||||
|
def on_data_received(self):
|
||||||
|
# Triggered whenever new bytes are received
|
||||||
|
# Read all available data and add to buffer
|
||||||
|
new_data = self.serial.readAll().data()
|
||||||
|
self.receive_buffer.extend(new_data)
|
||||||
|
|
||||||
|
# Process buffer to find packets
|
||||||
|
self.process_buffer()
|
||||||
|
|
||||||
|
def process_buffer(self):
|
||||||
|
# Look for sync bytes 0x41 & 0x52
|
||||||
|
while len(self.receive_buffer) >= 5: # Minimum posible packet size
|
||||||
|
# Look for sync bytes
|
||||||
|
if self.receive_buffer[0] == 0x41 and self.receive_buffer[1] == 0x52:
|
||||||
|
data_len = self.receive_buffer[2]
|
||||||
|
total_packet_size = 3 + data_len + 2
|
||||||
|
|
||||||
|
# Is it a full packet yet?
|
||||||
|
if len(self.receive_buffer) >= total_packet_size:
|
||||||
|
packet = self.receive_buffer[:total_packet_size]
|
||||||
|
del self.receive_buffer[:total_packet_size] # Clear packet from buffer
|
||||||
|
|
||||||
|
# Handle packet
|
||||||
|
self.handle_valid_packet(packet)
|
||||||
|
|
||||||
|
else:
|
||||||
|
# Wait for more data to arrive
|
||||||
|
break
|
||||||
|
|
||||||
|
else:
|
||||||
|
# Not a syns byte so discard the first byte and keep looking
|
||||||
|
self.receive_buffer.pop(0)
|
||||||
|
|
||||||
|
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}")
|
||||||
|
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def pack_integers_to_bytes(self, *integers: int) -> bytes:
|
def pack_integers_to_bytes(self, *integers: int) -> bytes:
|
||||||
# Setup header
|
# Setup header
|
||||||
sync_bytes = [0x41, 0x52]
|
sync_bytes = [0x41, 0x52]
|
||||||
|
|||||||
Reference in New Issue
Block a user