Added serial connection timeout and connection checks.

Improved visualization of faulty connection settings.
This commit is contained in:
jupfi 2023-08-21 10:17:30 +02:00
parent f45ca6a574
commit bb585141f1
2 changed files with 62 additions and 12 deletions

View file

@ -491,24 +491,58 @@ class AutoTMController(ModuleController):
logger.debug("Switching to atm")
self.send_command("ca")
def send_command(self, command: str) -> None:
def send_command(self, command: str) -> bool:
"""This method is used to send a command to the active serial connection.
Args:
command (str): The command that should be send to the atm system.
Returns:
bool: True if the command was send successfully, False otherwise.
"""
logger.debug("Sending command %s", command)
timeout = 1000 # ms
if self.module.model.serial is None:
logger.error("Could not send command. No serial connection")
self.module.view.add_error_text(
"Could not send command. No serial connection"
)
return False
if self.module.model.serial.isOpen() == False:
logger.error("Could not send command. Serial connection is not open")
self.module.view.add_error_text(
"Could not send command. Serial connection is not open"
)
return False
try:
self.module.model.serial.write(command.encode("utf-8"))
# Wait for 0.5 seconds
QTest.qWait(500)
# Make sure that the command is being send
QApplication.processEvents()
except AttributeError:
logger.error("Could not send command. No device connected.")
self.module.view.add_error_text(
"Could not send command. No device connected."
)
# Wait for the confirmation of the command ('c') to be read with a timeout of 1 second
if not self.module.model.serial.waitForReadyRead(timeout):
logger.error("Could not send command. Timeout")
self.module.view.add_error_text("Could not send command. Timeout")
return False
# Read the confirmation of the command
confirmation = self.module.model.serial.readAll().data().decode("utf-8")
logger.debug("Confirmation: %s", confirmation)
if confirmation == "c":
logger.debug("Command send successfully")
return True
else:
logger.error("Could not send command. No confirmation received")
self.module.view.add_error_text(
"Could not send command. No confirmation received"
)
return False
except Exception as e:
logger.error("Could not send command. %s", e)
self.module.view.add_error_text("Could not send command. %s" % e)
def homing(self) -> None:
"""This method is used to send the command 'h' to the atm system.

View file

@ -21,6 +21,7 @@ from PyQt6.QtWidgets import (
from PyQt6.QtCore import pyqtSlot, Qt
from nqrduck.module.module_view import ModuleView
from nqrduck.contrib.mplwidget import MplWidget
from nqrduck.assets.icons import Logos
from .widget import Ui_Form
logger = logging.getLogger(__name__)
@ -109,6 +110,10 @@ class AutoTMView(ModuleView):
Qt.AlignmentFlag.AlignTop
)
# Add button Icons
self._ui_form.startButton.setIcon(Logos.Play_16x16())
self._ui_form.startButton.setIconSize(self._ui_form.startButton.size())
self.init_plot()
self.init_labels()
@ -263,12 +268,23 @@ class AutoTMView(ModuleView):
Args:
text (str): Text to add to the error text box.
"""
message_widget = QWidget()
message_widget.setLayout(QHBoxLayout())
error_icon = QLabel()
error_icon.setPixmap(
Logos.Error_16x16().pixmap(Logos.Error_16x16().availableSizes()[0])
)
# Add a timestamp to the text
timestamp = datetime.now().strftime("%H:%M:%S")
text = "[%s] %s ERROR:" % (timestamp, text)
text = "[%s] %s" % (timestamp, text)
text_label = QLabel(text)
text_label.setStyleSheet("font-size: 25px; color: red;")
self._ui_form.scrollAreaWidgetContents.layout().addWidget(text_label)
message_widget.layout().addWidget(error_icon)
message_widget.layout().addWidget(text_label)
self._ui_form.scrollAreaWidgetContents.layout().addWidget(message_widget)
self._ui_form.scrollArea.verticalScrollBar().setValue(
self._ui_form.scrollArea.verticalScrollBar().maximum()
)