稳定版本备份
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
#include <qserialport.h>
|
||||
#include <unordered_map>
|
||||
#include <windowsHardwareCtrl.hpp>
|
||||
#include <qthread.h>
|
||||
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();
|
||||
}
|
||||
};
|
||||
class lightSourceManager : public QThread
|
||||
{
|
||||
public:
|
||||
lightSourceManager()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
void run()
|
||||
{
|
||||
init();
|
||||
}
|
||||
void init()
|
||||
{
|
||||
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
|
||||
Reference in New Issue
Block a user