这段代码以作备忘,项目为电芯生产环境而创,为多线程交互结合我的C++框架,总体流程为:扫码、拍照(4个拍照位,一块电芯两端,前后不同角度来拍)、分拣;
其中扫码完成做一次入站检测,第4次拍照完成会做一次出站检测;
而每次一扫码完成之后便会进生一次算法检测,第四次拍照并出站完成,会做一次图片总合成,给出算法图并存入本地磁盘:
功能亮点:多线程接合超时判断,框架应用
DWORD MainXXXThreadDo()
{
XD->WriteLog(_T("进入主控线程"));
std::vector
data.resize(16);
gSenserScan.Reset();
gSenserCap1.Reset();
gSenserCap2.Reset();
gSenserCap3.Reset();
gSenserCap4.Reset();
gSenserSortCatch.Reset();
CString strTimes;
int res = 0;
while (!g_bExitSystem && m_isStartTest)
{
//plc发信号关xray
/*gNewIOFinsManager.getSignal("PLC_XrayOFF", res);
if (1 == res)
{
for (int i = 0; i < g_iXrayCount; i++)
{
g_xrayTubeManager[i]->XRayOff();
Sleep(100);
}
}*/
res = gSignalFins.getSignal("Plc_Reset", data, 10);
if (/*data[0]*/ data[0] == 1 || pcSignal.Reset
|| g_SimData[0] == 1)
{
m_bResetSystem = true;
g_SimData[0] = 0;
XD->WriteLog(_T("收到复位信号"));
//
WaitForSingleObject(m_hEventGetID, INFINITE);
//WaitForSingleObject(m_hEventCatchNg, INFINITE);
WaitForSingleObject(m_hEventDataProcess, INFINITE);
CloseHandle(m_hEventGetID);
//CloseHandle(m_hEventCatchNg);
CloseHandle(m_hEventDataProcess);
m_hEventCatchNg = NULL;
m_hEventDataProcess = NULL;
m_hEventGetID = NULL;
for (int i = 0; i < CORNER_NUM; i++)
{
WaitForSingleObject(m_hEventGetSeatImage1[i], INFINITE);
CloseHandle(m_hEventGetSeatImage1[i]);
m_hEventGetSeatImage1[i] = NULL;
}
XTF->addTask(E_TASK_Update_LED, std::make_shared
XD->WriteLog(_T("同步复位信号,数据开始清空"));
InitIOFinsSignal();
DataResize();
Sleep(1000);
XD->WriteLog(_T("复位完成!"));
gSenserScan.Reset();
gSenserCap1.Reset();
gSenserCap2.Reset();
gSenserCap3.Reset();
gSenserCap4.Reset();
gSenserSortCatch.Reset();
SetMainSignal("Pc_ResetFilish", 1);
XTF->addTask(E_TASK_Update_LED, std::make_shared
WaitingTriggerSignalOff("Plc_Reset");
XTF->addTask(E_TASK_Update_LED, std::make_shared
}
else
{
XTF->addTask(E_TASK_Update_LED, std::make_shared
Sleep(10);
m_bResetSystem = false;
}
if (data[1] > 0 /*data[1]*/ || g_SimData[1] == 1)
{
g_SimData[1] = 0;
//XD->WriteLog(_T("收到扫码信号"));
gSenserScan.Flash(1);
if (m_bGetIDFinished)
{
gSenserScan.UpdateAck();
XTF->addTask(E_TASK_Update_LED, std::make_shared
m_bGetIDFinished = false;
GetIDThreadFunThreadDo();
XD->WriteLog(_T("扫码信号线程启动完成"));
}
else
{
if (!m_bResetSystem && gSenserScan.isTimeout())
{
gSenserScan.UpdateAck();
XTF->addTask(E_TASK_TimeoutProces, std::make_shared
//note to PLC ,the scanID is itmeout...
}
}
}
else
{
gSenserScan.UpdateAck();
XTF->addTask(E_TASK_Update_LED, std::make_shared
}
#pragma region 工位一
if (data[2] > 0 /*data[2]*/ || g_SimData[2] == 1)
{
g_SimData[2] = 0;
//XD->WriteLog(_T("收到拍照1信号"));
gSenserCap1.Flash(1);
if (m_bGetImageFinished1[0])
{
gSenserCap1.UpdateAck();
m_bGetImageFinished1[0] = false;
XTF->addTask(E_TASK_Update_LED, std::make_shared
LED_ON));
GetImage1ThreadFunThreadDo();
XD->WriteLog(_T("拍照1信号线程启动完成"));
}
else
{
//XD->WriteLog(_T("收到拍照1信号,但因前一个未完成,故而未执行"));
if (!m_bResetSystem && gSenserCap1.isTimeout())
{
gSenserCap1.UpdateAck();
XTF->addTask(E_TASK_TimeoutProces, std::make_shared
}
}
}
else
{
XTF->addTask(E_TASK_Update_LED, std::make_shared
}
#pragma endregion
#pragma region 工位二
if (data[3] > 0 /*data[3]*/ || g_SimData[3] == 1)
{
g_SimData[3] = 0;
//XD->WriteLog(_T("收到拍照2信号"));
gSenserCap2.Flash(1);
if (m_bGetImageFinished1[1])
{
gSenserCap2.UpdateAck();
XTF->addTask(E_TASK_Update_LED, std::make_shared
LED_ON));
m_bGetImageFinished1[1] = false;
GetImage2ThreadFunThreadDo();
XD->WriteLog(_T("拍照2信号线程启动完成"));
}
else
{
//XD->WriteLog(_T("收到拍照2信号,但因前一个未完成,故而未执行"));
if (!m_bResetSystem && gSenserCap2.isTimeout())
{
gSenserCap2.UpdateAck();
XTF->addTask(E_TASK_TimeoutProces, std::make_shared
}
}
}
else
{
XTF->addTask(E_TASK_Update_LED, std::make_shared
}
#pragma endregion
#pragma region 工位三
if (1 == data[4] > 0 /*data[4]*/ || g_SimData[4] == 1)
{
g_SimData[4] = 0;
//XD->WriteLog(_T("收到拍照3信号"));
gSenserCap3.Flash(1);
if (m_bGetImageFinished1[2]) {
m_bGetImageFinished1[2] = false;
gSenserCap3.UpdateAck();
XTF->addTask(E_TASK_Update_LED, std::make_shared
GetImage3ThreadFunThreadDo();
XD->WriteLog(_T("拍照3信号线程启动完成"));
}
else
{
//XD->WriteLog(_T("收到拍照3信号,但因前一个未完成,故而未执行"));
if (!m_bResetSystem && gSenserCap3.isTimeout()) //禁止重复给值
{
gSenserCap3.UpdateAck();
XTF->addTask(E_TASK_TimeoutProces, std::make_shared
}
}
}
else
{
XTF->addTask(E_TASK_Update_LED, std::make_shared
}
#pragma endregion
#pragma region 工位四
if (data[5] > 0 /*data[5]*/ || g_SimData[5] == 1)
{
g_SimData[5] = 0;
//XD->WriteLog(_T("收到拍照4信号"));
gSenserCap4.Flash(1);
if (m_bGetImageFinished1[3])
{
gSenserCap4.UpdateAck();
XTF->addTask(E_TASK_Update_LED, std::make_shared
m_bGetImageFinished1[3] = false;
GetImage4ThreadFunThreadDo();
XD->WriteLog(_T("拍照4信号线程启动完成"));
}
else
{
if (!m_bResetSystem && gSenserCap4.isTimeout()) //禁止重复给值
{
gSenserCap4.UpdateAck();
XTF->addTask(E_TASK_TimeoutProces, std::make_shared
}
//XD->WriteLog(_T("收到拍照4信号,但因前一个未完成,故而未执行"));
}
}
else
{
XTF->addTask(E_TASK_Update_LED, std::make_shared
}
#pragma endregion
#pragma region 分拣
if (data[6] > 0 /*data[6]*/ || g_SimData[6] == 1)
{
g_SimData[6] = 0;
//XD->WriteLog(_T("收到分拣信号"));
if (m_bDataProcessFinished)
{
XTF->addTask(E_TASK_Update_LED, std::make_shared
m_bDataProcessFinished = false;
DataProcessFunThreadDo();
XD->WriteLog(_T("分拣信号线程启动完成"));
}
else
{
//XD->WriteLog(_T("收到分拣信号,但因前一个未完成,故而未执行"));
}
}
else
{
XTF->addTask(E_TASK_Update_LED, std::make_shared
}
#pragma endregion
g_EquipmentState = 0;
//CheckTime();
Sleep(10);
}
XD->WriteLog(_T("退出 主控制线程"));
return 0;
}
工程中用到的C++框架库:
https://download.csdn.net/download/sukeman/86974471