aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-12-10 16:05:58 +0100
committerlonkaars <loek@pipeframe.xyz>2022-12-10 16:05:58 +0100
commitc265bc642b33c15f8446ceef318373a2f2bdee1d (patch)
treecdf2bcc9f19badf54a22a7c9fe7dde5ea8a7080a
parent931fbf84e5a46649c9adf073e9b3d1d776af0b54 (diff)
serial port list
-rw-r--r--confui/confui.pro34
-rw-r--r--confui/mainwindow.cpp9
-rw-r--r--confui/mainwindow.h2
-rw-r--r--confui/serial.cpp60
-rw-r--r--confui/serial.h39
5 files changed, 126 insertions, 18 deletions
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<string> 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 <QDebug>
#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 <QDebug>
+#include <QSerialPort>
+#include <QSerialPortInfo>
+
+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<string> CDSerialConnector::get_ports() {
+ vector<string> 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 <QSerialPort>
+#include <QString>
+#include <vector>
+
+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<string> 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;
+