#include "serial.h" #include #include "zigateplugin.h" #include //extern QApplication a; extern ZigatePlugin zigateBkd; using namespace std; SerialManager::SerialManager() { } SerialManager::~SerialManager() { this->close(); } void SerialManager::initSerial() { bool test; bool result; while (1) { result = this->findSerialDevice(); if (result) { debug("Device found :" + this->serialDevicePath, INFO); break; }else { cout << "Device not found, waiting 10 seconds before retrying" << endl; sleep(10); } } this->setBaudRate(115200); this->setDataBits(QSerialPort::Data8); this->setStopBits(QSerialPort::OneStop); this->setParity(QSerialPort::NoParity); this->setPortName(this->serialDevicePath); do { test = this->open(QIODevice::ReadWrite); if (!test) { cout << this->errorString().toStdString() << "=>" << this->serialDevicePath.toStdString() << endl; sleep(5); } }while(!test); QFutureWatcher watcher; QFuture future = QtConcurrent::run([=]() { do { this->dataRead.clear(); //cout << "getData()" << endl; if (this->waitForReadyRead()) { while (!this->atEnd()) { cout << "reading datas" << endl; this->dataRead.append(this->readAll()); //cout << this->dataRead.toHex().toStdString() << endl; } } emit this->datasReady(this->dataRead); //zigateBkd.interpretResult(this->dataRead); }while(1); }); watcher.setFuture(future); } void SerialManager::write(QByteArray msg) { cout << "writing to serial" << endl; dataWriteSize = msg.count(); cout << "size = " << dataWriteSize << endl; this->writeData(msg, msg.count()+1); cout << msg.toHex().toStdString() << endl; } bool SerialManager::findSerialDevice() { const auto serialPortInfos = QSerialPortInfo::availablePorts(); for (const QSerialPortInfo &portInfo : serialPortInfos) { cout << portInfo.serialNumber().toStdString() << this->deviceName.toStdString() << endl; if (portInfo.serialNumber().contains(this->deviceName)) { this->portName = portInfo.portName(); this->serialDevicePath = portInfo.systemLocation(); this->serialDeviceDescription = portInfo.description(); this->serialDeviceManufacturer = portInfo.manufacturer(); this->serialDeviceNumber = portInfo.serialNumber(); this->serialDeviceVendor = portInfo.vendorIdentifier(); this->serialDeviceProduct = portInfo.productIdentifier(); return true; } } return false; }