1 #include "serialhandler.h" 9 class SerialHandler::_SerialHandlerImpl{
16 QSerialPort *getPort()
const;
17 void writeToDevice(
const QString &str);
18 void setPort(QSerialPort *port);
25 _pimpl{std::make_unique<SerialHandler::_SerialHandlerImpl>(
this)}
30 SerialHandler::~SerialHandler()
34 QSerialPort *SerialHandler::getPort()
36 return _pimpl->getPort();
39 void SerialHandler::setPort(QSerialPort *port)
41 _pimpl->setPort(port);
46 SerialHandler::_SerialHandlerImpl::_SerialHandlerImpl(
SerialHandler *
const ptr):
51 QSerialPort *SerialHandler::_SerialHandlerImpl::getPort()
const 56 void SerialHandler::_SerialHandlerImpl::writeToDevice(
const QString &str)
58 if(_currentData == str)
60 QRegExp exp(
"\\d+, \\d+, \\d+");
61 if(!exp.exactMatch(str)){
62 emit _ptr->errorIODevice(
"Error Writing To Device");
65 QString s = QString(
"(%1)").arg(str);
66 if(!_port->isOpen() || !_port->isWritable())
67 emit _ptr->errorIODevice(
"Error Port isn't Avialable");
68 _port->write(s.toLocal8Bit().constData());
72 void SerialHandler::_SerialHandlerImpl::setPort(QSerialPort *port)
75 connect(_port, &QSerialPort::errorOccurred, [&](QSerialPort::SerialPortError error){
76 emit _ptr->errorIODevice(QVariant(error).toString());
80 void SerialHandler::_SerialHandlerImpl::openDevice()
82 if(!_port->open(QIODevice::WriteOnly))
83 emit _ptr->errorIODevice(
"Error Opening Device");
86 void SerialHandler::_SerialHandlerImpl::closeDevice()
92 void SerialHandler::writeToDevice(
const QString &str)
94 _pimpl->writeToDevice(str);
98 void Devices::SerialHandler::closeDevice()
100 _pimpl->closeDevice();
103 void SerialHandler::openDevice()
105 _pimpl->openDevice();