From c265bc642b33c15f8446ceef318373a2f2bdee1d Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sat, 10 Dec 2022 16:05:58 +0100 Subject: serial port list --- confui/confui.pro | 34 +++++++++++++++-------------- confui/mainwindow.cpp | 9 ++++++-- confui/mainwindow.h | 2 ++ confui/serial.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ confui/serial.h | 39 +++++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+), 18 deletions(-) create mode 100644 confui/serial.cpp create mode 100644 confui/serial.h diff --git a/confui/confui.pro b/confui/confui.pro index 2c4df61..2992f70 100644 --- a/confui/confui.pro +++ b/confui/confui.pro @@ -1,23 +1,25 @@ -QT += core gui widgets +QT += core gui widgets serialport SOURCES += \ - main.cpp \ - mainwindow.cpp \ - mesh_connector.cpp \ - ui_tab_automations.cpp \ - ui_tab_node_overview.cpp \ - ui_node.cpp \ - ui_automation.cpp \ - ui_scroll_container.cpp + main.cpp \ + mainwindow.cpp \ + mesh_connector.cpp \ + ui_tab_automations.cpp \ + ui_tab_node_overview.cpp \ + ui_node.cpp \ + ui_automation.cpp \ + ui_scroll_container.cpp \ + serial.cpp HEADERS += \ - mainwindow.h \ - mesh_connector.h \ - ui_tab_automations.h \ - ui_tab_node_overview.h \ - ui_node.h \ - ui_automation.h \ - ui_scroll_container.h + mainwindow.h \ + mesh_connector.h \ + ui_tab_automations.h \ + ui_tab_node_overview.h \ + ui_node.h \ + ui_automation.h \ + ui_scroll_container.h \ + serial.h CONFIG += c++17 CONFIG += force_debug_info diff --git a/confui/mainwindow.cpp b/confui/mainwindow.cpp index cff5002..8dcf077 100644 --- a/confui/mainwindow.cpp +++ b/confui/mainwindow.cpp @@ -5,14 +5,18 @@ #include "mainwindow.h" #include "ui_tab_automations.h" #include "ui_tab_node_overview.h" +#include "serial.h" CDMeshConnector *g_cd_mesh_connector = nullptr; +CDSerialConnector *g_cd_serial = nullptr; CDMainWindow::~CDMainWindow() { delete this->mesh_connector; } CDMainWindow::CDMainWindow(QWidget *parent) : QMainWindow(parent) { g_cd_mesh_connector = new CDMeshConnector(); this->mesh_connector = g_cd_mesh_connector; + g_cd_serial = new CDSerialConnector(); + this->serial_connector = g_cd_serial; menu_bar = new QMenuBar(this); @@ -47,8 +51,9 @@ void CDMainWindow::update() { QMenu *menu_options_serialport = menu_options->addMenu("serial port (FIXME)"); - menu_options_serialport->addAction("FIXME A"); - menu_options_serialport->addAction("FIXME B"); + vector ports = CDSerialConnector::get_ports(); + for (string port : ports) + menu_options_serialport->addAction(QString::fromStdString(port)); } void CDMainWindow::menu_refresh() { update(); } diff --git a/confui/mainwindow.h b/confui/mainwindow.h index 3a0b4f7..41a0eac 100644 --- a/confui/mainwindow.h +++ b/confui/mainwindow.h @@ -6,6 +6,7 @@ #include #include "mesh_connector.h" +#include "serial.h" class CDAutomationsTabWidget; class CDNodeOverviewTabWidget; @@ -26,6 +27,7 @@ private: public: CDMeshConnector *mesh_connector = nullptr; + CDSerialConnector *serial_connector = nullptr; CDMainWindow(QWidget *parent = nullptr); ~CDMainWindow(); diff --git a/confui/serial.cpp b/confui/serial.cpp new file mode 100644 index 0000000..96c5c0f --- /dev/null +++ b/confui/serial.cpp @@ -0,0 +1,60 @@ +#include "serial.h" + +#include +#include +#include + +CDSerialConnector::CDSerialConnector() { + this->_serial = new QSerialPort; + + _serial->setPortName("/Users/jregnier/ttypx"); + if (!_serial->setBaudRate(QSerialPort::Baud9600)) qDebug() << _serial->errorString(); + if (!_serial->setDataBits(QSerialPort::Data8)) // data is 8 bits + qDebug() << _serial->errorString(); + if (!_serial->setParity(QSerialPort::NoParity)) qDebug() << _serial->errorString(); + if (!_serial->setFlowControl(QSerialPort::NoFlowControl)) // default: QSerialPort::NoFlowControl + qDebug() << _serial->errorString(); + if (!_serial->setStopBits(QSerialPort::OneStop)) // default + qDebug() << _serial->errorString(); +} + +CDSerialConnector::~CDSerialConnector() { delete this->_serial; } + +void CDSerialConnector::action() { + // this is called when readyRead(); + int bytes = _serial->bytesAvailable(); + if (bytes > 0) { + qDebug() << "new Data arived" << _serial->bytesAvailable(); + _msg = _serial->readAll(); + qDebug() << _msg; + } +} + +void CDSerialConnector::write(QByteArray msg) { + _serial->write(msg); + qDebug() << _serial->errorString(); +} + +void CDSerialConnector::connect() { + + // QIODevice::ReadOnly + if (!_serial->open(QIODevice::ReadWrite)) qDebug() << _serial->errorString(); + + QObject::connect(_serial, &QSerialPort::readyRead, [&] { + // this is called when readyRead(); + action(); + }); +} + +void CDSerialConnector::disconnect() { + // WIP? +} + +QByteArray CDSerialConnector::get_data() { return _msg; } + +vector CDSerialConnector::get_ports() { + vector ports; + for (QSerialPortInfo port : QSerialPortInfo::availablePorts()) + ports.push_back(port.portName().toStdString()); + return ports; +} diff --git a/confui/serial.h b/confui/serial.h new file mode 100644 index 0000000..9645227 --- /dev/null +++ b/confui/serial.h @@ -0,0 +1,39 @@ +#pragma once + +#include +#include +#include + +using std::string; +using std::vector; + +/** @brief manage asynchronous serial connection */ +class CDSerialConnector { +public: + CDSerialConnector(); + ~CDSerialConnector(); + + /** @brief get list of available serial ports */ + static vector get_ports(); + + /** @brief open serial port */ + virtual void connect(); + /** @brief close serial port */ + virtual void disconnect(); + + /** @brief write to serial port */ + virtual void write(QByteArray msg); + /** @brief get last message */ + virtual QByteArray get_data(); + +private: + /** @brief executed when new data is received */ + virtual void action(); + + QSerialPort *_serial = nullptr; + QByteArray _msg; +}; + +/** @brief global pointer to serial connector, initialized in CDMainWindow */ +extern CDSerialConnector *g_cd_serial; + -- cgit v1.2.3