#include "ModulatorCtl.h"
#include <QDebug>
#include <QMutex>
#include <QFile>
ModulatorCtl::ModulatorCtl(QObject *parent)
: QObject(parent)
{
InitVarible();
m_ModulatorRealize = new ModulatorRealize(this);
m_PollingTimer = new QTimer(this);
qRegisterMetaType<ModulatorMsgInfor>("ModulatorMsgInfor");
connect(m_PollingTimer,&QTimer::timeout, this, &ModulatorCtl::PollingTimerEvent);
connect(this, &ModulatorCtl::ModulatorMsgCtl, m_ModulatorRealize, &ModulatorRealize::onModulatorCtl);
connect(m_ModulatorRealize, &ModulatorRealize::ModulatorStateChanged, this, &ModulatorCtl::onModulatorStateChanged);
connect(m_ModulatorRealize, &ModulatorRealize::ModulatorMsgRec, this, &ModulatorCtl::onModulatorMsgRec);
}
ModulatorCtl::~ModulatorCtl()
{
ControlPolling(false);
m_pmodularParamList.clear();
m_SendThread.store(false);
}
bool ModulatorCtl::IsNoInterlock()
{
return ((m_ModulatorInterlock[0]>0) || (m_ModulatorInterlock[1]>0) || (m_ModulatorInterlock[2])) ? false : TRUE;
}
void ModulatorCtl::SendSetAuthority(int authority)
{
if (authority == m_authority)
return;
ModulatorMsgInfor tmpMsg;
ModulatorDev tempParam;
tempParam.valueType = MV_CHARARRAY;
tempParam.addressNo = 5121;
switch (authority)
{
case 0:
QString("0000000000").toWCharArray(tempParam.charArray);
break;
case 1:
QString("7777777").toWCharArray(tempParam.charArray);
break;
case 2:
QString("88888888").toWCharArray(tempParam.charArray);
break;
}
tmpMsg.isReadCmd = false;
tmpMsg.writeCmd = tempParam;
AddMsgToList(tmpMsg);
}
void ModulatorCtl::SendSetModulatorState(ModulatorState state)
{
ModulatorMsgInfor tmpMsg;
ModulatorDev tempParam;
tempParam.valueType = MV_WORD;
tempParam.addressNo = 5120;
tempParam.wordVal = (int)state;
tmpMsg.isReadCmd = false;
tmpMsg.writeCmd = tempParam;
AddMsgToList(tmpMsg);
}
void ModulatorCtl::SendResetCommand()
{
ModulatorMsgInfor tmpMsg;
ModulatorDev tempParam;
tempParam.valueType = MV_WORD;
tempParam.addressNo = 5140;
tmpMsg.isReadCmd = false;
tmpMsg.writeCmd = tempParam;
AddMsgToList(tmpMsg);
}
void ModulatorCtl::DeviceCtl(bool CtrlTag)
{
if (CtrlTag)
{
m_ModulatorRealize->InitDevice();
}
else
{
ControlPolling(false);
}
}
void ModulatorCtl::InitVarible()
{
m_IsDeviceConnectd = false;
m_modulatorState = MS_OFF;
for (int i = 0; i < 2; i++)
m_ModulatorInterlock[i] = 0;
m_authority = 0;
m_pmodularParamList.clear();
isPollingGlobal = true;
m_SendThread.store(true);
GeTParamDirctory();
for (auto iter = m_pmodularParamList.begin(); iter != m_pmodularParamList.end(); iter++)
{
switch (iter->valueType)
{
case MV_WORD:
iter->wordVal = 0;
break;
case MV_DWORD:
iter->dwordVal = 0;
break;
case MV_CHARARRAY:
break;
case MV_FLOAT:
iter->floatVal = 0;
break;
case MV_BYTEARRAY:
break;
case MV_READCAL:
break;
case MV_SETCAL:
break;
}
}
}
void ModulatorCtl::AddMsgToList(const ModulatorMsgInfor nmsg)
{
static QMutex mutex;
mutex.lock();
m_psendlist.push(nmsg);
mutex.unlock();
}
void ModulatorCtl::ThreadModulator()
{
int nFailedCount = 0;
ModulatorMsgInfor tmpMsg;
while (1)
{
if (!m_SendThread.load())
{
break;
}
if (!m_psendlist.IsEmpty())
{
m_psendlist.pop(&tmpMsg);
emit ModulatorMsgCtl(tmpMsg);
}
}
}
void ModulatorCtl::RealtimeStateGet()
{
int tempVal;
ModulatorDev tempParam;
tempVal = m_pmodularParamList.find(5480).value().wordVal;
if ((ModulatorState)tempVal != m_modulatorState)
{
m_modulatorState = (ModulatorState)tempVal;
}
tempVal = m_pmodularParamList.find(5532).value().dwordVal;
if (m_ModulatorInterlock[0] != tempVal)
{
m_ModulatorInterlock[0] = tempVal;
//连锁处理
}
tempVal = m_pmodularParamList.find(5534).value().dwordVal;
if (m_ModulatorInterlock[1] != tempVal)
{
m_ModulatorInterlock[1] = tempVal;
//连锁处理
}
tempVal = m_pmodularParamList.find(5536).value().dwordVal;
if (m_ModulatorInterlock[2] != tempVal)
{
m_ModulatorInterlock[2] = tempVal;
//连锁处理
}
tempVal = m_pmodularParamList.find(5559).value().wordVal;
if (m_authority != tempVal)
{
m_authority = tempVal;
SendSetModulatorState(MS_STANDBY);
qDebug() << m_authority;
}
}
void ModulatorCtl::GeTParamDirctory()
{
QFile file("E:/C_Dlinac/C_Dlinac/bin64/debug/Config/M1_Modbus_Address.txt");
QStringList ParamStrList, TypeList, StrList;
QByteArray line;
QString str;
ModulatorDev tempParam;
int ParamCount = 0;
TypeList << "WORD" << "DWORD" << "CHAR_ARRAY_10" << "FLOAT" << "BYTE_ARRAY_06" << "READ_CALIB" << "SET_CALIB";
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
while (!file.atEnd())
{
line = file.readLine();
str = QString(line);
ParamStrList = str.split("=");
if (ParamStrList.count() < 2)
break;
StrList = ParamStrList[1].split(" ");
tempParam.addressNo = StrList[0].toInt();
tempParam.valueType = MOD_VALUE_TYPE(TypeList.indexOf(StrList[1]));
m_pmodularParamList.insert(tempParam.addressNo, tempParam);
ParamCount++;
}
file.close();
}
}
void ModulatorCtl::ControlPolling(bool PollingTag, int PollingCircle /*= 100*/)
{
if (PollingTag)
{
m_PollingTimer->setInterval(PollingCircle);
m_PollingTimer->start();
}
else
{
m_PollingTimer->stop();
}
}
void ModulatorCtl::PollingTimerEvent()
{
ModulatorMsgInfor tmpMsg;
if (isPollingGlobal)
{
isPollingGlobal = false;
tmpMsg.isReadCmd = true;
tmpMsg.ReadStartAddress = 5120;
tmpMsg.ReadValCount = 100;
AddMsgToList(tmpMsg);
tmpMsg.ReadStartAddress = 5220;
tmpMsg.ReadValCount = 98;
AddMsgToList(tmpMsg);
tmpMsg.ReadStartAddress = 5318;
tmpMsg.ReadValCount = 90;
AddMsgToList(tmpMsg);
tmpMsg.ReadStartAddress = 5408;
tmpMsg.ReadValCount = 101;
AddMsgToList(tmpMsg);
tmpMsg.ReadStartAddress = 5509;
tmpMsg.ReadValCount = 63;
AddMsgToList(tmpMsg);
tmpMsg.ReadStartAddress = 6000;
tmpMsg.ReadValCount = 10;
AddMsgToList(tmpMsg);
}
else
{
tmpMsg.isReadCmd = true;
tmpMsg.ReadStartAddress = 5480;
tmpMsg.ReadValCount = 105;
AddMsgToList(tmpMsg);
}
}
void ModulatorCtl::onModulatorStateChanged(bool isConnected)
{
if (isConnected)
{
SendSetAuthority(1);
m_ModulatorThread = new std::thread(&ModulatorCtl::ThreadModulator, this);
m_ModulatorThread->detach();
ControlPolling(true);
}
}
void ModulatorCtl::onModulatorMsgRec(QModbusDataUnit unit)
{
int readValIndex = 0;
long tempIntVal;
int valIndex = unit.startAddress();
while (valIndex <= (unit.startAddress() + unit.valueCount()))
{
if (!m_pmodularParamList.contains(valIndex))
{
// 起始寄存器错误
valIndex += 1;
continue;
}
else
{
auto tempParam = m_pmodularParamList.find(valIndex);
readValIndex = valIndex - unit.startAddress();
switch (tempParam.value().valueType)
{
case MV_WORD:
tempParam.value().wordVal = unit.value(readValIndex);
valIndex += 1;
break;
case MV_DWORD:
if ((unit.startAddress() + unit.valueCount() - valIndex) >= 2)
{
tempParam.value().dwordVal = MAKELONG(unit.value(readValIndex), unit.value(readValIndex + 1));
valIndex += 2;
}
else
valIndex += 2;
break;
case MV_CHARARRAY:
valIndex += 5;
break;
case MV_FLOAT:
if ((unit.startAddress() + unit.valueCount() - valIndex) >= 2)
{
tempIntVal = MAKELONG(unit.value(readValIndex), unit.value(readValIndex + 1));
tempParam.value().floatVal = *(float*)&tempIntVal;
valIndex += 2;
}
else
valIndex += 2;
break;
case MV_BYTEARRAY:
valIndex += 3;
break;
case MV_READCAL:
valIndex += 15;
break;
case MV_SETCAL:
valIndex += 15;
break;
default:
valInde
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
Test.zip (39个子文件)
Test
LockFreeQueue.h 2KB
Test.vcxproj.user 964B
Test.vcxproj.filters 3KB
ModulatorRealize.cpp 5KB
ModulatorRealize.h 831B
ModulatorCtl.cpp 8KB
GeneratedFiles
ui_test.h 2KB
Debug
moc_ModulatorCtl.cpp 5KB
moc_ModulatorRealize.cpp 6KB
qrc_test.cpp 1KB
x64
Debug
vc140.pdb 2.28MB
ModulatorCtl.obj 577KB
moc_ModulatorCtl.obj 240KB
Test.Build.CppClean.log 1KB
qrc_test.obj 6KB
test.obj 624KB
moc_ModulatorRealize.obj 242KB
ModulatorRealize.obj 409KB
moc_test.obj 262KB
Test.log 484B
main.obj 233KB
Test.tlog
custombuild.read.1.tlog 726B
CL.read.1.tlog 179KB
custombuild.command.1.tlog 5KB
link.read.1.tlog 6KB
link.write.1.tlog 1KB
CL.write.1.tlog 12KB
link.command.1.tlog 3KB
Test.lastbuildstate 207B
CL.command.1.tlog 15KB
custombuild.write.1.tlog 1KB
LockFreeQueue.obj 13KB
main.cpp 262B
LockFreeQueue.cpp 1KB
Test.vcxproj 25KB
define.h 1KB
test.qrc 64B
ModulatorCtl.h 1KB
test.ui 775B
共 39 条
- 1
qq_21953729
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0