2024-08-07 09:16:35 +00:00
|
|
|
#include <qserialport.h>
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
#include <windowsHardwareCtrl.hpp>
|
2024-08-13 18:07:24 +08:00
|
|
|
#include <qthread.h>
|
2024-08-07 09:16:35 +00:00
|
|
|
enum lightSourceChannel
|
|
|
|
|
{
|
|
|
|
|
AChannel, BChannel, CChannel, DChannel
|
|
|
|
|
};
|
|
|
|
|
class lightSource
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
QSerialPort* serialPort = new QSerialPort();
|
|
|
|
|
lightSource(std::string comName, int baudRate, std::string initStr)
|
|
|
|
|
{
|
|
|
|
|
serialPort->close();
|
|
|
|
|
serialPort->setPortName(QString::fromStdString(comName));
|
|
|
|
|
serialPort->open(QIODevice::ReadWrite);
|
|
|
|
|
if (serialPort->isOpen() != true)
|
|
|
|
|
{
|
|
|
|
|
DisableComPort(comName.c_str());
|
|
|
|
|
_sleep(1000);
|
|
|
|
|
EnableComPort(comName.c_str());
|
|
|
|
|
serialPort->open(QIODevice::ReadWrite);
|
|
|
|
|
if (serialPort->isOpen() != true)
|
|
|
|
|
{
|
|
|
|
|
qDebug() << comName.c_str() << "open failure!!";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
serialPort->setBaudRate(baudRate);
|
|
|
|
|
serialPort->setDataBits(QSerialPort::Data8);
|
|
|
|
|
serialPort->setParity(QSerialPort::NoParity);
|
|
|
|
|
serialPort->setStopBits(QSerialPort::OneStop);
|
|
|
|
|
serialPort->setFlowControl(QSerialPort::NoFlowControl);
|
|
|
|
|
serialPort->write(initStr.c_str());
|
|
|
|
|
serialPort->waitForBytesWritten();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
void setValue(lightSourceChannel channelIn,unsigned char value)
|
|
|
|
|
{
|
|
|
|
|
std::string controlStr;
|
|
|
|
|
switch (channelIn)
|
|
|
|
|
{
|
|
|
|
|
case lightSourceChannel::AChannel:
|
|
|
|
|
controlStr = "SA0";
|
|
|
|
|
break;
|
|
|
|
|
case lightSourceChannel::BChannel:
|
|
|
|
|
controlStr = "SB0";
|
|
|
|
|
break;
|
|
|
|
|
case lightSourceChannel::CChannel:
|
|
|
|
|
controlStr = "SC0";
|
|
|
|
|
break;
|
|
|
|
|
case lightSourceChannel::DChannel:
|
|
|
|
|
controlStr = "SD0";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
std::string valueStr = std::to_string(value);
|
|
|
|
|
if (valueStr.length() < 3)
|
|
|
|
|
for (int i = 0; i < 3 - valueStr.length(); i++)
|
|
|
|
|
controlStr += "0";
|
|
|
|
|
controlStr += valueStr + "#";
|
|
|
|
|
serialPort->write(controlStr.c_str());
|
|
|
|
|
serialPort->waitForBytesWritten();
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-08-13 18:07:24 +08:00
|
|
|
class lightSourceManager : public QThread
|
2024-08-07 09:16:35 +00:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
lightSourceManager()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-08-13 18:07:24 +08:00
|
|
|
void run()
|
|
|
|
|
{
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
void init()
|
2024-08-07 09:16:35 +00:00
|
|
|
{
|
|
|
|
|
insertLightSource("COM9", 19200, "SA0000#SB0000#SC0000#SD0000#");
|
|
|
|
|
}
|
|
|
|
|
std::unordered_map<std::string, lightSource*>lightSources;
|
|
|
|
|
void insertLightSource(std::string comName ,int baudRate,std::string initStr)
|
|
|
|
|
{
|
|
|
|
|
lightSource* newLigthSource = new lightSource(comName, baudRate, initStr);
|
|
|
|
|
lightSources.insert(std::make_pair(comName, newLigthSource));
|
|
|
|
|
}
|
|
|
|
|
lightSource* getLightSourcePtr(std::string comName)
|
|
|
|
|
{
|
|
|
|
|
auto ptr = lightSources.find(comName);
|
|
|
|
|
if (ptr != lightSources.end())
|
|
|
|
|
return ptr->second;
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
#pragma once
|