📄 signalprocess.cpp
字号:
//signalprocess.cpp
#include "signalprocess.h"
#include "devmoduledlg.h"
#include "Device.h"
#include <qtabwidget.h>
#include <qtextbrowser.h>
#include "baseclass/publicclass.h"
extern CPublicClass g_PublicClass;
////////////////////////////////////////////设备启动方案///////////////////////////////////////////
/*
主模块传送来的是一个结构数组:st_deviceinfo[count]
1.先找出ParentIndex=""的设备,这种设备是独立的或是总线,管理机,启动这些设备的线程,
并产生设备的接点,接点中:m_deviceinfo存放当前设备信息.
2.按照设备接点在结构数组中查找:ParentIndex=当前接点的RecordIndex的设备,这些设备
是总线或管理机下的设备,这些设备的信息添加到接点中的:m_ChildDevList中,并填入线
程号
3.简单地说就是独立设备产生一个接点,其m_ChildDevList为空,每个总线或管理机产生一
个接点,其m_ChildDevList中存放其下级设备
*/
/////////////////////////////////////////////////////////////////////////////////////////////////////
CSystemInit::CSystemInit()
{
}
CSystemInit::~CSystemInit()
{
}
void CSystemInit::SendMsgTextToMain(int flag, QString sMsg)
{
QDateTime t = QDateTime::currentDateTime();
QString s1 = "";
s1 = "["+t.toString("yyyy-MM-dd hh:mm:ss")+"]:";
s1 = s1+_T(sMsg);
((DevModuleDlg*)g_PublicClass.m_MainForm)->SetLogText(flag, s1);
}
bool CSystemInit::StartDevices(void* ppNode)
{
CDataNode* pNode = (CDataNode*)ppNode;
if (!pNode) return false;
int count = pNode->m_nodeinfo.m_iSize/sizeof(st_deviceinfo);
st_deviceinfo dst[count];
memcpy(&dst, (void*)pNode->m_nodeinfo.m_SegmentPid, pNode->m_nodeinfo.m_iSize);
//启动一级保护通讯线程
for (int i=0; i<count; i++)
{
if (StartSomeTopDevice(&dst, i, count) == 0) continue;
}
return true;
}
//////////////////////////启动设备列表中的独立或顶级设备线程//////////////////////////////////
int CSystemInit::StartSomeTopDevice(void* dlist, int pi, int count)
{
CDeviceNode* pNode = NULL;
st_deviceinfo DevBuff;
memcpy(&DevBuff, (char*)dlist+pi*sizeof(st_deviceinfo), sizeof(st_deviceinfo));
//判断是否是总线或管理机
printf("---@@@Parent Index : DevBuff.m_ParentIndex@@@---\n");
printf("---@@@Record Index : DevBuff.m_RecordIndex@@@---\n");
if (DevBuff.m_ParentIndex[0] != '\0') return 0;
//按设备编号查找
int index = 0, ret = 0;
pthread_t threadid = 0;
pNode = g_PublicClass.m_NodeManage.FindDevicebyEquipNo(DevBuff.m_EquipNo, 0, index);
if (!pNode)
{
//产生顶级设备接点
pNode = new CDeviceNode();
pNode->m_ModuleState = 0;
pNode->m_Level = 0;
memcpy(&pNode->m_deviceinfo, &DevBuff, sizeof(st_deviceinfo));
g_PublicClass.m_NodeManage.m_DeviceNode.append(pNode);
//启动设备通讯线程
usleep(1);
ret = pthread_create(&threadid, NULL, DeviceCommunicateMainThread, pNode);
pNode->m_deviceinfo.m_ThreadId = threadid;//线程号付给接点线程号
pNode->AddChildDevices(dlist, count, threadid);
printf("---------------@@This Thread Id:%d\n",(int)threadid);
pNode->m_deviceinfo.m_SupportCmd = InstallDeviceMenu(pNode);
//当是点对点通信时,也考虑成总线方式,只不过该总线下只有一个设备而已
if (pNode->m_ChildDevList.count() == 0)
{
pNode->m_ChildDevList.append(pNode->m_deviceinfo);
}
pNode->m_ModuleState = 1;
//产生发送数据接点
st_deviceinfo di[1+pNode->m_ChildDevList.count()];
memcpy(&di[0], &pNode->m_deviceinfo, sizeof(st_deviceinfo));
if ((int)pNode->m_ChildDevList.count() > 0)
{
DeviceInfoListType::iterator it;
for (int i=0; i<(int)pNode->m_ChildDevList.count(); i++)
{
it = pNode->m_ChildDevList.at(i);
memcpy(&di[1+i], &(*it), sizeof(st_deviceinfo));
}
}
g_PublicClass.m_NodeManage.AddSendDataNode(
g_PublicClass.m_MainModuleNode.m_deviceinfo.mtype, 0, WM_SENDDEVTHREADID,
(void*)&di, (1+pNode->m_ChildDevList.count())*sizeof(st_deviceinfo));
//设备加入界面
((DevModuleDlg*)g_PublicClass.m_MainForm)->AddTopLevelDevice((void*)pNode);
((DevModuleDlg*)g_PublicClass.m_MainForm)->AddChildDevice((void*)pNode);
}
else//总线设备已经启动,现有新的设备启动
{
CDeviceNode* pDevNode = NULL;
st_deviceinfo Dev;
for (int i=0; i<count; i++)
{
memcpy(&Dev, (char*)dlist+i*sizeof(st_deviceinfo), sizeof(st_deviceinfo));
if (Dev.m_ParentIndex[0] == '\0') continue;
if (QString(Dev.m_ParentIndex) != QString(pNode->m_deviceinfo.m_RecordIndex)) continue;
pDevNode = g_PublicClass.m_NodeManage.FindDevicebyEquipNo(Dev.m_EquipNo, 1, index);
if (!pDevNode)//在设备列表中添加该设备
{
st_nodeinfo st;
memset(&st, 0x00, sizeof(st_nodeinfo));
st.m_CurrentThread = pNode->m_deviceinfo.m_ThreadId;
st.m_MachineNo = Dev.m_EquipNo;
st.m_DataType = WM_RUN_A_DEVICE;//设备启动命令
st_deviceinfo* pDev = new st_deviceinfo;
memcpy(pDev, &Dev, sizeof(st_deviceinfo));
st.m_SegmentPid = (long)pDev;
st.m_iSize = sizeof(st_deviceinfo);
CDataNode* pMsgNode = new CDataNode();
memcpy(&pMsgNode->m_nodeinfo, &st, sizeof(st_nodeinfo));
g_PublicClass.m_NodeManage.m_RecvNode.append(pMsgNode);
}
}
}
return 1;
}
//******************安装设备操作菜单的函数**************************//
//*从XML文件中读取菜单
int CSystemInit::InstallDeviceMenu(CDeviceNode* pNode)
{
DeviceInfoListType::iterator it;
QString xmlfile;
QDomNode node, cnode;
for (int i=0; i<(int)pNode->m_ChildDevList.count(); i++)
{
it = pNode->m_ChildDevList.at(i);
xmlfile.sprintf("%s", (*it).m_MachineType);
if (xmlfile == "" || xmlfile.isEmpty()) continue;
xmlfile = g_PublicClass.GetWorkPath()+"/devconfig/"+xmlfile+".xml";
CXMLReader xml(xmlfile);
xml.OpenDomXMLFile();
node = xml.GetDomNode("body", "CONFIG");
cnode = xml.GetChildNode(node, "MENU", "", "");
QString sztmp = "";
xml.GetNodeAttr(cnode, "value", sztmp);
(*it).m_SupportCmd = sztmp.toInt();
}
xmlfile.sprintf("%s", pNode->m_deviceinfo.m_MachineType);
if (xmlfile == "" || xmlfile.isEmpty()) return 0;
xmlfile = g_PublicClass.GetWorkPath()+"/devconfig/"+xmlfile+".xml";
CXMLReader xml(xmlfile);
xml.OpenDomXMLFile();
node = xml.GetDomNode("body", "CONFIG");
cnode = xml.GetChildNode(node, "MENU", "", "");
QString sztmp = "";
xml.GetNodeAttr(cnode, "value", sztmp);
return sztmp.toInt();
}
void SignalProcessDef1x(int signum, siginfo_t* info, void* myact)
{
CSystemInit csi;
QString s1 = "";
if (signum == 0 || !info || !myact) return;
if (info->si_int >= 0) //主模块送的是消息队列号
{
s1.sprintf("收到主模块发送队列号:%d;接收队列号:%d", g_PublicClass.m_SendMsgQueueID, g_PublicClass.m_ReceMsgQueueID);
}
else if (info->si_int < 0)//主模块的测试信号
{
((DevModuleDlg*)g_PublicClass.m_MainForm)->FlashLamp();
g_PublicClass.m_MainModuleNode.m_LastTime = QDateTime::currentDateTime();
g_PublicClass.m_Signal.SendSignal(info->si_pid, SIGUSR1, 0, NULL, 0);
s1.sprintf("收到主模块测试信号:%d", info->si_pid);
}
csi.SendMsgTextToMain(0, s1);
return;
}
void SignalProcessDef2x(int signum, siginfo_t* info, void* myact)
{
if (signum == 0 || !info || !myact) return;
return;
}
/////////////////////////////////////常驻接收信息线程//////////////////////////////////////
void* RecvMsgQueueThread(void* lp)
{
lp = NULL;
CMessageQueue cm;
st_nodeinfo st;
CSegment cs;
while (!g_PublicClass.m_Stop)
{
//当没有收到消息号时,等待
if (g_PublicClass.m_ReceMsgQueueID == 0)
{
usleep(100);
continue;
}
if (!cm.ReadMessage(g_PublicClass.m_ReceMsgQueueID, g_PublicClass.m_SelfPid, &st))
{
usleep(100);
continue;
}
CDataNode* pNode = new CDataNode();
memcpy(&pNode->m_nodeinfo, &st, sizeof(st_nodeinfo));
g_PublicClass.m_NodeManage.m_RecvNode.append(pNode);
usleep(10);
}
return NULL;
}
/////////////////////////////////////常驻发送信息线程//////////////////////////////////////
void* SendMsgQueueThread(void* lp)
{
lp = NULL;
CMessageQueue cm;
CSegment cs;
CDataNode* pNode = NULL;
while (!g_PublicClass.m_Stop)
{
//当发送队列没有形成时,等待
if (g_PublicClass.m_SendMsgQueueID == 0)
{
usleep(10);
continue;
}
pNode = g_PublicClass.m_NodeManage.FindSendDataNode();
if (!pNode)
{
usleep(10);
continue;
}
if (pNode->m_nodeinfo.m_DataType == WM_STOP_A_MODULE)
{
g_PublicClass.m_Stop = true;
sleep(2);
}
cm.SendMessage(g_PublicClass.m_SendMsgQueueID, (void*)pNode);
//删除数据接点
g_PublicClass.m_NodeManage.DeleteSendDataNode(pNode);
usleep(10);
}
return NULL;
}
////////////////////////////////////常驻处理接收信息线程//////////////////////////////////////
void* ProcessRecvMsgThread(void* lp)
{
lp = NULL;
CDataNode* pNode = NULL;
CSystemInit csi;
while (!g_PublicClass.m_Stop)
{
pNode = g_PublicClass.m_NodeManage.FindRecvDataNode(0);
if (!pNode)
{
usleep(10);
continue;
}
switch (pNode->m_nodeinfo.m_DataType)
{
case WM_STARTDEVICETHREAD://启动设备线程
csi.StartDevices((void*)pNode);
// printf("--@@Com103 start device @@--\n");
break;
case WM_MONITORSET:
if (pNode->m_nodeinfo.m_SegmentPid > 0 && pNode->m_nodeinfo.m_iSize > 0)
{
memcpy(&g_PublicClass.m_MonitorSrc, (char*)pNode->m_nodeinfo.m_SegmentPid, sizeof(st_monitorsrc));
}
break;
}
//删除数据接点
g_PublicClass.m_NodeManage.DeleteRecvDataNode(pNode);
}
return NULL;
}
void* DispDataThread(void* lp)
{
lp = NULL;
while (g_PublicClass.m_MainForm == NULL) usleep(100);
CDataNode* pNode = NULL;
DevModuleDlg* pDlg = (DevModuleDlg*)g_PublicClass.m_MainForm;
QTextBrowser* pBrower = NULL;
while (!g_PublicClass.m_Stop)
{
pNode = g_PublicClass.m_NodeManage.FindDispDataNode();
if (pNode)
{
QString dispstr = "", tmpstr = "";
uchar* pData = (uchar*)pNode->m_nodeinfo.m_SegmentPid;
int iSize = pNode->m_nodeinfo.m_iSize;
QDateTime dt = QDateTime::currentDateTime();
if (pNode->m_nodeinfo.m_DataType == DATA_SENDDATA)
{
tmpstr.sprintf("[%s]-Send:%d Dev:%d", dt.toString("yyyy-MM-dd-hh:mm:ss.zzz").data(), iSize, pNode->m_nodeinfo.m_MachineNo);
pBrower = (QTextBrowser*)pDlg->GetTextBrower(2);
}
else if (pNode->m_nodeinfo.m_DataType == DATA_RECVDATA)
{
tmpstr.sprintf("[%s]-Recv:%d Dev:%d", dt.toString("yyyy-MM-dd-hh:mm:ss.zzz").data(), iSize, pNode->m_nodeinfo.m_MachineNo);
pBrower = (QTextBrowser*)pDlg->GetTextBrower(1);
}
pBrower->setColor(QColor::QColor(255,0,0));
pBrower->append(tmpstr);
for (int i=0; i<iSize; i++)
{
tmpstr.sprintf("%02X ", pData[i]);
dispstr += tmpstr;
}
pBrower->setColor(QColor::QColor(0,0,255));
pBrower->append(dispstr);
if (pBrower->lines() > 1000) pBrower->clear();
g_PublicClass.m_NodeManage.DeleteDispDataNode(pNode);
usleep(10);
continue;
}
usleep(100);
}
return NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -