1
0
This repository has been archived on 2023-11-30. You can view files and clone it, but cannot push or open issues or pull requests.
dtux__zigbeemanager/serial.inc.cpp

110 lines
2.8 KiB
C++

#include "serial.inc.h"
#include <unistd.h>
#include "zigateBackend.h"
extern QApplication a;
extern ZigateBackend 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<void> watcher;
QFuture<void> future = QtConcurrent::run([=]()
{
do
{
this->dataRead.clear();
//cout << "getData()" << endl;
if (this->waitForReadyRead())
{
while (!this->atEnd())
{
cout << "reading datas" << endl;
this->dataRead += 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;
}