aboutsummaryrefslogtreecommitdiff
path: root/confui/serial.cpp
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 /confui/serial.cpp
parent931fbf84e5a46649c9adf073e9b3d1d776af0b54 (diff)
serial port list
Diffstat (limited to 'confui/serial.cpp')
-rw-r--r--confui/serial.cpp60
1 files changed, 60 insertions, 0 deletions
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;
+}