// DialMeasuTaskPacket.cpp : implementation file // #include "stdafx.h" #include "geomative.h" #include "DialMeasuTaskPacket.h" #include "DialCfgTaskPacket.h" #include "FileOperTools.h" #include "OperPLC.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CDialMeasuTaskPacket dialog #define MAX_TIMER_REPEAT_SND_CNT 90000 #define MAX_PACKET_LIST_SHOW 500 #define MODULE_NAME "DialMeasuTaskPacket" extern CGeoMativeApp theApp; extern SYSTEMTIME g_sysCurTime; extern int g_iUILanguage; extern float tcp_htonf(float f); extern float tcp_ntohf(float f); extern CString GetMeasuStatusByAlarm(BYTE ucAlarm); CDialMeasuTaskPacket::CDialMeasuTaskPacket(CWnd* pParent /*=NULL*/) : CDialog(CDialMeasuTaskPacket::IDD, pParent) { //{{AFX_DATA_INIT(CDialMeasuTaskPacket) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT m_iRepeatSndCnt = -1; m_bIsRunning = false; m_pThread = NULL; m_bIsOpenTbData = false; m_ucCmd = 0; memset(m_ucData, 0, sizeof(m_ucData)); m_iRecvDataLen = 0; m_bIsSuspend = false; m_ucCmd = 0; m_bIsThreadFinshed = true; m_bIsOpenTbData = false; m_ucProcStep = 0; m_uiDevID = 0; m_vtMeasureData.clear(); m_iLoopTimes = 0; m_iInterval = 0; m_strPlcID = ""; m_bIsSigMeasuring = false; } void CDialMeasuTaskPacket::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDialMeasuTaskPacket) DDX_Control(pDX, IDC_LIST_SHOW_DATA, m_listMeasuData); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDialMeasuTaskPacket, CDialog) //{{AFX_MSG_MAP(CDialMeasuTaskPacket) ON_BN_CLICKED(IDC_BUTTON_CFG, OnButtonCfg) ON_BN_CLICKED(IDC_BUTTON_MEASU_ALL, OnButtonMeasuAll) ON_BN_CLICKED(IDC_BUTTON_PAUSE, OnButtonPause) ON_BN_CLICKED(IDC_BUTTON_CONTINUE, OnButtonContinue) ON_WM_CLOSE() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDialMeasuTaskPacket message handlers void CDialMeasuTaskPacket::OnButtonCfg() { // TODO: Add your control notification handler code here CDialCfgTaskPacket dialCfg; if (IDOK == dialCfg.DoModal()) { if (!GetCfgTaskPacketAttr()) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("获取配置信息失败")); else MessageBoxEx(NULL, _T("Get config information failed."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } } } void CDialMeasuTaskPacket::ThreadJoin() { if (NULL == m_pThread) { return; } m_bIsRunning = false; Sleep(200); // bool bIsShow = false; while(!m_bIsThreadFinshed) { // if (!bIsShow) // { // AfxMessageBox(_T("线程正在关闭中,请稍候....")); // bIsShow = true; // } Sleep(100); } } void CDialMeasuTaskPacket::OnButtonMeasuAll() { // TODO: Add your control notification handler code here if (!m_bIsThreadFinshed) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("请不要重复测量")); else MessageBoxEx(NULL, _T("Please do not repeat measure."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return; } if (NULL == m_pThread) { m_vtTaskPacket.clear(); m_TaskdDBOper.QueryTaskPacketInfo(m_vtTaskPacket); if (m_vtTaskPacket.size() < 1) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("无法找到有效的计时器包")); else MessageBoxEx(NULL, _T("Can not found valid timer packet."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return ; } m_bIsRunning = true; m_pThread = AfxBeginThread(StartMeasuData,(LPVOID)this); if (NULL == m_pThread) { m_bIsRunning = false; if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("创建进程失败.")); else MessageBoxEx(NULL, _T("Create process data thread failed."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return ; } SetBtnStatus(EN_TIMER_PROC_MEASU_DATA); GetDlgItem(IDC_BUTTON_CFG)->EnableWindow(FALSE); m_listMeasuData.DeleteAllItems(); // GetDlgItem(IDC_BUTTON_TIMER_MRASU)->EnableWindow(FALSE); } } bool CDialMeasuTaskPacket::GetCfgTaskPacketAttr() { if (m_TaskdDBOper.QueryTaskPacketAttr(m_iLoopTimes, m_iInterval, m_strPlcID)) { return true; } else { m_iLoopTimes = m_iInterval = 0; m_strPlcID = ""; return false; } } void CDialMeasuTaskPacket::OnButtonPause() { // TODO: Add your control notification handler code here m_bIsSuspend = true; SetBtnStatus(EN_TIMER_PROC_MEASU_SUSPEND); } void CDialMeasuTaskPacket::OnButtonContinue() { // TODO: Add your control notification handler code here m_bIsSuspend = false; SetBtnStatus(EN_TIMER_PROC_MEASU_DATA); } BOOL CDialMeasuTaskPacket::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here CString strColTitle; int iColIndex = 0; m_listMeasuData.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP | LVS_EX_ONECLICKACTIVATE | LVS_EX_GRIDLINES); m_listMeasuData.InsertColumn(iColIndex++, _T("ID"),LVCFMT_LEFT,50); // strColTitle.Empty(); // strColTitle.LoadString(IDS_DB_SCRIPT_C1); m_listMeasuData.InsertColumn(iColIndex++, _T("A"), LVCFMT_CENTER, 50); // strColTitle.Empty(); // strColTitle.LoadString(IDS_DB_SCRIPT_C2); m_listMeasuData.InsertColumn(iColIndex++, _T("B"), LVCFMT_CENTER, 50); // strColTitle.Empty(); // strColTitle.LoadString(IDS_DB_SCRIPT_P1); m_listMeasuData.InsertColumn(iColIndex++, _T("M"), LVCFMT_CENTER, 50); // strColTitle.Empty(); // strColTitle.LoadString(IDS_DB_SCRIPT_P2); m_listMeasuData.InsertColumn(iColIndex++, _T("N"), LVCFMT_CENTER, 50); // strColTitle.Empty(); // strColTitle.LoadString(IDS_DB_SCRIPT_N); m_listMeasuData.InsertColumn(iColIndex++, _T("Stack"), LVCFMT_CENTER, 50); // strColTitle.Empty(); // strColTitle.LoadString(IDS_DB_SCRIPT_K); m_listMeasuData.InsertColumn(iColIndex++, _T("K"), LVCFMT_CENTER, 90); // strColTitle.Empty(); // strColTitle.LoadString(IDS_MEASU_TD_V); m_listMeasuData.InsertColumn(iColIndex++, _T("V(mV)"), LVCFMT_CENTER, 95); // strColTitle.Empty(); // strColTitle.LoadString(IDS_MEASU_TD_I); m_listMeasuData.InsertColumn(iColIndex++, _T("I(mA)"), LVCFMT_CENTER, 95); // strColTitle.Empty(); // strColTitle.LoadString(IDS_MEASU_TD_R0); m_listMeasuData.InsertColumn(iColIndex++, _T("R0(Ohm*m)"), LVCFMT_CENTER, 95); // strColTitle.Empty(); // strColTitle.LoadString(IDS_MEASU_TD_SP); m_listMeasuData.InsertColumn(iColIndex++, _T("SP(mV)"), LVCFMT_CENTER, 95); SetBtnStatus(EN_TIMER_PROC_MEASU_INITI); if (!GetCfgTaskPacketAttr()) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("获取配置信息失败.")); else MessageBoxEx(NULL, _T("Get config information failed."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } bool CDialMeasuTaskPacket::OperPLC(BYTE ucCmd, CString strPlcID, bool bIsShowErrSyn) { char chMsg[300]={0}; int iLen = 0; COperPLC::GetInstance()->GetPlcCmdInfo(ucCmd, chMsg, iLen, 300); if (iLen < 1) { return false; } DWORD dwDevID = atoi(strPlcID); CString strErr = _T(""); if (!theApp.m_NetWorkOper.SendCtrlMsgDirect(1,dwDevID, EN_DEV_PLC, chMsg, iLen)) { if (bIsShowErrSyn) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("同步失败")); else MessageBoxEx(NULL, _T("Synchronization failure."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } else { strErr.Empty(); strErr.Format(_T("[CDialTimerTaskMeasu][%d]send oper ctrl cmd(%d) failed."),__LINE__, ucCmd); CFileOperTools::GetInstance()->WriteComLog(strErr); } return false; } Sleep(300); if (EN_RECV_SUCCESS != theApp.m_NetWorkOper.RecvMsgDirect(chMsg, &iLen, 300, 90)) { if (bIsShowErrSyn) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("接收数据失败")); else MessageBoxEx(NULL, _T("Data received failed."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } else { strErr.Empty(); strErr.Format(_T("[CDialTimerTaskMeasu][%d]recv oper ctrl cmd(%d) response failed."),__LINE__, ucCmd); CFileOperTools::GetInstance()->WriteComLog(strErr); } return false; } if (iLen != sizeof(STRemPlcDataInfo)) { if (bIsShowErrSyn) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("长度校验失败")); else MessageBoxEx(NULL, _T("Length check failed."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } else { strErr.Empty(); strErr.Format(_T("[CDialTimerTaskMeasu][%d]recv oper ctrl cmd(%d) error,check format failed."),__LINE__, ucCmd); CFileOperTools::GetInstance()->WriteComLog(strErr); } return false; } STRemPlcDataInfo* pPlcInfo = (STRemPlcDataInfo*)chMsg; if ( 1== pPlcInfo->ucResult ) return true; else return false; } void CDialMeasuTaskPacket::StopMeasuDataInfo(bool bIsNormalExit) { int iID = 0; if (m_bIsOpenTbData) { m_TaskdDBOper.CloseTdData(); m_bIsOpenTbData = false; } m_bIsSigMeasuring = false; m_iRepeatSndCnt = -1; if (bIsNormalExit) m_ucProcStep = EN_TIMER_PROC_MEASU_FIN; else m_ucProcStep = EN_TIMER_PROC_MEASU_TRMI; } bool CDialMeasuTaskPacket::GetCfgTaskData(DWORD dwTdID,char* pData, int& iLen, int iMaxLen) { CString strLog = _T(""); iLen = 0; if (m_TaskdDBOper.QueryTdBasicInfo(dwTdID, &m_stQueryTdInfo)) { STMeasuTaskParam stTaskParam; memset(&stTaskParam, 0, sizeof(STMeasuTaskParam)); stTaskParam.ucTxPeriod = m_stQueryTdInfo.iTestPeriod; stTaskParam.ucTestType = m_stQueryTdInfo.iTestType; stTaskParam.ucSptType = m_stQueryTdInfo.iSptType; stTaskParam.ucCableLayout = m_stQueryTdInfo.iCableLayout; stTaskParam.ucStacking = m_stQueryTdInfo.ucStack; stTaskParam.iSAInterval = htonl(m_stQueryTdInfo.iSAInterval); stTaskParam.ucArrayType = m_stQueryTdInfo.iAR; stTaskParam.fElecSpace = tcp_htonf(m_stQueryTdInfo.fXElecDistance); stTaskParam.ucOrgFlg = m_stQueryTdInfo.ucOrgFlg; memcpy(stTaskParam.ucTaskID, m_stQueryTdInfo.ucTaskID, sizeof(stTaskParam.ucTaskID)); iLen = sizeof(STMeasuTaskParam); memcpy(pData, &stTaskParam, iLen); } else { strLog.Format(_T("[%s][%d]Query task information failed. taskid = %d"), dwTdID); CFileOperTools::GetInstance()->WriteComLog(strLog); if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("查询任务信息失败")); else MessageBoxEx(NULL, _T("Query task information failed."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return false; } return true; } int CDialMeasuTaskPacket::ProcRcvMeasuData() { CString strErr; if (m_iRecvDataLen < 1) { strErr.Empty(); strErr.Format(_T("[%s][%d]接收数据错误,接收字节数不能小于1"),MODULE_NAME, __LINE__); CFileOperTools::GetInstance()->WriteComLog(strErr); return EN_MEASU_PROC_DATA_ERR; } BYTE ucPacketNum = *((BYTE*)m_ucData+sizeof(m_stQueryTdInfo.ucTaskID)); if (ucPacketNum != m_vtMeasureData.size()) { strErr.Empty(); strErr.Format(_T("[%s][%d]测量数据包数目错误,本该测量包数=%d,实测包数=%d"), MODULE_NAME, __LINE__,m_vtMeasureData.size(),ucPacketNum); CFileOperTools::GetInstance()->WriteComLog(strErr); return EN_MEASU_PROC_DATA_ERR; } LPSTMeasuBasicDataRes pMeasuBasicRes = NULL; int iAttachLen = (1 == m_stQueryTdInfo.iTestType) ? sizeof(STMeasuDataResAttachIP) : 0; //这里暂时没有对粘包或者分包做处理 if (m_iRecvDataLen != (1 + sizeof(m_stQueryTdInfo.ucTaskID)+ucPacketNum*(sizeof(STMeasuDataResBasic)+iAttachLen))) { strErr.Empty(); strErr.Format(_T("[%s][%d]测量数据包长度错误,本该=%d,实际长度=%d"), MODULE_NAME, __LINE__,1+ucPacketNum*(sizeof(STMeasuDataResBasic)+iAttachLen),m_iRecvDataLen); CFileOperTools::GetInstance()->WriteComLog(strErr); return EN_MEASU_PROC_DATA_ERR; } for (int i = 0; i < ucPacketNum; i++) { pMeasuBasicRes = (LPSTMeasuBasicDataRes)(m_ucData + 1 + sizeof(m_stQueryTdInfo.ucTaskID) + i*(sizeof(STMeasuDataResBasic)+iAttachLen)); pMeasuBasicRes->iID = ntohl(pMeasuBasicRes->iID);//by quyx 20180902 if (pMeasuBasicRes->iID != m_vtMeasureData[i].dwTsn) { strErr.Empty(); strErr.Format(_T("[%s][%d]测量数据乱序,测量TSN(%d),实际接收数据的TSN(%d)"),MODULE_NAME, __LINE__, m_vtMeasureData[i].dwTsn, pMeasuBasicRes->iID); CFileOperTools::GetInstance()->WriteComLog(strErr); return EN_MEASU_PROC_DATA_ERR; } //处理告警 CString strMeasuStaus = _T(""); strMeasuStaus.Format(_T("Status: %s"), GetMeasuStatusByAlarm(pMeasuBasicRes->ucAlram)); GetDlgItem(IDC_STATIC_ALARM_PACKET)->SetWindowText(strMeasuStaus); //如果碰到下面告警,则测量必须要停下来 // if ((pMeasuBasicRes->ucAlram > 0 && pMeasuBasicRes->ucAlram < 3) || // (pMeasuBasicRes->ucAlram > 3 && pMeasuBasicRes->ucAlram < 7)) //modifyed by lsq 20171019 这里应张杰的要求,修改临时版本,允许过流保护和短路保护时,继续测量 if (2 == pMeasuBasicRes->ucAlram) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("遇到严重告警,终止测量")); else MessageBoxEx(NULL, _T("Stop measuring if severe warning is encountered."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return EN_MEASU_PROC_WARINNG; } //网络序转本机序 by quyx 20180902 pMeasuBasicRes->fI = tcp_ntohf(pMeasuBasicRes->fI); pMeasuBasicRes->fK = tcp_ntohf(pMeasuBasicRes->fK); pMeasuBasicRes->fR0 = tcp_ntohf(pMeasuBasicRes->fR0); pMeasuBasicRes->fR0_LC = tcp_ntohf(pMeasuBasicRes->fR0_LC); pMeasuBasicRes->fSP = tcp_ntohf(pMeasuBasicRes->fSP); pMeasuBasicRes->fV = tcp_ntohf(pMeasuBasicRes->fV); if (m_TaskdDBOper.UpdateTdBasicData(m_stQueryTdInfo.iSptType, m_stQueryTdInfo.iTestType,m_stQueryTdInfo.iTdChannelID, m_vtMeasureData[i].dwTsn, pMeasuBasicRes, m_ucData + 1 + sizeof(m_stQueryTdInfo.ucTaskID) + (i + 1)*sizeof(STMeasuDataResBasic)+i*iAttachLen)) { // m_iListDataID++; if (!m_bIsRunning) { return EN_MEASU_PROC_SUCCESS; } bool bIsNeedInsert = true; int iItemIndex; // if (m_iSelMeasuIndex>=0 && m_iSelMeasuIndex < m_dialListMeasuTask.m_listData.GetItemCount() ) // { // iItemIndex = m_iSelMeasuIndex++; // bIsNeedInsert = false; // } // else // { // m_iSelMeasuIndex = -1; if (m_listMeasuData.GetItemCount() >= MAX_PACKET_LIST_SHOW) m_listMeasuData.DeleteAllItems(); iItemIndex = m_listMeasuData.GetItemCount(); // } CString strTxt = _T(""); // if (bIsNeedInsert) // { strTxt.Empty(); strTxt.Format(_T("%d"), m_vtMeasureData[i].dwTsn); m_listMeasuData.InsertItem(iItemIndex,strTxt); // } int iColIndex = 1; strTxt.Empty(); if (0 == m_stQueryTdInfo.iSptType) strTxt.Format(_T("%.2f"), m_vtMeasureData[i].fA); else strTxt.Format(_T("%d"), (int)m_vtMeasureData[i].fA); m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt); strTxt.Empty(); if (0 == m_stQueryTdInfo.iSptType) strTxt.Format(_T("%.2f"), m_vtMeasureData[i].fB); else strTxt.Format(_T("%d"), (int)m_vtMeasureData[i].fB); m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt); strTxt.Empty(); if (0 == m_stQueryTdInfo.iSptType) strTxt.Format(_T("%.2f"), m_vtMeasureData[i].fM); else strTxt.Format(_T("%d"), (int)m_vtMeasureData[i].fM); m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt); strTxt.Empty(); if (0 == m_stQueryTdInfo.iSptType) strTxt.Format(_T("%.2f"), m_vtMeasureData[i].fN); else strTxt.Format(_T("%d"), (int)m_vtMeasureData[i].fN); m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt); strTxt.Empty(); strTxt.Format(_T("%d"), pMeasuBasicRes->ucStack); m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt); strTxt.Empty(); strTxt.Format(_T("%.2f"), pMeasuBasicRes->fK); m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt); strTxt.Empty(); strTxt.Format(_T("%.2f"), pMeasuBasicRes->fV); m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt); strTxt.Empty(); strTxt.Format(_T("%.2f"), pMeasuBasicRes->fI); m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt); strTxt.Empty(); strTxt.Format(_T("%.2f"), pMeasuBasicRes->fR0); m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt); strTxt.Empty(); strTxt.Format(_T("%.2f"), pMeasuBasicRes->fSP); m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt); m_listMeasuData.SetItemData(iItemIndex, m_vtMeasureData[i].dwTsn); m_listMeasuData.SetItemState(m_listMeasuData.GetItemCount()-1,LVNI_FOCUSED | LVIS_SELECTED, LVNI_FOCUSED | LVIS_SELECTED); m_listMeasuData.EnsureVisible(m_listMeasuData.GetItemCount()-1,false); // m_listMeasuData.SetFocus(); // m_listMeasuData.SetSelectionMark(m_listMeasuData.GetItemCount()-1); } else { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("插入测量数据信息失败")); else MessageBoxEx(NULL, _T("Failed to insert measurement data information."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return EN_MEASU_PROC_DB_ERR; } } return EN_MEASU_PROC_SUCCESS; } int CDialMeasuTaskPacket::GetNeedMeasuData(char* pData,int &iLen, int iMaxLen) { iLen = 0; m_vtMeasureData.clear(); if (m_TaskdDBOper.QueryNextTdBasicData(m_vtMeasureData) < 0) { return -1; } int iNum = m_vtMeasureData.size(); if (iNum > 0) { if (iMaxLen < iNum * sizeof(STSigSndDataInfo)) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("测量数据的长度是超长的")); else MessageBoxEx(NULL, _T("The length of measure data is overlength."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return -1; } memcpy(pData, m_stQueryTdInfo.ucTaskID, sizeof(m_stQueryTdInfo.ucTaskID)); pData[sizeof(m_stQueryTdInfo.ucTaskID)] = m_vtMeasureData.size(); //pData[0]= m_vtMeasureData.size(); for (int i = 0; i < m_vtMeasureData.size(); i++) { STSigSndDataInfo stSigData;//转网络序 memcpy(&stSigData, &m_vtMeasureData[i], sizeof(STSigSndDataInfo)); stSigData.dwTsn = htonl(stSigData.dwTsn); stSigData.fA = tcp_htonf(stSigData.fA); stSigData.fB = tcp_htonf(stSigData.fB); stSigData.fM = tcp_htonf(stSigData.fM); stSigData.fN = tcp_htonf(stSigData.fN); stSigData.fK = tcp_htonf(stSigData.fK); memcpy(pData + 1 + sizeof(m_stQueryTdInfo.ucTaskID) + i*sizeof(STSigSndDataInfo), &stSigData, sizeof(STSigSndDataInfo)); } iLen = 1 + sizeof(m_stQueryTdInfo.ucTaskID)+iNum*sizeof(STSigSndDataInfo); } return 1; } bool CDialMeasuTaskPacket::StartMeasuSigTask(const STTdBrowseInfo* pSigTask) { //首先开机 char chSndBuf[500] = {0}; int iSndLen = 0, iTimeout = 0, iMaxRcvLen = 0; // BYTE ucCmd; // m_ucProcStep =EN_TIMER_PROC_ROLLCALL; m_iRepeatSndCnt = -1; STRespDataInfo stRspData; STSendDataInfo stSendData; CString strLog; if (m_bIsOpenTbData) { m_TaskdDBOper.CloseTdData(); m_bIsOpenTbData = false; } GetDlgItem(IDC_STATIC_MEASU_TASK)->SetWindowText(pSigTask->strTaskName); strLog.Empty(); strLog.Format(_T("[%s][%d]Is ready to excute timer task(%s)"),MODULE_NAME, __LINE__, pSigTask->strTaskName); CFileOperTools::GetInstance()->WriteComLog(strLog); // m_bIsSigMeasuring = m_ucProcStep = EN_TIMER_PROC_CFG_TASK; //filled current test date and time to task by quyx 20180419 _CommandPtr pCmdUpd = NULL; CString szTDate = _T(""); CString szTTime = _T(""); CString szSql = _T(""); DWORD dwID = DWORD(pSigTask->iTaskID); szTDate.Empty(); szTDate.Format(_T("%04d-%02d-%02d"),g_sysCurTime.wYear, g_sysCurTime.wMonth, g_sysCurTime.wDay); szTTime.Empty(); szTTime.Format(_T("%02d:%02d:%02d"),g_sysCurTime.wHour, g_sysCurTime.wMinute, g_sysCurTime.wSecond); pCmdUpd.CreateInstance(_uuidof(Command)); pCmdUpd->ActiveConnection = theApp.m_pConnection; try { if ((szTDate.GetLength() != (int)VAL_ZERO) && (szTTime.GetLength() != (int)VAL_ZERO)) { szSql.Empty(); szSql.Format(_T("update td set Tdate = '%s',Ttime = '%s' where ID = %d"), szTDate, szTTime,dwID); theApp.m_pConnection->BeginTrans(); pCmdUpd->CommandText = szSql.AllocSysString(); pCmdUpd->Execute(NULL, NULL, adCmdText); theApp.m_pConnection->CommitTrans(); } } catch (_com_error e) { theApp.m_pConnection->RollbackTrans(); AfxMessageBox(e.Description()); return -1; } //end filled current test date and time to task by quyx 20180419 while(m_bIsRunning) { if (m_bIsSigMeasuring || m_bIsSuspend) { Sleep(30); continue; } // else if (EN_TIMER_PROC_ROLLCALL == m_ucProcStep) // { // m_ucCmd = EN_CTRL_ROLLCALL_CABLE; // GetRollCallData(chSndBuf, iSndLen, 500); // iMaxRcvLen = 200; // iTimeout = 40000; // } // else if (EN_TIMER_PROC_CFG_TASK == m_ucProcStep) { m_ucCmd = EN_CTRL_SET_TASK_PARAM; GetCfgTaskData(pSigTask->iTaskID,chSndBuf, iSndLen, 500); iMaxRcvLen = MAX_RECV_MEASURE; iTimeout = 12000; } else if (EN_TIMER_PROC_MEASU_DATA == m_ucProcStep) { if (!m_bIsOpenTbData) { if (!m_TaskdDBOper.OpenTdData(pSigTask->iTaskID,m_stQueryTdInfo.iSptType,1, theApp.m_ucMaxChannel)) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("打开数据表失败")); else MessageBoxEx(NULL, _T("Failed to open data table."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return false; } m_bIsOpenTbData = true; } m_ucCmd = EN_CTRL_MEASURE_DATA; iMaxRcvLen = MAX_RECV_MEASURE; //test为了屏蔽网络中断的影响,这里将单个点的最大超时时间改为1分钟 //那么最多9万重发机会的话, //激点数据后面再修改 iTimeout = 60000; if (m_iRepeatSndCnt <= 0) { //如果没有打开数据表的话,则进行打开数据表 memset(chSndBuf, 0, sizeof(chSndBuf)); iSndLen = 0; m_iRepeatSndCnt = -1;//初始状态 int iRes = GetNeedMeasuData(chSndBuf, iSndLen,500); if (0 == iSndLen) { if (iRes > 0) { StopMeasuDataInfo(); SetBtnStatus(m_ucProcStep); return true; } else { StopMeasuDataInfo(false); SetBtnStatus(m_ucProcStep); return false; } } } else { strLog.Empty(); strLog.Format(_T("[%s][%d]Is being repeat measure, reverse repeat cnt = %d"), MODULE_NAME, __LINE__, --m_iRepeatSndCnt); CFileOperTools::GetInstance()->WriteComLog(strLog); } } else { strLog.Empty(); strLog.Format(_T("[%s][%d]Unknow timer proc step(%d)"), MODULE_NAME, __LINE__,m_ucProcStep); CFileOperTools::GetInstance()->WriteComLog(strLog); SetBtnStatus(m_ucProcStep); return (m_ucProcStep == EN_TIMER_PROC_MEASU_FIN) ? true : false; } stSendData.pData = chSndBuf; stSendData.wDataLen = iSndLen; stSendData.ucDstType = EN_DEV_GD10; stSendData.uiDevID = m_uiDevID; ClearRecvBuffer(); stRspData.pData = (char*)m_ucData; stRspData.pRecvLen = &m_iRecvDataLen; stRspData.iMaxRcvLen = iMaxRcvLen; stRspData.iTimeout = iTimeout; if (false == theApp.m_NetWorkOper.TransferOper(m_ucCmd, &stSendData, true, &stRspData, NotifyResFunc, this)) { StopMeasuDataInfo(false); Sleep(30); continue; } m_bIsSigMeasuring = true; } if (!m_bIsRunning) return false; return true; } void CDialMeasuTaskPacket::SetBtnStatus(BYTE ucStatus) { switch (ucStatus) { case EN_TIMER_PROC_MEASU_INITI: case EN_TIMER_PROC_MEASU_TRMI: GetDlgItem(IDC_BUTTON_MEASU_ALL)->EnableWindow(TRUE); GetDlgItem(IDC_BUTTON_PAUSE)->EnableWindow(FALSE); GetDlgItem(IDC_BUTTON_CONTINUE)->EnableWindow(FALSE); GetDlgItem(IDC_BUTTON_CFG)->EnableWindow(TRUE); break; case EN_TIMER_PROC_CFG_TASK: case EN_TIMER_PROC_MEASU_DATA: case EN_TIMER_PROC_MEASU_FIN: GetDlgItem(IDC_BUTTON_MEASU_ALL)->EnableWindow(FALSE); GetDlgItem(IDC_BUTTON_PAUSE)->EnableWindow(TRUE); GetDlgItem(IDC_BUTTON_CONTINUE)->EnableWindow(FALSE); break; case EN_TIMER_PROC_MEASU_SUSPEND: GetDlgItem(IDC_BUTTON_MEASU_ALL)->EnableWindow(FALSE); GetDlgItem(IDC_BUTTON_PAUSE)->EnableWindow(FALSE); GetDlgItem(IDC_BUTTON_CONTINUE)->EnableWindow(TRUE); break; default: GetDlgItem(IDC_BUTTON_MEASU_ALL)->EnableWindow(FALSE); GetDlgItem(IDC_BUTTON_PAUSE)->EnableWindow(TRUE); GetDlgItem(IDC_BUTTON_CONTINUE)->EnableWindow(FALSE); break; } } UINT CDialMeasuTaskPacket::StartMeasuData(LPVOID pInfo) { CDialMeasuTaskPacket* pMeasuData = (CDialMeasuTaskPacket*)pInfo; char chSndBuf[500] = {0}; // if (m_vtTaskPacket.size() < 1) // { // AfxMessageBox(_T("can not found valid timer packet")); // return 0; // } // int iNeedLoopTimes =0, iIntervalTime = 0; // CString strPLCID = ""; // if (!pMeasuData->m_TaskdDBOper.QueryTaskPacketAttr(iNeedLoopTimes, iIntervalTime, strPLCID)) // { // AfxMessageBox(_T("can not quert task packet attr")); // return 0; // } pMeasuData->m_bIsThreadFinshed = false; CString strErr = _T(""); std::vector vtNewTaskPacket; CString strTime; int i; bool bRes = true; DWORD dwBeginTick = 0, dwCurTick = 0; int iIntveralTick = pMeasuData->m_iInterval * 60 * 1000; int iCurLoopCnt = 0; while(pMeasuData->m_bIsRunning) { if ( pMeasuData->m_bIsSuspend) { Sleep(30); continue; } //start to timer if (dwBeginTick > 0) { if((GetTickCount() - dwBeginTick) < iIntveralTick) { Sleep(200); continue; } } vtNewTaskPacket.clear(); strTime.Empty(); strTime.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"),g_sysCurTime.wYear, g_sysCurTime.wMonth, g_sysCurTime.wDay,g_sysCurTime.wHour,g_sysCurTime.wMinute,g_sysCurTime.wSecond); pMeasuData->m_TaskdDBOper.CreateTaskPacket(pMeasuData->m_vtTaskPacket, strTime, vtNewTaskPacket); for (i = 0; i < vtNewTaskPacket.size(); i++) { if (!pMeasuData->m_bIsRunning) { break; } bRes = pMeasuData->StartMeasuSigTask(&(vtNewTaskPacket[i])); if (!bRes) { break; } if (i < (vtNewTaskPacket.size()-1)) Sleep(30000); } if (!bRes) { //在这里m_bIsRunning如果为false,则表示是点击关闭按钮引起的,并不是测量错误 if (pMeasuData->m_bIsRunning) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("执行任务错误")); else MessageBoxEx(NULL, _T("Excute task error."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } break; } iCurLoopCnt++; if (iCurLoopCnt >= pMeasuData->m_iLoopTimes) break; dwBeginTick = GetTickCount(); } int iSndLen = 0; if ((iCurLoopCnt >= pMeasuData->m_iLoopTimes) && pMeasuData->m_bIsRunning) { pMeasuData->GetDlgItem(IDC_STATIC_MEASU_TASK)->SetWindowText("Measure finished"); if (false == pMeasuData->OperPLC(EN_PLC_POWER_OFF, pMeasuData->m_strPlcID,false)) { CString strErr = _T(""); strErr.Format(_T("[%s][%d]oper timer task power off cmd failed."),MODULE_NAME,__LINE__); CFileOperTools::GetInstance()->WriteComLog(strErr); } if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("测量失败")); else MessageBoxEx(NULL, _T("Measure finished"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } pMeasuData->m_bIsThreadFinshed = true; theApp.m_NetWorkOper.ClearNotfiyFuncInfo(); if (pMeasuData->m_bIsRunning) pMeasuData->SetBtnStatus(EN_TIMER_PROC_MEASU_TRMI); // pMeasuData->GetDlgItem(IDC_STATIC_MEASU_TASK)->SetWindowText("Measure finished"); // AfxMessageBox(_T("Measure finished")); pMeasuData->m_pThread = NULL; return 1; } void CDialMeasuTaskPacket::NotifyResFunc(LPARAM lOperType, LPARAM lOperRes, LPVOID lpWnd) { if (NULL == lpWnd) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("通知窗口指针不能为空")); else MessageBoxEx(NULL, _T("The pointer to the notification window cannot be empty"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return; } CDialMeasuTaskPacket* pDialMeasu = (CDialMeasuTaskPacket*)lpWnd; CString strTips = _T(""); //如果是发送失败,或者是下发任务失败的话,则直接进行提示 //按照和舒嘉讨论的结果,为了屏蔽网络的原因,现在模式改成如果没有收到相应的响应报文,就一直进行数据重发操作 //所以这段中断流程要屏蔽 /* if (EN_RECV_SUCCESS != lOperRes && (EN_NETWORK_SEND == lOperType || EN_CTRL_MEASURE_DATA != pDialMeasu->m_ucCmd)) { pDialMeasu->StopMeasuDataInfo(false); strTips.Empty(); strTips.Format(_T("[%s][%d]NotifyResFunc error. OperType = %d, OperRes = %d, cmd = %d"), MODULE_NAME, __LINE__,lOperType, lOperRes, pDialMeasu->m_ucCmd ); CFileOperTools::GetInstance()->WriteComLog(strTips); strTips.Empty(); strTips.Format(_T("测量失败,出错代码为(%d,%d,%d)"), lOperType, lOperRes,pDialMeasu->m_ucCmd); AfxMessageBox(strTips); return; } */ CString strLog = _T(""); if(EN_CTRL_MEASURE_DATA == pDialMeasu->m_ucCmd) { //如果是超时或者是数据错误的话,则运行进行重测 if (lOperRes == EN_RECV_TIMEOUT || lOperRes == EN_RECV_DATA_ERROR||lOperRes == EN_RECV_DATA_DISORDER || EN_CLOUND_SND_FAILED == lOperRes) { if (-1 == pDialMeasu->m_iRepeatSndCnt) { strLog.Empty(); strLog.Format(_T("[%s][%d]Is being trigger repeat measure, tripper reason = %d"), MODULE_NAME, __LINE__, lOperRes); CFileOperTools::GetInstance()->WriteComLog(strLog); pDialMeasu->m_iRepeatSndCnt = MAX_TIMER_REPEAT_SND_CNT+1; } else { strLog.Empty(); strLog.Format(_T("[%s][%d]measure data failed, OperType = %d,OperRes = %d,NeedRepeatCnt = %d"), MODULE_NAME, __LINE__,lOperType,lOperRes, pDialMeasu->m_iRepeatSndCnt); CFileOperTools::GetInstance()->WriteComLog(strLog); } } else if (lOperRes == EN_RECV_SUCCESS) { //如果测量成功,则给下面50毫秒的时间间隔 int iRes = pDialMeasu->ProcRcvMeasuData(); if (iRes == EN_MEASU_PROC_DB_ERR || iRes == EN_MEASU_PROC_WARINNG) { pDialMeasu->StopMeasuDataInfo(false); return; } else if (iRes == EN_MEASU_PROC_SUCCESS) pDialMeasu->m_iRepeatSndCnt = -1; else if (pDialMeasu->m_iRepeatSndCnt == -1) pDialMeasu->m_iRepeatSndCnt = MAX_TIMER_REPEAT_SND_CNT+1; } else { pDialMeasu->StopMeasuDataInfo(false); if (LANG_ZHCN == g_iUILanguage) { strTips.Format(_T("测量数据失败,出错代码为(%d,%d)"), lOperType, lOperRes); AfxMessageBox(strTips); } else { strTips.Format(_T("Measurement data failed, error code is (%d,%d)"), lOperType, lOperRes); MessageBoxEx(NULL, strTips, STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } return; } } //如果是点名或者设置设备参数的话,能执行到这部表示已经执行成功了,此时则进行下一步的操作 else if (EN_CTRL_ROLLCALL_CABLE == pDialMeasu->m_ucCmd || EN_CTRL_SET_TASK_PARAM == pDialMeasu->m_ucCmd || EN_PLC_POWER_ON == pDialMeasu->m_ucCmd) { //只有在成功的时候才会进行下一步的操作 if (EN_RECV_SUCCESS == lOperRes) { //如果是对PLC进行开关机的话,则需要判断是否开机正确 if (EN_TIMER_PROC_PLC_POWERON == pDialMeasu->m_ucProcStep) { int iPlcRes = COperPLC::GetInstance()->ParsePlcOperResInfo((char*)(pDialMeasu->m_ucData),pDialMeasu->m_iRecvDataLen); if (EN_PLC_RES_SUCCESS == iPlcRes) pDialMeasu->m_ucProcStep++; else { strLog.Empty(); strLog.Format(_T("[%s][%d]excute plc cmd failed, res = %d, recv_data_len = %d"), MODULE_NAME, __LINE__, iPlcRes, pDialMeasu->m_iRecvDataLen); CFileOperTools::GetInstance()->WriteComLog(strLog); } } else pDialMeasu->m_ucProcStep++; } else { strLog.Empty(); strLog.Format(_T("[%s][%d]excute ctrl cmd failed, proc_step = %d, res = %d"), MODULE_NAME, __LINE__, pDialMeasu->m_ucProcStep,lOperRes); CFileOperTools::GetInstance()->WriteComLog(strLog); } } else { strLog.Empty(); strLog.Format(_T("[%s][%d]Unknow ctrl command(%d)"), MODULE_NAME, __LINE__, pDialMeasu->m_ucCmd); CFileOperTools::GetInstance()->WriteComLog(strLog); } pDialMeasu->m_bIsSigMeasuring = false; } bool CDialMeasuTaskPacket::QuitDial() { if (m_bIsRunning) { int nRet = 0; if (LANG_ZHCN == g_iUILanguage) nRet = AfxMessageBox(_T("是否确定关闭?"), MB_YESNO, MB_ICONQUESTION); else nRet = MessageBoxEx(NULL, _T("Are you sure to colse?"), STRING_MESSAGEBOXEX_TITLE, MB_YESNO | MB_ICONQUESTION, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); if (IDYES != nRet) { return false; } } StopMeasuDataInfo(false); ThreadJoin(); return true; } void CDialMeasuTaskPacket::OnClose() { // TODO: Add your message handler code here and/or call default CDialog::OnClose(); } void CDialMeasuTaskPacket::OnCancel() { // TODO: Add extra cleanup here if (!QuitDial()) { return; } CDialog::OnCancel(); }