summaryrefslogtreecommitdiff
path: root/src/printerclient.cpp
diff options
context:
space:
mode:
authorNikita Kostovsky <nikita@kostovsky.me>2025-02-21 07:27:00 +0100
committerNikita Kostovsky <nikita@kostovsky.me>2025-02-21 07:27:00 +0100
commitd12498504c279a0a85bbfb024f7903e34dbe07db (patch)
tree0df9f3f8bf27470ac211a57bb8e44be0aa2f6138 /src/printerclient.cpp
parent27637ab117d8738236f6ab155300ff6e79e4843b (diff)
broken img calc; change dir struct
Diffstat (limited to 'src/printerclient.cpp')
-rw-r--r--src/printerclient.cpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/printerclient.cpp b/src/printerclient.cpp
new file mode 100644
index 0000000..4fed38f
--- /dev/null
+++ b/src/printerclient.cpp
@@ -0,0 +1,86 @@
+#include "printerclient.h"
+
+#include <exception>
+
+#include <QDebug>
+#include <QFile>
+#include <QSerialPort>
+#include <QSerialPortInfo>
+
+QString getFirstTtyUSB()
+{
+ auto ports = QSerialPortInfo::availablePorts();
+
+ std::remove_if(ports.begin(), ports.end(), [](const auto& port) {
+ return !port.portName().contains("ttyUSB");
+ });
+
+ return ports.isEmpty() ? "" : ports.first().portName();
+}
+
+PrinterClient::PrinterClient(QObject* parent)
+ : QObject{parent} // , m_serialPort{new QSerialPort{"/dev/ttyUSB0", this}}
+ , m_serialPort{new QSerialPort{getFirstTtyUSB(), this}}
+{
+ if (!m_serialPort->setBaudRate(QSerialPort::Baud115200)) {
+ throw std::runtime_error(
+ "serial port: cannot set baud rate: " +
+ m_serialPort->errorString().toStdString());
+
+ return;
+ }
+
+ if (!m_serialPort->open(QFile::ReadWrite)) {
+ throw std::runtime_error(
+ "cannot open serial port: " +
+ m_serialPort->errorString().toStdString());
+
+ return;
+ }
+
+ qDebug() << "serial port baud rate:" << m_serialPort->baudRate();
+
+ qDebug() << "serial port data bits:" << m_serialPort->dataBits();
+ qDebug() << "serial port parity:" << m_serialPort->parity();
+ qDebug() << "serial port stop bits:" << m_serialPort->stopBits();
+
+ QObject::connect(m_serialPort, &QSerialPort::readyRead,
+ this, &PrinterClient::onReadyRead);
+
+ QObject::connect(m_serialPort, &QSerialPort::errorOccurred,
+ this, &PrinterClient::onErrorOccured);
+
+ // m_serialPort->write(QByteArray { "G91\n" });
+ // m_serialPort->flush();
+ // m_serialPort->write(QByteArray { "G1 Z10\n" });
+ // m_serialPort->flush();
+ sendCommand("G91");
+ // sendCommand("G1 Z10");
+ // sendCommand("G1 Z-10");
+
+ onErrorOccured(QSerialPort::SerialPortError::PermissionError);
+}
+
+void PrinterClient::sendCommand(const QString command)
+{
+ const auto written = m_serialPort->write(command.toUtf8() + "\n");
+
+ qDebug() << QString("serialPort: send '%1': (written %2 bytes)")
+ .arg(command).arg(written);
+
+ m_serialPort->flush();
+}
+
+void PrinterClient::onReadyRead()
+{
+ const auto data = m_serialPort->readAll();
+ qDebug() << "serialPort: " << data;
+
+ // emit newData(data);
+}
+
+void PrinterClient::onErrorOccured(QSerialPort::SerialPortError error)
+{
+ qWarning() << "serial port error:" << m_serialPort->errorString()
+ << "-" << error;
+}