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

111 lines
2.9 KiB
C++
Raw Permalink Normal View History

2023-04-08 15:06:36 +02:00
#include "serial.inc.h"
2023-04-13 00:27:20 +02:00
#include <unistd.h>
2023-04-29 15:15:19 +02:00
#include "zigateBackend.h"
2023-04-08 15:06:36 +02:00
extern QApplication a;
2023-04-29 15:15:19 +02:00
extern ZigateBackend zigateBkd;
//extern SerialManager serialManager;
2023-04-08 15:06:36 +02:00
using namespace std;
2023-04-29 15:15:19 +02:00
SerialManager::SerialManager()
2023-04-13 00:27:20 +02:00
{
}
2023-04-29 15:15:19 +02:00
SerialManager::~SerialManager()
2023-04-13 00:27:20 +02:00
{
this->close();
}
2023-04-29 15:15:19 +02:00
void SerialManager::initSerial()
2023-04-08 15:06:36 +02:00
{
bool test;
2023-04-29 15:15:19 +02:00
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);
}
}
2023-04-08 15:06:36 +02:00
this->setBaudRate(115200);
this->setDataBits(QSerialPort::Data8);
this->setStopBits(QSerialPort::OneStop);
this->setParity(QSerialPort::NoParity);
this->setPortName(this->serialDevicePath);
2023-04-29 15:15:19 +02:00
2023-04-08 15:06:36 +02:00
do
{
test = this->open(QIODevice::ReadWrite);
if (!test)
{
cout << this->errorString().toStdString() << "=>" << this->serialDevicePath.toStdString() << endl;
sleep(5);
}
}while(!test);
2023-04-29 15:15:19 +02:00
QFutureWatcher<void> watcher;
QFuture<void> future = QtConcurrent::run([=]()
2023-04-13 00:27:20 +02:00
{
2023-04-29 15:15:19 +02:00
do
2023-04-20 13:42:00 +02:00
{
2023-04-29 15:15:19 +02:00
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);
2023-04-08 15:06:36 +02:00
}
2023-04-29 15:15:19 +02:00
void SerialManager::write(QByteArray msg)
2023-04-08 15:06:36 +02:00
{
2023-04-20 13:42:00 +02:00
cout << "writing to serial" << endl;
2023-04-13 00:27:20 +02:00
dataWriteSize = msg.count();
cout << "size = " << dataWriteSize << endl;
this->writeData(msg, msg.count()+1);
cout << msg.toHex().toStdString() << endl;
2023-04-08 15:06:36 +02:00
}
2023-04-29 15:15:19 +02:00
bool SerialManager::findSerialDevice()
2023-04-08 15:06:36 +02:00
{
2023-04-13 00:27:20 +02:00
const auto serialPortInfos = QSerialPortInfo::availablePorts();
for (const QSerialPortInfo &portInfo : serialPortInfos)
2023-04-08 15:06:36 +02:00
{
2023-04-13 00:27:20 +02:00
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;
}
2023-04-08 15:06:36 +02:00
}
2023-04-13 00:27:20 +02:00
return false;
2023-04-08 15:06:36 +02:00
}
2023-04-29 15:15:19 +02:00