diff options
| author | Nikita Kostovsky <luntik2012@gmail.com> | 2024-11-24 19:54:52 +0100 |
|---|---|---|
| committer | Nikita Kostovsky <luntik2012@gmail.com> | 2024-11-24 19:54:52 +0100 |
| commit | 53979d9d26c5bb51e86e70eb9c3a998bc50f713c (patch) | |
| tree | 4ab3fb55d37db89a8f0e61135efc69d89d2e4f11 /printerclient.cpp | |
| parent | e21934bca43b8dc68bbcf37e2ad1da6bd5ac6db6 (diff) | |
implement printer controls; implement calibration data collection
Diffstat (limited to 'printerclient.cpp')
| -rw-r--r-- | printerclient.cpp | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/printerclient.cpp b/printerclient.cpp new file mode 100644 index 0000000..c5bf189 --- /dev/null +++ b/printerclient.cpp @@ -0,0 +1,74 @@ +#include "printerclient.h" + +#include <exception> + +#include <QDebug> +#include <QFile> +#include <QSerialPort> + +PrinterClient::PrinterClient(QObject *parent) + : QObject { parent } + , m_serialPort { new QSerialPort { "/dev/ttyUSB0", 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; +} |
