#include "pch.h" #include "framework.h" #include "Resource.h" #include "MainFrm.h" #include "NavView.h" #include "MTerm1.h" #include "../MyLib/Functions.hpp" #define IDC_NAVTREE1 1046 class CClassViewMenuButton : public CMFCToolBarMenuButton { friend class CNavView; DECLARE_SERIAL(CClassViewMenuButton) public: CClassViewMenuButton(HMENU hMenu = nullptr) noexcept : CMFCToolBarMenuButton((UINT)-1, hMenu, -1) { } virtual void OnDraw(CDC* pDC, const CRect& rect, CMFCToolBarImages* pImages, BOOL bHorz = TRUE, BOOL bCustomizeMode = FALSE, BOOL bHighlight = FALSE, BOOL bDrawBorder = TRUE, BOOL bGrayDisabledButtons = TRUE) { pImages = CMFCToolBar::GetImages(); CAfxDrawState ds; pImages->PrepareDrawImage(ds); CMFCToolBarMenuButton::OnDraw(pDC, rect, pImages, bHorz, bCustomizeMode, bHighlight, bDrawBorder, bGrayDisabledButtons); pImages->EndDrawImage(ds); } }; IMPLEMENT_SERIAL(CClassViewMenuButton, CMFCToolBarMenuButton, 1) ////////////////////////////////////////////////////////////////////// // 构造/析构 ////////////////////////////////////////////////////////////////////// CNavView::CNavView() noexcept { m_nCurrSort = ID_SORTING_GROUPBYTYPE; } CNavView::~CNavView() { } BEGIN_MESSAGE_MAP(CNavView, CDockablePane) ON_WM_CREATE() ON_WM_SIZE() ON_WM_CONTEXTMENU() // ON_COMMAND(ID_CLASS_ADD_MEMBER_FUNCTION, OnClassAddMemberFunction) // ON_COMMAND(ID_CLASS_ADD_MEMBER_VARIABLE, OnClassAddMemberVariable) // ON_COMMAND(ID_CLASS_DEFINITION, OnClassDefinition) // ON_COMMAND(ID_CLASS_PROPERTIES, OnClassProperties) ON_COMMAND(ID_NEW_FOLDER, OnNewFolder) ON_COMMAND(ID_NAV_SETCOMM, OnSetComm) ON_COMMAND(ID_NAV_CONNECT, OnConnect) ON_COMMAND(ID_NAV_DISCONNECT, OnDisConnect) ON_WM_PAINT() ON_WM_SETFOCUS() ON_COMMAND_RANGE(ID_SORTING_GROUPBYTYPE, ID_SORTING_SORTBYACCESS, OnSort) ON_UPDATE_COMMAND_UI_RANGE(ID_SORTING_GROUPBYTYPE, ID_SORTING_SORTBYACCESS, OnUpdateSort) ON_NOTIFY(NM_DBLCLK, IDC_NAVTREE1, OnNMDblclkNavTree1) ON_NOTIFY(NM_CLICK, IDC_NAVTREE1, OnNMClkNavTree1) ON_WM_TIMER() ON_COMMAND(ID_MENU_NAV_SET_CONN, &CNavView::OnMenuNavSetConn) ON_COMMAND(ID_MENU_NAV_CONNECT, &CNavView::OnMenuNavConnect) ON_COMMAND(ID_MENU_NAV_DISCONNECT, &CNavView::OnMenuNavDisconnect) ON_COMMAND(ID_MENU_NV_UPDATE_FIRMWARE, &CNavView::OnMenuNvUpdateFirmware) ON_COMMAND(ID_MENU_NV_DEVICE_READ_INFO, &CNavView::OnMenuNvDeviceReadInfo) ON_COMMAND(ID_LED_BLINK, &CNavView::OnLedBlink) ON_COMMAND(ID_RESET_DEVICE, &CNavView::OnResetDevice) ON_COMMAND(ID_REMOTE_REFRESH_DATA, &CNavView::OnRemoteRefreshData) ON_COMMAND(ID_REMOTE_BLINK, &CNavView::OnRemoteBlinkLED) ON_COMMAND(ID_REMOTE_DEVICE_RESET, &CNavView::OnRemoteDeviceReset) ON_COMMAND(ID_REMOTE_UPDATE_FIRMWARE, &CNavView::OnRemoteUpdateFirmware) ON_COMMAND(ID_REMOTE_BLINK_LED_ALL, &CNavView::OnRemoteBlinkLedAll) ON_COMMAND(ID_PORT_RESET, &CNavView::OnPortReset) ON_COMMAND(ID_PORT_RESET_ALL_CHILD, &CNavView::OnPortResetAllChild) // ON_COMMAND(ID_COMMTEST, &CNavView::OnCommtest) // ON_COMMAND(ID_DEVICE_CONFIG, &CNavView::OnDeviceConfig) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CNavView 消息处理程序 int CNavView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CDockablePane::OnCreate(lpCreateStruct) == -1) return -1; CRect rectDummy; rectDummy.SetRectEmpty(); // 创建视图: const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | TVS_LINESATROOT | TVS_HASLINES | TVS_HASBUTTONS | WS_CLIPSIBLINGS | WS_CLIPCHILDREN ; // if (!m_NavViewTree.Create(dwViewStyle, rectDummy, this, IDC_NAVTREE1)) { TRACE0("未能创建导航栏视图\n"); return -1; // 未能创建 } m_NavViewTree.SetExtendedStyle(TVS_EX_DOUBLEBUFFER, TVS_EX_DOUBLEBUFFER); // 加载图像: m_wndToolBar.Create(this, AFX_DEFAULT_TOOLBAR_STYLE, IDR_NAV_TOOLBAR); m_wndToolBar.LoadToolBar(IDR_NAV_TOOLBAR, 0, 0, TRUE /* 已锁定*/); OnChangeVisualStyle(); m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() | CBRS_TOOLTIPS | CBRS_FLYBY); m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() & ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT)); m_wndToolBar.SetOwner(this); // 所有命令将通过此控件路由,而不是通过主框架路由: m_wndToolBar.SetRouteCommandsViaFrame(FALSE); CMenu menuSort; menuSort.LoadMenu(IDR_POPUP_NAV); m_wndToolBar.ReplaceButton(ID_SORT_MENU, CClassViewMenuButton(menuSort.GetSubMenu(0)->GetSafeHmenu())); CClassViewMenuButton* pButton = DYNAMIC_DOWNCAST(CClassViewMenuButton, m_wndToolBar.GetButton(0)); if (pButton != nullptr) { pButton->m_bText = FALSE; pButton->m_bImage = TRUE; pButton->SetImage(GetCmdMgr()->GetCmdImage(m_nCurrSort)); pButton->SetMessageWnd(this); } // 填入一些静态树视图数据(此处只需填入虚拟代码,而不是复杂的数据) SetTimer(0, 100, NULL); return 0; } void CNavView::OnSize(UINT nType, int cx, int cy) { CDockablePane::OnSize(nType, cx, cy); AdjustLayout(); } void CNavView::AdjustLayout() { if (GetSafeHwnd() == nullptr) { return; } CRect rectClient; GetClientRect(rectClient); int cyTlb = m_wndToolBar.CalcFixedLayout(FALSE, TRUE).cy; m_wndToolBar.SetWindowPos(nullptr, rectClient.left, rectClient.top, rectClient.Width(), cyTlb, SWP_NOACTIVATE | SWP_NOZORDER); m_NavViewTree.SetWindowPos(nullptr, rectClient.left + 1, rectClient.top + cyTlb + 1, rectClient.Width() - 2, rectClient.Height() - cyTlb - 2, SWP_NOACTIVATE | SWP_NOZORDER); } BOOL CNavView::PreTranslateMessage(MSG* pMsg) { return CDockablePane::PreTranslateMessage(pMsg); } void CNavView::OnSort(UINT id) { if (m_nCurrSort == id) { return; } m_nCurrSort = id; CClassViewMenuButton* pButton = DYNAMIC_DOWNCAST(CClassViewMenuButton, m_wndToolBar.GetButton(0)); if (pButton != nullptr) { pButton->SetImage(GetCmdMgr()->GetCmdImage(id)); m_wndToolBar.Invalidate(); m_wndToolBar.UpdateWindow(); } } void CNavView::OnTimer(UINT_PTR nIDEvent) { // TODO: 在此添加消息处理程序代码和/或调用默认值 if (nIDEvent == 0) { KillTimer(0); DelayInit(); SetTimer(1, 200, NULL); } else if (nIDEvent == 1) { UpdateDisplay(); } else if (nIDEvent == 2) { } else if (nIDEvent == 3) { } CDockablePane::OnTimer(nIDEvent); } int CNavView::DelayInit() { // TODO: 在此处添加实现代码. FillDeviceView(); return 0; } void CNavView::OnPaint() { CPaintDC dc(this); // 用于绘制的设备上下文 CRect rectTree; m_NavViewTree.GetWindowRect(rectTree); ScreenToClient(rectTree); rectTree.InflateRect(1, 1); dc.Draw3dRect(rectTree, ::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DSHADOW)); } void CNavView::OnSetFocus(CWnd* pOldWnd) { CDockablePane::OnSetFocus(pOldWnd); m_NavViewTree.SetFocus(); } void CNavView::OnChangeVisualStyle() { m_NavViewImages.DeleteImageList(); UINT uiBmpId = theApp.m_bHiColorIcons ? IDB_CLASS_VIEW_24 : IDB_CLASS_VIEW; // uiBmpId = IDB_NAV_VIEW1; CBitmap bmp; if (!bmp.LoadBitmap(uiBmpId)) { TRACE(_T("无法加载位图: %x\n"), uiBmpId); ASSERT(FALSE); return; } BITMAP bmpObj; bmp.GetBitmap(&bmpObj); UINT nFlags = ILC_MASK; nFlags |= (theApp.m_bHiColorIcons) ? ILC_COLOR24 : ILC_COLOR4; m_NavViewImages.Create(16, bmpObj.bmHeight, nFlags, 0, 0); m_NavViewImages.Add(&bmp, RGB(255, 0, 0)); m_NavViewTree.SetImageList(&m_NavViewImages, TVSIL_NORMAL); m_wndToolBar.CleanUpLockedImages(); m_wndToolBar.LoadBitmap(theApp.m_bHiColorIcons ? IDB_NAV_TOOLBAR_24 : IDR_NAV_TOOLBAR, 0, 0, TRUE /* 锁定*/); } void CNavView::FillDeviceView() { HTREEITEM hRoot1 = m_NavViewTree.InsertItem(_T("连接1 No Connect"), 0, 0); m_NavViewTree.SetItemState(hRoot1, TVIS_BOLD, TVIS_BOLD); HTREEITEM hItem; HTREEITEM hDevice1 = m_NavViewTree.InsertItem(_T("KL10-E08D-N1 1/1"), 5, 5, hRoot1); m_NavViewTree.SetItemData(hDevice1, MENU_DEVICE|0); m_NavViewTree.Expand(hRoot1, TVE_EXPAND); /* hItem = m_NavViewTree.InsertItem(_T("设备信息"), 3, 3, hDevice1); m_NavViewTree.SetItemData(hItem, MENU_PROPERTY|0); hItem = m_NavViewTree.InsertItem(_T("工作状态,系统参数"), 3, 3, hDevice1); m_NavViewTree.SetItemData(hItem, MENU_PROPERTY|1); HTREEITEM hPort1 = m_NavViewTree.InsertItem(_T("端口1 232 KLink 本机地址(1)/5 Run "), 1, 1, hDevice1); m_NavViewTree.SetItemData(hPort1, MENU_PORT|0); HTREEITEM hPort2 = m_NavViewTree.InsertItem(_T("端口2 485 KBus 本机地址(0)/5 Run "), 1, 1, hDevice1); m_NavViewTree.SetItemData(hPort2, MENU_PORT|1); hItem = m_NavViewTree.InsertItem(_T("1 KL10-E08D-N1 Run Good"), 5, 5, hPort2); m_NavViewTree.SetItemData(hItem, MENU_REMOTE_DEVICE|0); HTREEITEM hPort3 = m_NavViewTree.InsertItem(_T("端口3 WireLess "), 1, 1, hDevice1); m_NavViewTree.SetItemData(hPort3, MENU_PORT|2); m_NavViewTree.SetBkColor(COLORREF(0xf0f0f0f0)); HTREEITEM hDevice2 = m_NavViewTree.InsertItem(_T("KL10-E16D-N1 1/1"), 5, 5, hRoot1); m_NavViewTree.SetItemData(hDevice2, MENU_DEVICE | 1); // */ } void CNavView::OnContextMenu(CWnd* pWnd, CPoint point) { CTreeCtrl* pWndTree = (CTreeCtrl*)&m_NavViewTree; ASSERT_VALID(pWndTree); if (pWnd != pWndTree) { CDockablePane::OnContextMenu(pWnd, point); return; } HTREEITEM hTreeItem = nullptr; if (point != CPoint(-1, -1)) { // 选择已单击的项: CPoint ptTree = point; pWndTree->ScreenToClient(&ptTree); UINT flags = 0; hTreeItem = pWndTree->HitTest(ptTree, &flags); if (hTreeItem != nullptr) { pWndTree->SelectItem(hTreeItem); } } pWndTree->SetFocus(); DWORD_PTR ItemData = 0; if (hTreeItem != nullptr) { ItemData = m_NavViewTree.GetItemData(hTreeItem); } CMenu menu; // menu.LoadMenu(IDR_POPUP_SORT); menu.LoadMenu(IDR_POPUP_NAV); CMenu* pSubMenu; HTREEITEM hRoot1 = m_NavViewTree.GetRootItem(); HTREEITEM hDevice1 = m_NavViewTree.GetNextItem(hRoot1, TVGN_CHILD); if (hTreeItem == hRoot1) { pSubMenu = menu.GetSubMenu(1); // 连接菜单 } else if (hTreeItem == hDevice1 ){ pSubMenu = menu.GetSubMenu(2); //设备菜单 } else if (ItemData & MENU_PROPERTY){ pSubMenu = menu.GetSubMenu(3); //属性菜单 } else if (ItemData & MENU_PORT) { pSubMenu = menu.GetSubMenu(4); //端口菜单 } else if (ItemData & MENU_REMOTE_DEVICE) { pSubMenu = menu.GetSubMenu(5); //远程设备菜单 } else { pSubMenu = menu.GetSubMenu(0); //默认菜单 } pSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this); /* if (AfxGetMainWnd()->IsKindOf(RUNTIME_CLASS(CMDIFrameWndEx))) { CMFCPopupMenu* pPopupMenu = new CMFCPopupMenu; if (!pPopupMenu->Create(this, point.x, point.y, (HMENU)pSubMenu->m_hMenu, FALSE, TRUE)) return; ((CMDIFrameWndEx*)AfxGetMainWnd())->OnShowPopupMenu(pPopupMenu); UpdateDialogControls(this, FALSE); } */ menu.DestroyMenu(); } void CNavView::OnUpdateSort(CCmdUI* pCmdUI) { pCmdUI->SetCheck(pCmdUI->m_nID == m_nCurrSort); } void CNavView::OnClassAddMemberFunction() { AfxMessageBox(_T("添加成员函数...")); } void CNavView::OnClassAddMemberVariable() { // TODO: 在此处添加命令处理程序代码 } void CNavView::OnClassDefinition() { // TODO: 在此处添加命令处理程序代码 } void CNavView::OnClassProperties() { // TODO: 在此处添加命令处理程序代码 } void CNavView::OnNewFolder() { AfxMessageBox(_T("新建文件夹...")); } int CNavView::GetDocCount() { // TODO: 在此处添加实现代码. /* int nCount1 = 0; POSITION Templatepos = AfxGetApp()->m_pDocManager->GetFirstDocTemplatePosition(); while (Templatepos != NULL) { CDocTemplate* pDocTemplate = AfxGetApp()->m_pDocManager->GetNextDocTemplate(Templatepos); POSITION pos = pDocTemplate->GetFirstDocPosition(); while (pos != NULL) { CDocument * pDoc = pDocTemplate->GetNextDoc(pos); nCount1++; //if (pDoc->IsModified()) // 只保存被修改的文档 //{ // pDoc->DoFileSave(); //} } } return nCount1; */ return 0; } int CNavView::UpdateDisplay() { // TODO: 在此处添加实现代码. // m_NavViewTree.DeleteAllItems(); // FillDeviceView(); nDisplayCount++; KLink1* pMyKLink1 = &theApp.MyKLink1; CString s1; HTREEITEM hRoot1 = m_NavViewTree.GetRootItem(); if (theApp.MyKLink1.m_bOpened) { s1.Format(_T("连接1 ")); s1.AppendFormat(_T("COM%d %d %s "), pMyKLink1->MySerPort1.m_nPort, pMyKLink1->MySerPort1.m_nBaudRate, pMyKLink1->MySerPort1.m_Settings); s1.AppendFormat(_T("S %d R %d D %d"), theApp.MyKLink1.m_nTotalSendCount,theApp.MyKLink1.m_nTotalRecvCount, nDisplayCount); ///* if ( (nDisplayCount & 0x1f) == 0) { bPortRegsUpdated[0] = 0; bPortRegsUpdated[1] = 0; bPortRegsUpdated[2] = 0; } // */ } else { s1.Format(_T("连接1 No Connect")); } m_NavViewTree.SetItemText(hRoot1, s1); HTREEITEM hDevice1 = m_NavViewTree.GetChildItem(hRoot1); // m_NavViewTree.DeleteItem(hDevice1); if (!theApp.MyKLink1.m_bOpened) { s1.Format(_T("No Connect")); } else { s1.Format(_T("Dev %04X %s Run"), m_DeviceInfo.nDeviceTypeVer,DeviceTypeToStr(m_DeviceInfo.nDeviceTypeVer)); } m_NavViewTree.SetItemText(hDevice1, s1); if (!theApp.MyKLink1.m_bOpened) { HTREEITEM hItem11 = m_NavViewTree.GetNextItem(hDevice1, TVGN_CHILD); s1.Format(_T("设备参数")); m_NavViewTree.SetItemText(hItem11, s1); HTREEITEM hItem12 = m_NavViewTree.GetNextItem(hItem11, TVGN_NEXT); m_NavViewTree.SetItemText(hItem12, _T("工作状态")); HTREEITEM hItem13 = m_NavViewTree.GetNextItem(hItem12, TVGN_NEXT); if (hItem13 != nullptr) { bPortRegsUpdated[0] = 0; bPortRegsUpdated[1] = 0; bPortRegsUpdated[2] = 0; m_NavViewTree.DeleteItem(hItem13); HTREEITEM hItem13 = m_NavViewTree.GetNextItem(hItem12, TVGN_NEXT); } }else { HTREEITEM hItem11 = m_NavViewTree.GetNextItem(hDevice1, TVGN_CHILD); if (hItem11 == nullptr) { hItem11 = m_NavViewTree.InsertItem(_T("设备参数 "), 3, 3, hDevice1); m_NavViewTree.Expand(hDevice1, TVE_EXPAND); } s1.Format(_T("设备参数 V%d.%02d KLink %04X KBus %04X In %d Out %d"), m_infoblock.nProgVerMajor, m_infoblock.nProgVerMinor, m_infoblock.nKLinkVer, m_infoblock.nKBusVer, m_DeviceInfo.InBitCount, m_DeviceInfo.OutBitCount); m_NavViewTree.SetItemText(hItem11, s1); HTREEITEM hItem12 = m_NavViewTree.GetNextItem(hItem11, TVGN_NEXT); if (hItem12 == nullptr) { hItem12 = m_NavViewTree.InsertItem(_T("工作状态 "), 3, 3, hDevice1); m_NavViewTree.Expand(hDevice1, TVE_EXPAND); } m_NavViewTree.SetItemText(hItem12, _T("工作状态,系统参数,IO状态 hItem12")); HTREEITEM hPort1 = hItem12; USHORT nReaded = 0; USHORT DataBuf1[256]; for (int i = 0; i < m_DeviceInfo.nPortCount; i++) { hPort1 = m_NavViewTree.GetNextItem(hPort1, TVGN_NEXT); if (hPort1 == nullptr) { hPort1 = m_NavViewTree.InsertItem(_T("端口1 "), 1, 1, hDevice1); m_NavViewTree.Expand(hDevice1, TVE_EXPAND); } if (theApp.MyKLink1.m_bOpened && !bPortRegsUpdated[i]) { int res1 = theApp.MyKLink1.GetPortInfo(1, i, sizeof(stPortDef), &nReaded, DataBuf1); if (res1 == KLink1::KL_OK) { pstPortDef thepPortReg = (pstPortDef)DataBuf1; PortDefs[i] = *thepPortReg; s1.Format(_T("端口%d (%02X)%s (%02X)%s 能力 %04X M %d 站号 %d/%d H %d"), i + 1, thepPortReg->nPortHardType, PortHardTypeToStr(thepPortReg->nPortHardType), thepPortReg->nPortUseType, PortUseTypeToStr(thepPortReg->nPortUseType), thepPortReg->ability, thepPortReg->bMaster, thepPortReg->StationId, thepPortReg->nMaxStations, thepPortReg->nHealth); m_NavViewTree.SetItemText(hPort1, s1); m_NavViewTree.SetItemData(hPort1, MENU_PORT | i); bPortRegsUpdated[i] = 1; break; } else { s1.Format(_T("GetPortInfo %d = %d len %d %s"), i, res1, &nReaded,theApp.MyKLink1.m_resultStr); DbgLog(s1); s1.Format(_T("端口%d --------- ???? "), i + 1); m_NavViewTree.SetItemText(hPort1, s1); m_NavViewTree.SetItemData(hPort1, MENU_PORT | i); } } if (bPortRegsUpdated[i] && (PortDefs[i].nPortUseType == PortUse_KBus || PortDefs[i].nPortUseType == PortUse_KRF)) { HTREEITEM hChild1 = m_NavViewTree.GetNextItem(hPort1, TVGN_CHILD); int nChildCount = PortDefs[i].nMaxStations; for (int j = 1; j <= nChildCount; j++) { if (j != 1) hChild1 = m_NavViewTree.GetNextItem(hChild1, TVGN_NEXT); if (hChild1 == nullptr) { s1.Format(_T("子机%d "), j); hChild1 = m_NavViewTree.InsertItem(s1, 5, 5, hPort1); m_NavViewTree.SetItemData(hChild1, MENU_REMOTE_DEVICE | (j)); m_NavViewTree.Expand(hPort1, TVE_EXPAND); } //if (j < LastUpdateIndex) continue; if (bPortDeviceUpdated[j]) continue; int res2 = theApp.MyKLink1.GetPortChildInfo(1, i, j, sizeof(stDeviceInfo), &nReaded, DataBuf1); //sizeof(stDeviceInfo) if (res2 == KLink1::KL_OK) { s1.Format(_T("GetChildInfo %d %d = %d B\r\n"), i, j, nReaded); for (int k = 0; k < nReaded; k++) { s1.AppendFormat(_T("%02X "), ((unsigned char*)DataBuf1)[k]); } // SysLog(s1); stDeviceInfo* ptheInfo = (stDeviceInfo*)DataBuf1; PortDeviceInfos[j] = *ptheInfo; s1.Format(_T("子%d (%04X)%s V%d.%02d %d/%d "), j, ptheInfo->DeviceType, DeviceTypeToStr(ptheInfo->DeviceType), ptheInfo->DeviceVerMajor, ptheInfo->DeviceVerMinor, ptheInfo->InBitCount, ptheInfo->OutBitCount); m_NavViewTree.SetItemText(hChild1, s1); m_NavViewTree.SetItemData(hChild1, MENU_REMOTE_DEVICE | (j)); } bPortDeviceUpdated[j] = 1; break; } LastUpdateIndex[i]++; if (LastUpdateIndex[i] > nChildCount) { LastUpdateIndex[i] = 0; } s1.Format(_T("i %d LastUpdated %d nChildCount %d nMaxStations %d \r\n"),i, LastUpdateIndex[i],nChildCount, PortDefs[i].nMaxStations); // SysLog(s1); } ///* if (bPortRegsUpdated[i] && PortDefs[i].nPortUseType == PortUse_KBus ) { HTREEITEM hChild1 = m_NavViewTree.GetNextItem(hPort1, TVGN_CHILD); int nChildCount = PortDefs[i].nMaxStations; int j = LastUpdateIndex[i] + 1; for (int j = 1; j <= nChildCount; j++) { if (j != 1) hChild1 = m_NavViewTree.GetNextItem(hChild1, TVGN_NEXT); if (hChild1 == nullptr) { s1.Format(_T("子机%d "), j); hChild1 = m_NavViewTree.InsertItem(s1, 5, 5, hPort1); m_NavViewTree.Expand(hPort1, TVE_EXPAND); } if (j < LastUpdateIndex[i]) continue; //if (bPortDeviceUpdated[j]) continue; int res2 = theApp.MyKLink1.GetPortChnInfo(1, i, j, sizeof(stChnStat), &nReaded, DataBuf1); //sizeof(stDeviceInfo) if (res2 == KLink1::KL_OK) { s1.Format(_T("GetChnInfo %d %d = %d B\r\n"), i, j, nReaded); for (int k = 0; k < nReaded; k++) { s1.AppendFormat(_T("%02X "), ((unsigned char*)DataBuf1)[k]); } // SysLog(s1); stDeviceInfo* ptheInfo = &PortDeviceInfos[j]; stChnStat* ptheChnInfo = (stChnStat*)DataBuf1; s1.Format(_T("子%d (%04X)%s V%d.%02d %d/%d 丢包率(%.2f%%) 收发(%d/%d) "), j, ptheInfo->DeviceType, DeviceTypeToStr(ptheInfo->DeviceType), ptheInfo->DeviceVerMajor, ptheInfo->DeviceVerMinor, ptheInfo->InBitCount, ptheInfo->OutBitCount , ptheChnInfo->LostPackets * 100.0 / ptheChnInfo->SendPackets, ptheChnInfo->RecvPackets, ptheChnInfo->SendPackets); m_NavViewTree.SetItemText(hChild1, s1); } // bPortDeviceUpdated[j] = 1; break; } } // */ ///* if (bPortRegsUpdated[i] && PortDefs[i].nPortUseType == PortUse_KRF) { HTREEITEM hChild1 = m_NavViewTree.GetNextItem(hPort1, TVGN_CHILD); int nChildCount = PortDefs[i].nMaxStations; int j = LastUpdateIndex[i] + 1; for (int j = 1; j <= nChildCount; j++) { if (j != 1) hChild1 = m_NavViewTree.GetNextItem(hChild1, TVGN_NEXT); if (hChild1 == nullptr) { s1.Format(_T("子机%d "), j); hChild1 = m_NavViewTree.InsertItem(s1, 5, 5, hPort1); m_NavViewTree.Expand(hPort1, TVE_EXPAND); } if (j < LastUpdateIndex[i]) continue; //if (bPortDeviceUpdated[j]) continue; int res2 = theApp.MyKLink1.GetPortChnInfo(1, i, j, sizeof(stKwChnStat), &nReaded, DataBuf1); //sizeof(stDeviceInfo) if (res2 == KLink1::KL_OK) { s1.Format(_T("GetChnInfo %d %d = %d B\r\n"), i, j, nReaded); for (int k = 0; k < nReaded; k++) { s1.AppendFormat(_T("%02X "), ((unsigned char*)DataBuf1)[k]); } // SysLog(s1); stDeviceInfo* ptheInfo = &PortDeviceInfos[j]; stKwChnStat* ptheChnInfo = (stKwChnStat*)DataBuf1; s1.Format(_T("子%d (%04X)%s V%d.%02d %d/%d 丢包率(%.2f%%) 收发(%d/%d) "), j, ptheInfo->DeviceType, DeviceTypeToStr(ptheInfo->DeviceType), ptheInfo->DeviceVerMajor, ptheInfo->DeviceVerMinor, ptheInfo->InBitCount, ptheInfo->OutBitCount , ptheChnInfo->LostPackets * 100.0 / ptheChnInfo->sentCount, ptheChnInfo->recvCount, ptheChnInfo->sentCount); m_NavViewTree.SetItemText(hChild1, s1); } else { s1.Format(_T("GetChnInfo %d %d = %d B\r\n"), i, j, nReaded); SysLog(s1); } // bPortDeviceUpdated[j] = 1; break; } } // */ } } HTREEITEM hDevice2 = m_NavViewTree.GetNextItem(hDevice1, TVGN_NEXT); m_NavViewTree.SetItemText(hDevice2, _T("hDevice2")); // HTREEITEM hDevice1 = m_NavViewTree.InsertItem(s1, 5, 5, hRoot1); // m_NavViewTree.SetItemData(hDevice1, 10); // m_NavViewTree.SetItemText(hDevice1, s1); /* AfxGetApp()->m_pDocManager->GetFirstDocTemplatePosition CMDIFrameWnd* pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd; CMDIChildWnd* pChild = (CMDIChildWnd*)pFrame->GetActiveFrame(); CView* pV = (CView*)pChild->GetActiveView(); CMTerm1Doc* pDoc = (CMTerm1Doc*)(pV->GetDocument()); */ return 0; } void CNavView::OnNMClkNavTree1(NMHDR* pNMHDR, LRESULT* pResult) { LPNMTREEVIEW pNMTreeView = reinterpret_cast(pNMHDR); // HTREEITEM hTreeItem = pNMTreeView->itemNew.hItem; // HTREEITEM hTreeItem = m_NavViewTree.GetSelectedItem(); //以GetItemText()函数为例: // CString s2 = m_NavViewTree.GetItemText(hTreeItem); //AfxMessageBox(_T("You have selected ") + s1); // CString s1; // DWORD_PTR dw = m_NavViewTree.GetItemData(hTreeItem); // s1.Format(_T("Form Clk action %d, id %d oldid %d data %d x %d y %d text %s "), // pNMTreeView->action, hTreeItem, pNMTreeView->itemOld.hItem, dw, pNMTreeView->ptDrag.x, pNMTreeView->ptDrag.y, s2); // SysLog(s1); *pResult = 0; } void CNavView::OnNMDblclkNavTree1(NMHDR* pNMHDR, LRESULT* pResult) { LPNMTREEVIEW pNMTreeView = reinterpret_cast(pNMHDR); HTREEITEM hTreeItem = m_NavViewTree.GetSelectedItem(); //以GetItemText()函数为例: CString s2 = m_NavViewTree.GetItemText(hTreeItem); //AfxMessageBox(_T("You have selected ") + s1); CString s1; DWORD_PTR dw = m_NavViewTree.GetItemData(hTreeItem); s1.Format(_T("Form dbclk action %d, id %d oldid %d data %d x %d y %d text %s "), pNMTreeView->action, hTreeItem, pNMTreeView->itemOld.hItem, dw, pNMTreeView->ptDrag.x, pNMTreeView->ptDrag.y, s2); SysLog(s1); *pResult = 0; } void CNavView::OnMenuNavSetConn() { // TODO: 在此添加命令处理程序代码 theApp.MyKLink1.SetCommParam(); } void CNavView::OnSetComm() { // TODO: 在此添加命令处理程序代码 theApp.MyKLink1.SetCommParam(); } void CNavView::OnConnect() { // TODO: 在此添加命令处理程序代码 CString s1; unsigned short buf1[64]; unsigned short len1; if (!theApp.MyKLink1.m_bCommParamSet) { theApp.MyKLink1.m_nPort = 6; } int res = theApp.MyKLink1.Connect(); if (res == KLink1::KL_OK) { Sleep(100); theApp.MyKLink1.MySerPort1.ClearBuf(); int res2 = theApp.MyKLink1.GetInfo(1, &len1, buf1); s1.Format(_T("GetInfo = %d %d \r\n"), res2, len1); if (res2 == KLink1::KL_OK && len1 > 0) { pKMInfoBlock pinfob = (pKMInfoBlock)buf1; m_infoblock = *pinfob; for (int i = 0; i < 8; i++) { bPortRegsUpdated[i] = 0; } for (int i = 0; i < 16; i++) { bPortDeviceUpdated[i] = 0; } m_DeviceInfo.nDeviceTypeVer = pinfob->nDeviceTypeVer; m_DeviceInfo.InBitCount = pinfob->nDInput; m_DeviceInfo.OutBitCount = pinfob->nDOutput; m_DeviceInfo.nPortCount = pinfob->nPorts; HTREEITEM hRoot1 = m_NavViewTree.GetRootItem(); if (theApp.MyKLink1.m_bOpened) { s1.Format(_T("连接1 ")); s1.AppendFormat(_T("COM%d %d %s "), theApp.MyKLink1.MySerPort1.m_nPort, theApp.MyKLink1.MySerPort1.m_nBaudRate, theApp.MyKLink1.MySerPort1.m_Settings); s1.AppendFormat(_T("%d %d %d \r\n"), theApp.MyKLink1.m_nTotalSendCount, theApp.MyKLink1.m_nTotalRecvCount, nDisplayCount); } else { s1.Format(_T("连接1 No Connect")); } m_NavViewTree.SetItemText(hRoot1, s1); HTREEITEM hDevice1 = m_NavViewTree.GetChildItem(hRoot1); m_NavViewTree.Expand(hDevice1, TVE_EXPAND); 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); s1.AppendFormat(_T(" nKLinkVer %04X \t"), pinfob->nKLinkVer); s1.AppendFormat(_T(" nKBusVer %04X \r\n"), pinfob->nKBusVer); s1.AppendFormat(_T(" nCapacity1 %d k \t"), pinfob->nCapacity1); s1.AppendFormat(_T(" nCapacity2 %d k\r\n"), pinfob->nCapacity2); s1.AppendFormat(_T(" nDInput %d \t"), pinfob->nDInput); s1.AppendFormat(_T(" nDOutput %d \r\n"), pinfob->nDOutput); s1.AppendFormat(_T(" nAInput %d \t"), pinfob->nAInput); s1.AppendFormat(_T(" nAOutput %d \r\n"), pinfob->nAOutput); s1.AppendFormat(_T(" nHInput %d \t"), pinfob->nHInput); s1.AppendFormat(_T(" nHOutput %d \r\n"), pinfob->nHOutput); s1.AppendFormat(_T(" nExt1 %d \t"), pinfob->nExt1); s1.AppendFormat(_T(" nExt2 %d \r\n"), pinfob->nExt2); s1.AppendFormat(_T(" nLogSize %d \r\n"), pinfob->nLogSize); s1.AppendFormat(_T(" nPorts %d \r\n"), pinfob->nPorts); s1.AppendFormat(_T(" nManSize %d \r\n"), pinfob->nManSize); s1.AppendFormat(_T(" nAbility %d \r\n"), pinfob->nAbility); s1.AppendFormat(_T(" nSwitchBits %d \r\n"), pinfob->nSwitchBits); } SysLog(s1); int len0 = sizeof(stKMSysCfg); UCHAR nType = 0; res2 = theApp.MyKLink1.ReadSysCfgData(1, nType, 0, len0, &len1, buf1); s1.Format(_T("GetSysCfg %d = %d %d \r\n"), len0, res2, len1); if (res2 == KLink1::KL_OK && len1 > 0) { for (int i = 0; i < len1 / 2; i++) { s1.AppendFormat(_T("%04X "), buf1[i]); } s1 += _T("\r\n"); pstKMSysCfg pSysCfg = (pstKMSysCfg)buf1; s1.AppendFormat(_T("Version %04X \r\n"), pSysCfg->Version); s1.AppendFormat(_T("WorkMode %04X \r\n"), pSysCfg->workmode); s1.AppendFormat(_T("SwitchFunc %04X \r\n"), pSysCfg->SwitchFunc); s1.AppendFormat(_T("CfgBlockCount %d \r\n"), pSysCfg->nCfgBlockCount); for (int j = 0; j < 2; j++) { s1.AppendFormat(_T("COM %d \r\n"), j + 1); s1.AppendFormat(_T("WorkMode %d \r\n"), pSysCfg->PortParams[j].WorkMode); s1.AppendFormat(_T("Station %d \r\n"), pSysCfg->PortParams[j].Station); s1.AppendFormat(_T("BaudRate %d \r\n"), pSysCfg->PortParams[j].BaudRate*100); s1.AppendFormat(_T("ByteSize %d Parity %d StopBit %d \r\n"), pSysCfg->PortParams[j].ByteSize, pSysCfg->PortParams[j].Parity, pSysCfg->PortParams[j].StopBits); s1.AppendFormat(_T("EndType %d \r\n"), pSysCfg->PortParams[j].EndType); s1.AppendFormat(_T("EofChar %d \r\n"), pSysCfg->PortParams[j].EofChar); s1.AppendFormat(_T("SofChar %d \r\n"), pSysCfg->PortParams[j].SofChar); s1.AppendFormat(_T("EndTime %d \r\n"), pSysCfg->PortParams[j].EndTime); s1.AppendFormat(_T("RecvAddr %d \r\n"), pSysCfg->PortParams[j].RecvAddr); s1.AppendFormat(_T("RecvSize %d \r\n"), pSysCfg->PortParams[j].RecvSize); } /* s1.AppendFormat(_T("InputParam \r\n")); for (int j = 0; j < 16; j++) { s1.AppendFormat(_T("%2d Filter0 %d Filter1 %d \r\n"),j, pSysCfg->InputParams[j].Filter0, pSysCfg->InputParams[j].Filter1); } s1.AppendFormat(_T("OutputParam \r\n")); for (int j = 0; j < 16; j++) { s1.AppendFormat(_T("%2d Hold1 %d Hold2 %d \r\n"),j, pSysCfg->OutputParams[j].Hold1, pSysCfg->OutputParams[j].Hold2); } // */ s1.AppendFormat(_T("OutMapping \r\n")); for (int j = 0; j < 8; j++) { // s1.AppendFormat(_T("%d Map %04X \r\n"),j, pSysCfg->OutMappings[j]); s1.AppendFormat(_T("%d Map %04X type %X Addr %02X bitPos %X\r\n"),j, pSysCfg->OutMappings[j].value,pSysCfg->OutMappings[j].type,pSysCfg->OutMappings[j].byteAddr,pSysCfg->OutMappings[j].bitPos); } s1.AppendFormat(_T("CfgBlockCount %d\r\n"), pSysCfg->nCfgBlockCount); for (int j = 0; j < pSysCfg->nCfgBlockCount; j++) { // s1.AppendFormat(_T("%d Map %04X \r\n"),j, pSysCfg->OutMappings[j]); s1.AppendFormat(_T("%d type %X Size %d \r\n"), j, pSysCfg->CfgBlockInfos[j].nBlockType, pSysCfg->CfgBlockInfos[j].nBlockSize); } } SysLog(s1); } } static const uint16_t crctalbeabs[] = { 0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401, 0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400 }; static 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 CNavView::OnDisConnect() { // TODO: 在此添加命令处理程序代码 theApp.MyKLink1.DisConnect(); } void CNavView::OnMenuNavConnect() { // TODO: 在此添加命令处理程序代码 OnConnect(); } void CNavView::OnMenuNavDisconnect() { // TODO: 在此添加命令处理程序代码 OnDisConnect(); } void CNavView::OnMenuNvDeviceReadInfo() { // TODO: 在此添加命令处理程序代码 CString s1; s1.Format(_T("读取设备 信息")); SysLog(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(); } void CNavView::OnDeviceConfig() { // TODO: 在此添加命令处理程序代码 CString s1; s1.Format(_T("设置参数")); SysLog(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(); } void CNavView::OnLedBlink() { // TODO: 在此添加命令处理程序代码 CString s1; theApp.MyKLink1.BlinkLED(1, 5); } void CNavView::OnResetDevice() { // TODO: 在此添加命令处理程序代码 CString s1; int r5 = theApp.MyKLink1.ResetDevice(1, 0); s1.Format(_T("ResetDevice = %d"), r5); SysLog(s1); for (int i = 0; i < 8; i++) { bPortRegsUpdated[i] = 0; } for (int i = 0; i < 16; i++) { bPortDeviceUpdated[i] = 0; } } void CNavView::OnCommtest() { // TODO: 在此添加命令处理程序代码 CString s1; /* CView* pView; pView = FindView(RUNTIME_CLASS(CMTerm1CtrlView)); if (pView != NULL) { ((CMDIFrameWndEx*)AfxGetMainWnd())->MDIActivate(pView->GetParent()); // pView->SetActiveWindow(); } else { s1 = GetTitle(); //AfxMessageBox(s1); ASSERT_VALID(theApp.m_pCtrlViewTemplate); CFrameWnd* pFrame = theApp.m_pCtrlViewTemplate->CreateNewFrame(this, NULL); ASSERT_KINDOF(CFrameWnd, pFrame); theApp.m_pCtrlViewTemplate->InitialUpdateFrame(pFrame, this); } */ int a = 0; #pragma omp parrelle for for (int i = 0; i < 1000; i++) { a += i; } s1.Format(_T("a = %d "), a); DbgLog(s1); } void CNavView::OnMenuNvUpdateFirmware() { // TODO: 在此添加命令处理程序代码 CString s1; s1.Format(_T("更新设备 固件")); SysLog(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 Appbuf[131072]; unsigned char firmwarebuf[262144]; int nBtldrInfoOffset = 256; int AppOffset = 4096; int nAppInfoOffset = 256; int btldrLength = 0; int AppLength = 0; int FirmwareSize = 0; bool btldrfileLoaded = false; bool appfileLoaded = false; bool combinefileSaved = false; unsigned char filebuf[65536]; unsigned int blocksize = 64; 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 fileLen1 = (int)file1.GetLength(); file1.Read(filebuf, fileLen1); file1.Close(); unsigned int nBlkType = 0; unsigned int nBlkVer = 0; s1.Empty(); pAppInfoBlock pAppinfo = (pAppInfoBlock)(filebuf + nAppInfoOffset); s1.Format(_T("文件信息 \r\n")); // SetDlgItemText(IDC_STATIC_APP_INFO, s1); if (pAppinfo->Hdr.nBlkSign == 0xaa55) { s1.Empty(); s1.Format(_T("文件信息 ")); // s1.AppendFormat(_T("启动文件 %s \r\n"), sFilePathName); s1.AppendFormat(_T("文件大小: %d \r\n"), fileLen1); s1.AppendFormat(_T("信息块位置: 0x%04X 信息块大小: %d \r\n"), nAppInfoOffset, pAppinfo->Hdr.nBlkSize); s1.AppendFormat(_T("信息块类型: 0x%04X "), pAppinfo->Hdr.nBlkTypeVer); nBlkType = (pAppinfo->Hdr.nBlkTypeVer) >> 8; nBlkVer = pAppinfo->Hdr.nBlkTypeVer & 0xff; if (nBlkType == 0x01) { s1.AppendFormat(_T(" (BootLoader 类型1 版本 %d) "), nBlkVer); } else if (nBlkType == 0x02) { s1.AppendFormat(_T(" (BootLoader 类型2 版本 %d) "), nBlkVer); } else if (nBlkType == 0x03) { s1.AppendFormat(_T(" (App类型1 版本 %d) "), nBlkVer); } else if (nBlkType == 0x04) { s1.AppendFormat(_T(" (App类型2 版本 %d) "), nBlkVer); } else { s1.AppendFormat(_T(" (未知类型) ")); } s1.AppendFormat(_T("\r\n")); s1.AppendFormat(_T("应用区版本: 0x%04X 对应模块类型: 0x%04X\r\n"), pAppinfo->nAppVer, pAppinfo->nAppDevice); s1.AppendFormat(_T("应用区大小: 0x%04X 实际代码大小: 0x%04X \r\n"), pAppinfo->nAppSize, pAppinfo->nAppDataSize); s1.AppendFormat(_T("应用区地址: 0x%08X (偏移 %d )\r\n"), pAppinfo->nAppStartAddr, pAppinfo->nAppStartAddr - 0x08000000); s1.AppendFormat(_T("信息暂存区: 0x%08X \r\n应用暂存区: 0x%08X \r\n"), pAppinfo->nAppStartOffset, pAppinfo->nApp); // SetDlgItemText(IDC_STATIC_APP_INFO, s1); if (nBlkType == 0x03 || nBlkType == 0x04) { memmove(Appbuf, filebuf , fileLen1); appfileLoaded = true; AppLength = fileLen1; } else { if ((nBlkType == 0x01 || nBlkType == 0x02) && fileLen1 >= AppOffset + nAppInfoOffset + sizeof(stAppInfoBlock)) { pAppInfoBlock pAppinfo2 = (pAppInfoBlock)(filebuf + AppOffset + nAppInfoOffset); if ((pAppinfo2->Hdr.nBlkSign == 0xaa55) && pAppinfo2->Hdr.nBlkTypeVer == 0x0301) { s1.Format(_T("文件是bootloader和App合成的文件 , 是否从中提取固件并继续")); int r3 = AfxMessageBox(s1, MB_YESNO); if (r3 == IDYES) { memmove(Appbuf, filebuf + AppOffset, fileLen1 - AppOffset); appfileLoaded = true; AppLength = fileLen1 - AppOffset; } } else { s1.Format(_T("文件 不是App文件 , 是否继续 ")); int r3 = AfxMessageBox(s1, MB_YESNO); if (r3 == IDYES) { memmove(Appbuf, filebuf , fileLen1 ); appfileLoaded = true; AppLength = fileLen1; } } } else { s1.Format(_T("文件 不是App文件 , 是否继续 ")); int r3 = AfxMessageBox(s1, MB_YESNO); if (r3 == IDYES) { memmove(Appbuf, filebuf , fileLen1 ); appfileLoaded = true; AppLength = fileLen1; } } } // s1.Format(_T(" BootLoader启动文件 版本 %04X 硬件类型 %04X 占用空间 %04X, %d Bytes\r\n 是否继续"), pinfob->nBtldrVer, pinfob->nBtldrDevice, pinfob->nBtldrSize, fileLen1); } else { s1.Format(_T("文件中未找到信息块, 文件大小 %d Bytes\r\n 是否继续"), fileLen1); int r3 = AfxMessageBox(s1, MB_YESNO); if (r3 == IDYES) { memmove(Appbuf, filebuf , fileLen1 ); appfileLoaded = true; AppLength = fileLen1; } } unsigned short crc2 = crc16tablefast(Appbuf, AppLength); unsigned char* buf2 = Appbuf + 0x1000; ; //s1.Format(_T("GetInfo From File = %d %d \r\n"), j, fileLen1); pKMInfoBlock pinfof = (pKMInfoBlock)buf2; s1.Format(_T(" 目标模块 类型 %04X 版本 %d.%02d, \r\n 固件文件 类型 %04X 版本 %d.%02d, %d Bytes\r\n 是否继续"), pinfob->nDeviceTypeVer, pinfob->nProgVerMajor,pinfob->nProgVerMinor, pinfof->nDeviceTypeVer, pinfof->nProgVerMajor,pinfof->nProgVerMinor, fileLen1); int r3 = AfxMessageBox(s1, MB_YESNO); if (r3 == IDYES) { int bError = 0; for (int i = 0; i < AppLength; i += blocksize) { int sendsize = AppLength - 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, Appbuf + 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 = AppLength; 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); } theApp.MyKLink1.DisConnect();; Sleep(1000); OnMenuNavConnect(); } } } } void CNavView::OnRemoteBlinkLedAll() { // TODO: 在此添加命令处理程序代码 CString s1; int n = m_NavViewTree.GetSelectedCount(); HTREEITEM hItem1 = nullptr; DWORD_PTR itemData = 0; if (n == 1) { hItem1 = m_NavViewTree.GetSelectedItem(); itemData = m_NavViewTree.GetItemData(hItem1); } s1.Format(_T("Selected Count %d Data %08X"), n, itemData); SysLog(s1); int nDst = 1; int nPort = 1; int nTime = 5; if (itemData & MENU_PORT) { int nChildId = 0xff; theApp.MyKLink1.RunRemoteReq(nDst, nPort, nChildId, ReqBlinkLED, nTime, 0, 0, 0); s1.Format(_T("Remote BlinkLED Port %d Child %d Timer %d"), nPort, nChildId, nTime); SysLog(s1); } } void CNavView::OnPortReset() { // TODO: 在此添加命令处理程序代码 } void CNavView::OnPortResetAllChild() { // TODO: 在此添加命令处理程序代码 CString s1; int n = m_NavViewTree.GetSelectedCount(); HTREEITEM hItem1 = nullptr; DWORD_PTR itemData = 0; if (n == 1) { hItem1 = m_NavViewTree.GetSelectedItem(); itemData = m_NavViewTree.GetItemData(hItem1); } s1.Format(_T("Selected Count %d Data %08X"), n, itemData); SysLog(s1); int nDst = 1; int nPort = 1; int nTime = 5; if (itemData & MENU_PORT) { int nChildId = 0xff; theApp.MyKLink1.RunRemoteReq(nDst, nPort, nChildId, ReqReset, nTime, 0, 0, 0); s1.Format(_T("Remote Reset Port %d Child %d Timer %d"), nPort, nChildId, nTime); SysLog(s1); } } void CNavView::OnRemoteRefreshData() { // TODO: 在此添加命令处理程序代码 CString s1; int n = m_NavViewTree.GetSelectedCount(); HTREEITEM hItem1 = nullptr; DWORD_PTR itemData =0; if (n == 1) { hItem1 = m_NavViewTree.GetSelectedItem(); itemData = m_NavViewTree.GetItemData(hItem1); } s1.Format(_T("Selected Count %d Data %08X"), n, itemData); SysLog(s1); } void CNavView::OnRemoteBlinkLED() { // TODO: 在此添加命令处理程序代码 CString s1; int n = m_NavViewTree.GetSelectedCount(); HTREEITEM hItem1 = nullptr; DWORD_PTR itemData = 0; if (n == 1) { hItem1 = m_NavViewTree.GetSelectedItem(); itemData = m_NavViewTree.GetItemData(hItem1); } s1.Format(_T("Selected Count %d Data %08X"), n, itemData); SysLog(s1); int nDst = 1; int nPort = 1; int nTime = 5; if (itemData & MENU_REMOTE_DEVICE) { int nChildId = itemData & 0xff; theApp.MyKLink1.RunRemoteReq(nDst, nPort, nChildId, ReqBlinkLED, nTime, 0, 0, 0); s1.Format(_T("Remote BlinkLED Port %d Child %d Timer %d"), nPort, nChildId,nTime); SysLog(s1); } } void CNavView::OnRemoteDeviceReset() { // TODO: 在此添加命令处理程序代码 CString s1; int n = m_NavViewTree.GetSelectedCount(); HTREEITEM hItem1 = nullptr; DWORD_PTR itemData = 0; if (n == 1) { hItem1 = m_NavViewTree.GetSelectedItem(); itemData = m_NavViewTree.GetItemData(hItem1); } s1.Format(_T("Selected Count %d Data %08X"), n, itemData); SysLog(s1); int nDst = 1; int nPort = 1; int nTime = 5; if (itemData & MENU_REMOTE_DEVICE) { int nChildId = itemData & 0xff; theApp.MyKLink1.RunRemoteReq(nDst, nPort, nChildId, ReqReset, nTime, 0, 0, 0); s1.Format(_T("Remote Reset Port %d Child %d Timer %d"), nPort, nChildId, nTime); SysLog(s1); } } void CNavView::OnRemoteUpdateFirmware() { // TODO: 在此添加命令处理程序代码 CString s1; int n = m_NavViewTree.GetSelectedCount(); HTREEITEM hItem1 = nullptr; DWORD_PTR itemData = 0; if (n == 1) { hItem1 = m_NavViewTree.GetSelectedItem(); itemData = m_NavViewTree.GetItemData(hItem1); } s1.Format(_T("Selected Count %d Data %08X"), n, itemData); SysLog(s1); int nDst = 1; int nPort = 1; int nChildId = 0; if (itemData & MENU_REMOTE_DEVICE) { nChildId = itemData & 0xff; } else { return; } // CString s1; s1.Format(_T("更新远程设备 固件")); SysLog(s1); USHORT len1 = 0; USHORT buf1[1024]; int r2 = 0; stDeviceInfo* ptheDeviceInfo =&PortDeviceInfos[nChildId]; int res2 = theApp.MyKLink1.GetPortChildInfo(1, nPort, nChildId , sizeof(stDeviceInfo), &len1, buf1); //sizeof(stDeviceInfo) if (res2 == KLink1::KL_OK) { s1.Format(_T("GetChildInfo %d %d = %d B\r\n"), nPort, nChildId , len1); // for (int k = 0; k < len1; k++) { // s1.AppendFormat(_T("%02X "), ((unsigned char*)buf1)[k]); // } SysLog(s1); stDeviceInfo* ptheInfo = (stDeviceInfo*)buf1; // PortDeviceInfos[nChildId] = *ptheInfo; s1.Format(_T("子%d (%04X)%s V%d.%02d %d/%d "), nChildId, ptheInfo->DeviceType, DeviceTypeToStr(ptheInfo->DeviceType), ptheInfo->DeviceVerMajor, ptheInfo->DeviceVerMinor, ptheInfo->InBitCount, ptheInfo->OutBitCount); SysLog(s1); } myLogger1.UpdateLogDisplay(); unsigned char Appbuf[131072]; unsigned char firmwarebuf[262144]; int nBtldrInfoOffset = 256; int AppOffset = 4096; int nAppInfoOffset = 256; int btldrLength = 0; int AppLength = 0; int FirmwareSize = 0; bool btldrfileLoaded = false; bool appfileLoaded = false; bool combinefileSaved = false; unsigned char filebuf[65536]; unsigned int blocksize = 32; stNewAppInfoBlock NewAppInfo; CFileDialog dialog1(true); INT_PTR r = dialog1.DoModal(); if (r != IDOK) { return; } CString sFilePathName = dialog1.GetPathName(); //OpenFile CFile file1; CFileException e; bool r3 = file1.Open(sFilePathName, CFile::modeRead | CFile::typeBinary, &e); s1.Format(_T("Open File %s = %d"), sFilePathName, r); DbgLog(s1); if (!r3) { return; } int fileLen2 = (int)file1.GetLength(); file1.Read(filebuf, fileLen2); file1.Close(); unsigned int nBlkType = 0; unsigned int nBlkVer = 0; s1.Empty(); pAppInfoBlock pAppinfo = (pAppInfoBlock)(filebuf + nAppInfoOffset); s1.Format(_T("文件信息 \r\n")); // SetDlgItemText(IDC_STATIC_APP_INFO, s1); if (pAppinfo->Hdr.nBlkSign == 0xaa55) { s1.Empty(); s1.Format(_T("文件信息 ")); // s1.AppendFormat(_T("启动文件 %s \r\n"), sFilePathName); s1.AppendFormat(_T("文件大小: %d \r\n"), fileLen2); s1.AppendFormat(_T("信息块位置: 0x%04X 信息块大小: %d \r\n"), nAppInfoOffset, pAppinfo->Hdr.nBlkSize); s1.AppendFormat(_T("信息块类型: 0x%04X "), pAppinfo->Hdr.nBlkTypeVer); nBlkType = (pAppinfo->Hdr.nBlkTypeVer) >> 8; nBlkVer = pAppinfo->Hdr.nBlkTypeVer & 0xff; if (nBlkType == 0x01) { s1.AppendFormat(_T(" (BootLoader 类型1 版本 %d) "), nBlkVer); } else if (nBlkType == 0x02) { s1.AppendFormat(_T(" (BootLoader 类型2 版本 %d) "), nBlkVer); } else if (nBlkType == 0x03) { s1.AppendFormat(_T(" (App类型1 版本 %d) "), nBlkVer); } else if (nBlkType == 0x04) { s1.AppendFormat(_T(" (App类型2 版本 %d) "), nBlkVer); } else { s1.AppendFormat(_T(" (未知类型) ")); } s1.AppendFormat(_T("\r\n")); s1.AppendFormat(_T("应用区版本: 0x%04X 对应模块类型: 0x%04X\r\n"), pAppinfo->nAppVer, pAppinfo->nAppDevice); s1.AppendFormat(_T("应用区大小: 0x%04X 实际代码大小: 0x%04X \r\n"), pAppinfo->nAppSize, pAppinfo->nAppDataSize); s1.AppendFormat(_T("应用区地址: 0x%08X (偏移 %d )\r\n"), pAppinfo->nAppStartAddr, pAppinfo->nAppStartAddr - 0x08000000); s1.AppendFormat(_T("信息暂存区: 0x%08X \r\n应用暂存区: 0x%08X \r\n"), pAppinfo->nAppStartOffset, pAppinfo->nApp); // SetDlgItemText(IDC_STATIC_APP_INFO, s1); if (nBlkType == 0x03 || nBlkType == 0x04) { memmove(Appbuf, filebuf, fileLen2); appfileLoaded = true; AppLength = fileLen2; } else { if ((nBlkType == 0x01 || nBlkType == 0x02) && fileLen2 >= AppOffset + nAppInfoOffset + sizeof(stAppInfoBlock)) { pAppInfoBlock pAppinfo2 = (pAppInfoBlock)(filebuf + AppOffset + nAppInfoOffset); if ((pAppinfo2->Hdr.nBlkSign == 0xaa55) && pAppinfo2->Hdr.nBlkTypeVer == 0x0301) { s1.Format(_T("文件是bootloader和App合成的文件 , 是否从中提取固件并继续")); int r3 = AfxMessageBox(s1, MB_YESNO); if (r3 == IDYES) { memmove(Appbuf, filebuf + AppOffset, fileLen2 - AppOffset); appfileLoaded = true; AppLength = fileLen2 - AppOffset; } } else { s1.Format(_T("文件 不是App文件 , 是否继续 ")); int r3 = AfxMessageBox(s1, MB_YESNO); if (r3 == IDYES) { memmove(Appbuf, filebuf, fileLen2); appfileLoaded = true; AppLength = fileLen2; } } } else { s1.Format(_T("文件 不是App文件 , 是否继续 ")); int r3 = AfxMessageBox(s1, MB_YESNO); if (r3 == IDYES) { memmove(Appbuf, filebuf, fileLen2); appfileLoaded = true; AppLength = fileLen2; } } } // s1.Format(_T(" BootLoader启动文件 版本 %04X 硬件类型 %04X 占用空间 %04X, %d Bytes\r\n 是否继续"), pinfob->nBtldrVer, pinfob->nBtldrDevice, pinfob->nBtldrSize, fileLen1); } else { s1.Format(_T("文件中未找到信息块, 文件大小 %d Bytes\r\n 是否继续"), fileLen2); int r3 = AfxMessageBox(s1, MB_YESNO); if (r3 == IDYES) { memmove(Appbuf, filebuf, fileLen2); appfileLoaded = true; AppLength = fileLen2; } } unsigned short crc2 = crc16tablefast(Appbuf, AppLength); unsigned char* buf2 = Appbuf + 0x1000; ; //s1.Format(_T("GetInfo From File = %d %d \r\n"), j, fileLen1); pKMInfoBlock pinfof = (pKMInfoBlock)buf2; s1.Format(_T(" 目标模块 类型 %04X 版本 %d.%02d, \r\n 固件文件 类型 %04X 版本 %d.%02d, %d Bytes\r\n 是否继续"), ptheDeviceInfo->DeviceType, ptheDeviceInfo->DeviceVerMajor,ptheDeviceInfo->DeviceVerMinor, pinfof->nDeviceTypeVer, pinfof->nProgVerMajor,pinfof->nProgVerMinor, fileLen2); int r4 = AfxMessageBox(s1, MB_YESNO); if (r4 != IDYES) { return; } { int bError = 0; for (int i = 0; i < AppLength; i += blocksize) { int sendsize = AppLength - 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.RunRemoteReq(nDst, nPort,nChildId, ReqUpdateFirm,0,i, sendsize, Appbuf + i); Sleep(10); 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 = AppLength; NewAppInfo.Sign = 0x55AA; NewAppInfo.Version = 0x109; NewAppInfo.nCRC = crc2; int k = theApp.MyKLink1.RunRemoteReq(nDst, nPort, nChildId, ReqUpdateFirmInfo, 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.RunRemoteReq(nDst, nPort, nChildId, ReqReset, 0); s1.Format(_T("ResetRemoteDevice Port %d Child %d = %d"), nPort, nChildId, r5); SysLog(s1); /* int r6 = theApp.MyKLink1.ResetDevice(nDst, 0); s1.Format(_T("ResetDevice = %d"), r6); SysLog(s1); for (int i = 0; i < 8; i++) { bPortRegsUpdated[i] = 0; } for (int i = 0; i < 16; i++) { bPortDeviceUpdated[i] = 0; } myLogger1.UpdateLogDisplay(); */ // Sleep(2000); } // theApp.MyKLink1.DisConnect();; // OnMenuNavConnect(); } }