// 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) ON_BN_CLICKED(IDC_BUTTON5, &CMTerm1CommDevView::OnBnClickedButton5) ON_BN_CLICKED(IDC_BUTTON9, &CMTerm1CommDevView::OnBnClickedButton9) 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 */ BOOL CMTerm1CommDevView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) { // TODO: 在此添加专用代码和/或调用基类 return CFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext); } // 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(); theApp.MyKLink1.MySerPort1.TotalSendBytes = 0; theApp.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; theApp.MyKLink1.MySerPort1.CalSpeed(); s1.Format(_T("Send %8d %5d Bps \r\nRecv %8d %5d Bps\r\n"), theApp.MyKLink1.MySerPort1.TotalSendBytes, theApp.MyKLink1.MySerPort1.SendSpeed, theApp.MyKLink1.MySerPort1.TotalRecvBytes, theApp.MyKLink1.MySerPort1.RecvSpeed); // s1.AppendFormat(_T("Total S %8d \r\nTotal R %8d \r\n"), MySerialCom1.TotalSendBytes, MySerialCom1.TotalRecvBytes); s1 += intToString(theApp.MyKLink1.MySerPort1.TotalRecvBytes) + _T("\r\n"); // stepCount++; // static double LastCalTime = 0; // double thisTime = GetTimemS(); theApp.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 "), theApp.MyKLink1.m_nTotalSendCount, theApp.MyKLink1.m_nSeq, theApp.MyKLink1.m_nRSeq, theApp.MyKLink1.m_SendSpeed, theApp.MyKLink1.m_nTotalRecvCount, theApp.MyKLink1.m_nTotalSendCount-theApp.MyKLink1.m_nTotalRecvCount); SetDlgItemText(IDC_STATIC_SPEED, s1); if (!m_bMonitoring) return 0; 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 < 16; j++) { s2.AppendFormat(_T("%02X"), theApp.MyKLink1.MEM.WXB[i * 16 + j]); if (j == 7) { 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 < 16; j++) { s2.AppendFormat(_T("%02X"), theApp.MyKLink1.MEM.WYB[i * 16 + j]); if (j == 7) { s2.Append(_T(" ")); } else { s2.Append(_T(" ")); } } s2.Append(_T("\r\n")); } nCount = 16; s2.Append(_T("WLX ")); for (int i = 0; i < (nCount + 15) / 16; i++) { //s2.AppendFormat(_T("%02X: "), i * 16); for (int j = 0; j < 16; j++) { s2.AppendFormat(_T("%02X"), theApp.MyKLink1.MEM.WLXB[i * 16 + j]); if (j == 7) { s2.Append(_T(" ")); } else { s2.Append(_T(" ")); } } s2.Append(_T("\r\n")); } nCount = 16; s2.Append(_T("WLY ")); for (int i = 0; i < (nCount + 15) / 16; i++) { // s2.AppendFormat(_T("%02X: "), i * 16); for (int j = 0; j < 16; j++) { s2.AppendFormat(_T("%02X"), theApp.MyKLink1.MEM.WLYB[i * 16 + j]); if (j == 7) { s2.Append(_T(" ")); } else { s2.Append(_T(" ")); } } s2.Append(_T("\r\n")); } /* nCount = 16; s2.Append(_T("WFX ")); for (int i = 0; i < (nCount + 15) / 16; i++) { //s2.AppendFormat(_T("%02X: "), i * 16); for (int j = 0; j < 16; j++) { s2.AppendFormat(_T("%04X"), theApp.MyKLink1.MEM.WFXB[i * 16 + j]); if (j == 3) { s2.Append(_T(" ")); } else { s2.Append(_T(" ")); } } s2.Append(_T("\r\n")); } nCount = 16; s2.Append(_T("WFY ")); for (int i = 0; i < (nCount + 15) / 16; i++) { // s2.AppendFormat(_T("%02X: "), i * 16); for (int j = 0; j < 16; j++) { s2.AppendFormat(_T("%04X"), theApp.MyKLink1.MEM.WFYB[i * 16 + 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"), theApp.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"), theApp.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"), theApp.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"), theApp.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"), theApp.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"), theApp.MyKLink1.MEM.SDD[0], intToBinString(theApp.MyKLink1.MEM.SDD[0]), theApp.MyKLink1.MEM.SDD[1]); // s1.AppendFormat(_T("当前跳线 %02X %s\r\n"), theApp.MyKLink1.MEM.SDD[1], intToBinString(theApp.MyKLink1.MEM.SDD[1])); s1.AppendFormat(_T("02 Tick计数 %u \r\n"), theApp.MyKLink1.MEM.SDD[2]); s1.AppendFormat(_T("03 nRunCount %u \r\n"), theApp.MyKLink1.MEM.SDD[3]); s1.AppendFormat(_T("04 RunStat %d \r\n"), theApp.MyKLink1.MEM.SDD[4]); s1.AppendFormat(_T("05 ErrStat %d \r\n"), theApp.MyKLink1.MEM.SDD[5]); s1.AppendFormat(_T("06 PwrOnCount %d \r\n"), theApp.MyKLink1.MEM.SDD[6]); int nTime1 = theApp.MyKLink1.MEM.SDD[7]; s1.AppendFormat(_T("07 ThisRunTime %d %dd %02d:%02d:%02d\r\n"), nTime1, nTime1 / 86400, nTime1 / 3600 % 24, nTime1 / 60 % 60, nTime1 % 60); nTime1 = theApp.MyKLink1.MEM.SDD[8]; s1.AppendFormat(_T("08 TotalTime %d %dd %02d:%02d:%02d\r\n"), nTime1, nTime1 / 86400, nTime1 / 3600 % 24, nTime1 / 60 % 60, nTime1 % 60); s1.AppendFormat(_T("09 CurTime %d "), theApp.MyKLink1.MEM.SDD[9]); nTime1 = theApp.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"), theApp.MyKLink1.MEM.SDD[10]); s1.AppendFormat(_T("11 LastPwrFailTime %d \r\n"), theApp.MyKLink1.MEM.SDD[11]); nTime1 = theApp.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"), theApp.MyKLink1.MEM.SDD[12]); s1.AppendFormat(_T("13 ScanTime uS %d \r\n"), theApp.MyKLink1.MEM.SDD[13]); s1.AppendFormat(_T("14 MinScanTime uS %d \r\n"), theApp.MyKLink1.MEM.SDD[14]); s1.AppendFormat(_T("15 %d \r\n"), theApp.MyKLink1.MEM.SDD[15]); s1.AppendFormat(_T("16 %d \r\n"), theApp.MyKLink1.MEM.SDD[16]); s1.AppendFormat(_T("17 %d \r\n"), theApp.MyKLink1.MEM.SDD[17]); s1.AppendFormat(_T("18 %d \r\n"), theApp.MyKLink1.MEM.SDD[18]); s1.AppendFormat(_T("19 %d \r\n"), theApp.MyKLink1.MEM.SDD[19]); s1.AppendFormat(_T("20 %d \r\n"), theApp.MyKLink1.MEM.SDD[20]); // s1.AppendFormat(_T("21 %d \r\n"), theApp.MyKLink1.MEM.SDD[21]); // s1.AppendFormat(_T("22 %d \r\n"), theApp.MyKLink1.MEM.SDD[22]); // s1.AppendFormat(_T("23 %d \r\n"), theApp.MyKLink1.MEM.SDD[23]); float Vref = 1.2f; //theApp.MyKLink1.MEM.SDT[56] = 1521; float Vcor = (float)theApp.MyKLink1.MEM.SDT[48 + 18] / theApp.MyKLink1.MEM.SDT[48 + 17]; float V33 = 3.3f * Vcor; s1.AppendFormat(_T("24 ADC0 %d \r\n"), theApp.MyKLink1.MEM.SDT[48 + 0]); s1.AppendFormat(_T("25 ADC1 %d \r\n"), theApp.MyKLink1.MEM.SDT[48 + 1]); s1.AppendFormat(_T("26 ADC2 %d \r\n"), theApp.MyKLink1.MEM.SDT[48 + 2]); s1.AppendFormat(_T("27 ADC3 %d \r\n"), theApp.MyKLink1.MEM.SDT[48 + 3]); s1.AppendFormat(_T("28 ADC4 %d \r\n"), theApp.MyKLink1.MEM.SDT[48 + 4]); s1.AppendFormat(_T("29 ADC5 %d \r\n"), theApp.MyKLink1.MEM.SDT[48 + 5]); s1.AppendFormat(_T("30 ADC6 %d \r\n"), theApp.MyKLink1.MEM.SDT[48 + 6]); s1.AppendFormat(_T("30 ADC7 %d \r\n"), theApp.MyKLink1.MEM.SDT[48 + 7]); s1.AppendFormat(_T("30 ADC8 %d \r\n"), theApp.MyKLink1.MEM.SDT[48 + 8]); float temp = (1430 - theApp.MyKLink1.MEM.SDT[48 + 16]* 0.806f ) / 4.3f + 25; s1.AppendFormat(_T("31 芯片温度 %d %.1f ℃ \r\n"), theApp.MyKLink1.MEM.SDT[48 + 16],temp); s1.AppendFormat(_T("32 1.2V参考 %d 3.3V电压 %.3f V \r\n"), theApp.MyKLink1.MEM.SDT[48 + 17], V33); s1.AppendFormat(_T("33 1.2V校准电压 %d \r\n"), theApp.MyKLink1.MEM.SDT[48 + 18]); s2 += s1; s2.AppendFormat(_T("\r\n")); //型号 //版本 //协议版本 //生产日期 //生产批号 //序列号 //开机次数 //总开机时间 //最大连续开机时间 //本次开机时间 //电源闪断次数 //总看门狗动作次数 //本次看门狗动作次数 //电源电压 //核心温度 //输入输出 //IO状态,开路,短路,熔断..... //包顺序错误 //令牌 广播形式, 环路形式 // s1.Format(_T("PosX %d \r\n"), (signed short)theApp.MyKLink1.MEM.WDT[24]); s1.AppendFormat(_T("PosY %d \r\n"),(signed short) theApp.MyKLink1.MEM.WDT[25]); s1.AppendFormat(_T("PosZ %d \r\n"), theApp.MyKLink1.MEM.WDT[26]); s1.AppendFormat(_T("PosZ1 %d \r\n"), theApp.MyKLink1.MEM.WDT[27]); s1.AppendFormat(_T("PosZ2 %d \r\n"), theApp.MyKLink1.MEM.WDT[28]); s1.AppendFormat(_T("PosZ3 %d \r\n"), theApp.MyKLink1.MEM.WDT[29]); s1.AppendFormat(_T("result0 %d \r\n"), (signed short)theApp.MyKLink1.MEM.WDT[32]); s1.AppendFormat(_T("result1 %d \r\n"), (signed short)theApp.MyKLink1.MEM.WDT[33]); s1.AppendFormat(_T("result2 %d \r\n"), (signed short)theApp.MyKLink1.MEM.WDT[34]); s1.AppendFormat(_T("result3 %d \r\n"), (signed short)theApp.MyKLink1.MEM.WDT[35]); s1.AppendFormat(_T("result4 %d \r\n"), (signed short)theApp.MyKLink1.MEM.WDT[36]); s1.AppendFormat(_T("result5 %d \r\n"), (signed short)theApp.MyKLink1.MEM.WDT[37]); s1.AppendFormat(_T("result6 %d \r\n"), (signed short)theApp.MyKLink1.MEM.WDT[38]); s1.AppendFormat(_T("result7 %d \r\n"), (signed short)theApp.MyKLink1.MEM.WDT[39]); s2 += s1; /* int nOffset = 32; s1.Empty(); s1.Format(_T("子机1\r\n")); KLink1::pChnStat pchnstat1 = (KLink1::pChnStat)&(theApp.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)&(theApp.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)(theApp.MyKLink1.MEM.KWLB); s1 = KWRunStatToString((pstWLRunStat)(theApp.MyKLink1.MEM.KWLB)); SetDlgItemText(IDC_EDIT_MON4, s1); //SetRedraw(TRUE); //s1.Append(s2); //s1.Append(_T("\r\n")); UpdateStatusBar(-1); return 0; } CString CMTerm1CommDevView::KWRunStatToString(pstWLRunStat pWLRunStat1) { CString s1; // pstWLRunStat pWLRunStat1 = (pstWLRunStat)(theApp.MyKLink1.MEM.KWLB); s1.Format(_T("stsize %d 无线状态 %04X %04X 步骤 %04X \r\n"), sizeof(stWLRunStat), pWLRunStat1->Status, pWLRunStat1->curStat, pWLRunStat1->runStep); s1.AppendFormat(_T("频率 %.3f Bw %d TxP %ddBm Fctr %d CDR %d ToA %dmS\r\n"), float(pWLRunStat1->RF_Freq / 1000000.0f), pWLRunStat1->LoraBandWidth, pWLRunStat1->Tx_Power, pWLRunStat1->LoRaFactor, pWLRunStat1->LoRaCodingRate, pWLRunStat1->nTimeOnAir); s1 += _T("当前子机: ") + intToString(pWLRunStat1->nCurClient) + _T("\r\n"); // s1 += _T("频率: ") + intToString(pWLRunStat1->RF_Freq) + _T(" 空中时间: ") + intToString(pWLRunStat1->nTimeOnAir) + _T("\r\n"); // s1 += _T("BW: ") + intToString(pWLRunStat1->LoraBandWidth) + _T(" Tx_Pwr: ") + intToString(pWLRunStat1->Tx_Power) +_T(" "); // s1 += _T("Factor: ") + intToString(pWLRunStat1->LoRaFactor) + _T(" CodingRate : ") + intToString(pWLRunStat1->LoRaCodingRate) + _T("\r\n"); s1 += _T("发送数量: ") + intToString(pWLRunStat1->sentCount) + _T(" 对方收到: ") + intToString(pWLRunStat1->targetRecvdCount) + _T(" 差值: ") + intToString(pWLRunStat1->sentCount - pWLRunStat1->targetRecvdCount) + _T("\r\n"); s1 += _T("接收数量: ") + intToString(pWLRunStat1->recvCount) + _T(" 对方发出: ") + intToString(pWLRunStat1->targetSentCount) + _T(" 差值: ") + intToString(pWLRunStat1->targetSentCount - pWLRunStat1->recvCount) + _T("\r\n"); s1 += _T("发送时间: ") + fixToString(pWLRunStat1->lastSendtime, 0, 1) + _T(" mS\r\n"); s1 += _T("发完时间: ") + fixToString(pWLRunStat1->lastSenttime, 0, 1) + _T(" ") + fixToString(pWLRunStat1->lastSenttime - pWLRunStat1->lastSendtime, 0, 1) + _T(" mS\r\n"); s1 += _T("接收时间: ") + fixToString(pWLRunStat1->lastRecvtime, 0, 1) + _T(" mS\r\n"); s1 += _T("收到时间: ") + fixToString(pWLRunStat1->lastRecvdtime, 0, 1) + _T(" ") + fixToString(pWLRunStat1->lastRecvdtime - pWLRunStat1->lastRecvtime, 0, 1) + +_T(" mS\r\n"); s1 += _T("动作时间: ") + fixToString(pWLRunStat1->lastActTime, 0, 1) + _T(" mS\r\n"); s1 += _T("响应时间: ") + fixToString(pWLRunStat1->lastAckTime, 0, 1) + _T(" ") + fixToString(pWLRunStat1->lastAckTime - pWLRunStat1->lastActTime, 0, 1) + _T(" mS\r\n"); s1 += _T("循环时间: ") + fixToString(pWLRunStat1->cycleTime, 0, 1) + _T(" mS\r\n"); s1 += _T("延迟: ") + fixToString(pWLRunStat1->latancy, 0, 1) + _T(" mS\r\n"); s1.AppendFormat(_T("丢包数量: %d 连续丢包: %d 最大连续 %d\r\n"), pWLRunStat1->LostPackets, pWLRunStat1->CtnLstPkts, pWLRunStat1->MaxCtnLstPkts); s1.AppendFormat(_T("发送错误: %d 接收错误: %d 时间 %s mS\r\n"), pWLRunStat1->TXErr, pWLRunStat1->RXErr, fixToString(pWLRunStat1->lastErrTime, 0, 1)); s1.AppendFormat(_T("CRC错误: %d 包格式错误 %d 地址错误 %d %04X\r\n"), pWLRunStat1->CRCErr, pWLRunStat1->PktErr, pWLRunStat1->ChnErr, pWLRunStat1->nErrChn); s1.AppendFormat(_T("CAD次数: %d OK %d NG %d TimeOut %d Per %.2f%% \r\n"), pWLRunStat1->CADDoneCount, pWLRunStat1->CADOkCount, pWLRunStat1->CADNgCount, pWLRunStat1->CADTimeOut, pWLRunStat1->CADNgCount*100.f/pWLRunStat1->CADDoneCount); s1.AppendFormat(_T("小错误1: %d 中错误2: %d 大错误3: %d\r\n"), pWLRunStat1->Err1Count, pWLRunStat1->Err2Count, pWLRunStat1->Err3Count); s1.AppendFormat(_T("步骤错误1: %d 步骤错误2: %d\r\n"), pWLRunStat1->StepErr1, pWLRunStat1->StepErr2); CString s2; if (pWLRunStat1->RSSI < -100) { s2.Format(_T("(0%%)")); } else if (pWLRunStat1->RSSI > 0) { s2.Format(_T("(100%%)")); } else { s2.Format(_T("(%d%%)"), pWLRunStat1->RSSI + 100); } s1.AppendFormat(_T("本机信号强度: %d dBm SNR %d dB ▁▃▅▇\r\n"), pWLRunStat1->RSSI, pWLRunStat1->SNR); if (pWLRunStat1->tRSSI < -100) { s2.Format(_T("(0%%)")); } else if (pWLRunStat1->tRSSI > 0) { s2.Format(_T("(100%%)")); } else { s2.Format(_T("(%d%%)"), pWLRunStat1->tRSSI + 100); } s1.AppendFormat(_T("对方信号强度: %d dBm SNR %d dB ▁▃▅▇\r\n"), pWLRunStat1->tRSSI, pWLRunStat1->tSNR); return s1; } CString CMTerm1CommDevView::KWRunStatToString(pstWLRunStatV12 pWLRunStat1) { CString s1; // pstWLRunStat pWLRunStat1 = (pstWLRunStat)(theApp.MyKLink1.MEM.KWLB); s1.Format(_T("stsize %d 无线状态 %04X %04X 步骤 %04X \r\n"),sizeof(stWLRunStatV12), pWLRunStat1->Status, pWLRunStat1->curStat, pWLRunStat1->runStep); s1 += _T("频率: ") + intToString(pWLRunStat1->RF_Freq) + _T(" 空中时间: ") + intToString(pWLRunStat1->nTimeOnAir) + _T("\r\n"); s1 += _T("发送数量: ") + intToString(pWLRunStat1->sentCount) + _T(" 对方收到: ") + intToString(pWLRunStat1->targetRecvdCount) + _T(" 差值: ") + intToString(pWLRunStat1->sentCount - pWLRunStat1->targetRecvdCount) + _T("\r\n"); s1 += _T("接收数量: ") + intToString(pWLRunStat1->recvCount) + _T(" 对方发出: ") + intToString(pWLRunStat1->targetSentCount) + _T(" 差值: ") + intToString(pWLRunStat1->targetSentCount - pWLRunStat1->recvCount) + _T("\r\n"); s1 += _T("发送时间: ") + fixToString(pWLRunStat1->lastSendtime, 0, 1) + _T(" mS\r\n"); s1 += _T("发完时间: ") + fixToString(pWLRunStat1->lastSenttime, 0, 1) + _T(" ") + fixToString(pWLRunStat1->lastSenttime - pWLRunStat1->lastSendtime, 0, 1) + _T(" mS\r\n"); s1 += _T("接收时间: ") + fixToString(pWLRunStat1->lastRecvtime, 0, 1) + _T(" mS\r\n"); s1 += _T("收到时间: ") + fixToString(pWLRunStat1->lastRecvdtime, 0, 1) + _T(" ") + fixToString(pWLRunStat1->lastRecvdtime - pWLRunStat1->lastRecvtime, 0, 1) + +_T(" mS\r\n"); s1 += _T("动作时间: ") + fixToString(pWLRunStat1->lastActTime, 0, 1) + _T(" mS\r\n"); s1 += _T("响应时间: ") + fixToString(pWLRunStat1->lastAckTime, 0, 1) + _T(" ") + fixToString(pWLRunStat1->lastAckTime - pWLRunStat1->lastActTime, 0, 1) + _T(" mS\r\n"); s1 += _T("循环时间: ") + fixToString(pWLRunStat1->cycleTime, 0, 1) + _T(" mS\r\n"); s1 += _T("延迟: ") + fixToString(pWLRunStat1->latancy, 0, 1) + _T(" mS\r\n"); s1.AppendFormat(_T("丢包数量: %d 连续丢包: %d 最大连续 %d\r\n"), pWLRunStat1->LostPackets, pWLRunStat1->CtnLstPkts, pWLRunStat1->MaxCtnLstPkts); s1.AppendFormat(_T("发送错误: %d 接收错误: %d 时间 %s mS\r\n"), pWLRunStat1->TXErr, pWLRunStat1->RXErr, fixToString(pWLRunStat1->lastErrTime, 0, 1)); s1.AppendFormat(_T("CRC错误: %d 频道错误 %d 包错误 %d\r\n"), pWLRunStat1->CRCErr, pWLRunStat1->ChnErr, pWLRunStat1->PktErr); s1.AppendFormat(_T("CAD次数: %d\r\n"), pWLRunStat1->CADDoneCount); s1.AppendFormat(_T("小错误1: %d 中错误2: %d 大错误3: %d\r\n"), pWLRunStat1->Err1Count, pWLRunStat1->Err2Count, pWLRunStat1->Err3Count); s1.AppendFormat(_T("步骤错误1: %d 步骤错误2: %d\r\n"), pWLRunStat1->StepErr1, pWLRunStat1->StepErr2); CString s2; if (pWLRunStat1->RSSI < -100) { s2.Format(_T("(0%%)")); } else if (pWLRunStat1->RSSI > 0) { s2.Format(_T("(100%%)")); } else { s2.Format(_T("(%d%%)"), pWLRunStat1->RSSI + 100); } s1.AppendFormat(_T("本机信号强度: %d dBm ▁▃▅▇ SNR %d dB\r\n"), pWLRunStat1->RSSI, pWLRunStat1->SNR); if (pWLRunStat1->tRSSI < -100) { s2.Format(_T("(0%%)")); } else if (pWLRunStat1->tRSSI > 0) { s2.Format(_T("(100%%)")); } else { s2.Format(_T("(%d%%)"), pWLRunStat1->tRSSI + 100); } s1.AppendFormat(_T("对方信号强度: %d dBm ▁▃▅▇ SNR %d dB\r\n"), pWLRunStat1->tRSSI, pWLRunStat1->tSNR); return s1; } CString CMTerm1CommDevView::KWRunStatToString(pstWLRunStatV1 pWLRunStat1) { CString s1; // pstWLRunStat pWLRunStat1 = (pstWLRunStat)(theApp.MyKLink1.MEM.KWLB); s1.Format(_T("stsize %d 无线状态 %04X %04X 步骤 %04X \r\n"), sizeof(stWLRunStatV1), pWLRunStat1->Status, pWLRunStat1->curStat, pWLRunStat1->runStep); // s1 += _T("频率: ") + intToString(pWLRunStat1->RF_Freq) + _T(" 空中时间: ") + intToString(pWLRunStat1->nTimeOnAir) + _T("\r\n"); s1 += _T("发送数量: ") + intToString(pWLRunStat1->sentCount) + _T(" 对方收到: ") + intToString(pWLRunStat1->targetRecvdCount) + _T(" 差值: ") + intToString(pWLRunStat1->sentCount - pWLRunStat1->targetRecvdCount) + _T("\r\n"); s1 += _T("接收数量: ") + intToString(pWLRunStat1->recvCount) + _T(" 对方发出: ") + intToString(pWLRunStat1->targetSentCount) + _T(" 差值: ") + intToString(pWLRunStat1->targetSentCount - pWLRunStat1->recvCount) + _T("\r\n"); s1 += _T("发送时间: ") + fixToString(pWLRunStat1->lastSendtime, 0, 1) + _T(" mS\r\n"); s1 += _T("发完时间: ") + fixToString(pWLRunStat1->lastSenttime, 0, 1) + _T(" ") + fixToString(pWLRunStat1->lastSenttime - pWLRunStat1->lastSendtime, 0, 1) + _T(" mS\r\n"); s1 += _T("接收时间: ") + fixToString(pWLRunStat1->lastRecvtime, 0, 1) + _T(" mS\r\n"); s1 += _T("收到时间: ") + fixToString(pWLRunStat1->lastRecvdtime, 0, 1) + _T(" ") + fixToString(pWLRunStat1->lastRecvdtime - pWLRunStat1->lastRecvtime, 0, 1) + +_T(" mS\r\n"); s1 += _T("动作时间: ") + fixToString(pWLRunStat1->lastActTime, 0, 1) + _T(" mS\r\n"); s1 += _T("响应时间: ") + fixToString(pWLRunStat1->lastAckTime, 0, 1) + _T(" ") + fixToString(pWLRunStat1->lastAckTime - pWLRunStat1->lastActTime, 0, 1) + _T(" mS\r\n"); s1 += _T("循环时间: ") + fixToString(pWLRunStat1->cycleTime, 0, 1) + _T(" mS\r\n"); s1 += _T("延迟: ") + fixToString(pWLRunStat1->latancy, 0, 1) + _T(" mS\r\n"); s1.AppendFormat(_T("丢包数量: %d 连续丢包: %d 最大连续 %d\r\n"), pWLRunStat1->LostPackets, pWLRunStat1->CtnLstPkts, pWLRunStat1->MaxCtnLstPkts); s1.AppendFormat(_T("发送错误: %d 接收错误: %d 时间 %s mS\r\n"), pWLRunStat1->TXErr, pWLRunStat1->RXErr, fixToString(pWLRunStat1->lastErrTime, 0, 1)); s1.AppendFormat(_T("CRC错误: %d \r\n"), pWLRunStat1->CRCErr); s1.AppendFormat(_T("CAD次数: %d\r\n"), pWLRunStat1->CADDoneCount); s1.AppendFormat(_T("小错误1: %d 中错误2: %d 大错误3: %d\r\n"), pWLRunStat1->Err1Count, pWLRunStat1->Err2Count, pWLRunStat1->Err3Count); s1.AppendFormat(_T("步骤错误1: %d 步骤错误2: %d\r\n"), pWLRunStat1->StepErr1, pWLRunStat1->StepErr2); CString s2; if (pWLRunStat1->RSSI < -100) { s2.Format(_T("(0%%)")); } else if (pWLRunStat1->RSSI > 0) { s2.Format(_T("(100%%)")); } else { s2.Format(_T("(%d%%)"), pWLRunStat1->RSSI+100); } s1.AppendFormat(_T("本机信号强度: %ddBm %s ▁▃▅▇ 信噪比 SNR %d dB\r\n"), pWLRunStat1->RSSI, s2, pWLRunStat1->SNR); if (pWLRunStat1->tRSSI < -100) { s2.Format(_T("(0%%)")); } else if (pWLRunStat1->tRSSI > 0) { s2.Format(_T("(100%%)")); } else { s2.Format(_T("(%d%%)"), pWLRunStat1->tRSSI + 100); } s1.AppendFormat(_T("对方信号强度: %ddBm %s ▁▃▅▇ 信噪比 SNR %d dB\r\n"), pWLRunStat1->tRSSI,s2, pWLRunStat1->tSNR); return s1; } int CMTerm1CommDevView::UpdateStatusBar(int nIndex) { // TODO: 在此处添加实现代码. CString s1; return 0; 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); //MonitorPLC(); 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("115200"); 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(); int r = theApp.MyKLink1.Connect(); s1.Format(_T("Open %s = %d"), theApp.MyKLink1.m_resultStr, r); SysLog(s1); if (r == theApp.MyKLink1.MySerPort1.R_OK) { m_bResourceOpened = true; // theApp.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(); theApp.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 && theApp.MyKLink1.MySerPort1.m_bOpened) { unsigned char DstAddr = 1; unsigned char DataType = theApp.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 = theApp.MyKLink1.KLDataTypeWX; DAddr = 0; DCount = 16; res = theApp.MyKLink1.ReadDataByte(DstAddr, DataType, DAddr, DCount, &nCount, &theApp.MyKLink1.MEM.WXB[DAddr]); nCount = DCount; if (res == theApp.MyKLink1.KL_OK) { nThisSuccessCount++; } else { nThisFailCount++; s1.Format(_T("R:= %d %s \r\n"), res, theApp.MyKLink1.m_resultStr); SysLog(s1); } } //*/ ///* //WY if (nStep == 0) { DataType = theApp.MyKLink1.KLDataTypeWY; DAddr = 0; DCount = 16; res = theApp.MyKLink1.ReadDataByte(DstAddr, DataType, DAddr, DCount, &nCount, &theApp.MyKLink1.MEM.WYB[DAddr]); nCount = DCount; if (res == theApp.MyKLink1.KL_OK) { nThisSuccessCount++; } else { nThisFailCount++; s1.Format(_T("R:= %d %s \r\n"), res, theApp.MyKLink1.m_resultStr); SysLog(s1); } } //*/ // WLX ///* if (nStep == 0) { DataType = theApp.MyKLink1.KLDataTypeWLX; DAddr = 0; DCount = 16; res = theApp.MyKLink1.ReadDataByte(DstAddr, DataType, DAddr, DCount, &nCount, &theApp.MyKLink1.MEM.WLXB[DAddr]); nCount = DCount; if (res == theApp.MyKLink1.KL_OK) { nThisSuccessCount++; } else { nThisFailCount++; s1.Format(_T("R:= %d %s \r\n"), res, theApp.MyKLink1.m_resultStr); SysLog(s1); } } //*/ ///* //WLY if (nStep == 0) { DataType = theApp.MyKLink1.KLDataTypeWLY; DAddr = 0; DCount = 16; res = theApp.MyKLink1.ReadDataByte(DstAddr, DataType, DAddr, DCount, &nCount, &theApp.MyKLink1.MEM.WLYB[DAddr]); nCount = DCount; if (res == theApp.MyKLink1.KL_OK) { nThisSuccessCount++; } else { nThisFailCount++; s1.Format(_T("R:= %d %s \r\n"), res, theApp.MyKLink1.m_resultStr); SysLog(s1); } } //*/ ///* //WR if (nStep==0) { DataType = theApp.MyKLink1.KLDataTypeWR; DAddr = 0; DCount = 32; res = theApp.MyKLink1.ReadDataByte(DstAddr, DataType, DAddr, DCount, &nCount, &theApp.MyKLink1.MEM.WRB[DAddr]); nCount = DCount; if (res == theApp.MyKLink1.KL_OK) { nThisSuccessCount++; } else { nThisFailCount++; s1.Format(_T("R:= %d %s \r\n"), res, theApp.MyKLink1.m_resultStr); SysLog(s1); } } //*/ //DT if (nStep == 1) { DataType = theApp.MyKLink1.KLDataTypeDT; DAddr = 0; DCount = 96; res = theApp.MyKLink1.ReadDataByte(DstAddr, DataType, DAddr, DCount, &nCount, &theApp.MyKLink1.MEM.DTB[DAddr]); nCount = DCount; if (res == theApp.MyKLink1.KL_OK) { nThisSuccessCount++; } else { nThisFailCount++; s1.Format(_T("R:= %d %s \r\n"), res, theApp.MyKLink1.m_resultStr); SysLog(s1); } } int ByteAddr = 0; //SDT if (nStep >= 2 && nStep <= 4) { int nBlock = nStep - 2; DataType = theApp.MyKLink1.KLDataTypeSDT; ByteAddr = nBlock * 128; DCount = 128; res = theApp.MyKLink1.ReadDataByte(DstAddr, DataType, ByteAddr, DCount, &nCount, &theApp.MyKLink1.MEM.SDB[ByteAddr]); nCount = DCount; if (res == theApp.MyKLink1.KL_OK) { for (int i = 0; i < DCount; i++) { SDTbuf[ByteAddr + i] = theApp.MyKLink1.MEM.SDB[ByteAddr + i]; } nThisSuccessCount++; } else { nThisFailCount++; s1.Format(_T("R:= %d %s \r\n"), res, theApp.MyKLink1.m_resultStr); SysLog(s1); } } if (nStep >= 5 && nStep <= 7) { int nBlock = nStep - 5; DataType = theApp.MyKLink1.KLDataTypeWDT; ByteAddr = nBlock * 128; DCount = 128; res = theApp.MyKLink1.ReadDataByte(DstAddr, DataType, ByteAddr, DCount, &nCount, &theApp.MyKLink1.MEM.WDB[ByteAddr]); nCount = DCount; if (res == theApp.MyKLink1.KL_OK) { for (int i = 0; i < DCount; i++) { WDTbuf[ByteAddr + i] = theApp.MyKLink1.MEM.WDB[ByteAddr + i]; } nThisSuccessCount++; } else { nThisFailCount++; s1.Format(_T("R:= %d %s \r\n"), res, theApp.MyKLink1.m_resultStr); SysLog(s1); } } if (nStep >= 8 && nStep <= 13) { int nBlock = nStep - 8; DataType = theApp.MyKLink1.KLDataTypeKBD; ByteAddr = nBlock * 128; DCount = 128; res = theApp.MyKLink1.ReadDataByte(DstAddr, DataType, ByteAddr, DCount, &nCount, &theApp.MyKLink1.MEM.KBDB[ByteAddr]); nCount = DCount; if (res == theApp.MyKLink1.KL_OK) { //for (int i = 0; i < DCount; i++) { WDTbuf[ByteAddr + i] = theApp.MyKLink1.MEM.WDB[ByteAddr + i]; } nThisSuccessCount++; } else { nThisFailCount++; s1.Format(_T("R:= %d %s \r\n"), res, theApp.MyKLink1.m_resultStr); SysLog(s1); } } if (nStep >= 14 && nStep <= 14) { int nBlock = nStep - 14; DataType = theApp.MyKLink1.KLDataTypeKWLD; ByteAddr = nBlock * 128; DCount = 128; res = theApp.MyKLink1.ReadDataByte(DstAddr, DataType, ByteAddr, DCount, &nCount, &theApp.MyKLink1.MEM.KWLB[ByteAddr]); nCount = DCount; if (res == theApp.MyKLink1.KL_OK) { //for (int i = 0; i < DCount; i++) { WDTbuf[ByteAddr + i] = theApp.MyKLink1.MEM.WDB[ByteAddr + i]; } nThisSuccessCount++; } else { nThisFailCount++; s1.Format(_T("R:= %d %s \r\n"), res, theApp.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 = theApp.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 = theApp.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 = theApp.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; theApp.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 = 0; // 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")); } theApp.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 = theApp.MyKLink1.MakeExtDataFrame(buf2, ExtDataDst, ExtDataType, ExtDataLen, Data2); KLink1::unKLStat nStat1 = { 0 }; nStat1.HasExt = bExtData; nStat1.nSEQ = theApp.MyKLink1.GetNextSeq(); // Cmd += 0x30; //KLink::cmdRead int len1 = theApp.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 = theApp.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 = theApp.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]; theApp.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")); } theApp.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; /* CString s1; theApp.MyKLink1.MySerPort1.SetParams(1, 115200, _T("8-N-1")); int r = theApp.MyKLink1.MySerPort1.Open(); s1.Format(_T("%d %s"), r, theApp.MyKLink1.MySerPort1.m_strResult); SysLog(s1); */ int k = theApp.MyKLink1.ResetDevice(1, 0); s1.Format(_T("ResetDevice = %d"), k); SysLog(s1); } void CMTerm1CommDevView::OnBnClickedButton13() { // TODO: 在此添加控件通知处理程序代码 // CMTerm1Doc *pDoc = (CMTerm1Doc *)GetDocument(); /* CString s1; int r = theApp.MyKLink1.MySerPort1.Close(); s1.Format(_T("%d %s"), r, theApp.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; } } // 热电阻电压转温度 查表 const short tempTab[] = { 261, -20, 276, -19, 291, -18, 307, -17, 324, -16, 341, -15, 359, -14, 378, -13, 397, -12, 417, -11, 438, -10, 460, -9, 483, -8, 506, -7, 530, -6, 555, -5, 581, -4, 608, -3, 635, -2, 663, -1, 692, 0, 722, 1, 753, 2, 784, 3, 817, 4, 850, 5, 884, 6, 918, 7, 954, 8, 990, 9, 1026, 10, 1064, 11, 1102, 12, 1140, 13, 1179, 14, 1219, 15, 1259, 16, 1300, 17, 1341, 18, 1383, 19, 1425, 20, 1467, 21, 1509, 22, 1552, 23, 1595, 24, 1638, 25, 1682, 26, 1725, 27, 1768, 28, 1812, 29, 1855, 30, 1899, 31, 1942, 32, 1985, 33, 2028, 34, 2070, 35, 2113, 36, 2155, 37, 2196, 38, 2238, 39, 2279, 40, 2319, 41, 2359, 42, 2399, 43, 2438, 44, 2477, 45, 2515, 46, 2552, 47, 2589, 48, 2626, 49, 2661, 50, 2697, 51, 2731, 52, 2765, 53, 2798, 54, 2831, 55, 2863, 56, 2895, 57, 2926, 58, 2956, 59, 2985, 60, 3014, 61, 3042, 62, 3070, 63, 3097, 64, 3123, 65, 3149, 66, 3174, 67, 3199, 68, 3222, 69, 3246, 70, 3268, 71, 3290, 72, 3312, 73, 3333, 74, 3354, 75, 3374, 76, 3393, 77, 3412, 78, 3430, 79, 3448, 80, 3466, 81, 3483, 82, 3499, 83, 3515, 84, 3531, 85, 3546, 86, 3561, 87, 3575, 88, 3589, 89, 3603, 90, 3616, 91, 3629, 92, 3641, 93, 3653, 94, 3665, 95, 3676, 96, 3687, 97, 3698, 98, 3708, 99, 3719, 100, 3729, 101, 3738, 102, 3747, 103, 3757, 104, 3765, 105, 3774, 106, 3782, 107, 3790, 108, 3798, 109, 3806, 110, }; int lookupTempTab(unsigned short value) { int n = sizeof(tempTab) / (sizeof(short) * 2); int p, q, r; p = 0; q = n - 1; r = (p + q) / 2; while (p < q && p != r && q != r) { short tempValue = tempTab[r * 2]; if (tempValue == value) { break; } else if (tempValue < value) { p = r; r = (p + q) / 2; } else if (tempValue > value) { q = r; r = (p + q) / 2; } } short value1 = tempTab[r * 2]; short temp1 = tempTab[r * 2 + 1] * 10; if (r == n - 1) return temp1; short value2 = tempTab[(r + 1) * 2]; short temp2 = tempTab[(r + 1) * 2 + 1] * 10; int temp3 = temp1 + (temp2 - temp1) * (value - value1) / (value2 - value1); // 线性插值 return temp3; } void CMTerm1CommDevView::OnBnClickedButton5() { // TODO: 在此添加控件通知处理程序代码 double a, b, c, d, e; double m = 0; int nCount = 360; int nMaxIndex=0; for (int i = 0; i < nCount; i++) { a = (double)i * 2 * 3.141592653589793238462643383279 / nCount; b = (double)(i+1) * 2 * 3.141592653589793238462643383279 / nCount ; c = (sin(a) + sin(b)) / 2; d = sin((a + b) / 2); e = abs(d - c); if (e > m) { m = e; nMaxIndex = i; } } CString s1; s1.Format(_T("检验查表插值法 计算正弦值 最大误差\r\n")); s1.AppendFormat(_T("0-180 正弦表分为 %d 点, 两点之间进行插值,找最大误差点\r\n"),nCount); s1.AppendFormat(_T("max diff %lf index %d"), m, nMaxIndex); SysLog(s1); nCount = 10; int nDigit = 8; s1.Format(_T("随机输出 %d 个 %d 位数\r\n"),nCount,nDigit); for (int i = 0; i < nCount; i++) { CString s2; for (int j = 0; j < nDigit; j++) { int k = rand() % 10; s2.AppendFormat(_T("%d"), k); } s1 += s2 + _T("\r\n"); } SysLog(s1); s1.Empty(); CString s2; for (int i = 0; i < 4096; i++) { int j = lookupTempTab(i); s2.Format(_T("%d %d \r\n"), i, j); s1 += s2; } SysLog(s1); } void CMTerm1CommDevView::OnBnClickedButton9() { // TODO: 在此添加控件通知处理程序代码 // Select File CMTerm1Doc* pDoc = (CMTerm1Doc*)GetDocument(); CString s1; USHORT len1; USHORT buf1[1024]; int r2 = theApp.MyKLink1.GetInfo(1, &len1, buf1); pKMInfoBlock pinfob = (pKMInfoBlock)buf1; if (r2 == 0 && len1 > 0) { for (int i = 0; i < len1 / 2; i++) { s1.AppendFormat(_T("%04X "), buf1[i]); } s1 += _T("\r\n"); s1.AppendFormat(_T(" DeviceType %04X \t "), pinfob->nDeviceTypeVer); s1.AppendFormat(_T(" nProgVer %04X \r\n"), pinfob->nProgVer); } SysLog(s1); myLogger1.UpdateLogDisplay(); unsigned char filebuf[65536]; unsigned int blocksize = 128; stNewAppInfoBlock NewAppInfo; CFileDialog dialog1(true); INT_PTR r = dialog1.DoModal(); if (r == IDOK) { CString sFilePathName = dialog1.GetPathName(); //OpenFile CFile file1; CFileException e; bool r = file1.Open(sFilePathName, CFile::modeRead | CFile::typeBinary, &e); s1.Format(_T("Open File %s = %d"), sFilePathName, r); DbgLog(s1); if (r) { int len1 = (int)file1.GetLength(); file1.Read(filebuf, len1); file1.Close(); unsigned short crc2 = crc16tablefast(filebuf, len1); unsigned char* buf2 = filebuf + 0x1000; ; //s1.Format(_T("GetInfo From File = %d %d \r\n"), j, len1); pKMInfoBlock pinfof = (pKMInfoBlock)buf2; s1.Format(_T(" 目标模块 类型 %04X 版本 %x, \r\n 固件文件 类型 %04X 版本 %x, %d Bytes\r\n 是否继续"), pinfob->nDeviceTypeVer, pinfob->nProgVer,pinfof->nDeviceTypeVer,pinfof->nProgVer,len1); int r3 = AfxMessageBox(s1, MB_YESNO); if (r3 == IDYES) { int bError = 0; for (int i = 0; i < len1; i += blocksize) { int sendsize = len1 - i; if (sendsize > blocksize) { sendsize = blocksize; } int k = 999; int j = 0; double time1 = GetTimemS(); for (j = 0; j < 5 && k != 0; j++) { k = theApp.MyKLink1.WriteFirmware(1, 0, i, sendsize, filebuf + i); if (k) { double time2 = GetTimemS(); s1.Format(_T("Wr@ %x %dB = %d re %d %.2fmS %s "), i, sendsize, k, j, time2 - time1, theApp.MyKLink1.m_resultStr); SysLog(s1); myLogger1.UpdateLogDisplay(); Sleep(30); } } double time2 = GetTimemS(); s1.Format(_T("Wr@ %x %dB = %d re %d %.2fmS"), i, sendsize, k, j, time2 - time1); SysLog(s1); myLogger1.UpdateLogDisplay(); if (k) { // 出现错误; bError = 1; CString sErr; sErr = KLink1::GetErrDescStr(k); s1.Format(_T("下载出错 %d, %s"), k,sErr); int r3 = AfxMessageBox(s1); break; //跳出 } } if (bError == 0) { NewAppInfo.Length = len1; NewAppInfo.Sign = 0x55AA; NewAppInfo.Version = 0x109; NewAppInfo.nCRC = crc2; int k = theApp.MyKLink1.WriteFirmInfo(1, 0, 0, sizeof(NewAppInfo), (UCHAR*)&NewAppInfo); s1.Format(_T("Write InfoBlock %d bytes = %d"), sizeof(NewAppInfo), k); SysLog(s1); Sleep(100); int r5 = theApp.MyKLink1.ResetDevice(1, 0); s1.Format(_T("ResetDevice = %d"), r5); SysLog(s1); myLogger1.UpdateLogDisplay(); Sleep(2000); } OnMenuOffline(); OnMenuOnline(); } } } }