{
for (int i = 0; i < MAX_COM_COUNT; i++)
{
m_hEventReply[i] = CreateEvent(NULL, TRUE, FALSE, NULL);
m_nFailCount[i] = 0;
}
m_nReadPos = 0;
m_nWritePos = 0;
m_pPsLogSystem = &CPsSystem::GetInstance()->m_psLogSystem;
InitializeCriticalSection(&m_csLock);
}
CPsReadIDThread::~CPsReadIDThread(void)
{
for(int i = 0; i < MAX_COM_COUNT; i++)
CloseHandle(m_hEventReply[i]);
StopThread(TRUE);
DeleteCriticalSection(&m_csLock);
}
//两个串口读码器发送数据
void CPsReadIDThread::Run()
{
CFsSerial *fsSerial[MAX_COM_COUNT];
for(int i = 0; i < MAX_COM_COUNT; i++)
fsSerial[i] = &CPsSystem::GetInstance()->m_psEnvi.GetReadIDSerial(i);
BOOL bOpenSuccess = TRUE;
while(FALSE == m_bExitThread)
{
int iPos = m_nWritePos - m_nReadPos;
if (iPos < MAX_IDBUF_SIZE)
{
for (int i = 0; i < MAX_COM_COUNT; i++)
{
if (TRUE == fsSerial[i]->IsOpen())
{
UCHAR cCommand[64] = "+";
ResetEvent(m_hEventReply);//首先将事件至为无效
fsSerial[i]->SendData(cCommand, 4); //Send Data 发送至串口之后触发 串口发送回复数据,,之后将事件至为有效setEvent()
}
else
{
m_pPsLogSystem->AddInfo(0, LOG_WARNING, _T("读码器串口没有打开!"));
bOpenSuccess = FALSE;
Sleep(5000);
StopThread(TRUE);
}
}
//Wait Reply
if (TRUE == bOpenSuccess)
{
for (int i = 0; i < MAX_COM_COUNT; i++)
{
if (WAIT_OBJECT_0 == WaitForSingleObject(m_hEventReply[i], 5000))//等待串口发送的回复数据(recvData)
{
m_pPsLogSystem->AddInfo(0, LOG_INFO, _T("接收串口指令成功 Comport:%d"),CPsSystem::GetInstance()->m_psEnvi.GetReadIDSerial(i).GetPort());
m_nFailCount[i] = 0;
}
else
{
m_pPsLogSystem->AddInfo(0, LOG_WARNING, _T("接收串口指令超时 Comport:%d"),CPsSystem::GetInstance()->m_psEnvi.GetReadIDSerial(i).GetPort());
m_nFailCount[i]++;
}
//}
//
//Check TimeOut
//for (int i = 0; i < MAX_COM_COUNT; i++)
//{
if (m_nFailCount[i] >= TIME_OUT)
{
m_pPsLogSystem->AddInfo(0, LOG_WARNING, _T("连续接收串口指令超时请检查串口是否异常!"));
StopThread(TRUE);
}
}
}
}
m_nWritePos ++;
}
}
void CPsReadIDThread::GetPanelID(CString& szPanelID1, int iPos) //j获得串口发送的数据Buf里面
{
/*EnterCriticalSection(&m_csLock);*/
if(m_nReadPos != iPos)
m_nReadPos = iPos;
szPanelID1 = m_szPanelBuf1[m_nReadPos % MAX_IDBUF_SIZE];
//szPanelID2 = m_szPanelBuf2[m_nReadPos % MAX_IDBUF_SIZE];
m_nReadPos++;
/*LeaveCriticalSection(&m_csLock);*/
}