// MTerm1CtrlView.cpp: 实现文件 // // MultiTerminal1Dlg.cpp: 实现文件 // #include "pch.h" #include "framework.h" #include "MTerm1.h" #include "MTerm1Doc.h" #include "ChildFrm.h" #include "MTerm1CommDevView.h" #include #include #include "../MyLib/Functions.hpp" #include "../MyLib/UI_Func/UI_Func.hpp" #include "AnsiParser.h" #include "DataParser1.h" //#include "KLink.h" //#include "../MyLib/SerialCom/SerialCom.hpp" #include #include "DialogCommSet1.h" #include "DialogSysRegSet.h" #include "DialogStatusShow.h" #include "DialogInfoDisplay.h" #include "DialogCoilMon.h" #include "DialogDataMon.h" #include "DialogProgress.h" #include "DialogEventLog.h" #include "DialogDateTime.h" #include "DialogForceIO.h" //#include "HvSerialPort.h" #ifdef _DEBUG #define new DEBUG_NEW #endif //#pragma comment(lib,"setupapi.lib") bool bAutoStart; //int nComPort; //int nBaud; stCfgValCtrl myCfgValCtrls[] = { "AutoStart", vtbool, &bAutoStart, IDC_CHECK_AUTO_START, ctCheckBox, // "LastComPort", vtInt, &nComPort, IDC_COMBO_COMPORT, ctComboBoxIndex, // "Baud", vtInt, &nBaud, IDC_COMBO_COM_BAUD, ctComboBoxString, "Ref1", NULL, nullptr, IDC_EDIT_REF1, ctEdit, }; int nMarkConfigs = sizeof(myCfgValCtrls) / sizeof(stCfgValCtrl); int nTest1 = 1; float nTest2 = 2; double nTest3 = 3; int MonitorTotalCount = 0; int MonitorSuccessCount = 0; int MonitorFailCount = 0; int MonitorSpeed = 0; int LastMonitorTotalCount = 0; stCfgValCtrl myCfgTest1[] = { "Test1", vtInt, &nTest1, 0,0, "Test2", vtFloat, &nTest2, 0,0, "Test3", vtDouble, &nTest3, 0,0, }; int nCfgTestCount = sizeof(myCfgTest1) / sizeof(stCfgValCtrl); unsigned char send1[40000]; unsigned char recv1[40000]; volatile int nDataToSend = 0; volatile int BulkSendTest = 0; unsigned char SDTbuf[512]; unsigned char WYbuf[256]; unsigned char WDTbuf[512]; // CMTerm1CommDevView IMPLEMENT_DYNCREATE(CMTerm1CommDevView, CFormView) CMTerm1CommDevView::CMTerm1CommDevView() : CFormView(IDD_MTerm1CommDevView2) { } CMTerm1CommDevView::~CMTerm1CommDevView() { } void CMTerm1CommDevView::DoDataExchange(CDataExchange* pDX) { CFormView::DoDataExchange(pDX); DDX_Control(pDX, IDC_COMBO_COMPORT, m_combo_comport); DDX_Control(pDX, IDC_COMBO_COM_BAUD, m_combo_com_baud); DDX_Control(pDX, IDC_EDIT_DISPLAY, m_edit_display); DDX_Control(pDX, IDC_COMBO_INPUT, m_combo_input); // DDX_Control(pDX, IDC_TREE1, m_treectrl1); DDX_Control(pDX, IDC_EDIT_MON, m_edit_mon1); DDX_Control(pDX, IDC_EDIT_MON2, m_edit_mon2); DDX_Control(pDX, IDC_STATIC_CONNECT, m_static_connect); } BEGIN_MESSAGE_MAP(CMTerm1CommDevView, CFormView) // ON_WM_SYSCOMMAND() // ON_WM_PAINT() // ON_WM_CLOSE() // ON_WM_SIZE() ON_WM_TIMER() ON_BN_CLICKED(IDC_BUTTON_CONNECT, &CMTerm1CommDevView::OnBnClickedButtonConnect) ON_BN_CLICKED(IDC_BUTTON_DISCONNECT, &CMTerm1CommDevView::OnBnClickedButtonDisconnect) ON_BN_CLICKED(IDC_BUTTON_START, &CMTerm1CommDevView::OnBnClickedButtonStart) ON_BN_CLICKED(IDC_BUTTON_STOP, &CMTerm1CommDevView::OnBnClickedButtonStop) ON_BN_CLICKED(IDC_BUTTON_CLRSCR, &CMTerm1CommDevView::OnBnClickedButtonClrscr) ON_BN_CLICKED(IDC_BUTTON_SEND, &CMTerm1CommDevView::OnBnClickedButtonSend) // ON_CBN_EDITCHANGE(IDC_COMBO_INPUT, &CMTerm1CommDevView::OnCbnEditchangeComboInput) // ON_CBN_SELCHANGE(IDC_COMBO_RES, &CMTerm1CommDevView::OnCbnSelchangeComboRes) ON_BN_CLICKED(IDC_BUTTON_DO, &CMTerm1CommDevView::OnBnClickedButtonDo) // ON_CBN_SELCHANGE(IDC_COMBO_CMD_TYPE, &CMTerm1CommDevView::OnCbnSelchangeComboCmdType) // ON_CBN_SELCHANGE(IDC_COMBO_DCOUNT, &CMTerm1CommDevView::OnCbnSelchangeComboDcount) // ON_BN_CLICKED(IDC_BUTTON_START_BULK_TEST, &CMTerm1CommDevView::OnBnClickedButtonStartBulkTest) // ON_BN_CLICKED(IDC_BUTTON_STOP_BULK_TEST, &CMTerm1CommDevView::OnBnClickedButtonStopBulkTest) // ON_WM_DEVICECHANGE() // ON_WM_INITMENUPOPUP() // ON_WM_MOUSEWHEEL() // ON_BN_CLICKED(IDC_BUTTON_CLEAR_STAT, &CMTerm1CommDevView::OnBnClickedButtonClearStat) // ON_BN_CLICKED(IDC_BUTTON_READ, &CMTerm1CommDevView::OnBnClickedButtonRead) // ON_BN_CLICKED(IDC_BUTTON11, &CMTerm1CommDevView::OnBnClickedButton11) ON_BN_CLICKED(IDC_BUTTON12, &CMTerm1CommDevView::OnBnClickedButton12) ON_BN_CLICKED(IDC_BUTTON13, &CMTerm1CommDevView::OnBnClickedButton13) ON_BN_CLICKED(IDC_BUTTON31, &CMTerm1CommDevView::OnBnClickedButton31) ON_BN_CLICKED(IDC_BUTTON2, &CMTerm1CommDevView::OnBnClickedButton2) ON_BN_CLICKED(IDC_BUTTON3, &CMTerm1CommDevView::OnBnClickedButton3) END_MESSAGE_MAP() // CMTerm1CommDevView 诊断 #ifdef _DEBUG void CMTerm1CommDevView::AssertValid() const { CFormView::AssertValid(); } #ifndef _WIN32_WCE void CMTerm1CommDevView::Dump(CDumpContext& dc) const { CFormView::Dump(dc); } #endif #endif //_DEBUG // CMTerm1CommDevView 消息处理程序 void CMTerm1CommDevView::OnInitialUpdate() { CFormView::OnInitialUpdate(); // TODO: 在此添加专用代码和/或调用基类 // ResizeParentToFit(); // ((CChildFrame *)GetParentFrame())->RecalcLayout(); m_pStatusBar = ((CChildFrame *)GetParentFrame())->GetStatusBar(); m_bResourceOpened = 0; LoadResourceList(); DisplayParams(); LoadModel(); /* // m_treectrl1.SetExtendedStyle(TVS_EX_DRAWIMAGEASYNC,TVS_EX_DRAWIMAGEASYNC); m_ColorTreeImages.Create(IDB_FILE_VIEW, 16, 0, RGB(255, 0, 255)); // m_ColorTreeImages.Create(IDB_PNG1,220,0,RGB(255,0,255)); m_treectrl1.SetImageList(&m_ColorTreeImages, TVSIL_NORMAL); HTREEITEM hItem, hGroupItem, hSubItem; hItem = m_treectrl1.InsertItem(_T("Parent1"), 0, 0, TVI_ROOT); // 在根结点上添加Parent1 hGroupItem = m_treectrl1.InsertItem(_T("Group1_1"), 1, 1, hItem);//在Parent1上添加一个子结点 hSubItem = m_treectrl1.InsertItem(_T("Child1_1"), 2, 2, hGroupItem);//在Parent1上添加一个子结点 hSubItem = m_treectrl1.InsertItem(_T("Child1_2"), 2, 2, hGroupItem, hSubItem);//在Parent1上添加一个子结点,排在Child1_1后面 hSubItem = m_treectrl1.InsertItem(_T("Child1_3"), 2, 2, hGroupItem, hSubItem); hGroupItem = m_treectrl1.InsertItem(_T("Group1_2"), 1, 1, hItem, hGroupItem);//在Parent1上添加一个子结点 hSubItem = m_treectrl1.InsertItem(_T("Child1_4"), 2, 2, hGroupItem);//在Parent1上添加一个子结点 hSubItem = m_treectrl1.InsertItem(_T("Child1_5"), 2, 2, hGroupItem, hSubItem);//在Parent1上添加一个子结点,排在Child1_1后面 hSubItem = m_treectrl1.InsertItem(_T("Child1_6"), 2, 2, hGroupItem, hSubItem); hItem = m_treectrl1.InsertItem(_T("Parent2"), TVI_ROOT, hItem); hGroupItem = m_treectrl1.InsertItem(_T("Group2_1"), 1, 1, hItem);//在Parent1上添加一个子结点 hSubItem = m_treectrl1.InsertItem(_T("Child2_1"), 2, 2, hGroupItem);//在Parent1上添加一个子结点 hSubItem = m_treectrl1.InsertItem(_T("Child2_2"), 2, 2, hGroupItem, hSubItem);//在Parent1上添加一个子结点,排在Child1_1后面 hSubItem = m_treectrl1.InsertItem(_T("Child2_3"), 2, 2, hGroupItem, hSubItem); hGroupItem = m_treectrl1.InsertItem(_T("Group2_2"), 1, 1, hItem, hGroupItem);//在Parent1上添加一个子结点 hSubItem = m_treectrl1.InsertItem(_T("Child2_4"), 2, 2, hGroupItem);//在Parent1上添加一个子结点 hSubItem = m_treectrl1.InsertItem(_T("Child2_5"), 2, 2, hGroupItem, hSubItem);//在Parent1上添加一个子结点,排在Child1_1后面 hSubItem = m_treectrl1.InsertItem(_T("Child2_6"), 2, 2, hGroupItem, hSubItem); hItem = m_treectrl1.InsertItem(_T("Parent3"), TVI_ROOT, hItem); hGroupItem = m_treectrl1.InsertItem(_T("Group3_1"), 1, 1, hItem);//在Parent1上添加一个子结点 hSubItem = m_treectrl1.InsertItem(_T("Child3_1"), 2, 2, hGroupItem);//在Parent1上添加一个子结点 hSubItem = m_treectrl1.InsertItem(_T("Child3_2"), 2, 2, hGroupItem, hSubItem);//在Parent1上添加一个子结点,排在Child1_1后面 hSubItem = m_treectrl1.InsertItem(_T("Child3_3"), 2, 2, hGroupItem, hSubItem); hGroupItem = m_treectrl1.InsertItem(_T("Group3_2"), 1, 1, hItem, hGroupItem);//在Parent1上添加一个子结点 hSubItem = m_treectrl1.InsertItem(_T("Child3_4"), 2, 2, hGroupItem);//在Parent1上添加一个子结点 hSubItem = m_treectrl1.InsertItem(_T("Child3_5"), 2, 2, hGroupItem, hSubItem);//在Parent1上添加一个子结点,排在Child1_1后面 hSubItem = m_treectrl1.InsertItem(_T("Child3_6"), 2, 2, hGroupItem, hSubItem); */ // TVINSERTSTRUCT TVI_ROOT // m_treectrl1.SetItemData(htree15,15); // m_status_Cusp_Size.SetCtlColor(RGB(0xff, 0x40, 0x40)); // m_status_Hz.SetCtlColor(RGB(0xff, 0x40, 0x40)); // m_status_db.SetCtlColor(RGB(0xff, 0x40, 0x40)); SetTimer(0, 50, NULL);//delayinit SetTimer(2, 100, NULL);//display m_static_connect.SetCtlColor(RGB(0, 255, 0)); } int CMTerm1CommDevView::DelayInit() { // InitCamera(); // InitPlcComm(); // InitAnalyzer(); SetTimer(1, 20, NULL); //plc SetTimer(3, 300, NULL); //Analyzer SetTimer(4, 1000, NULL); //Speed; m_static_connect.SetCtlColor(RGB(255, 0, 0)); // m_static_connect.RedrawWindow(); return 0; } int CMTerm1CommDevView::Clear_COM_Stats() { // TODO: 在此处添加实现代码. CMTerm1Doc *pDoc = (CMTerm1Doc *)GetDocument(); pDoc->MyKLink1.MySerPort1.TotalSendBytes = 0; pDoc->MyKLink1.MySerPort1.TotalRecvBytes = 0; MonitorTotalCount = 0; MonitorSuccessCount = 0; MonitorFailCount = 0; return 0; } int CMTerm1CommDevView::UpdateDataDisplay() { CMTerm1Doc *pDoc = (CMTerm1Doc *)GetDocument(); if (!m_bOnline) return 0; static int stepCount = 0; CString s1, s2; pDoc->MyKLink1.MySerPort1.CalSpeed(); s1.Format(_T("Send %8d %5d Bps \r\nRecv %8d %5d Bps\r\n"), pDoc->MyKLink1.MySerPort1.TotalSendBytes, pDoc->MyKLink1.MySerPort1.SendSpeed, pDoc->MyKLink1.MySerPort1.TotalRecvBytes, pDoc->MyKLink1.MySerPort1.RecvSpeed); // s1.AppendFormat(_T("Total S %8d \r\nTotal R %8d \r\n"), MySerialCom1.TotalSendBytes, MySerialCom1.TotalRecvBytes); s1 += intToString(pDoc->MyKLink1.MySerPort1.TotalRecvBytes) + _T("\r\n"); // stepCount++; // static double LastCalTime = 0; // double thisTime = GetTimemS(); pDoc->MyKLink1.CalSpeed(); /* if (thisTime - LastCalTime > 500) { MonitorSpeed = int ((MonitorTotalCount - LastMonitorTotalCount) * 1000 / (thisTime - LastCalTime)); LastMonitorTotalCount = MonitorTotalCount; LastCalTime = thisTime; // stepCount = 0; } */ s1.AppendFormat(_T("MT %6d Seq %d/%d %d/S \r\nOK %6d NG %d "), pDoc->MyKLink1.m_nTotalSendCount, pDoc->MyKLink1.m_nSeq, pDoc->MyKLink1.m_nRSeq, pDoc->MyKLink1.m_SendSpeed, pDoc->MyKLink1.m_nTotalRecvCount, pDoc->MyKLink1.m_nTotalSendCount-pDoc->MyKLink1.m_nTotalRecvCount); SetDlgItemText(IDC_STATIC_SPEED, s1); int nCount = 32; s2.Empty();// s2.AppendFormat(_T("%d "), MonitorSuccessCount); nCount = 16; s2.Append(_T("WX ")); for (int i = 0; i < (nCount + 15) / 16; i++) { //s2.AppendFormat(_T("%02X: "), i * 16); for (int j = 0; j < 8; j++) { s2.AppendFormat(_T("%04X"), pDoc->MyKLink1.MEM.WX[i * 8 + j]); if (j == 3) { s2.Append(_T(" ")); } else { s2.Append(_T(" ")); } } s2.Append(_T("\r\n")); } nCount = 16; s2.Append(_T("WY ")); for (int i = 0; i < (nCount + 15) / 16; i++) { // s2.AppendFormat(_T("%02X: "), i * 16); for (int j = 0; j < 8; j++) { s2.AppendFormat(_T("%04X"), pDoc->MyKLink1.MEM.WY[i * 8 + j]); if (j == 3) { s2.Append(_T(" ")); } else { s2.Append(_T(" ")); } } s2.Append(_T("\r\n")); } nCount = 32; s2.Append(_T(" WR\r\n")); for (int i = 0; i < (nCount + 15) / 16; i++) { s2.AppendFormat(_T("%02X: "), i * 16); for (int j = 0; j < 8; j++) { s2.AppendFormat(_T("%04X"), pDoc->MyKLink1.MEM.WR[i * 8 + j]); if (j == 3) { s2.Append(_T(" ")); } else { s2.Append(_T(" ")); } } s2.Append(_T("\r\n")); } nCount = 96; s2.Append(_T(" DT\r\n")); for (int i = 0; i < (nCount + 15) / 16; i++) { s2.AppendFormat(_T("%02X: "), i * 16); for (int j = 0; j < 8; j++) { s2.AppendFormat(_T("%04X"), pDoc->MyKLink1.MEM.DT[i * 8 + j]); if (j == 3) { s2.Append(_T(" ")); } else { s2.Append(_T(" ")); } } s2.Append(_T("\r\n")); } nCount = 256; s2.Append(_T(" SDT\r\n")); for (int i = 0; i < (nCount + 15) / 16; i++) { s2.AppendFormat(_T("%3X: "), i * 16); for (int j = 0; j < 8; j++) { s2.AppendFormat(_T("%04X"), pDoc->MyKLink1.MEM.SDT[i * 8 + j]); if (j == 3) { s2.Append(_T(" ")); } else { s2.Append(_T(" ")); } } s2.Append(_T("\r\n")); } if (m_bMonitoring) { SetDlgItemText(IDC_EDIT_MON, s2); } s2.Empty(); nCount = 256; for (int i = 0; i < (nCount + 15) / 16; i++) { s2.AppendFormat(_T("%3X: "), i * 16); for (int j = 0; j < 16; j++) { s2.AppendFormat(_T("%02X"), pDoc->MyKLink1.MEM.WDB[i * 16 + j]); if (j == 7) { s2.Append(_T(" ")); } else { s2.Append(_T(" ")); } } s2.Append(_T("\r\n")); } if (m_bMonitoring) { SetDlgItemText(IDC_EDIT_MON3, s2); } // s2.AppendFormat(_T("%d\r\n"), pDoc->MyKLink1.MEM.SDD[5]); nCount = 32; //SetRedraw(FALSE); s2.Empty(); if (m_bMonitoring) { s1.Empty(); s1.Format(_T("主机\r\n")); s1.AppendFormat(_T("00 跳线 %02X %s 当前 %02X\r\n"), pDoc->MyKLink1.MEM.SDD[0], intToBinString(pDoc->MyKLink1.MEM.SDD[0]), pDoc->MyKLink1.MEM.SDD[1]); // s1.AppendFormat(_T("当前跳线 %02X %s\r\n"), pDoc->MyKLink1.MEM.SDD[1], intToBinString(pDoc->MyKLink1.MEM.SDD[1])); s1.AppendFormat(_T("02 Tick计数 %u \r\n"), pDoc->MyKLink1.MEM.SDD[2]); s1.AppendFormat(_T("03 nRunCount %u \r\n"), pDoc->MyKLink1.MEM.SDD[3]); s1.AppendFormat(_T("04 RunStat %d \r\n"), pDoc->MyKLink1.MEM.SDD[4]); s1.AppendFormat(_T("05 ErrStat %d \r\n"), pDoc->MyKLink1.MEM.SDD[5]); s1.AppendFormat(_T("06 PwrOnCount %d \r\n"), pDoc->MyKLink1.MEM.SDD[6]); s1.AppendFormat(_T("07 ThisRunTime %d \r\n"), pDoc->MyKLink1.MEM.SDD[7]); int nTime1 = pDoc->MyKLink1.MEM.SDD[7]; s1.AppendFormat(_T("%dd %02d:%02d:%02d \r\n"), nTime1 / 86400, nTime1 / 3600 % 24, nTime1 / 60 % 60, nTime1 % 60); s1.AppendFormat(_T("08 TotalTime %d \r\n"), pDoc->MyKLink1.MEM.SDD[8]); nTime1 = pDoc->MyKLink1.MEM.SDD[8]; s1.AppendFormat(_T("%dd %02d:%02d:%02d \r\n"), nTime1 / 86400, nTime1 / 3600 % 24, nTime1 / 60 % 60, nTime1 % 60); s1.AppendFormat(_T("09 CurTime %d \r\n"), pDoc->MyKLink1.MEM.SDD[9]); nTime1 = pDoc->MyKLink1.MEM.SDD[9]; __time32_t time1 = nTime1; CString s3; //_tctime32_s(s3.GetBuffer(1024),1024, &time1); //s3.ReleaseBuffer(); CTime ctime1 = time1; if (nTime1 >= 0) s3 = ctime1.Format(_T("%Y-%m-%d %H:%M:%S")); s1.Append(s3 + _T("\r\n")); // s1.AppendFormat(_T("%04d-%02d-%02d %02d:%02d:%02d \r\n"), nTime1 / 86400, nTime1 / 3600 % 24, nTime1 / 60 % 60, nTime1 % 60); s1.AppendFormat(_T("10 PwrFailCount %d \r\n"), pDoc->MyKLink1.MEM.SDD[10]); s1.AppendFormat(_T("11 LastPwrFailTime %d \r\n"), pDoc->MyKLink1.MEM.SDD[11]); nTime1 = pDoc->MyKLink1.MEM.SDD[11]; time1 = nTime1; _tctime32_s(s3.GetBuffer(1024), 1024, &time1); s3.ReleaseBuffer(); ctime1 = time1; ctime1 += (30 * 365 + 7) * 24 * 3600; ctime1 = CStringToCTime(_T("2000-01-01 08:00:00")); ctime1 += nTime1; s3 = ctime1.Format(_T("%Y-%m-%d %H:%M:%S")); s1.Append(s3 + _T("\r\n")); s1.AppendFormat(_T("12 LastScanTime uS %u \r\n"), pDoc->MyKLink1.MEM.SDD[12]); s1.AppendFormat(_T("13 ScanTime uS %d \r\n"), pDoc->MyKLink1.MEM.SDD[13]); s1.AppendFormat(_T("14 MinScanTime uS %d \r\n"), pDoc->MyKLink1.MEM.SDD[14]); s1.AppendFormat(_T("15 %d \r\n"), pDoc->MyKLink1.MEM.SDD[15]); s1.AppendFormat(_T("16 %d \r\n"), pDoc->MyKLink1.MEM.SDD[16]); s1.AppendFormat(_T("17 %d \r\n"), pDoc->MyKLink1.MEM.SDD[17]); s1.AppendFormat(_T("18 %d \r\n"), pDoc->MyKLink1.MEM.SDD[18]); s1.AppendFormat(_T("19 %d \r\n"), pDoc->MyKLink1.MEM.SDD[19]); s1.AppendFormat(_T("20 %d \r\n"), pDoc->MyKLink1.MEM.SDD[20]); s1.AppendFormat(_T("21 %d \r\n"), pDoc->MyKLink1.MEM.SDD[21]); s1.AppendFormat(_T("22 %d \r\n"), pDoc->MyKLink1.MEM.SDD[22]); s1.AppendFormat(_T("23 %d \r\n"), pDoc->MyKLink1.MEM.SDD[23]); float Vref = 1.2f; float Vcor = (float)pDoc->MyKLink1.MEM.SDT[53] / pDoc->MyKLink1.MEM.SDT[55]; float V33 = 3.3f * Vcor; s1.AppendFormat(_T("24 24V电压 %d %.2f V \r\n"), pDoc->MyKLink1.MEM.SDT[48], pDoc->MyKLink1.MEM.SDT[48] * V33 / 4096 * 11); s1.AppendFormat(_T("25 %d \r\n"), pDoc->MyKLink1.MEM.SDT[49]); s1.AppendFormat(_T("26 5V 电压 %d %.3f V \r\n"), pDoc->MyKLink1.MEM.SDT[50], pDoc->MyKLink1.MEM.SDT[50] * V33 / 4096 * 2); s1.AppendFormat(_T("27 %d \r\n"), pDoc->MyKLink1.MEM.SDT[51]); s1.AppendFormat(_T("28 %d \r\n"), pDoc->MyKLink1.MEM.SDT[52]); s1.AppendFormat(_T("29 %d \r\n"), pDoc->MyKLink1.MEM.SDT[53]); float temp = (1430 - pDoc->MyKLink1.MEM.SDT[54]* 0.806f ) / 4.3f + 25; s1.AppendFormat(_T("30 芯片温度 %d %.1f ℃ \r\n"), pDoc->MyKLink1.MEM.SDT[54],temp); s1.AppendFormat(_T("31 1.2V参考 %d 3.3V电压 %.3f V \r\n"), pDoc->MyKLink1.MEM.SDT[55], V33); //型号 //版本 //协议版本 //生产日期 //生产批号 //序列号 //开机次数 //总开机时间 //最大连续开机时间 //本次开机时间 //电源闪断次数 //总看门狗动作次数 //本次看门狗动作次数 //电源电压 //核心温度 //输入输出 //IO状态,开路,短路,熔断..... //包顺序错误 //令牌 广播形式, 环路形式 // s2 += s1; s2.AppendFormat(_T("\r\n")); int nOffset = 32; s1.Empty(); s1.Format(_T("子机1\r\n")); KLink1::pChnStat pchnstat1 = (KLink1::pChnStat)&(pDoc->MyKLink1.MEM.SDD[nOffset + 0]); unsigned int nSendPackets = pchnstat1->SendPackets; s1.AppendFormat(_T("当前状态: %04X\r\n"), pchnstat1->Stat); s1.AppendFormat(_T("发收包数:%6d/%6d(%.2f%%)\r\n发送时间 %10u (uS)\r\n"), pchnstat1->SendPackets, pchnstat1->RecvPackets, pchnstat1->RecvPackets *100.0 / nSendPackets, pchnstat1->LastSentTimeuS); s1.AppendFormat(_T("丢包计数:%d(%.2f%%) 连续 %d(Max:%d)\r\n"), pchnstat1->LostPackets, pchnstat1->LostPackets *100.0 / nSendPackets, pchnstat1->CtnLstPkts, pchnstat1->MaxCtnLstPkts); s1.AppendFormat(_T("返回延迟 %d(max:%d) uS\r\n"), pchnstat1->Delay, pchnstat1->MaxDelay); s1.AppendFormat(_T("循环时间 %d uS\r\n"), pchnstat1->SendTimeInterval); s1.AppendFormat(_T("子机收发 %6d/%6d \r\n"), pchnstat1->ClientRecvPkts, pchnstat1->ClientSendPkts); s2 += s1; s2.AppendFormat(_T("\r\n")); nOffset = 32 + 24; s1.Format(_T("子机2\r\n")); pchnstat1 = (KLink1::pChnStat)&(pDoc->MyKLink1.MEM.SDD[nOffset + 0]); nSendPackets = pchnstat1->SendPackets; s1.AppendFormat(_T("当前状态: %04X\r\n"), pchnstat1->Stat); s1.AppendFormat(_T("发收包数:%6d/%6d(%.2f%%)\r\n发送时间 %10u (uS)\r\n"), pchnstat1->SendPackets, pchnstat1->RecvPackets, pchnstat1->RecvPackets *100.0 / nSendPackets, pchnstat1->LastSentTimeuS); s1.AppendFormat(_T("丢包计数:%d(%.2f%%) 连续 %d(Max:%d)\r\n"), pchnstat1->LostPackets, pchnstat1->LostPackets *100.0 / nSendPackets, pchnstat1->CtnLstPkts, pchnstat1->MaxCtnLstPkts); s1.AppendFormat(_T("返回延迟 %d(max:%d) uS\r\n"), pchnstat1->Delay, pchnstat1->MaxDelay); s1.AppendFormat(_T("循环时间 %d uS\r\n"), pchnstat1->SendTimeInterval); s1.AppendFormat(_T("子机收发 %6d/%6d \r\n"), pchnstat1->ClientRecvPkts, pchnstat1->ClientSendPkts); s2 += s1; //m_edit_mon2.SetSel(0, -1); //m_edit_mon2.ReplaceSel(s2); SetDlgItemText(IDC_EDIT_MON2, s2); } s1.Empty(); pstWLRunStat pWLRunStat = (pstWLRunStat)(&pDoc->KWLB); s1 += _T("发送数量: ") + intToString(pWLRunStat->sentCount) + _T("\r\n"); s1 += _T("接收数量: ") + intToString(pWLRunStat->recvCount) + _T("\r\n"); s1 += _T("发送时间: ") + intToString(pWLRunStat->lastSendtime) + _T(" uS\r\n"); s1 += _T("接收时间: ") + intToString(pWLRunStat->lastRecvtime) + _T(" uS\r\n"); s1 += _T("动作时间: ") + intToString(pWLRunStat->lastActTime) + _T(" uS\r\n"); s1 += _T("相应时间: ") + intToString(pWLRunStat->lastAckTime) + _T(" uS\r\n"); s1 += _T("循环时间: ") + intToString(pWLRunStat->cycleTime,9) + _T(" uS\r\n"); s1 += _T("延迟: ") + intToString(pWLRunStat->latancy,9) + _T(" uS\r\n"); s1.AppendFormat(_T("丢包数量: %d\r\n"), pWLRunStat->LostPackets); s1.AppendFormat(_T("连续丢包: %d\r\n"), pWLRunStat->CtnLstPkts); s1.AppendFormat(_T("最大连续: %d\r\n"), pWLRunStat->MaxCtnLstPkts); s1.AppendFormat(_T("发送错误: %d\r\n"), pWLRunStat->TXErr); s1.AppendFormat(_T("接收错误: %d\r\n"), pWLRunStat->RXErr); s1.AppendFormat(_T("CRC错误: %d\r\n"), pWLRunStat->CRCErr); s1.AppendFormat(_T("本机信号强度: %d dBm %d dBm\r\n"), pWLRunStat->RSSI, pWLRunStat->SNR); s1.AppendFormat(_T("对方信号强度: %d dBm %d dBm\r\n"), pWLRunStat->tRSSI, pWLRunStat->tSNR); SetDlgItemText(IDC_EDIT_MON4, s1); //SetRedraw(TRUE); //s1.Append(s2); //s1.Append(_T("\r\n")); UpdateStatusBar(-1); return 0; } int CMTerm1CommDevView::UpdateStatusBar(int nIndex) { // TODO: 在此处添加实现代码. CString s1; CMTerm1Doc * pDoc = (CMTerm1Doc*)GetDocument(); if (nIndex == idxMachineType || nIndex == -1) { //机型代码 s1 = pDoc->m_sMachineType; m_pStatusBar->SetPaneText(idxMachineType, s1); } if (nIndex == idxProgPos || nIndex == -1) { //程序位置/步数 // s1.Format(_T("%5d/%5d"), m_nCurProgStep, pDoc->m_nProgSteps); // m_pStatusBar->SetPaneText(idxProgPos, s1); } if (nIndex == idxOnline || nIndex == -1) { //在线 /离线 if (!pDoc->m_bOnline) { s1 = _T("离线"); m_pStatusBar->SetPaneText(idxOnline, s1); m_pStatusBar->SetPaneBackgroundColor(idxOnline); } else if (pDoc->m_bOnline) { if (pDoc->m_bSimulate) { s1 = _T("在线(仿真)"); m_pStatusBar->SetPaneText(2, s1); m_pStatusBar->SetPaneBackgroundColor(idxOnline, OnlineColor); } else { s1 = _T("在线"); m_pStatusBar->SetPaneText(2, s1); m_pStatusBar->SetPaneBackgroundColor(idxOnline, OnlineColor); } } } if (nIndex == idxRunning || nIndex == -1) { // 运行 / 停止 if (!pDoc->m_bOnline) { s1 = _T(""); m_pStatusBar->SetPaneText(idxRunning, s1); m_pStatusBar->SetPaneBackgroundColor(idxRunning); m_pStatusBar->SetPaneWidth(idxRunning, 0); m_pStatusBar->SetPaneStyle(idxRunning, SBPS_DISABLED); } else if (!pDoc->m_bPlcRunning) { s1 = _T("PROG"); m_pStatusBar->SetPaneText(idxRunning, s1); m_pStatusBar->SetPaneWidth(idxRunning, s1.GetLength() * 8); m_pStatusBar->SetPaneBackgroundColor(idxRunning, ProgColor); } else { s1 = _T("RUN"); m_pStatusBar->SetPaneText(idxRunning, s1); m_pStatusBar->SetPaneWidth(idxRunning, s1.GetLength() * 8); m_pStatusBar->SetPaneBackgroundColor(idxRunning, RunningColor); } } if (nIndex == idxMonitor || nIndex == -1) { // 监控 // // s1 = pDoc->m_sMachineType; if (!m_bMonitoring) { s1 = _T("[监控停止]"); m_pStatusBar->SetPaneText(idxMonitor, s1); m_pStatusBar->SetPaneBackgroundColor(idxMonitor); } else { s1 = _T("[正在监控]"); m_pStatusBar->SetPaneText(idxMonitor, s1); m_pStatusBar->SetPaneBackgroundColor(idxMonitor, MonitorColor); } } if (nIndex == idxAddress || nIndex == -1) { // 地址,本站 // s1 = pDoc->m_sMachineType; if (!pDoc->m_bOnline) { s1 = _T("本站"); m_pStatusBar->SetPaneText(idxAddress, s1); m_pStatusBar->SetPaneBackgroundColor(idxMonitor); } else { s1 = _T("本站"); m_pStatusBar->SetPaneText(idxAddress, s1); m_pStatusBar->SetPaneBackgroundColor(idxMonitor, AddressColor); } } if (nIndex == idxInfoDisp || nIndex == -1) { // INFO DISPLAY // s1 = pDoc->m_sMachineType; m_pStatusBar->SetPaneText(idxInfoDisp, s1); } return 0; } void CMTerm1CommDevView::OnTimer(UINT_PTR nIDEvent) { // TODO: 在此添加消息处理程序代码和/或调用默认值 CString s1, s2; if (nIDEvent == 0) { KillTimer(0); DelayInit(); } else if (nIDEvent == 1) { //ProcessPLC(); MonitorPLC(); } else if (nIDEvent == 2) { // ProcessInput(); //DrawPic1(); // MyLogger1.UpdateLogDisplay(0); UpdateDataDisplay(); UpdateLEDDisplay(); } else if (nIDEvent == 3) { if (m_bResourceOpened && !m_bPause) { // int n=AcqureDatas(m_nChannel); int n = 0; if (n > 0) { } } } else if (nIDEvent == 4) {} else {} CFormView::OnTimer(nIDEvent); } // 如果向对话框添加最小化按钮,则需要下面的代码 // 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序, // 这将由框架自动完成。 /* BOOL CMTerm1CommDevView::PreTranslateMessage(MSG* pMsg) { // TODO: 在此添加专用代码和/或调用基类 CString s1; if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN) { if (GetDlgItem(IDC_COMBO_INPUT) == GetFocus()) { OnBnClickedButtonSend(); } return FALSE; } if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_BACK) { return FALSE; } if (pMsg->message == WM_KEYDOWN) { BOOL bCtrl = ::GetKeyState(VK_CONTROL) & 0x80; switch (pMsg->wParam) { case 'a': case 'A': if (bCtrl) { CEdit* edit = (CEdit*)(GetDlgItem(IDC_EDIT_LOG1)); //edit->SetFocus(); edit->SetSel(0, -1); return FALSE; } } } try { return CFormView::PreTranslateMessage(pMsg); } catch (CMemoryException* e) { CString Serror; e->GetErrorMessage(Serror.GetBuffer(256), 256); Serror.ReleaseBuffer(); s1.Format(_T("程序 发生 内存 错误 %s \r\n"), Serror); PopupMessage(s1); } catch (CFileException* e) { CString Serror; e->GetErrorMessage(Serror.GetBuffer(256), 256); Serror.ReleaseBuffer(); s1.Format(_T("程序 发生 文件 错误 %s \r\n"), Serror); PopupMessage(s1); } catch (SE_Exception & e) { s1.Format(_T("程序 发生 SE 错误 Code %08X Addr %08X \r\n%s\r\n"), e.getSeNumber(), e.getAddress(), e.getInfoStr()); PopupMessage(s1); } catch (CException* e) { CString Serror; e->GetErrorMessage(Serror.GetBuffer(256), 256); Serror.ReleaseBuffer(); s1.Format(_T("程序 发生 C++ 错误 %s \r\n"), Serror); PopupMessage(s1); } catch (...) { s1.Format(_T("程序 发生未知错误 \r\n")); PopupMessage(s1); } return TRUE; return CFormView::PreTranslateMessage(pMsg); } */ void CMTerm1CommDevView::FindComPort() { CString s1, s2; m_combo_comport.ResetContent(); for (int i = 1; i <= 16; i++) { s1.Format(_T("COM%d"), i); if (get_com_name(s1, s2)) s1 = s2; m_combo_comport.AddString(s1); } sComSelStr = _T("COM3"); m_combo_comport.SelectString(0, sComSelStr); m_combo_comport.SetCurSel(2); return; HKEY hKey; if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Hardware\\DeviceMap\\SerialComm"), NULL, KEY_READ, &hKey) == ERROR_SUCCESS) { TCHAR szPortName[256], szComName[256]; DWORD dwLong, dwSize; int nCount = 0; m_combo_comport.ResetContent(); while (true) { dwLong = dwSize = 256; if (RegEnumValue(hKey, nCount, szPortName, &dwLong, NULL, NULL, (PUCHAR)szComName, &dwSize) == ERROR_NO_MORE_ITEMS) break; m_combo_comport.InsertString(nCount, szComName); nCount++; } RegCloseKey(hKey); m_combo_comport.SetCurSel(0); } } BOOL CMTerm1CommDevView::OnDeviceChange(UINT nEventType, DWORD_PTR dwData) { if (nEventType == DBT_DEVNODES_CHANGED) { FindComPort(); } return TRUE; } int CMTerm1CommDevView::LoadResourceList() { Hash & ResourceCfg = MyCfg1["Resources"]; int nCount; nCount = _tstoi(ResourceCfg["Count"]); CString s1, s2; for (int i = 0; i < nCount; i++) { s1.Format(_T("Addr%d"), i); s2 = ResourceCfg[s1]; ((CComboBox *)GetDlgItem(IDC_COMBO_RES))->AddString(s2); } s1 = ResourceCfg["LastAddr"]; ((CComboBox *)GetDlgItem(IDC_COMBO_RES))->SetWindowText(s1); return nCount; } int CMTerm1CommDevView::SaveResourceList() { Hash & ResourceCfg = MyCfg1["Resources"]; int nCount1 = _tstoi(ResourceCfg["Count"]); CString s1, s2, s3; ((CComboBox *)GetDlgItem(IDC_COMBO_RES))->GetWindowText(s3); int bFound = 0; int nCount2 = ((CComboBox *)GetDlgItem(IDC_COMBO_RES))->GetCount(); for (int i = 0; i < nCount2; i++) { ((CComboBox *)GetDlgItem(IDC_COMBO_RES))->GetLBText(i, s2); if (s2 == s3) { bFound = 1; break; } } if (!bFound) { ((CComboBox *)GetDlgItem(IDC_COMBO_RES))->AddString(s3); s1.Format(_T("Addr%d"), nCount1); ResourceCfg[s1] = s3; nCount1 = nCount1 + 1; s1.Format(_T("%d"), nCount1); ResourceCfg["Count"] = s1; } ResourceCfg["LastAddr"] = s3; return nCount1; } int CMTerm1CommDevView::DisplayParams() { CMTerm1Doc *pDoc = (CMTerm1Doc *)GetDocument(); CString s1, s2; Hash & ModelConfg = MyCfg1["Model"]; ModelConfg.SetCtrlList(this, myCfgValCtrls, nMarkConfigs); for (int i = 1; i <= 16; i++) { s1.Format(_T("COM%d"), i); if (get_com_name(s1, s2)) (s1 = s2); m_combo_comport.AddString(s1); } sComSelStr = _T("COM3"); m_combo_comport.SelectString(0, sComSelStr); m_combo_comport.SetCurSel(2); m_combo_com_baud.AddString(_T("1200")); m_combo_com_baud.AddString(_T("2400")); m_combo_com_baud.AddString(_T("4800")); m_combo_com_baud.AddString(_T("9600")); m_combo_com_baud.AddString(_T("19200")); m_combo_com_baud.AddString(_T("38400")); m_combo_com_baud.AddString(_T("57600")); m_combo_com_baud.AddString(_T("115200")); m_combo_com_baud.AddString(_T("128000")); m_combo_com_baud.AddString(_T("187500")); m_combo_com_baud.AddString(_T("230400")); m_combo_com_baud.AddString(_T("460800")); m_combo_com_baud.AddString(_T("921600")); m_combo_com_baud.AddString(_T("1000000")); m_combo_com_baud.AddString(_T("2000000")); m_combo_com_baud.AddString(_T("3000000")); sBaudSelStr = _T("230400"); m_combo_com_baud.SelectString(0, sBaudSelStr); MyThreadProc1ToRun = 0; MyThreadProc1Running = 0; ((CComboBox *)GetDlgItem(IDC_COMBO_MADDR))->SetCurSel(0); ((CComboBox *)GetDlgItem(IDC_COMBO_CMD))->SetCurSel(2); ((CComboBox *)GetDlgItem(IDC_COMBO_CMD_TYPE))->SetCurSel(0); ((CComboBox *)GetDlgItem(IDC_COMBO_DADDR))->SetCurSel(0); ((CComboBox *)GetDlgItem(IDC_COMBO_DCOUNT))->SetCurSel(6); return 0; } int CMTerm1CommDevView::GetParams() { CString s1, s2; Hash & ModelConfg = MyCfg1["Model"]; ModelConfg.GetCtrlList(this, myCfgValCtrls, nMarkConfigs); return 0; } int CMTerm1CommDevView::LoadModel() { CString s1; Hash & ModelConfg = MyCfg1["Model"]; return 0; } int CMTerm1CommDevView::SaveModel() { CString s1, s2; Hash & ModelConfg = MyCfg1["Model"]; return 0; } int CMTerm1CommDevView::OpenResource(CStringA ResourceStr) { CString s1; CMTerm1Doc *pDoc = (CMTerm1Doc *) GetDocument(); int r = pDoc->Connect(); s1.Format(_T("Open %s = %d"), pDoc->MyKLink1.m_resultStr, r); SysLog(s1); if (r == pDoc->MyKLink1.MySerPort1.R_OK) { m_bResourceOpened = true; // pDoc->MyKLink1.Open(); m_static_connect.SetCtlColor(RGB(0, 255, 0)); return true; } return false; } int CMTerm1CommDevView::CloseResource() { CMTerm1Doc *pDoc = (CMTerm1Doc *)GetDocument(); m_static_connect.SetCtlColor(RGB(80, 80, 80)); if (m_bResourceOpened) { pDoc->DisConnect(); pDoc->MyKLink1.Close(); m_bResourceOpened = false; } return 0; } int CMTerm1CommDevView::LoadData(CString sFilePathName) { CFile file1; CFileException e1; CStringA s1a; int nDataCount = 0; int r = file1.Open(sFilePathName, CFile::modeRead, &e1); if (r) { int len1 = (int)file1.GetLength(); file1.Read(s1a.GetBufferSetLength(len1 + 100), len1); s1a.ReleaseBuffer(len1); int k = s1a.Find("\n", 0); int n = atoi(s1a.Left(k)); CStringA s2A; CStringA resultstr[100]; for (int i = 0; i < n; i++) { int j = s1a.Find("\n", k + 1); s2A = s1a.Mid(k + 1, j - k - 1); Split(s2A, ",", resultstr); float a = (float)atof(resultstr[0]); float b = (float)atof(resultstr[1]); float c = (float)atof(resultstr[2]); k = j; } nDataCount = n; } return nDataCount; } int CMTerm1CommDevView::SaveData(CString sFilePathName) { CFile file1; CFileException e1; CStringA s1a; int nDataCount = 0; int r = file1.Open(sFilePathName, CFile::modeCreate | CFile::modeWrite, &e1); if (r) { s1a.Format("%d\r\n", nDataCount); for (int i = 0; i < nDataCount; i++) { s1a.AppendFormat("%f,%f,%f\r\n", 0, 0, 0); } file1.Write(s1a.GetBuffer(), s1a.GetLength()); file1.Close(); } return 0; } void CMTerm1CommDevView::OnSize(UINT nType, int cx, int cy) { CFormView::OnSize(nType, cx, cy); if (!this->IsWindowVisible() && !BInited) { return; } CString s1; s1.Format(_T("Dialog OnSize Type %d cx %d cy %d \r\n"), nType, cx, cy); // SysLog(s1); switch (nType) { case SIZE_MAXHIDE: case SIZE_MAXSHOW: case SIZE_MINIMIZED: return; } if (nType == SIZE_MAXIMIZED || nType == SIZE_RESTORED) { // calLayerOuts(); // CalButtonsLayerOut(); } // TODO: 在此处添加消息处理程序代码 } int CMTerm1CommDevView::calLayerOuts() { GetClientRect(&MainClientRect); MainClientRect.bottom -= 20; int w = MainClientRect.Width(); int h = MainClientRect.Height(); DivRectX(MainClientRect, LeftRect, RightRect, 600, 0.7f); DivRectY3(LeftRect, ItemRect, ButtonRect, LogRect, 0.7f, 0.1f, 0.2f); CRect ItemRect1, ItemRect2; DivRectX(ItemRect, ItemRect1, ItemRect2, 0.5f, 0.5f); PicRect = RightRect; DivRectY(PicRect, PicRectUp, PicRectDown, 24, 0.95f); // dialogitemshow1.MoveWindow(&ItemRect1); // dialogitemshow2.MoveWindow(&ItemRect2); GetDlgItem(IDC_EDIT_DISPLAY)->MoveWindow(&PicRectUp); GetDlgItem(IDC_COMBO_INPUT)->MoveWindow(&PicRectDown); GetDlgItem(IDC_EDIT_LOG1)->MoveWindow(&LogRect); //CalButtonsLayerOut(); RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0); return 0; } int CMTerm1CommDevView::MonitorPLC() { CMTerm1Doc *pDoc = (CMTerm1Doc *)GetDocument(); if (m_bMonitoring && pDoc->MyKLink1.MySerPort1.m_bOpened) { unsigned char DstAddr = 1; unsigned char DataType = pDoc->MyKLink1.KLDataTypeSDT; unsigned short DAddr = 0; unsigned char DCount = 32; // unsigned char Data1[256]; unsigned char ExtFrameLen = 0; unsigned short nCount; CString s1, s2; MonitorTotalCount++; int nThisSuccessCount = 0; int nThisFailCount = 0; int res; static int nStep = 0; // WX ///* if (nStep == 0) { DataType = pDoc->MyKLink1.KLDataTypeWX; DCount = 16; res = pDoc->MyKLink1.ReadDataByte(DstAddr, DataType, DAddr, DCount, &nCount, &pDoc->MyKLink1.MEM.WXB[DAddr]); nCount = DCount; if (res == pDoc->MyKLink1.KL_OK) { nThisSuccessCount++; } else { nThisFailCount++; s1.Format(_T("R:= %d %s \r\n"), res, pDoc->MyKLink1.m_resultStr); SysLog(s1); } } //*/ ///* //WY if (nStep == 0) { DataType = pDoc->MyKLink1.KLDataTypeWY; DCount = 16; res = pDoc->MyKLink1.ReadDataByte(DstAddr, DataType, DAddr, DCount, &nCount, &pDoc->MyKLink1.MEM.WYB[DAddr]); nCount = DCount; if (res == pDoc->MyKLink1.KL_OK) { nThisSuccessCount++; } else { nThisFailCount++; s1.Format(_T("R:= %d %s \r\n"), res, pDoc->MyKLink1.m_resultStr); SysLog(s1); } //*/ } ///* //WR if (nStep==0) { DataType = pDoc->MyKLink1.KLDataTypeWR; DCount = 32; res = pDoc->MyKLink1.ReadDataByte(DstAddr, DataType, DAddr, DCount, &nCount, &pDoc->MyKLink1.MEM.WRB[DAddr]); nCount = DCount; if (res == pDoc->MyKLink1.KL_OK) { nThisSuccessCount++; } else { nThisFailCount++; s1.Format(_T("R:= %d %s \r\n"), res, pDoc->MyKLink1.m_resultStr); SysLog(s1); } } //*/ //DT if (nStep == 1) { DataType = pDoc->MyKLink1.KLDataTypeDT; DCount = 96; res = pDoc->MyKLink1.ReadDataByte(DstAddr, DataType, DAddr, DCount, &nCount, &pDoc->MyKLink1.MEM.DTB[DAddr]); nCount = DCount; if (res == pDoc->MyKLink1.KL_OK) { nThisSuccessCount++; } else { nThisFailCount++; s1.Format(_T("R:= %d %s \r\n"), res, pDoc->MyKLink1.m_resultStr); SysLog(s1); } } int ByteAddr = 0; //SDT if (nStep >= 2 && nStep <= 4) { int nBlock = nStep - 2; DataType = pDoc->MyKLink1.KLDataTypeSDT; ByteAddr = nBlock * 128; DCount = 128; res = pDoc->MyKLink1.ReadDataByte(DstAddr, DataType, ByteAddr, DCount, &nCount, &pDoc->MyKLink1.MEM.SDB[ByteAddr]); nCount = DCount; if (res == pDoc->MyKLink1.KL_OK) { for (int i = 0; i < DCount; i++) { SDTbuf[ByteAddr + i] = pDoc->MyKLink1.MEM.SDB[ByteAddr + i]; } nThisSuccessCount++; } else { nThisFailCount++; s1.Format(_T("R:= %d %s \r\n"), res, pDoc->MyKLink1.m_resultStr); SysLog(s1); } } if (nStep >= 5 && nStep <= 7) { int nBlock = nStep - 5; DataType = pDoc->MyKLink1.KLDataTypeWDT; ByteAddr = nBlock * 128; DCount = 128; res = pDoc->MyKLink1.ReadDataByte(DstAddr, DataType, ByteAddr, DCount, &nCount, &pDoc->MyKLink1.MEM.WDB[ByteAddr]); nCount = DCount; if (res == pDoc->MyKLink1.KL_OK) { for (int i = 0; i < DCount; i++) { WDTbuf[ByteAddr + i] = pDoc->MyKLink1.MEM.WDB[ByteAddr + i]; } nThisSuccessCount++; } else { nThisFailCount++; s1.Format(_T("R:= %d %s \r\n"), res, pDoc->MyKLink1.m_resultStr); SysLog(s1); } } if (nStep >= 8 && nStep <= 13) { int nBlock = nStep - 8; DataType = pDoc->MyKLink1.KLDataTypeKBD; ByteAddr = nBlock * 128; DCount = 128; res = pDoc->MyKLink1.ReadDataByte(DstAddr, DataType, ByteAddr, DCount, &nCount, &pDoc->KBDB[ByteAddr]); nCount = DCount; if (res == pDoc->MyKLink1.KL_OK) { //for (int i = 0; i < DCount; i++) { WDTbuf[ByteAddr + i] = pDoc->MyKLink1.MEM.WDB[ByteAddr + i]; } nThisSuccessCount++; } else { nThisFailCount++; s1.Format(_T("R:= %d %s \r\n"), res, pDoc->MyKLink1.m_resultStr); SysLog(s1); } } if (nStep >= 14 && nStep <= 14) { int nBlock = nStep - 14; DataType = pDoc->MyKLink1.KLDataTypeKWLD; ByteAddr = nBlock * 128; DCount = 128; res = pDoc->MyKLink1.ReadDataByte(DstAddr, DataType, ByteAddr, DCount, &nCount, &pDoc->KWLB[ByteAddr]); nCount = DCount; if (res == pDoc->MyKLink1.KL_OK) { //for (int i = 0; i < DCount; i++) { WDTbuf[ByteAddr + i] = pDoc->MyKLink1.MEM.WDB[ByteAddr + i]; } nThisSuccessCount++; } else { nThisFailCount++; s1.Format(_T("R:= %d %s \r\n"), res, pDoc->MyKLink1.m_resultStr); SysLog(s1); } } nStep++; // if (nStep == 2) { nStep = 3; } if (nStep > 14) { nStep = 0; } MonitorSuccessCount += nThisSuccessCount; MonitorFailCount += nThisFailCount; if (nThisFailCount == 0) { } else { } } return 0; } void CMTerm1CommDevView::OnClose() { // TODO: 在此添加消息处理程序代码和/或调用默认值 // if (pHvCamera1 != nullptr) { delete pHvCamera1; pHvCamera1 = nullptr; } MyThreadProc1ToRun = 0; CloseResource(); GetParams(); SaveMyConfig(); Sleep(100); CFormView::OnClose(); } int DrawCross(HWND hWnd1, int handx, int handy) { ///* using namespace Gdiplus; CRect rect1; GetClientRect(hWnd1, &rect1); int ClientWidth = rect1.Width(); int ClientHeight = rect1.Height(); int CenterX = ClientWidth / 2; int CenterY = ClientHeight / 2; int ZeroRage = 14; int rZeroRage = ZeroRage * ClientWidth / 256; Bitmap bmp1(ClientWidth, ClientHeight); Graphics gr1(&bmp1); gr1.Clear(Gdiplus::Color::White); Pen pen1(Color::Gray, 1); Pen pen2(Color::Black, 2); //画大十字线 gr1.DrawLine(&pen1, 0, CenterY, ClientWidth, CenterY); gr1.DrawLine(&pen1, CenterX, 0, CenterX, ClientHeight); //画中心零点的范围 gr1.DrawRectangle(&pen1, CenterX - rZeroRage, CenterY - rZeroRage, rZeroRage * 2, rZeroRage * 2); //画允许框的范围 gr1.DrawRectangle(&pen1, ClientWidth * 10 / 100, ClientHeight * 10 / 100, ClientWidth * 80 / 100, ClientHeight * 80 / 100); //画小十字线 int curx = handx * ClientWidth / 256; int cury = (255 - handy) *ClientHeight / 256; if (curx < 0) { curx = 0; } if (curx > ClientWidth) { curx = ClientWidth; } if (cury < 0) { cury = 0; } if (cury > ClientHeight) { cury = ClientHeight; } gr1.DrawLine(&pen2, curx - 8, cury, curx + 8, cury); gr1.DrawLine(&pen2, curx, cury - 8, curx, cury + 8); HDC hdc1 = GetDC(hWnd1); SetTextColor(hdc1, 0x00c0c0c0); SetBkColor(hdc1, 0x00000000); Graphics gr2(hdc1); gr2.DrawImage(&bmp1, 0, 0); ReleaseDC(hWnd1, hdc1); return 0; } DWORD WINAPI CMTerm1CommDevView::MyThreadProc1(LPVOID pParam) { CString s1, s2; SysLog(_T("线程开始运行\r\n")); MyThreadProc1Running = 1; CMTerm1Doc *pDoc = (CMTerm1Doc *)GetDocument(); /* static OVERLAPPED ovlap1, ovlap2; COMMTIMEOUTS ctimeout1; ovlap1.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); ovlap2.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); memset(&ctimeout1, 0, sizeof(ctimeout1)); ctimeout1.ReadIntervalTimeout = 1; ctimeout1.ReadTotalTimeoutMultiplier = 0; ctimeout1.ReadTotalTimeoutConstant = 1; ctimeout1.WriteTotalTimeoutConstant = 0; ctimeout1.WriteTotalTimeoutMultiplier = 0; */ //s2.GetLength(); // memcpy(send1,s2,bytetosend1); pDoc->MyAnsiParser1.AttachWnd(m_edit_display.GetSafeHwnd()); pDoc->MyAnsiParser1.SetScreenWH(150, 40); // CClientDC hcurDC(this); HWND hDrawXY = GetDlgItem(IDC_STATIC_DRAW_XY)->m_hWnd; int handx = 0, handy = 0; int oldhandx = 0, oldhandy = 0; SysLog(_T("准备接收\r\n")); DWORD dNumtoRead = 1000; dNumtoRead = 64; nDataToSend = 0; DWORD wCount2 = 0; DWORD dwSent = 0; int nRunCount = 0; int nTotalRecv = 0; //计算接收速度; int LastSpeedTime = (int)GetTickCountmS(); int nThisRecv = 0; double AvgSpeed = 0; double CurSpeed = 0; long nCount1 = 0; int LastSendLength = 0; unsigned char databuf1[256]; for (; MyThreadProc1ToRun == 1;) { // AppendText("H"); nCount1++; if (BulkSendTest == 1) { //nDataToSend += 400; nDataToSend = LastSendLength; // s1.Format(_T("BulkToSend %d Bytes"), nDataToSend); // SysLog(s1); dwSent = pDoc->MyKLink1.SendPacket((char *)send1, nDataToSend); s1.Format(_T("S %d ->"), nDataToSend); for (int i = 0; i < nDataToSend; i++) { s1.AppendFormat(_T("%02X "), send1[i]); } // SysLog(s1); //WriteFile(hCom, send1, nDataToSend, &dwSent, NULL); nDataToSend = 0; //continue; } int SendAndRead = 0; if (nDataToSend > 0) { s1.Format(_T("ToSend %d Bytes"), nDataToSend); SysLog(s1); dwSent = pDoc->MyKLink1.SendPacket((char *)send1, nDataToSend); s1.Format(_T("S %d ->"), nDataToSend); for (int i = 0; i < nDataToSend; i++) { s1.AppendFormat(_T("%02X "), send1[i]); } SysLog(s1); SendAndRead = 1; //WriteFile(hCom, send1, nDataToSend, &dwSent, NULL); LastSendLength = nDataToSend; nDataToSend = 0; } // ResetEvent(ovlap2.hEvent); if (SendAndRead == 1) { s1.Format(_T("Start to Read %d "), dNumtoRead); SysLog(s1); } //ReadFile(hCom, recv1, dNumtoRead, &wCount2, &ovlap2); wCount2 = pDoc->MyKLink1.RecvPacket((char *)recv1, dNumtoRead); // WaitForSingleObject(ovlap2.hEvent,800); // GetOverlappedResult(hCom,&ovlap2,&wCount2,TRUE); if (wCount2 == 0) continue; // continue; int ThisTime = static_cast (GetTickCountmS()); // s3.Format("%d %d ",recv2length,wCount2);AppendText(s3); nTotalRecv += wCount2; nThisRecv += wCount2; s1.Format(_T("R %d ->"), wCount2); for (DWORD i = 0; i < wCount2; i++) { s1.AppendFormat(_T("%02X "), recv1[i]); } unsigned char nCmd, nStatus; unsigned short nCount; s1.Append(_T("\r\n")); // SysLog(s1); //continue; pDoc->MyKLink1.ParseRplyPacket(recv1, wCount2, &nCmd, &nStatus, &nCount, databuf1); s1.Format(_T("R-> Cmd:%02X Status:%04X Count:%02d \r\nData: "), nCmd, nStatus, nCount); s2.Empty(); for (int i = 0; i < nCount; i++) { s2.AppendFormat(_T("%02X "), databuf1[i]); } // SetDlgItemText(IDC_EDIT_DATA2, s2); s1.Append(s2); s1.Append(_T("\r\n")); SysLog(s1); int HasData = pDoc->MyDataParser1.PutIn(recv1, wCount2); CStringA sA1; TToAnsi(s1, sA1); // MyAnsiParser1.PutIn((unsigned char *)sA1.GetBuffer(), sA1.GetLength()); sA1.ReleaseBuffer(); // MyAnsiParser1.PutIn(recv1, wCount2); if (HasData) { pDoc->MyDataParser1.GetOutput(&handx, &handy); if (oldhandx != handx || oldhandy != handy) { DrawCross(hDrawXY, handx, handy); oldhandx = handx; oldhandy = handy; } // m_edit2.SetSel(currenty*82+currentx,currenty*82+currentx+1,FALSE); s1.Format(_T("X=%3d(%2d%%) Y=%3d(%2d%%)"), handx, handx * 100 / 256, handy, handy * 100 / 256); SetDlgItemText(IDC_STATIC_STAT_XY, s1); // GetWindowDlg(IDC_STATIC5)->SetWindowText("X: Y:"); } nRunCount++; if (ThisTime - LastSpeedTime > 500) { CurSpeed = (double)nThisRecv / (ThisTime - LastSpeedTime); nThisRecv = 0; LastSpeedTime = ThisTime; } s1.Format(_T("Runing %d \r %.3fkBps"), nRunCount, CurSpeed); SetDlgItemText(IDC_STATIC_STATUS_CON, s1); //SysLog(_T(".")); } SysLog(_T("跳出接收循环\r\n")); // hcurDC.SelectObject(def_font); // CloseHandle(hCom); SysLog(_T("线程结束运行\r\n\r\n")); MyThreadProc1Running = 0; return 0; } UINT CMTerm1CommDevView::MyJumper1(LPVOID pParam) //线程跳板 { CMTerm1CommDevView *pInput = (CMTerm1CommDevView *)pParam; //反跳到真正的执行程序 return pInput->MyThreadProc1(0); } void CMTerm1CommDevView::OnBnClickedButtonConnect() { OnMenuOnline(); if (!m_bResourceOpened) { return; } m_bMonitoring = false; // TODO: 在此添加控件通知处理程序代码 } void CMTerm1CommDevView::OnBnClickedButtonDisconnect() { OnMenuOffline(); m_bMonitoring = false; if (!m_bResourceOpened) { return; } // TODO: 在此添加控件通知处理程序代码 } void CMTerm1CommDevView::OnBnClickedButtonStart() { // TODO: 在此添加控件通知处理程序代码 OnMenuOnline(); if (!m_bResourceOpened) { return; } m_bMonitoring = false; OnMenuMonitor(); return; if (MyThreadProc1ToRun == 1 || MyThreadProc1Running == 1) { SysLog(_T("线程在运行中,不能再次运行\r\n")); } else { MyThreadProc1ToRun = 1; AfxBeginThread(MyJumper1, (LPVOID)this); } } void CMTerm1CommDevView::OnBnClickedButtonStop() { // TODO: 在此添加控件通知处理程序代码 CMTerm1Doc *pDoc = (CMTerm1Doc *)GetDocument(); m_bMonitoring = true; OnMenuMonitor(); // OnMenuOffline(); return; if (MyThreadProc1Running) { if (MyThreadProc1ToRun) { SysLog(_T("停止线程运行\r\n")); MyThreadProc1ToRun = 0; } else { SysLog(_T("线程停止中\r\n")); } } else { SysLog(_T("线程未运行\r\n")); } pDoc->MyKLink1.Close(); } void CMTerm1CommDevView::OnBnClickedButtonClrscr() { // TODO: 在此添加控件通知处理程序代码 CMTerm1Doc *pDoc = (CMTerm1Doc *)GetDocument(); CString s1; s1.Format(_T("Clear Screen \r\n")); SysLog(s1); pDoc->MyAnsiParser1.ClrScreen(); } void CMTerm1CommDevView::OnBnClickedButtonSend() { // TODO: 在此添加控件通知处理程序代码 CString s1, sInput; //get text m_combo_input.GetWindowText(sInput); int j = m_combo_input.SelectString(0, sInput); if (j == -1) { m_combo_input.InsertString(-1, sInput); int k = m_combo_input.GetCount(); m_combo_input.SetCurSel(k - 1); } else { m_combo_input.GetWindowText(s1); if (s1 != sInput) { m_combo_input.InsertString(-1, sInput); int k = m_combo_input.GetCount(); m_combo_input.SetCurSel(k - 1); } } int res = 0; // send text; if (nDataToSend == 0) { CStringA sA1; TToAnsi(sInput, sA1); int k = sA1.GetLength(); memcpy(send1, sA1.GetBuffer(), k); sA1.ReleaseBuffer(); nDataToSend = k; res = 1; } else { } s1.Format(_T("Send Cmd %s %d %d\r\n"), sInput, sInput.GetLength(), res); SysLog(s1); } void CMTerm1CommDevView::OnCbnSelchangeComboRes() { // TODO: 在此添加控件通知处理程序代码 } void CMTerm1CommDevView::OnCbnEditchangeComboInput() { // TODO: 在此添加控件通知处理程序代码 CString s1; // s1.Format(_T("Combo_Input Edit Changed \r\n")); // SysLog(s1); } void CMTerm1CommDevView::OnBnClickedButtonDo() { // TODO: 在此添加控件通知处理程序代码 CMTerm1Doc *pDoc = (CMTerm1Doc *)GetDocument(); unsigned char buf1[256]; unsigned char buf2[256]; unsigned char Data1[256]; unsigned char Data2[256]; CString s1, sInput; CString MAddrStr; CString CMDstr; CString TypeStr; CString DAddrStr; CString DCountStr; CString Data1Str; GetDlgItemText(IDC_COMBO_MADDR, MAddrStr); GetDlgItemText(IDC_COMBO_CMD, CMDstr); GetDlgItemText(IDC_COMBO_CMD_TYPE, TypeStr); GetDlgItemText(IDC_COMBO_DADDR, DAddrStr); GetDlgItemText(IDC_COMBO_DCOUNT, DCountStr); GetDlgItemText(IDC_EDIT_DATA1, Data1Str); int DstAddr = _tstoi(MAddrStr); int Cmd = _tstoi(CMDstr); int DataType = _tstoi(TypeStr); int DAddr = _tstoi(DAddrStr); int DCount = _tstoi(DCountStr); CString Resultstrs[100]; int j = Split(Data1Str, _T(" "), Resultstrs); for (int i = 0; i < j; i++) { Data1[i] = Xtoi(Resultstrs[i]); } int bExtData = ((CButton *)GetDlgItem(IDC_CHECK_EXT_DATA))->GetCheck(); CString ExtDataDstStr; CString ExtDataTypeStr; CString ExtDataLenStr; CString ExtDataStr; GetDlgItemText(IDC_COMBO_EXT_DST, ExtDataDstStr); GetDlgItemText(IDC_COMBO_EXT_TYPE, ExtDataTypeStr); GetDlgItemText(IDC_COMBO_EXT_DATA_LEN, ExtDataLenStr); GetDlgItemText(IDC_EDIT_EXT_DATA1, ExtDataStr); int ExtDataDst = _tstoi(ExtDataDstStr); int ExtDataType = _tstoi(ExtDataTypeStr); int ExtDataLen = _tstoi(ExtDataLenStr); j = Split(ExtDataStr, _T(" "), Resultstrs); for (int i = 0; i < j; i++) { Data2[i] = Xtoi(Resultstrs[i]); } int ExtFrameLen = pDoc->MyKLink1.MakeExtDataFrame(buf2, ExtDataDst, ExtDataType, ExtDataLen, Data2); KLink1::unKLStat nStat1 = { 0 }; nStat1.HasExt = bExtData; nStat1.nSEQ = pDoc->MyKLink1.GetNextSeq(); // Cmd += 0x30; //KLink::cmdRead int len1 = pDoc->MyKLink1.MakeReqPacketEx(buf1, DstAddr, nStat1.StatByte, Cmd, DataType, DAddr, DCount, Data1, ExtFrameLen, buf2); s1.Format(_T("S %d ->"), len1); int res = 0; int SendType = 1; if (SendType == 1) { res = pDoc->MyKLink1.SendPacket((char *)buf1, len1); for (int i = 0; i < len1; i++) { s1.AppendFormat(_T("%02X "), buf1[i]); } } else if (SendType == 2) { if (nDataToSend == 0) { int k = len1; memcpy(send1, buf1, k); nDataToSend = k; res = 1; } } s1.AppendFormat(_T(" = %d\r\n"), res); SysLog(s1); DWORD wCount2; DWORD dNumtoRead = DCount + 6;; int numToRead = dNumtoRead; int len2 = 0; int nTryCount = 0; int nTryCount2 = 0; // 在没有收到数据的情况向,连续重试10次,在收到数据后,又没有数据,再重试3次。 for (int i = 0; i < 10; i++) { nTryCount++; int len = pDoc->MyKLink1.RecvPacket((char *)recv1 + len2, numToRead - len2); if (len >0) len2 += len; if (len2 >= numToRead) break; if (len2 > 0) { nTryCount2++; if (len > 0) { nTryCount2 = 0; } if (nTryCount2 >= 1) break; } } //wCount2 = MySerialCom1.Read((char *)recv1, dNumtoRead); wCount2 = len2; // WaitForSingleObject(ovlap2.hEvent,800); // GetOverlappedResult(hCom,&ovlap2,&wCount2,TRUE); //if (wCount2 == 0) continue; // continue; int ThisTime = static_cast (GetTickCountmS()); // s3.Format("%d %d ",recv2length,wCount2);AppendText(s3); s1.Format(_T("R %d(Try %d %d)=%d->"), dNumtoRead, nTryCount, nTryCount2, wCount2); for (DWORD i = 0; i < 32 && i < wCount2; i++) { s1.AppendFormat(_T("%02X "), recv1[i]); } s1.Append(_T("... \r\n")); SysLog(s1); unsigned char nCmd = 0, nStatus = 0; unsigned short nCount = 0; //continue; unsigned short databuf1[256]; pDoc->MyKLink1.ParseRplyPacket(recv1, wCount2, &nCmd, &nStatus, &nCount, databuf1); s1.Format(_T("R: Cmd:%02X St:%02X N:%02d D:\r\n"), nCmd, nStatus, nCount); CString s2; s2.Empty(); for (int i = 0; i < (nCount + 15) / 16; i++) { s2.AppendFormat(_T("%02X: "), i * 16); for (int j = 0; j < 8 && i * 8 + j < nCount / 2; j++) { s2.AppendFormat(_T("%04X"), databuf1[i * 8 + j]); if (j == 3) { s2.Append(_T(" ")); } else { s2.Append(_T(" ")); } } s2.Append(_T("\r\n")); } // for (int i = 0; i < nCount; i++) // { // s2.AppendFormat(_T("%02X "), databuf1[i]); // } // SetDlgItemText(IDC_EDIT_DATA2, s2); s1.Append(s2); // s1.Append(_T("\r\n")); SysLog(s1); } void CMTerm1CommDevView::OnCbnSelchangeComboCmdType() { // TODO: 在此添加控件通知处理程序代码 } void CMTerm1CommDevView::OnCbnSelchangeComboDcount() { // TODO: 在此添加控件通知处理程序代码 } void CMTerm1CommDevView::OnBnClickedButtonStartBulkTest() { // TODO: 在此添加控件通知处理程序代码 BulkSendTest = 1; } void CMTerm1CommDevView::OnBnClickedButtonStopBulkTest() { // TODO: 在此添加控件通知处理程序代码 BulkSendTest = 0; } void CMTerm1CommDevView::OnMenuCommset() { // TODO: 在此添加命令处理程序代码 CDialogCommSet1 dialog1; dialog1.DoModal(); } void CMTerm1CommDevView::OnMenuDownload() { // TODO: 在此添加命令处理程序代码 } void CMTerm1CommDevView::OnMenuUpload() { // TODO: 在此添加命令处理程序代码 } void CMTerm1CommDevView::OnMenuOnline() { // TODO: 在此添加命令处理程序代码 CString s1; s1.Format(_T("切换在线")); SysLog(s1); if (m_bOnline) { SysLog(_T("已经在线")); return; } int r = OpenResource(""); if (r) { m_bOnline = true; // m_wndToolBar.GetToolBarCtrl().CheckDlgButton(ID_MENU_ONLINE, m_bOnline); // ((CButton *)m_wndToolBar.GetDlgItem(ID_MENU_ONLINE))->SetCheck(m_bOnline); if (m_bOnline) { // m_wndToolBar.SetButtonStyle(17, TBBS_CHECKBOX); // m_wndToolBar.SetButtonStyle(18, TBBS_CHECKED); } } else { // m_wndToolBar.SetButtonStyle(17, TBBS_CHECKED); // m_wndToolBar.SetButtonStyle(18, TBBS_CHECKBOX); } return; } void CMTerm1CommDevView::OnUpdateMenuOnline(CCmdUI *pCmdUI) { // TODO: 在此添加命令更新用户界面处理程序代码 pCmdUI->SetCheck(m_bOnline); // m_wndToolBar.GetToolBarCtrl().CheckDlgButton(ID_MENU_ONLINE, m_bOnline); } void CMTerm1CommDevView::OnMenuOffline() { // TODO: 在此添加命令处理程序代码 CMTerm1Doc *pDoc = (CMTerm1Doc *)GetDocument(); CString s1; m_bOnline = false; // m_wndToolBar.CheckDlgButton(ID_MENU_ONLINE, m_bOnline); s1.Format(_T("切换离线")); SysLog(s1); CloseResource(); return; if (MyThreadProc1Running) { if (MyThreadProc1ToRun) { SysLog(_T("停止线程运行\r\n")); MyThreadProc1ToRun = 0; } else { SysLog(_T("线程停止中\r\n")); } } else { SysLog(_T("线程未运行\r\n")); } pDoc->MyKLink1.Close(); } void CMTerm1CommDevView::OnUpdateMenuOffline(CCmdUI *pCmdUI) { // TODO: 在此添加命令更新用户界面处理程序代码 pCmdUI->SetCheck(!m_bOnline); } void CMTerm1CommDevView::OnMenuMonitor() { // TODO: 在此添加命令处理程序代码 CString s1; m_bMonitoring = !m_bMonitoring; s1.Format(_T("切换监控 -> %d"), m_bMonitoring); SysLog(s1); } void CMTerm1CommDevView::OnUpdateMenuMonitor(CCmdUI *pCmdUI) { // TODO: 在此添加命令更新用户界面处理程序代码 CString s1; pCmdUI->SetCheck(m_bMonitoring); } void CMTerm1CommDevView::OnMenuRun() { // TODO: 在此添加命令处理程序代码 CString s1; m_bRunning = !m_bRunning; s1.Format(_T("切换运行 -> %d "), m_bRunning); SysLog(s1); } void CMTerm1CommDevView::OnUpdateMenuRun(CCmdUI *pCmdUI) { // TODO: 在此添加命令更新用户界面处理程序代码 pCmdUI->SetCheck(m_bRunning); } void CMTerm1CommDevView::OnMenuSysregSet() { // TODO: 在此添加命令处理程序代码 CDialogSysRegSet dialog1; INT_PTR r = dialog1.DoModal(); } void CMTerm1CommDevView::OnMenuStatusShow() { // TODO: 在此添加命令处理程序代码 CDialogStatusShow dialog1; INT_PTR r = dialog1.DoModal(); } void CMTerm1CommDevView::OnMenuInfoDisplay() { // TODO: 在此添加命令处理程序代码 CDialogInfoDisplay dialog1; INT_PTR r = dialog1.DoModal(); } void CMTerm1CommDevView::OnMenuCoilMon() { // TODO: 在此添加命令处理程序代码 CDialogCoilMon dialog1; INT_PTR r = dialog1.DoModal(); } void CMTerm1CommDevView::OnMenuDataMon() { // TODO: 在此添加命令处理程序代码 CDialogDataMon dialog1; INT_PTR r = dialog1.DoModal(); } void CMTerm1CommDevView::OnMenuCommTool() { // TODO: 在此添加命令处理程序代码 // CDialogCommTest dialog1; // INT_PTR r = dialog1.DoModal(); } BOOL CMTerm1CommDevView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) { // TODO: 在此添加消息处理程序代码和/或调用默认值 return CFormView::OnMouseWheel(nFlags, zDelta, pt); } void CMTerm1CommDevView::OnBnClickedButtonClearStat() { // TODO: 在此添加控件通知处理程序代码 Clear_COM_Stats(); } void CMTerm1CommDevView::OnBnClickedButtonRead() { //CDialogProgress dialog1; //INT_PTR r = dialog1.DoModal(); CDialogProgress * pdialog1 = new CDialogProgress(); pdialog1->Create(IDD_DIALOG_PROGRESS, this); pdialog1->ShowWindow(SW_SHOW); pdialog1->UpdateWindow(); for (int i = 0; i <= 200; i++) { pdialog1->SetPos(i / 2); // pdialog1->UpdateWindow(); // DoEvents(); Sleep(50); } // pdialog1->SetPos(99); // pdialog1->UpdateWindow(); for (int i = 0; i < 10; i++) { DoEvents(); Sleep(10); } pdialog1->DestroyWindow(); delete pdialog1; pdialog1 = NULL; // TODO: 在此添加控件通知处理程序代码 } void CMTerm1CommDevView::OnMenuEventLog() { // TODO: 在此添加命令处理程序代码 CDialogEventLog dialog1; INT_PTR r = dialog1.DoModal(); } void CMTerm1CommDevView::OnMenuCommTest() { // TODO: 在此添加命令处理程序代码 // CDialogCommTest dialog1; // INT_PTR r = dialog1.DoModal(); } void CMTerm1CommDevView::OnMenuForceIo() { // TODO: 在此添加命令处理程序代码 CDialogForceIO dialog1; INT_PTR r = dialog1.DoModal(); } void CMTerm1CommDevView::OnMenuDatetime() { // TODO: 在此添加命令处理程序代码 CDialogDateTime dialog1; INT_PTR r = dialog1.DoModal(); } typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef unsigned int uint32_t; typedef int int32_t; const uint16_t polynom = 0xA001; uint16_t crc16bitbybit(uint8_t *ptr, uint16_t len) { uint8_t i; uint16_t crc = 0xffff; if (len == 0) { len = 1; } while (len--) { crc ^= *ptr; for (i = 0; i < 8; i++) { if (crc & 1) { crc >>= 1; crc ^= polynom; } else { crc >>= 1; } } ptr++; } return(crc); } /* Table of CRC values for high–order byte */ const uint8_t crctablehi[] = { 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 }; /* Table of CRC values for low–order byte */ const uint8_t crctablelo[] = { 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40 }; uint16_t crc16table(uint8_t *ptr, uint16_t len) { uint8_t crchi = 0xff; uint8_t crclo = 0xff; uint16_t index; while (len--) { index = crclo ^ *ptr++; crclo = crchi ^ crctablehi[index]; crchi = crctablelo[index]; } return (crchi << 8 | crclo); } const uint16_t crctalbeabs[] = { 0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401, 0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400 }; uint16_t crc16tablefast(uint8_t *ptr, uint16_t len) { uint16_t crc = 0xffff; uint16_t i; uint8_t ch; for (i = 0; i < len; i++) { ch = *ptr++; crc = crctalbeabs[(ch ^ crc) & 15] ^ (crc >> 4); crc = crctalbeabs[((ch >> 4) ^ crc) & 15] ^ (crc >> 4); } return crc; } void modbuscrc16test() { printf("\n"); printf(" Modbus CRC16 tester\n"); printf("-----------------------------------------------------------------------\n"); uint8_t crc16_data[] = { 0x01, 0x04, 0x04, 0x43, 0x6b, 0x58, 0x0e }; // expected crc value 0xD825. printf(" modbus crc16table test, expected value : 0xd825, calculate value : 0x%x\n", crc16table(crc16_data, sizeof(crc16_data))); printf(" modbus crc16tablefast test, expected value : 0xd825, calculate value : 0x%x\n", crc16tablefast(crc16_data, sizeof(crc16_data))); printf(" modbus crc16bitbybit test, expected value : 0xd825, calculate value : 0x%x\n", crc16bitbybit(crc16_data, sizeof(crc16_data))); } void CMTerm1CommDevView::OnBnClickedButton11() { // TODO: 在此添加控件通知处理程序代码 uint8_t crc16_data[] = { 0x01, 0x04, 0x04, 0x43, 0x6b, 0x58, 0x0e }; // expected crc value 0xD825. uint16_t crc1, crc2, crc3; double time1 = GetTimemS(); for (int i = 0; i < 10000; i++) crc1 = crc16table(crc16_data, sizeof(crc16_data)); double time2 = GetTimemS(); for (int i = 0; i < 10000; i++) crc2 = crc16tablefast(crc16_data, sizeof(crc16_data)); double time3 = GetTimemS(); for (int i = 0; i < 10000; i++) crc3 = crc16bitbybit(crc16_data, sizeof(crc16_data)); double time4 = GetTimemS(); CString s1; s1.Format(_T("crc results \r\n")); s1.AppendFormat(_T("crc result1 %x time %f \r\n"), crc1, time2 - time1); s1.AppendFormat(_T("crc result2 %x time %f \r\n"), crc2, time3 - time2); s1.AppendFormat(_T("crc result3 %x time %f \r\n"), crc3, time4 - time3); SysLog(s1); } void CMTerm1CommDevView::OnBnClickedButton12() { // TODO: 在此添加控件通知处理程序代码 CMTerm1Doc *pDoc = (CMTerm1Doc *)GetDocument(); /* CString s1; pDoc->MyKLink1.MySerPort1.SetParams(1, 115200, _T("8-N-1")); int r = pDoc->MyKLink1.MySerPort1.Open(); s1.Format(_T("%d %s"), r, pDoc->MyKLink1.MySerPort1.m_strResult); SysLog(s1); */ } void CMTerm1CommDevView::OnBnClickedButton13() { // TODO: 在此添加控件通知处理程序代码 CMTerm1Doc *pDoc = (CMTerm1Doc *)GetDocument(); /* CString s1; int r = pDoc->MyKLink1.MySerPort1.Close(); s1.Format(_T("%d %s"), r, pDoc->MyKLink1.MySerPort1.m_strResult); SysLog(s1); double REPO_RATE(0.0); REPO_RATE = _tstof(_T("")); s1.Format(_T("RAPO_RATE = %f \n"), REPO_RATE); SysLog(s1); */ } enum enStat { normal=0, setchn=1, setvalue=2, setonoff=3, }; int nStat = 0; int ntimecount = 0; #define MaxTimeOut 50 int nCurChn = 0; int nValue[4] = { 100,200,300,400 }; int LedOn[4] = { 0 }; int flickTimer = 0; int CMTerm1CommDevView::UpdateLEDDisplay() { // TODO: 在此处添加实现代码. flickTimer++; int bHide = 0; if ((flickTimer & 0x7) > 3) { bHide = 1; } CString s1; s1 = _T("0000"); s1.Format(_T("%d%03d"), nCurChn, nValue[nCurChn]); if (nStat == setchn && bHide) { s1.Format(_T(" %03d"), nValue[nCurChn]); } if (nStat == setvalue && bHide) { s1.Format(_T("%d "),nCurChn); } if (nStat == setonoff && bHide) { s1.Format(_T(" "), nCurChn); } SetDlgItemText(IDC_EDIT31, s1); s1.Format(_T("%d %d %d %d"), LedOn[0], LedOn[1], LedOn[2], LedOn[3]); SetDlgItemText(IDC_EDIT32, s1); ntimecount++; if (ntimecount >= MaxTimeOut) { nStat = normal; } return 0; } void CMTerm1CommDevView::OnBnClickedButton31() { // TODO: 在此添加控件通知处理程序代码 ntimecount = 0; if (nStat == setchn) { nCurChn++; if (nCurChn >= 4) { nCurChn = 0; } } if (nStat == setvalue) { nValue[nCurChn] ++; if (nValue[nCurChn] > 999) { nValue[nCurChn] = 999; } } if (nStat == setonoff) { LedOn[nCurChn] =1; } } void CMTerm1CommDevView::OnBnClickedButton2() { // TODO: 在此添加控件通知处理程序代码 ntimecount = 0; if (nStat == setchn) { nCurChn--; if (nCurChn <0) { nCurChn = 3; } } if (nStat == setvalue) { nValue[nCurChn] --; if (nValue[nCurChn] <= 0) { nValue[nCurChn] = 0; } } if (nStat == setonoff) { LedOn[nCurChn] = 0; } } void CMTerm1CommDevView::OnBnClickedButton3() { // TODO: 在此添加控件通知处理程序代码 ntimecount = 0; if (nStat == normal) { nStat = setchn; } else if (nStat == setchn) { nStat = setvalue; } else if (nStat == setvalue) { nStat = setonoff; } else if (nStat == setonoff) { nStat = setchn; } }