QuakeGod
2024-12-24 61deef5cdf96cbfdd6ad45be49e80d597c00ca65
提交 | 用户 | age
418cb3 1 
Q 2 #include "pch.h"
3 #include "framework.h"
4 #include "Resource.h"
5 #include "MainFrm.h"
6 #include "NavView.h"
7
8 #include "MTerm1.h"
61deef 9 #include "../MyLib/Functions.hpp"
418cb3 10
Q 11 #define IDC_NAVTREE1 1046
12 class CClassViewMenuButton : public CMFCToolBarMenuButton
13 {
14     friend class CNavView;
15
16     DECLARE_SERIAL(CClassViewMenuButton)
17
18 public:
19     CClassViewMenuButton(HMENU hMenu = nullptr) noexcept : CMFCToolBarMenuButton((UINT)-1, hMenu, -1)
20     {
21     }
22
23     virtual void OnDraw(CDC* pDC, const CRect& rect, CMFCToolBarImages* pImages, BOOL bHorz = TRUE,
24         BOOL bCustomizeMode = FALSE, BOOL bHighlight = FALSE, BOOL bDrawBorder = TRUE, BOOL bGrayDisabledButtons = TRUE)
25     {
26         pImages = CMFCToolBar::GetImages();
27
28         CAfxDrawState ds;
29         pImages->PrepareDrawImage(ds);
30
31         CMFCToolBarMenuButton::OnDraw(pDC, rect, pImages, bHorz, bCustomizeMode, bHighlight, bDrawBorder, bGrayDisabledButtons);
32
33         pImages->EndDrawImage(ds);
34     }
35 };
36
37 IMPLEMENT_SERIAL(CClassViewMenuButton, CMFCToolBarMenuButton, 1)
38
39 //////////////////////////////////////////////////////////////////////
40 // 构造/析构
41 //////////////////////////////////////////////////////////////////////
42
43 CNavView::CNavView() noexcept
44 {
45     m_nCurrSort = ID_SORTING_GROUPBYTYPE;
46 }
47
48 CNavView::~CNavView()
49 {
50 }
51
52 BEGIN_MESSAGE_MAP(CNavView, CDockablePane)
53     ON_WM_CREATE()
54     ON_WM_SIZE()
55     ON_WM_CONTEXTMENU()
56 //    ON_COMMAND(ID_CLASS_ADD_MEMBER_FUNCTION, OnClassAddMemberFunction)
57 //    ON_COMMAND(ID_CLASS_ADD_MEMBER_VARIABLE, OnClassAddMemberVariable)
58 //    ON_COMMAND(ID_CLASS_DEFINITION, OnClassDefinition)
59 //    ON_COMMAND(ID_CLASS_PROPERTIES, OnClassProperties)
60     ON_COMMAND(ID_NEW_FOLDER, OnNewFolder)
61deef 61     ON_COMMAND(ID_NAV_SETCOMM, OnSetComm)
Q 62     ON_COMMAND(ID_NAV_CONNECT, OnConnect)
63     ON_COMMAND(ID_NAV_DISCONNECT, OnDisConnect)
418cb3 64     ON_WM_PAINT()
Q 65     ON_WM_SETFOCUS()
66     ON_COMMAND_RANGE(ID_SORTING_GROUPBYTYPE, ID_SORTING_SORTBYACCESS, OnSort)
67     ON_UPDATE_COMMAND_UI_RANGE(ID_SORTING_GROUPBYTYPE, ID_SORTING_SORTBYACCESS, OnUpdateSort)
68     ON_NOTIFY(NM_DBLCLK, IDC_NAVTREE1, OnNMDblclkNavTree1)
69     ON_NOTIFY(NM_CLICK, IDC_NAVTREE1, OnNMClkNavTree1)
70
71     ON_WM_TIMER()
61deef 72     ON_COMMAND(ID_MENU_NAV_SET_CONN, &CNavView::OnMenuNavSetConn)
Q 73     ON_COMMAND(ID_MENU_NAV_CONNECT, &CNavView::OnMenuNavConnect)
74     ON_COMMAND(ID_MENU_NAV_DISCONNECT, &CNavView::OnMenuNavDisconnect)
75     ON_COMMAND(ID_MENU_NV_UPDATE_FIRMWARE, &CNavView::OnMenuNvUpdateFirmware)
76     ON_COMMAND(ID_MENU_NV_DEVICE_READ_INFO, &CNavView::OnMenuNvDeviceReadInfo)
77
78     ON_COMMAND(ID_LED_BLINK, &CNavView::OnLedBlink)
79     ON_COMMAND(ID_RESET_DEVICE, &CNavView::OnResetDevice)
80     ON_COMMAND(ID_REMOTE_REFRESH_DATA, &CNavView::OnRemoteRefreshData)
81     ON_COMMAND(ID_REMOTE_BLINK, &CNavView::OnRemoteBlinkLED)
82     ON_COMMAND(ID_REMOTE_DEVICE_RESET, &CNavView::OnRemoteDeviceReset)
83     ON_COMMAND(ID_REMOTE_UPDATE_FIRMWARE, &CNavView::OnRemoteUpdateFirmware)
84     ON_COMMAND(ID_REMOTE_BLINK_LED_ALL, &CNavView::OnRemoteBlinkLedAll)
85     ON_COMMAND(ID_PORT_RESET, &CNavView::OnPortReset)
86     ON_COMMAND(ID_PORT_RESET_ALL_CHILD, &CNavView::OnPortResetAllChild)
87 //    ON_COMMAND(ID_COMMTEST, &CNavView::OnCommtest)
88 //    ON_COMMAND(ID_DEVICE_CONFIG, &CNavView::OnDeviceConfig)
418cb3 89 END_MESSAGE_MAP()
Q 90
91 /////////////////////////////////////////////////////////////////////////////
92 // CNavView 消息处理程序
93
94 int CNavView::OnCreate(LPCREATESTRUCT lpCreateStruct)
95 {
96     if (CDockablePane::OnCreate(lpCreateStruct) == -1)
97         return -1;
98
99     CRect rectDummy;
100     rectDummy.SetRectEmpty();
101
102     // 创建视图: 
103     const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | TVS_LINESATROOT | TVS_HASLINES  | TVS_HASBUTTONS | WS_CLIPSIBLINGS | WS_CLIPCHILDREN ; //
104
105     if (!m_NavViewTree.Create(dwViewStyle, rectDummy, this, IDC_NAVTREE1))
106     {
107         TRACE0("未能创建导航栏视图\n");
108         return -1;      // 未能创建
109     }
110     m_NavViewTree.SetExtendedStyle(TVS_EX_DOUBLEBUFFER, TVS_EX_DOUBLEBUFFER);
111     // 加载图像: 
61deef 112     m_wndToolBar.Create(this, AFX_DEFAULT_TOOLBAR_STYLE, IDR_NAV_TOOLBAR);
Q 113     m_wndToolBar.LoadToolBar(IDR_NAV_TOOLBAR, 0, 0, TRUE /* 已锁定*/);
418cb3 114
Q 115     OnChangeVisualStyle();
116
117     m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() | CBRS_TOOLTIPS | CBRS_FLYBY);
118     m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() & ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT));
119
120     m_wndToolBar.SetOwner(this);
121
122     // 所有命令将通过此控件路由,而不是通过主框架路由: 
123     m_wndToolBar.SetRouteCommandsViaFrame(FALSE);
124
125     CMenu menuSort;
61deef 126     menuSort.LoadMenu(IDR_POPUP_NAV);
418cb3 127
Q 128     m_wndToolBar.ReplaceButton(ID_SORT_MENU, CClassViewMenuButton(menuSort.GetSubMenu(0)->GetSafeHmenu()));
129
130     CClassViewMenuButton* pButton =  DYNAMIC_DOWNCAST(CClassViewMenuButton, m_wndToolBar.GetButton(0));
131
132     if (pButton != nullptr)
133     {
134         pButton->m_bText = FALSE;
135         pButton->m_bImage = TRUE;
136         pButton->SetImage(GetCmdMgr()->GetCmdImage(m_nCurrSort));
137         pButton->SetMessageWnd(this);
138     }
139
140     // 填入一些静态树视图数据(此处只需填入虚拟代码,而不是复杂的数据)
141     SetTimer(0, 100, NULL);
142     return 0;
143 }
144
145 void CNavView::OnSize(UINT nType, int cx, int cy)
146 {
147     CDockablePane::OnSize(nType, cx, cy);
148     AdjustLayout();
149 }
150
151
152 void CNavView::AdjustLayout()
153 {
154     if (GetSafeHwnd() == nullptr)
155     {
156         return;
157     }
158
159     CRect rectClient;
160     GetClientRect(rectClient);
161
162     int cyTlb = m_wndToolBar.CalcFixedLayout(FALSE, TRUE).cy;
163
164     m_wndToolBar.SetWindowPos(nullptr, rectClient.left, rectClient.top, rectClient.Width(), cyTlb, SWP_NOACTIVATE | SWP_NOZORDER);
165     m_NavViewTree.SetWindowPos(nullptr, rectClient.left + 1, rectClient.top + cyTlb + 1, rectClient.Width() - 2, rectClient.Height() - cyTlb - 2, SWP_NOACTIVATE | SWP_NOZORDER);
166 }
167
168 BOOL CNavView::PreTranslateMessage(MSG* pMsg)
169 {
170     return CDockablePane::PreTranslateMessage(pMsg);
171 }
172
173 void CNavView::OnSort(UINT id)
174 {
175     if (m_nCurrSort == id)
176     {
177         return;
178     }
179
180     m_nCurrSort = id;
181
182     CClassViewMenuButton* pButton =  DYNAMIC_DOWNCAST(CClassViewMenuButton, m_wndToolBar.GetButton(0));
183
184     if (pButton != nullptr)
185     {
186         pButton->SetImage(GetCmdMgr()->GetCmdImage(id));
187         m_wndToolBar.Invalidate();
188         m_wndToolBar.UpdateWindow();
189     }
190 }
191
192
193 void CNavView::OnTimer(UINT_PTR nIDEvent)
194 {
195     // TODO: 在此添加消息处理程序代码和/或调用默认值
196     if (nIDEvent == 0) {
197         KillTimer(0);
198         DelayInit();
199         SetTimer(1, 200, NULL);
200     }
201     else if (nIDEvent == 1) {
202         UpdateDisplay();
203     }
204     else if (nIDEvent == 2) {
205
206     }
207     else if (nIDEvent == 3) {
208
209     }
210     CDockablePane::OnTimer(nIDEvent);
211 }
212
213
214 int CNavView::DelayInit()
215 {
216     // TODO: 在此处添加实现代码.
61deef 217     FillDeviceView();
418cb3 218     return 0;
Q 219 }
220
221 void CNavView::OnPaint()
222 {
223     CPaintDC dc(this); // 用于绘制的设备上下文
224
225     CRect rectTree;
226     m_NavViewTree.GetWindowRect(rectTree);
227     ScreenToClient(rectTree);
228
229     rectTree.InflateRect(1, 1);
230     dc.Draw3dRect(rectTree, ::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DSHADOW));
231 }
232
233 void CNavView::OnSetFocus(CWnd* pOldWnd)
234 {
235     CDockablePane::OnSetFocus(pOldWnd);
236
237     m_NavViewTree.SetFocus();
238 }
239
240 void CNavView::OnChangeVisualStyle()
241 {
242     m_NavViewImages.DeleteImageList();
243
244     UINT uiBmpId = theApp.m_bHiColorIcons ? IDB_CLASS_VIEW_24 : IDB_CLASS_VIEW;
245 //    uiBmpId = IDB_NAV_VIEW1;
246     CBitmap bmp;
247     if (!bmp.LoadBitmap(uiBmpId))
248     {
249         TRACE(_T("无法加载位图: %x\n"), uiBmpId);
250         ASSERT(FALSE);
251         return;
252     }
253
254     BITMAP bmpObj;
255     bmp.GetBitmap(&bmpObj);
256
257     UINT nFlags = ILC_MASK;
258
259     nFlags |= (theApp.m_bHiColorIcons) ? ILC_COLOR24 : ILC_COLOR4;
260
261     m_NavViewImages.Create(16, bmpObj.bmHeight, nFlags, 0, 0);
262     m_NavViewImages.Add(&bmp, RGB(255, 0, 0));
263
264     m_NavViewTree.SetImageList(&m_NavViewImages, TVSIL_NORMAL);
265
266     m_wndToolBar.CleanUpLockedImages();
61deef 267     m_wndToolBar.LoadBitmap(theApp.m_bHiColorIcons ? IDB_NAV_TOOLBAR_24 : IDR_NAV_TOOLBAR, 0, 0, TRUE /* 锁定*/);
418cb3 268 }
Q 269
61deef 270 void CNavView::FillDeviceView()
Q 271 {
272     HTREEITEM hRoot1 = m_NavViewTree.InsertItem(_T("连接1 No Connect"), 0, 0);
273     m_NavViewTree.SetItemState(hRoot1, TVIS_BOLD, TVIS_BOLD);
274
275     HTREEITEM hItem;
276     HTREEITEM hDevice1 = m_NavViewTree.InsertItem(_T("KL10-E08D-N1  1/1"), 5, 5, hRoot1);
277     m_NavViewTree.SetItemData(hDevice1, MENU_DEVICE|0);
278
279     m_NavViewTree.Expand(hRoot1, TVE_EXPAND);
280
281 /*
282     hItem = m_NavViewTree.InsertItem(_T("设备信息"), 3, 3, hDevice1);
283     m_NavViewTree.SetItemData(hItem, MENU_PROPERTY|0);
284     hItem = m_NavViewTree.InsertItem(_T("工作状态,系统参数"), 3, 3, hDevice1);
285     m_NavViewTree.SetItemData(hItem, MENU_PROPERTY|1);
286     HTREEITEM hPort1 = m_NavViewTree.InsertItem(_T("端口1 232 KLink 本机地址(1)/5 Run "), 1, 1, hDevice1);
287     m_NavViewTree.SetItemData(hPort1, MENU_PORT|0);
288
289     HTREEITEM hPort2 = m_NavViewTree.InsertItem(_T("端口2 485 KBus 本机地址(0)/5 Run "), 1, 1, hDevice1);
290     m_NavViewTree.SetItemData(hPort2, MENU_PORT|1);
291     hItem = m_NavViewTree.InsertItem(_T("1 KL10-E08D-N1 Run Good"), 5, 5, hPort2);
292     m_NavViewTree.SetItemData(hItem, MENU_REMOTE_DEVICE|0);
293     HTREEITEM hPort3 = m_NavViewTree.InsertItem(_T("端口3 WireLess "), 1, 1, hDevice1);
294     m_NavViewTree.SetItemData(hPort3, MENU_PORT|2);
295
296     m_NavViewTree.SetBkColor(COLORREF(0xf0f0f0f0));
297
298     HTREEITEM hDevice2 = m_NavViewTree.InsertItem(_T("KL10-E16D-N1  1/1"), 5, 5, hRoot1);
299     m_NavViewTree.SetItemData(hDevice2, MENU_DEVICE | 1);
300
301     // */
302 }
303
304 void CNavView::OnContextMenu(CWnd* pWnd, CPoint point)
305 {
306     CTreeCtrl* pWndTree = (CTreeCtrl*)&m_NavViewTree;
307     ASSERT_VALID(pWndTree);
308
309     if (pWnd != pWndTree)
310     {
311         CDockablePane::OnContextMenu(pWnd, point);
312         return;
313     }
314     HTREEITEM hTreeItem = nullptr;
315     if (point != CPoint(-1, -1))
316     {
317         // 选择已单击的项: 
318         CPoint ptTree = point;
319         pWndTree->ScreenToClient(&ptTree);
320
321         UINT flags = 0;
322         hTreeItem = pWndTree->HitTest(ptTree, &flags);
323         if (hTreeItem != nullptr)
324         {
325             pWndTree->SelectItem(hTreeItem);
326         }
327     }
328     pWndTree->SetFocus();
329     DWORD_PTR ItemData = 0;
330     if (hTreeItem != nullptr) {
331         ItemData = m_NavViewTree.GetItemData(hTreeItem);
332     }
333
334     CMenu menu;
335     //    menu.LoadMenu(IDR_POPUP_SORT);
336     menu.LoadMenu(IDR_POPUP_NAV);
337     CMenu* pSubMenu;
338     HTREEITEM hRoot1 = m_NavViewTree.GetRootItem();
339     HTREEITEM hDevice1 = m_NavViewTree.GetNextItem(hRoot1, TVGN_CHILD);
340     if (hTreeItem == hRoot1) {
341         pSubMenu = menu.GetSubMenu(1);        // 连接菜单
342     }
343     else if (hTreeItem == hDevice1 ){
344         pSubMenu = menu.GetSubMenu(2);        //设备菜单
345     }
346     else if (ItemData & MENU_PROPERTY){
347         pSubMenu = menu.GetSubMenu(3);        //属性菜单
348     }
349     else if (ItemData & MENU_PORT) {
350         pSubMenu = menu.GetSubMenu(4);        //端口菜单
351     }
352     else if (ItemData & MENU_REMOTE_DEVICE) {
353         pSubMenu = menu.GetSubMenu(5);        //远程设备菜单
354     }
355     else {
356         pSubMenu = menu.GetSubMenu(0);        //默认菜单
357     }
358
359
360     pSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
361 /*
362     if (AfxGetMainWnd()->IsKindOf(RUNTIME_CLASS(CMDIFrameWndEx)))
363     {
364         CMFCPopupMenu* pPopupMenu = new CMFCPopupMenu;
365
366         if (!pPopupMenu->Create(this, point.x, point.y, (HMENU)pSubMenu->m_hMenu, FALSE, TRUE))
367             return;
368
369         ((CMDIFrameWndEx*)AfxGetMainWnd())->OnShowPopupMenu(pPopupMenu);
370         UpdateDialogControls(this, FALSE);
371     }
372 */
373     menu.DestroyMenu();
374
375 }
376
377 void CNavView::OnUpdateSort(CCmdUI* pCmdUI)
378 {
379     pCmdUI->SetCheck(pCmdUI->m_nID == m_nCurrSort);
380 }
381
382 void CNavView::OnClassAddMemberFunction()
383 {
384     AfxMessageBox(_T("添加成员函数..."));
385 }
386
387 void CNavView::OnClassAddMemberVariable()
388 {
389     // TODO: 在此处添加命令处理程序代码
390 }
391
392 void CNavView::OnClassDefinition()
393 {
394     // TODO: 在此处添加命令处理程序代码
395 }
396
397 void CNavView::OnClassProperties()
398 {
399     // TODO: 在此处添加命令处理程序代码
400 }
401
402 void CNavView::OnNewFolder()
403 {
404     AfxMessageBox(_T("新建文件夹..."));
405 }
406
407 int CNavView::GetDocCount()
408 {
409     // TODO: 在此处添加实现代码.
410 /*
411
412     int nCount1 = 0;
413
414     POSITION Templatepos = AfxGetApp()->m_pDocManager->GetFirstDocTemplatePosition();
415     while (Templatepos != NULL)
416     {
417         CDocTemplate* pDocTemplate = AfxGetApp()->m_pDocManager->GetNextDocTemplate(Templatepos);
418
419         POSITION pos = pDocTemplate->GetFirstDocPosition();
420         while (pos != NULL)
421         {
422             CDocument * pDoc = pDocTemplate->GetNextDoc(pos);
423             nCount1++;
424             //if (pDoc->IsModified()) // 只保存被修改的文档
425             //{
426             //    pDoc->DoFileSave();
427             //}
428         }
429     }
430     return nCount1;
431 */
432     return 0;
433 }
434
435 int CNavView::UpdateDisplay()
436 {
437     // TODO: 在此处添加实现代码.
438 //    m_NavViewTree.DeleteAllItems();
439 //    FillDeviceView();
440     nDisplayCount++;
441
442     KLink1* pMyKLink1 = &theApp.MyKLink1;
443     CString s1;
444     HTREEITEM hRoot1 = m_NavViewTree.GetRootItem();
445     if (theApp.MyKLink1.m_bOpened) {
446         s1.Format(_T("连接1 "));
447         s1.AppendFormat(_T("COM%d %d %s "), pMyKLink1->MySerPort1.m_nPort, pMyKLink1->MySerPort1.m_nBaudRate, pMyKLink1->MySerPort1.m_Settings);
448         s1.AppendFormat(_T("S %d R %d D %d"), theApp.MyKLink1.m_nTotalSendCount,theApp.MyKLink1.m_nTotalRecvCount, nDisplayCount);
449 ///*
450         if ( (nDisplayCount & 0x1f) == 0) {
451             bPortRegsUpdated[0] = 0;
452             bPortRegsUpdated[1] = 0;
453             bPortRegsUpdated[2] = 0;
454         }
455 // */
456     }
457     else {
458         s1.Format(_T("连接1 No Connect"));
459     }
460     m_NavViewTree.SetItemText(hRoot1, s1);
461     HTREEITEM hDevice1 = m_NavViewTree.GetChildItem(hRoot1);
462
463 //    m_NavViewTree.DeleteItem(hDevice1);
464
465     if (!theApp.MyKLink1.m_bOpened) {
466         s1.Format(_T("No Connect"));
467     }
468     else {
469         s1.Format(_T("Dev %04X %s Run"), m_DeviceInfo.nDeviceTypeVer,DeviceTypeToStr(m_DeviceInfo.nDeviceTypeVer));
470     }
471     m_NavViewTree.SetItemText(hDevice1, s1);
472     if (!theApp.MyKLink1.m_bOpened)
473     {
474         HTREEITEM hItem11 = m_NavViewTree.GetNextItem(hDevice1, TVGN_CHILD);
475         s1.Format(_T("设备参数"));
476         m_NavViewTree.SetItemText(hItem11, s1);
477         HTREEITEM hItem12 = m_NavViewTree.GetNextItem(hItem11, TVGN_NEXT);
478         m_NavViewTree.SetItemText(hItem12, _T("工作状态"));
479         
480         HTREEITEM hItem13 = m_NavViewTree.GetNextItem(hItem12, TVGN_NEXT);
481         if (hItem13 != nullptr) {
482             bPortRegsUpdated[0] = 0;
483             bPortRegsUpdated[1] = 0;
484             bPortRegsUpdated[2] = 0;
485             m_NavViewTree.DeleteItem(hItem13);
486             HTREEITEM hItem13 = m_NavViewTree.GetNextItem(hItem12, TVGN_NEXT);
487         }
488
489     }else 
490     {
491         HTREEITEM hItem11 = m_NavViewTree.GetNextItem(hDevice1, TVGN_CHILD);
492         if (hItem11 == nullptr) {
493             hItem11 = m_NavViewTree.InsertItem(_T("设备参数 "), 3, 3, hDevice1);
494             m_NavViewTree.Expand(hDevice1, TVE_EXPAND);
495         }
496         s1.Format(_T("设备参数 V%d.%02d KLink %04X KBus %04X In %d Out %d"),
497             m_infoblock.nProgVerMajor, m_infoblock.nProgVerMinor, m_infoblock.nKLinkVer, m_infoblock.nKBusVer, m_DeviceInfo.InBitCount, m_DeviceInfo.OutBitCount);
498         m_NavViewTree.SetItemText(hItem11, s1);
499         HTREEITEM hItem12 = m_NavViewTree.GetNextItem(hItem11, TVGN_NEXT);
500         if (hItem12 == nullptr) {
501             hItem12 = m_NavViewTree.InsertItem(_T("工作状态 "), 3, 3, hDevice1);
502             m_NavViewTree.Expand(hDevice1, TVE_EXPAND);
503         }
504         m_NavViewTree.SetItemText(hItem12, _T("工作状态,系统参数,IO状态 hItem12"));
505         HTREEITEM hPort1 = hItem12;
506         USHORT nReaded = 0;
507         USHORT DataBuf1[256];
508         for (int i = 0; i < m_DeviceInfo.nPortCount; i++) {
509             hPort1 = m_NavViewTree.GetNextItem(hPort1, TVGN_NEXT);
510             if (hPort1 == nullptr) {
511                 hPort1 = m_NavViewTree.InsertItem(_T("端口1 "), 1, 1, hDevice1);
512                 m_NavViewTree.Expand(hDevice1, TVE_EXPAND);
513             }
514             if (theApp.MyKLink1.m_bOpened && !bPortRegsUpdated[i]) {
515                 int res1 = theApp.MyKLink1.GetPortInfo(1, i, sizeof(stPortDef), &nReaded, DataBuf1);
516                 if (res1 == KLink1::KL_OK) {
517                     pstPortDef thepPortReg = (pstPortDef)DataBuf1;
518                     PortDefs[i] = *thepPortReg;
519                     s1.Format(_T("端口%d (%02X)%s (%02X)%s 能力 %04X M %d 站号 %d/%d H %d"), i + 1,
520                         thepPortReg->nPortHardType, PortHardTypeToStr(thepPortReg->nPortHardType), thepPortReg->nPortUseType,
521                         PortUseTypeToStr(thepPortReg->nPortUseType), thepPortReg->ability, thepPortReg->bMaster, thepPortReg->StationId, thepPortReg->nMaxStations, thepPortReg->nHealth);
522                     m_NavViewTree.SetItemText(hPort1, s1);
523                     m_NavViewTree.SetItemData(hPort1, MENU_PORT | i);
524
525                     bPortRegsUpdated[i] = 1;
526                     break;
527                 }
528                 else {
529                     s1.Format(_T("GetPortInfo %d = %d len %d  %s"), i, res1, &nReaded,theApp.MyKLink1.m_resultStr);
530                     DbgLog(s1);
531                     s1.Format(_T("端口%d --------- ???? "), i + 1);
532                     m_NavViewTree.SetItemText(hPort1, s1);
533                     m_NavViewTree.SetItemData(hPort1, MENU_PORT | i);
534                 }
535             }
536             if (bPortRegsUpdated[i] && (PortDefs[i].nPortUseType == PortUse_KBus || PortDefs[i].nPortUseType == PortUse_KRF)) {
537                 HTREEITEM hChild1 = m_NavViewTree.GetNextItem(hPort1, TVGN_CHILD);
538                 int nChildCount = PortDefs[i].nMaxStations;
539                 for (int j = 1; j <= nChildCount; j++) {
540                     if (j != 1) hChild1 = m_NavViewTree.GetNextItem(hChild1, TVGN_NEXT);
541                     if (hChild1 == nullptr) {
542                         s1.Format(_T("子机%d "), j);
543                         hChild1 = m_NavViewTree.InsertItem(s1, 5, 5, hPort1);
544                         m_NavViewTree.SetItemData(hChild1, MENU_REMOTE_DEVICE | (j));
545                         m_NavViewTree.Expand(hPort1, TVE_EXPAND);
546                     }
547                     //if (j < LastUpdateIndex) continue;
548                     if (bPortDeviceUpdated[j]) continue;
549                     int res2 = theApp.MyKLink1.GetPortChildInfo(1, i, j, sizeof(stDeviceInfo), &nReaded, DataBuf1);  //sizeof(stDeviceInfo)
550                     if (res2 == KLink1::KL_OK) {
551                         s1.Format(_T("GetChildInfo %d %d = %d B\r\n"), i, j, nReaded);
552                         for (int k = 0; k < nReaded; k++) {
553                             s1.AppendFormat(_T("%02X "), ((unsigned char*)DataBuf1)[k]);
554                         }
555                         // SysLog(s1);
556                         stDeviceInfo* ptheInfo = (stDeviceInfo*)DataBuf1;
557                         PortDeviceInfos[j] = *ptheInfo;
558                         s1.Format(_T("子%d (%04X)%s V%d.%02d %d/%d "),
559                             j, ptheInfo->DeviceType, DeviceTypeToStr(ptheInfo->DeviceType), ptheInfo->DeviceVerMajor, ptheInfo->DeviceVerMinor, ptheInfo->InBitCount, ptheInfo->OutBitCount);
560                         m_NavViewTree.SetItemText(hChild1, s1);
561                         m_NavViewTree.SetItemData(hChild1, MENU_REMOTE_DEVICE | (j));
562                     }
563                     bPortDeviceUpdated[j] = 1;
564                     break;
565                 }
566                 LastUpdateIndex[i]++;
567                 if (LastUpdateIndex[i] > nChildCount) { LastUpdateIndex[i] = 0; }
568                 s1.Format(_T("i %d LastUpdated %d  nChildCount %d  nMaxStations %d \r\n"),i, LastUpdateIndex[i],nChildCount, PortDefs[i].nMaxStations);
569             //    SysLog(s1);
570
571             }
572             ///*
573             if (bPortRegsUpdated[i] && PortDefs[i].nPortUseType == PortUse_KBus ) {
574                 HTREEITEM hChild1 = m_NavViewTree.GetNextItem(hPort1, TVGN_CHILD);
575                 int nChildCount = PortDefs[i].nMaxStations;
576                 int j = LastUpdateIndex[i] + 1;
577                 for (int j = 1; j <= nChildCount; j++) {
578                     if (j != 1) hChild1 = m_NavViewTree.GetNextItem(hChild1, TVGN_NEXT);
579                     if (hChild1 == nullptr) {
580                         s1.Format(_T("子机%d "), j);
581                         hChild1 = m_NavViewTree.InsertItem(s1, 5, 5, hPort1);
582                         m_NavViewTree.Expand(hPort1, TVE_EXPAND);
583                     }
584                     if (j < LastUpdateIndex[i]) continue;
585                     //if (bPortDeviceUpdated[j]) continue;
586                     int res2 = theApp.MyKLink1.GetPortChnInfo(1, i, j, sizeof(stChnStat), &nReaded, DataBuf1);  //sizeof(stDeviceInfo)
587                     if (res2 == KLink1::KL_OK) {
588                         s1.Format(_T("GetChnInfo %d %d = %d B\r\n"), i, j, nReaded);
589                         for (int k = 0; k < nReaded; k++) {
590                             s1.AppendFormat(_T("%02X "), ((unsigned char*)DataBuf1)[k]);
591                         }
592                         //    SysLog(s1);
593                         stDeviceInfo* ptheInfo = &PortDeviceInfos[j];
594                         stChnStat* ptheChnInfo = (stChnStat*)DataBuf1;
595
596                         s1.Format(_T("子%d (%04X)%s V%d.%02d %d/%d 丢包率(%.2f%%) 收发(%d/%d) "), j,
597                             ptheInfo->DeviceType, DeviceTypeToStr(ptheInfo->DeviceType), ptheInfo->DeviceVerMajor, ptheInfo->DeviceVerMinor, ptheInfo->InBitCount, ptheInfo->OutBitCount
598                             , ptheChnInfo->LostPackets * 100.0 / ptheChnInfo->SendPackets, ptheChnInfo->RecvPackets, ptheChnInfo->SendPackets);
599                         m_NavViewTree.SetItemText(hChild1, s1);
600                     }
601                     //    bPortDeviceUpdated[j] = 1;
602                     break;
603                 }
604             }
605             // */
606             ///*
607             if (bPortRegsUpdated[i] &&  PortDefs[i].nPortUseType == PortUse_KRF) {
608                 HTREEITEM hChild1 = m_NavViewTree.GetNextItem(hPort1, TVGN_CHILD);
609                 int nChildCount = PortDefs[i].nMaxStations;
610                 int j = LastUpdateIndex[i] + 1;
611                 for (int j = 1; j <= nChildCount; j++) {
612                     if (j != 1) hChild1 = m_NavViewTree.GetNextItem(hChild1, TVGN_NEXT);
613                     if (hChild1 == nullptr) {
614                         s1.Format(_T("子机%d "), j);
615                         hChild1 = m_NavViewTree.InsertItem(s1, 5, 5, hPort1);
616                         m_NavViewTree.Expand(hPort1, TVE_EXPAND);
617                     }
618                     if (j < LastUpdateIndex[i]) continue;
619                     //if (bPortDeviceUpdated[j]) continue;
620                     int res2 = theApp.MyKLink1.GetPortChnInfo(1, i, j, sizeof(stKwChnStat), &nReaded, DataBuf1);  //sizeof(stDeviceInfo)
621                     if (res2 == KLink1::KL_OK) {
622                         s1.Format(_T("GetChnInfo %d %d = %d B\r\n"), i, j, nReaded);
623                         for (int k = 0; k < nReaded; k++) {
624                             s1.AppendFormat(_T("%02X "), ((unsigned char*)DataBuf1)[k]);
625                         }
626                         //    SysLog(s1);
627                         stDeviceInfo* ptheInfo = &PortDeviceInfos[j];
628                         stKwChnStat* ptheChnInfo = (stKwChnStat*)DataBuf1;
629
630                         s1.Format(_T("子%d (%04X)%s V%d.%02d %d/%d 丢包率(%.2f%%) 收发(%d/%d) "), j,
631                             ptheInfo->DeviceType, DeviceTypeToStr(ptheInfo->DeviceType), ptheInfo->DeviceVerMajor, ptheInfo->DeviceVerMinor, ptheInfo->InBitCount, ptheInfo->OutBitCount
632                             , ptheChnInfo->LostPackets * 100.0 / ptheChnInfo->sentCount, ptheChnInfo->recvCount, ptheChnInfo->sentCount);
633                         m_NavViewTree.SetItemText(hChild1, s1);
634                     }
635                     else {
636                         s1.Format(_T("GetChnInfo %d %d = %d B\r\n"), i, j, nReaded);
637                         SysLog(s1);
638                     }
639                     //    bPortDeviceUpdated[j] = 1;
640                     break;
641                 }
642             }
643             // */
644         }
645     }
646
647
648     HTREEITEM hDevice2 = m_NavViewTree.GetNextItem(hDevice1, TVGN_NEXT);
649     m_NavViewTree.SetItemText(hDevice2, _T("hDevice2"));
650
651
652 //    HTREEITEM hDevice1 = m_NavViewTree.InsertItem(s1, 5, 5, hRoot1);
653 //    m_NavViewTree.SetItemData(hDevice1, 10);
654 //    m_NavViewTree.SetItemText(hDevice1, s1);
655
656     /*
657         AfxGetApp()->m_pDocManager->GetFirstDocTemplatePosition
658         CMDIFrameWnd* pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
659         CMDIChildWnd* pChild = (CMDIChildWnd*)pFrame->GetActiveFrame();
660         CView* pV = (CView*)pChild->GetActiveView();
661         CMTerm1Doc* pDoc = (CMTerm1Doc*)(pV->GetDocument());
662     */
663
664     return 0;
665 }
418cb3 666 void CNavView::OnNMClkNavTree1(NMHDR* pNMHDR, LRESULT* pResult)
Q 667 {
668     LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
669 //    HTREEITEM hTreeItem = pNMTreeView->itemNew.hItem;
670 //    HTREEITEM hTreeItem = m_NavViewTree.GetSelectedItem();
671     //以GetItemText()函数为例: 
672     
673 //    CString s2 = m_NavViewTree.GetItemText(hTreeItem);
674     //AfxMessageBox(_T("You have selected ") + s1);
675 //    CString s1;
676 //    DWORD_PTR dw = m_NavViewTree.GetItemData(hTreeItem);
677 //    s1.Format(_T("Form Clk action %d, id %d oldid %d data %d x %d y %d  text %s "),
678 //        pNMTreeView->action, hTreeItem, pNMTreeView->itemOld.hItem, dw, pNMTreeView->ptDrag.x, pNMTreeView->ptDrag.y, s2);
679
680 //    SysLog(s1);
681
682     *pResult = 0;
683 }
684
685
686 void CNavView::OnNMDblclkNavTree1(NMHDR* pNMHDR, LRESULT* pResult)
687 {
688     LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
689
690     HTREEITEM hTreeItem = m_NavViewTree.GetSelectedItem();
691     //以GetItemText()函数为例: 
692     CString s2 = m_NavViewTree.GetItemText(hTreeItem);
693     //AfxMessageBox(_T("You have selected ") + s1);
694     CString s1;
695     DWORD_PTR dw = m_NavViewTree.GetItemData(hTreeItem);
696     s1.Format(_T("Form dbclk action %d, id %d oldid %d data %d x %d y %d  text %s "),
697         pNMTreeView->action, hTreeItem, pNMTreeView->itemOld.hItem, dw, pNMTreeView->ptDrag.x, pNMTreeView->ptDrag.y, s2);
698
699     SysLog(s1);
700
701     *pResult = 0;
702 }
703
61deef 704 void CNavView::OnMenuNavSetConn()
Q 705 {
706     // TODO: 在此添加命令处理程序代码
707     theApp.MyKLink1.SetCommParam();
708 }
709
710 void CNavView::OnSetComm()
711 {
712     // TODO: 在此添加命令处理程序代码
713     theApp.MyKLink1.SetCommParam();
714 }
715
716 void CNavView::OnConnect()
717 {
718     // TODO: 在此添加命令处理程序代码
719     CString s1;
720     unsigned short buf1[64];
721     unsigned short len1;
722     if (!theApp.MyKLink1.m_bCommParamSet) {
723         theApp.MyKLink1.m_nPort = 6;
724     }
725     int res = theApp.MyKLink1.Connect();
726     if (res == KLink1::KL_OK) {
727         Sleep(100);
728         theApp.MyKLink1.MySerPort1.ClearBuf();
729
730         int res2 = theApp.MyKLink1.GetInfo(1, &len1, buf1);
731         s1.Format(_T("GetInfo = %d %d \r\n"), res2, len1);
732         if (res2 == KLink1::KL_OK && len1 > 0) {
733             pKMInfoBlock pinfob = (pKMInfoBlock)buf1;
734             m_infoblock = *pinfob;
735
736             for (int i = 0; i < 8; i++) {
737                 bPortRegsUpdated[i] = 0;
738             }
739             for (int i = 0; i < 16; i++) {
740                 bPortDeviceUpdated[i] = 0;
741             }
742             m_DeviceInfo.nDeviceTypeVer = pinfob->nDeviceTypeVer;
743             m_DeviceInfo.InBitCount = pinfob->nDInput;
744             m_DeviceInfo.OutBitCount = pinfob->nDOutput;
745             m_DeviceInfo.nPortCount = pinfob->nPorts;
746
747             HTREEITEM hRoot1 = m_NavViewTree.GetRootItem();
748             if (theApp.MyKLink1.m_bOpened) {
749                 s1.Format(_T("连接1 "));
750                 s1.AppendFormat(_T("COM%d %d %s "), theApp.MyKLink1.MySerPort1.m_nPort, theApp.MyKLink1.MySerPort1.m_nBaudRate, theApp.MyKLink1.MySerPort1.m_Settings);
751                 s1.AppendFormat(_T("%d %d  %d \r\n"), theApp.MyKLink1.m_nTotalSendCount, theApp.MyKLink1.m_nTotalRecvCount, nDisplayCount);
752             }
753             else {
754                 s1.Format(_T("连接1 No Connect"));
755             }
756             m_NavViewTree.SetItemText(hRoot1, s1);
757             HTREEITEM hDevice1 = m_NavViewTree.GetChildItem(hRoot1);
758             m_NavViewTree.Expand(hDevice1, TVE_EXPAND);
759
760             for (int i = 0; i < len1 / 2; i++) {
761                 s1.AppendFormat(_T("%04X "), buf1[i]);
762             }
763             s1 += _T("\r\n");
764
765             s1.AppendFormat(_T(" DeviceType %04X \t "), pinfob->nDeviceTypeVer);
766             s1.AppendFormat(_T(" nProgVer %04X \r\n"), pinfob->nProgVer);
767             s1.AppendFormat(_T(" nKLinkVer %04X \t"), pinfob->nKLinkVer);
768             s1.AppendFormat(_T(" nKBusVer %04X \r\n"), pinfob->nKBusVer);
769             s1.AppendFormat(_T(" nCapacity1 %d k \t"), pinfob->nCapacity1);
770             s1.AppendFormat(_T(" nCapacity2 %d k\r\n"), pinfob->nCapacity2);
771             s1.AppendFormat(_T(" nDInput %d \t"), pinfob->nDInput);
772             s1.AppendFormat(_T(" nDOutput %d \r\n"), pinfob->nDOutput);
773             s1.AppendFormat(_T(" nAInput %d \t"), pinfob->nAInput);
774             s1.AppendFormat(_T(" nAOutput %d \r\n"), pinfob->nAOutput);
775             s1.AppendFormat(_T(" nHInput %d   \t"), pinfob->nHInput);
776             s1.AppendFormat(_T(" nHOutput %d \r\n"), pinfob->nHOutput);
777             s1.AppendFormat(_T(" nExt1 %d \t"), pinfob->nExt1);
778             s1.AppendFormat(_T(" nExt2 %d \r\n"), pinfob->nExt2);
779             s1.AppendFormat(_T(" nLogSize %d \r\n"), pinfob->nLogSize);
780             s1.AppendFormat(_T(" nPorts %d \r\n"), pinfob->nPorts);
781             s1.AppendFormat(_T(" nManSize %d \r\n"), pinfob->nManSize);
782             s1.AppendFormat(_T(" nAbility %d \r\n"), pinfob->nAbility);
783             s1.AppendFormat(_T(" nSwitchBits %d \r\n"), pinfob->nSwitchBits);
784         }
785         SysLog(s1);
786         int len0 = sizeof(stKMSysCfg);
787         UCHAR nType = 0;
788         res2 = theApp.MyKLink1.ReadSysCfgData(1, nType, 0, len0, &len1, buf1);
789         s1.Format(_T("GetSysCfg %d = %d %d \r\n"), len0, res2, len1);
790         if (res2 == KLink1::KL_OK && len1 > 0) {
791             for (int i = 0; i < len1 / 2; i++) {
792                 s1.AppendFormat(_T("%04X "), buf1[i]);
793             }
794             s1 += _T("\r\n");
795             pstKMSysCfg pSysCfg = (pstKMSysCfg)buf1;
796             s1.AppendFormat(_T("Version %04X \r\n"), pSysCfg->Version);
797             s1.AppendFormat(_T("WorkMode %04X \r\n"), pSysCfg->workmode);
798             s1.AppendFormat(_T("SwitchFunc %04X \r\n"), pSysCfg->SwitchFunc);
799             s1.AppendFormat(_T("CfgBlockCount %d \r\n"), pSysCfg->nCfgBlockCount);
800
801             for (int j = 0; j < 2; j++) {
802                 s1.AppendFormat(_T("COM %d \r\n"), j + 1);
803                 s1.AppendFormat(_T("WorkMode %d \r\n"), pSysCfg->PortParams[j].WorkMode);
804                 s1.AppendFormat(_T("Station %d \r\n"), pSysCfg->PortParams[j].Station);
805                 s1.AppendFormat(_T("BaudRate %d \r\n"), pSysCfg->PortParams[j].BaudRate*100);
806                 s1.AppendFormat(_T("ByteSize %d Parity %d StopBit %d \r\n"), pSysCfg->PortParams[j].ByteSize, pSysCfg->PortParams[j].Parity, pSysCfg->PortParams[j].StopBits);
807                 s1.AppendFormat(_T("EndType %d \r\n"), pSysCfg->PortParams[j].EndType);
808                 s1.AppendFormat(_T("EofChar %d \r\n"), pSysCfg->PortParams[j].EofChar);
809                 s1.AppendFormat(_T("SofChar %d \r\n"), pSysCfg->PortParams[j].SofChar);
810                 s1.AppendFormat(_T("EndTime %d \r\n"), pSysCfg->PortParams[j].EndTime);
811                 s1.AppendFormat(_T("RecvAddr %d \r\n"), pSysCfg->PortParams[j].RecvAddr);
812                 s1.AppendFormat(_T("RecvSize %d \r\n"), pSysCfg->PortParams[j].RecvSize);
813             }
814 /*
815             s1.AppendFormat(_T("InputParam \r\n"));
816             for (int j = 0; j < 16; j++) {
817                 s1.AppendFormat(_T("%2d Filter0 %d  Filter1 %d \r\n"),j, pSysCfg->InputParams[j].Filter0, pSysCfg->InputParams[j].Filter1);
818             }
819             s1.AppendFormat(_T("OutputParam \r\n"));
820             for (int j = 0; j < 16; j++) {
821                 s1.AppendFormat(_T("%2d Hold1 %d  Hold2 %d \r\n"),j, pSysCfg->OutputParams[j].Hold1, pSysCfg->OutputParams[j].Hold2);
822             }
823 // */
824             s1.AppendFormat(_T("OutMapping \r\n"));
825             for (int j = 0; j < 8; j++) {
826 //                s1.AppendFormat(_T("%d Map %04X \r\n"),j, pSysCfg->OutMappings[j]);
827                 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);
828             }
829             s1.AppendFormat(_T("CfgBlockCount %d\r\n"), pSysCfg->nCfgBlockCount);
830             for (int j = 0; j < pSysCfg->nCfgBlockCount; j++) {
831                 //                s1.AppendFormat(_T("%d Map %04X \r\n"),j, pSysCfg->OutMappings[j]);
832                 s1.AppendFormat(_T("%d type %X Size %d \r\n"), j, pSysCfg->CfgBlockInfos[j].nBlockType, pSysCfg->CfgBlockInfos[j].nBlockSize);
833             }
834
835         }
836         SysLog(s1);
837     }
838 }
839 static const uint16_t crctalbeabs[] = {
840     0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401,
841     0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400
842 };
843
844 static uint16_t crc16tablefast(uint8_t* ptr, uint16_t len)
845 {
846     uint16_t crc = 0xffff;
847     uint16_t i;
848     uint8_t ch;
849
850     for (i = 0; i < len; i++) {
851         ch = *ptr++;
852         crc = crctalbeabs[(ch ^ crc) & 15] ^ (crc >> 4);
853         crc = crctalbeabs[((ch >> 4) ^ crc) & 15] ^ (crc >> 4);
854     }
855
856     return crc;
857 }
858
859 void CNavView::OnDisConnect()
860 {
861     // TODO: 在此添加命令处理程序代码
862     theApp.MyKLink1.DisConnect();
863     
864 }
865
866 void CNavView::OnMenuNavConnect()
867 {
868     // TODO: 在此添加命令处理程序代码
869     OnConnect();
870 }
871
872 void CNavView::OnMenuNavDisconnect()
873 {
874     // TODO: 在此添加命令处理程序代码
875     OnDisConnect();
876
877 }
878
879 void CNavView::OnMenuNvDeviceReadInfo()
880 {
881     // TODO: 在此添加命令处理程序代码
882     CString s1;
883     s1.Format(_T("读取设备 信息"));
884     SysLog(s1);
885     USHORT len1;
886     USHORT buf1[1024];
887     int r2 = theApp.MyKLink1.GetInfo(1, &len1, buf1);
888     pKMInfoBlock pinfob = (pKMInfoBlock)buf1;
889     if (r2 == 0 && len1 > 0) {
890         for (int i = 0; i < len1 / 2; i++) {
891             s1.AppendFormat(_T("%04X "), buf1[i]);
892         }
893         s1 += _T("\r\n");
894
895         s1.AppendFormat(_T(" DeviceType %04X \t "), pinfob->nDeviceTypeVer);
896         s1.AppendFormat(_T(" nProgVer %04X \r\n"), pinfob->nProgVer);
897     }
898     SysLog(s1);
899     myLogger1.UpdateLogDisplay();
900 }
901
902 void CNavView::OnDeviceConfig()
903 {
904     // TODO: 在此添加命令处理程序代码
905     CString s1;
906     s1.Format(_T("设置参数"));
907     SysLog(s1);
908     USHORT len1;
909     USHORT buf1[1024];
910     int r2 = theApp.MyKLink1.GetInfo(1, &len1, buf1);
911     pKMInfoBlock pinfob = (pKMInfoBlock)buf1;
912     if (r2 == 0 && len1 > 0) {
913         for (int i = 0; i < len1 / 2; i++) {
914             s1.AppendFormat(_T("%04X "), buf1[i]);
915         }
916         s1 += _T("\r\n");
917
918         s1.AppendFormat(_T(" DeviceType %04X \t "), pinfob->nDeviceTypeVer);
919         s1.AppendFormat(_T(" nProgVer %04X \r\n"), pinfob->nProgVer);
920     }
921     SysLog(s1);
922     myLogger1.UpdateLogDisplay();
923
924
925 }
926
927 void CNavView::OnLedBlink()
928 {
929     // TODO: 在此添加命令处理程序代码
930     CString s1;
931     theApp.MyKLink1.BlinkLED(1, 5);    
932 }
933
934 void CNavView::OnResetDevice()
935 {
936     // TODO: 在此添加命令处理程序代码
937     CString s1;
938     int r5 = theApp.MyKLink1.ResetDevice(1, 0);
939     s1.Format(_T("ResetDevice  = %d"), r5);
940     SysLog(s1);
941     for (int i = 0; i < 8; i++) {
942         bPortRegsUpdated[i] = 0;
943     }
944     for (int i = 0; i < 16; i++) {
945         bPortDeviceUpdated[i] = 0;
946     }
947 }
948 void CNavView::OnCommtest()
949 {
950     // TODO: 在此添加命令处理程序代码
951     CString s1;
952
953     /*
954         CView* pView;
955         pView = FindView(RUNTIME_CLASS(CMTerm1CtrlView));
956         if (pView != NULL) {
957             ((CMDIFrameWndEx*)AfxGetMainWnd())->MDIActivate(pView->GetParent());
958             //        pView->SetActiveWindow();
959         }
960         else {
961             s1 = GetTitle();
962             //AfxMessageBox(s1);
963             ASSERT_VALID(theApp.m_pCtrlViewTemplate);
964             CFrameWnd* pFrame = theApp.m_pCtrlViewTemplate->CreateNewFrame(this, NULL);
965             ASSERT_KINDOF(CFrameWnd, pFrame);
966             theApp.m_pCtrlViewTemplate->InitialUpdateFrame(pFrame, this);
967         }
968     */
969     int a = 0;
970 #pragma omp parrelle for
971     for (int i = 0; i < 1000; i++) {
972         a += i;
973     }
974     s1.Format(_T("a = %d "), a);
975     DbgLog(s1);
976
977
978
979
980 }
981
982 void CNavView::OnMenuNvUpdateFirmware()
983 {
984     // TODO: 在此添加命令处理程序代码
985     CString s1;
986     s1.Format(_T("更新设备 固件"));
987     SysLog(s1);
988     USHORT len1;
989     USHORT buf1[1024];
990     int r2 = theApp.MyKLink1.GetInfo(1, &len1, buf1);
991     pKMInfoBlock pinfob = (pKMInfoBlock)buf1;
992     if (r2 == 0 && len1 > 0) {
993         for (int i = 0; i < len1 / 2; i++) {
994             s1.AppendFormat(_T("%04X "), buf1[i]);
995         }
996         s1 += _T("\r\n");
997
998         s1.AppendFormat(_T(" DeviceType %04X \t "), pinfob->nDeviceTypeVer);
999         s1.AppendFormat(_T(" nProgVer %04X \r\n"), pinfob->nProgVer);
1000     }
1001     SysLog(s1);
1002     myLogger1.UpdateLogDisplay();
1003     unsigned char Appbuf[131072];
1004     unsigned char firmwarebuf[262144];
1005
1006     int nBtldrInfoOffset = 256;
1007     int AppOffset = 4096;
1008     int nAppInfoOffset = 256;
1009
1010     int btldrLength = 0;
1011     int AppLength = 0;
1012     int FirmwareSize = 0;
1013
1014     bool btldrfileLoaded = false;
1015     bool appfileLoaded = false;
1016     bool combinefileSaved = false;
1017
1018     unsigned char filebuf[65536];
1019     unsigned int blocksize = 64;
1020     stNewAppInfoBlock NewAppInfo;
1021
1022
1023     CFileDialog dialog1(true);
1024     INT_PTR r = dialog1.DoModal();
1025     if (r == IDOK)
1026     {
1027         CString sFilePathName = dialog1.GetPathName();
1028         //OpenFile
1029         CFile file1;
1030         CFileException e;
1031         bool r = file1.Open(sFilePathName, CFile::modeRead | CFile::typeBinary, &e);
1032         s1.Format(_T("Open File %s = %d"), sFilePathName, r);
1033         DbgLog(s1);
1034         if (r)
1035         {
1036             int fileLen1 = (int)file1.GetLength();
1037             file1.Read(filebuf, fileLen1);
1038             file1.Close();
1039             unsigned int nBlkType = 0;
1040             unsigned int nBlkVer = 0;
1041             s1.Empty();
1042             pAppInfoBlock pAppinfo = (pAppInfoBlock)(filebuf + nAppInfoOffset);
1043             s1.Format(_T("文件信息  \r\n"));
1044 //            SetDlgItemText(IDC_STATIC_APP_INFO, s1);
1045             if (pAppinfo->Hdr.nBlkSign == 0xaa55) {
1046                 s1.Empty();
1047                 s1.Format(_T("文件信息  "));
1048                 // s1.AppendFormat(_T("启动文件 %s \r\n"), sFilePathName);
1049
1050                 s1.AppendFormat(_T("文件大小: %d \r\n"), fileLen1);
1051                 s1.AppendFormat(_T("信息块位置: 0x%04X  信息块大小: %d  \r\n"), nAppInfoOffset, pAppinfo->Hdr.nBlkSize);
1052                 s1.AppendFormat(_T("信息块类型: 0x%04X  "), pAppinfo->Hdr.nBlkTypeVer);
1053                 nBlkType = (pAppinfo->Hdr.nBlkTypeVer) >> 8;
1054                 nBlkVer = pAppinfo->Hdr.nBlkTypeVer & 0xff;
1055                 if (nBlkType == 0x01) { s1.AppendFormat(_T(" (BootLoader 类型1 版本 %d) "), nBlkVer); }
1056                 else if (nBlkType == 0x02) { s1.AppendFormat(_T(" (BootLoader 类型2 版本 %d) "), nBlkVer); }
1057                 else if (nBlkType == 0x03) { s1.AppendFormat(_T(" (App类型1 版本 %d) "), nBlkVer); }
1058                 else if (nBlkType == 0x04) { s1.AppendFormat(_T(" (App类型2 版本 %d) "), nBlkVer); }
1059                 else { s1.AppendFormat(_T(" (未知类型)  ")); }
1060                 s1.AppendFormat(_T("\r\n"));
1061                 s1.AppendFormat(_T("应用区版本: 0x%04X  对应模块类型: 0x%04X\r\n"), pAppinfo->nAppVer, pAppinfo->nAppDevice);
1062                 s1.AppendFormat(_T("应用区大小: 0x%04X  实际代码大小: 0x%04X \r\n"), pAppinfo->nAppSize, pAppinfo->nAppDataSize);
1063                 s1.AppendFormat(_T("应用区地址: 0x%08X (偏移 %d )\r\n"), pAppinfo->nAppStartAddr, pAppinfo->nAppStartAddr - 0x08000000);
1064                 s1.AppendFormat(_T("信息暂存区: 0x%08X \r\n应用暂存区: 0x%08X \r\n"), pAppinfo->nAppStartOffset, pAppinfo->nApp);
1065 //                SetDlgItemText(IDC_STATIC_APP_INFO, s1);
1066
1067                 if (nBlkType == 0x03 || nBlkType == 0x04) {
1068                     memmove(Appbuf, filebuf , fileLen1);
1069                     appfileLoaded = true;
1070                     AppLength = fileLen1;
1071                 }
1072                 else {
1073                     if ((nBlkType == 0x01 || nBlkType == 0x02) && fileLen1 >= AppOffset + nAppInfoOffset + sizeof(stAppInfoBlock))
1074                     {
1075                         pAppInfoBlock pAppinfo2 = (pAppInfoBlock)(filebuf + AppOffset + nAppInfoOffset);
1076                         if ((pAppinfo2->Hdr.nBlkSign == 0xaa55) && pAppinfo2->Hdr.nBlkTypeVer == 0x0301) {
1077                             s1.Format(_T("文件是bootloader和App合成的文件 , 是否从中提取固件并继续"));
1078                             int r3 = AfxMessageBox(s1, MB_YESNO);
1079                             if (r3 == IDYES) {
1080                                 memmove(Appbuf, filebuf + AppOffset, fileLen1 - AppOffset);
1081                                 appfileLoaded = true;
1082                                 AppLength = fileLen1 - AppOffset;
1083                             }
1084                         }
1085                         else {
1086                             s1.Format(_T("文件 不是App文件 , 是否继续 "));
1087                             int r3 = AfxMessageBox(s1, MB_YESNO);
1088                             if (r3 == IDYES) {
1089                                 memmove(Appbuf, filebuf , fileLen1 );
1090                                 appfileLoaded = true;
1091                                 AppLength = fileLen1;
1092                             }
1093                         }
1094                     }
1095                     else {
1096                         s1.Format(_T("文件 不是App文件 , 是否继续 "));
1097                         int r3 = AfxMessageBox(s1, MB_YESNO);
1098                         if (r3 == IDYES) {
1099                             memmove(Appbuf, filebuf , fileLen1 );
1100                             appfileLoaded = true;
1101                             AppLength = fileLen1;
1102                         }
1103                     }
1104                 }
1105                 //            s1.Format(_T("  BootLoader启动文件  版本 %04X 硬件类型 %04X 占用空间 %04X,  %d Bytes\r\n 是否继续"), pinfob->nBtldrVer, pinfob->nBtldrDevice, pinfob->nBtldrSize, fileLen1);
1106             }
1107             else {
1108                 s1.Format(_T("文件中未找到信息块, 文件大小  %d Bytes\r\n 是否继续"), fileLen1);
1109                 int r3 = AfxMessageBox(s1, MB_YESNO);
1110                 if (r3 == IDYES) {
1111                     memmove(Appbuf, filebuf , fileLen1 );
1112                     appfileLoaded = true;
1113                     AppLength = fileLen1;
1114                 }
1115             }
1116
1117
1118
1119             unsigned short crc2 = crc16tablefast(Appbuf, AppLength);
1120             unsigned char* buf2 = Appbuf + 0x1000; ;
1121             //s1.Format(_T("GetInfo From File  = %d %d \r\n"), j, fileLen1);
1122
1123             pKMInfoBlock pinfof = (pKMInfoBlock)buf2;
1124
1125
1126             s1.Format(_T(" 目标模块 类型 %04X 版本 %d.%02d, \r\n 固件文件 类型 %04X 版本 %d.%02d,  %d Bytes\r\n 是否继续"), 
1127                 pinfob->nDeviceTypeVer, pinfob->nProgVerMajor,pinfob->nProgVerMinor, 
1128                 pinfof->nDeviceTypeVer, pinfof->nProgVerMajor,pinfof->nProgVerMinor, fileLen1);
1129             int r3 = AfxMessageBox(s1, MB_YESNO);
1130             if (r3 == IDYES) {
1131                 int bError = 0;
1132                 for (int i = 0; i < AppLength; i += blocksize)
1133                 {
1134                     int sendsize = AppLength - i;
1135                     if (sendsize > blocksize) { sendsize = blocksize; }
1136                     int k = 999;
1137                     int j = 0;
1138                     double time1 = GetTimemS();
1139                     for (j = 0; j < 5 && k != 0; j++) {
1140                         k = theApp.MyKLink1.WriteFirmware(1, 0, i, sendsize, Appbuf + i);
1141                         if (k) {
1142                             double time2 = GetTimemS();
1143                             s1.Format(_T("Wr@ %x %dB = %d  re %d %.2fmS %s "), i, sendsize, k, j, time2 - time1, theApp.MyKLink1.m_resultStr);
1144                             SysLog(s1);
1145                             myLogger1.UpdateLogDisplay();
1146                             Sleep(30);
1147                         }
1148                     }
1149                     double time2 = GetTimemS();
1150                     s1.Format(_T("Wr@ %x %dB = %d  re %d %.2fmS"), i, sendsize, k, j, time2 - time1);
1151                     SysLog(s1);
1152                     myLogger1.UpdateLogDisplay();
1153                     if (k) {   // 出现错误; 
1154                         bError = 1;
1155                         CString sErr;
1156                         sErr = KLink1::GetErrDescStr(k);
1157                         s1.Format(_T("下载出错 %d, %s"), k, sErr);
1158                         int r3 = AfxMessageBox(s1);
1159                         break;  //跳出 
1160                     }
1161                 }
1162                 if (bError == 0) {
1163                     NewAppInfo.Length = AppLength;
1164                     NewAppInfo.Sign = 0x55AA;
1165                     NewAppInfo.Version = 0x109;
1166                     NewAppInfo.nCRC = crc2;
1167
1168                     int k = theApp.MyKLink1.WriteFirmInfo(1, 0, 0, sizeof(NewAppInfo), (UCHAR*)&NewAppInfo);
1169                     s1.Format(_T("Write InfoBlock %d bytes = %d"), sizeof(NewAppInfo), k);
1170                     SysLog(s1);
1171                     Sleep(100);
1172                     int r5 = theApp.MyKLink1.ResetDevice(1, 0);
1173                     s1.Format(_T("ResetDevice  = %d"), r5);
1174                     SysLog(s1);
1175                     myLogger1.UpdateLogDisplay();
1176                     Sleep(2000);
1177                 }
1178                 theApp.MyKLink1.DisConnect();;
1179                 Sleep(1000);
1180                 OnMenuNavConnect();
1181
1182             }
1183         }
1184     }
1185 }
1186
1187
1188 void CNavView::OnRemoteBlinkLedAll()
1189 {
1190     // TODO: 在此添加命令处理程序代码
1191     CString s1;
1192     int n = m_NavViewTree.GetSelectedCount();
1193
1194     HTREEITEM hItem1 = nullptr;
1195     DWORD_PTR itemData = 0;
1196     if (n == 1) {
1197         hItem1 = m_NavViewTree.GetSelectedItem();
1198         itemData = m_NavViewTree.GetItemData(hItem1);
1199     }
1200     s1.Format(_T("Selected Count %d  Data %08X"), n, itemData);
1201     SysLog(s1);
1202     int nDst = 1;
1203     int nPort = 1;
1204     int nTime = 5;
1205     if (itemData & MENU_PORT) {
1206         int nChildId = 0xff;
1207         theApp.MyKLink1.RunRemoteReq(nDst, nPort, nChildId, ReqBlinkLED, nTime, 0, 0, 0);
1208         s1.Format(_T("Remote BlinkLED Port %d Child %d Timer %d"), nPort, nChildId, nTime);
1209         SysLog(s1);
1210     }
1211 }
1212
1213
1214 void CNavView::OnPortReset()
1215 {
1216     // TODO: 在此添加命令处理程序代码
1217 }
1218
1219
1220 void CNavView::OnPortResetAllChild()
1221 {
1222     // TODO: 在此添加命令处理程序代码
1223     CString s1;
1224     int n = m_NavViewTree.GetSelectedCount();
1225
1226     HTREEITEM hItem1 = nullptr;
1227     DWORD_PTR itemData = 0;
1228     if (n == 1) {
1229         hItem1 = m_NavViewTree.GetSelectedItem();
1230         itemData = m_NavViewTree.GetItemData(hItem1);
1231     }
1232     s1.Format(_T("Selected Count %d  Data %08X"), n, itemData);
1233     SysLog(s1);
1234     int nDst = 1;
1235     int nPort = 1;
1236     int nTime = 5;
1237     if (itemData & MENU_PORT) {
1238         int nChildId =  0xff;
1239         theApp.MyKLink1.RunRemoteReq(nDst, nPort, nChildId, ReqReset, nTime, 0, 0, 0);
1240         s1.Format(_T("Remote Reset Port %d Child %d Timer %d"), nPort, nChildId, nTime);
1241         SysLog(s1);
1242     }
1243 }
1244
1245
1246 void CNavView::OnRemoteRefreshData()
1247 {
1248     // TODO: 在此添加命令处理程序代码
1249     CString s1;
1250     int n = m_NavViewTree.GetSelectedCount();
1251
1252     HTREEITEM hItem1 = nullptr;
1253     DWORD_PTR itemData =0;
1254     if (n == 1) {
1255         hItem1 = m_NavViewTree.GetSelectedItem();
1256         itemData = m_NavViewTree.GetItemData(hItem1);
1257     }
1258     s1.Format(_T("Selected Count %d  Data %08X"), n, itemData);
1259     SysLog(s1);
1260 }
1261
1262
1263 void CNavView::OnRemoteBlinkLED()
1264 {
1265     // TODO: 在此添加命令处理程序代码
1266     CString s1;
1267     int n = m_NavViewTree.GetSelectedCount();
1268
1269     HTREEITEM hItem1 = nullptr;
1270     DWORD_PTR itemData = 0;
1271     if (n == 1) {
1272         hItem1 = m_NavViewTree.GetSelectedItem();
1273         itemData = m_NavViewTree.GetItemData(hItem1);
1274     }
1275     s1.Format(_T("Selected Count %d  Data %08X"), n, itemData);
1276     SysLog(s1);
1277     int nDst = 1;
1278     int nPort = 1;
1279     int nTime = 5;
1280     if (itemData & MENU_REMOTE_DEVICE) {
1281         int nChildId = itemData & 0xff;
1282         theApp.MyKLink1.RunRemoteReq(nDst, nPort, nChildId, ReqBlinkLED, nTime, 0, 0, 0);
1283         s1.Format(_T("Remote BlinkLED Port %d Child %d Timer %d"), nPort, nChildId,nTime);
1284         SysLog(s1);
1285     }
1286 }
1287
1288 void CNavView::OnRemoteDeviceReset()
1289 {
1290     // TODO: 在此添加命令处理程序代码
1291     CString s1;
1292     int n = m_NavViewTree.GetSelectedCount();
1293
1294     HTREEITEM hItem1 = nullptr;
1295     DWORD_PTR itemData = 0;
1296     if (n == 1) {
1297         hItem1 = m_NavViewTree.GetSelectedItem();
1298         itemData = m_NavViewTree.GetItemData(hItem1);
1299     }
1300     s1.Format(_T("Selected Count %d  Data %08X"), n, itemData);
1301     SysLog(s1);
1302     int nDst = 1;
1303     int nPort = 1;
1304     int nTime = 5;
1305     if (itemData & MENU_REMOTE_DEVICE) {
1306         int nChildId = itemData & 0xff;
1307         theApp.MyKLink1.RunRemoteReq(nDst, nPort, nChildId, ReqReset, nTime, 0, 0, 0);
1308         s1.Format(_T("Remote Reset Port %d Child %d Timer %d"), nPort, nChildId, nTime);
1309         SysLog(s1);
1310     }
1311     
1312 }
1313
1314 void CNavView::OnRemoteUpdateFirmware()
1315 {
1316     // TODO: 在此添加命令处理程序代码
1317     CString s1;
1318     int n = m_NavViewTree.GetSelectedCount();
1319
1320     HTREEITEM hItem1 = nullptr;
1321     DWORD_PTR itemData = 0;
1322     if (n == 1) {
1323         hItem1 = m_NavViewTree.GetSelectedItem();
1324         itemData = m_NavViewTree.GetItemData(hItem1);
1325     }
1326     s1.Format(_T("Selected Count %d  Data %08X"), n, itemData);
1327     SysLog(s1);
1328     int nDst = 1;
1329     int nPort = 1;
1330     int nChildId = 0;
1331     if (itemData & MENU_REMOTE_DEVICE) {
1332         nChildId = itemData & 0xff;
1333     }
1334     else { return; }
1335
1336 //    CString s1;
1337     s1.Format(_T("更新远程设备 固件"));
1338     SysLog(s1);
1339     USHORT len1 = 0;
1340     USHORT buf1[1024];
1341     int r2 = 0;
1342     stDeviceInfo* ptheDeviceInfo =&PortDeviceInfos[nChildId];
1343     int res2 = theApp.MyKLink1.GetPortChildInfo(1, nPort, nChildId , sizeof(stDeviceInfo), &len1, buf1);  //sizeof(stDeviceInfo)
1344     if (res2 == KLink1::KL_OK) {
1345         s1.Format(_T("GetChildInfo %d %d = %d B\r\n"), nPort, nChildId , len1);
1346 //        for (int k = 0; k < len1; k++) {
1347 //            s1.AppendFormat(_T("%02X "), ((unsigned char*)buf1)[k]);
1348 //        }
1349         SysLog(s1);
1350         stDeviceInfo* ptheInfo = (stDeviceInfo*)buf1;
1351 //        PortDeviceInfos[nChildId] = *ptheInfo;
1352         s1.Format(_T("子%d (%04X)%s V%d.%02d %d/%d "),
1353             nChildId, ptheInfo->DeviceType, DeviceTypeToStr(ptheInfo->DeviceType), ptheInfo->DeviceVerMajor, ptheInfo->DeviceVerMinor, ptheInfo->InBitCount, ptheInfo->OutBitCount);
1354         SysLog(s1);
1355     }
1356     myLogger1.UpdateLogDisplay();
1357
1358     unsigned char Appbuf[131072];
1359     unsigned char firmwarebuf[262144];
1360
1361     int nBtldrInfoOffset = 256;
1362     int AppOffset = 4096;
1363     int nAppInfoOffset = 256;
1364
1365     int btldrLength = 0;
1366     int AppLength = 0;
1367     int FirmwareSize = 0;
1368
1369     bool btldrfileLoaded = false;
1370     bool appfileLoaded = false;
1371     bool combinefileSaved = false;
1372
1373     unsigned char filebuf[65536];
1374     unsigned int blocksize = 32;
1375     stNewAppInfoBlock NewAppInfo;
1376
1377
1378     CFileDialog dialog1(true);
1379     INT_PTR r = dialog1.DoModal();
1380     if (r != IDOK) { return; }
1381     CString sFilePathName = dialog1.GetPathName();
1382     //OpenFile
1383     CFile file1;
1384     CFileException e;
1385     bool r3 = file1.Open(sFilePathName, CFile::modeRead | CFile::typeBinary, &e);
1386     s1.Format(_T("Open File %s = %d"), sFilePathName, r);
1387     DbgLog(s1);
1388     if (!r3) {
1389         
1390         return;
1391     }
1392     int fileLen2 = (int)file1.GetLength();
1393     file1.Read(filebuf, fileLen2);
1394     file1.Close();
1395     unsigned int nBlkType = 0;
1396     unsigned int nBlkVer = 0;
1397     s1.Empty();
1398     pAppInfoBlock pAppinfo = (pAppInfoBlock)(filebuf + nAppInfoOffset);
1399     s1.Format(_T("文件信息  \r\n"));
1400     //            SetDlgItemText(IDC_STATIC_APP_INFO, s1);
1401     if (pAppinfo->Hdr.nBlkSign == 0xaa55) {
1402         s1.Empty();
1403         s1.Format(_T("文件信息  "));
1404         // s1.AppendFormat(_T("启动文件 %s \r\n"), sFilePathName);
1405
1406         s1.AppendFormat(_T("文件大小: %d \r\n"), fileLen2);
1407         s1.AppendFormat(_T("信息块位置: 0x%04X  信息块大小: %d  \r\n"), nAppInfoOffset, pAppinfo->Hdr.nBlkSize);
1408         s1.AppendFormat(_T("信息块类型: 0x%04X  "), pAppinfo->Hdr.nBlkTypeVer);
1409         nBlkType = (pAppinfo->Hdr.nBlkTypeVer) >> 8;
1410         nBlkVer = pAppinfo->Hdr.nBlkTypeVer & 0xff;
1411         if (nBlkType == 0x01) { s1.AppendFormat(_T(" (BootLoader 类型1 版本 %d) "), nBlkVer); }
1412         else if (nBlkType == 0x02) { s1.AppendFormat(_T(" (BootLoader 类型2 版本 %d) "), nBlkVer); }
1413         else if (nBlkType == 0x03) { s1.AppendFormat(_T(" (App类型1 版本 %d) "), nBlkVer); }
1414         else if (nBlkType == 0x04) { s1.AppendFormat(_T(" (App类型2 版本 %d) "), nBlkVer); }
1415         else { s1.AppendFormat(_T(" (未知类型)  ")); }
1416         s1.AppendFormat(_T("\r\n"));
1417         s1.AppendFormat(_T("应用区版本: 0x%04X  对应模块类型: 0x%04X\r\n"), pAppinfo->nAppVer, pAppinfo->nAppDevice);
1418         s1.AppendFormat(_T("应用区大小: 0x%04X  实际代码大小: 0x%04X \r\n"), pAppinfo->nAppSize, pAppinfo->nAppDataSize);
1419         s1.AppendFormat(_T("应用区地址: 0x%08X (偏移 %d )\r\n"), pAppinfo->nAppStartAddr, pAppinfo->nAppStartAddr - 0x08000000);
1420         s1.AppendFormat(_T("信息暂存区: 0x%08X \r\n应用暂存区: 0x%08X \r\n"), pAppinfo->nAppStartOffset, pAppinfo->nApp);
1421         //                SetDlgItemText(IDC_STATIC_APP_INFO, s1);
1422
1423         if (nBlkType == 0x03 || nBlkType == 0x04) {
1424             memmove(Appbuf, filebuf, fileLen2);
1425             appfileLoaded = true;
1426             AppLength = fileLen2;
1427         }
1428         else {
1429             if ((nBlkType == 0x01 || nBlkType == 0x02) && fileLen2 >= AppOffset + nAppInfoOffset + sizeof(stAppInfoBlock))
1430             {
1431                 pAppInfoBlock pAppinfo2 = (pAppInfoBlock)(filebuf + AppOffset + nAppInfoOffset);
1432                 if ((pAppinfo2->Hdr.nBlkSign == 0xaa55) && pAppinfo2->Hdr.nBlkTypeVer == 0x0301) {
1433                     s1.Format(_T("文件是bootloader和App合成的文件 , 是否从中提取固件并继续"));
1434                     int r3 = AfxMessageBox(s1, MB_YESNO);
1435                     if (r3 == IDYES) {
1436                         memmove(Appbuf, filebuf + AppOffset, fileLen2 - AppOffset);
1437                         appfileLoaded = true;
1438                         AppLength = fileLen2 - AppOffset;
1439                     }
1440                 }
1441                 else {
1442                     s1.Format(_T("文件 不是App文件 , 是否继续 "));
1443                     int r3 = AfxMessageBox(s1, MB_YESNO);
1444                     if (r3 == IDYES) {
1445                         memmove(Appbuf, filebuf, fileLen2);
1446                         appfileLoaded = true;
1447                         AppLength = fileLen2;
1448                     }
1449                 }
1450             }
1451             else {
1452                 s1.Format(_T("文件 不是App文件 , 是否继续 "));
1453                 int r3 = AfxMessageBox(s1, MB_YESNO);
1454                 if (r3 == IDYES) {
1455                     memmove(Appbuf, filebuf, fileLen2);
1456                     appfileLoaded = true;
1457                     AppLength = fileLen2;
1458                 }
1459             }
1460         }
1461         //            s1.Format(_T("  BootLoader启动文件  版本 %04X 硬件类型 %04X 占用空间 %04X,  %d Bytes\r\n 是否继续"), pinfob->nBtldrVer, pinfob->nBtldrDevice, pinfob->nBtldrSize, fileLen1);
1462     }
1463     else {
1464         s1.Format(_T("文件中未找到信息块, 文件大小  %d Bytes\r\n 是否继续"), fileLen2);
1465         int r3 = AfxMessageBox(s1, MB_YESNO);
1466         if (r3 == IDYES) {
1467             memmove(Appbuf, filebuf, fileLen2);
1468             appfileLoaded = true;
1469             AppLength = fileLen2;
1470         }
1471     }
1472
1473
1474     unsigned short crc2 = crc16tablefast(Appbuf, AppLength);
1475     unsigned char* buf2 = Appbuf + 0x1000; ;
1476     //s1.Format(_T("GetInfo From File  = %d %d \r\n"), j, fileLen1);
1477
1478     pKMInfoBlock pinfof = (pKMInfoBlock)buf2;
1479
1480
1481     s1.Format(_T(" 目标模块 类型 %04X 版本 %d.%02d, \r\n 固件文件 类型 %04X 版本 %d.%02d,  %d Bytes\r\n 是否继续"), 
1482         ptheDeviceInfo->DeviceType, ptheDeviceInfo->DeviceVerMajor,ptheDeviceInfo->DeviceVerMinor, 
1483         pinfof->nDeviceTypeVer, pinfof->nProgVerMajor,pinfof->nProgVerMinor, fileLen2);
1484     int r4 = AfxMessageBox(s1, MB_YESNO);
1485     if (r4 != IDYES) { return; }
1486
1487     {
1488         int bError = 0;
1489         for (int i = 0; i < AppLength; i += blocksize)
1490         {
1491             int sendsize = AppLength - i;
1492             if (sendsize > blocksize) { sendsize = blocksize; }
1493             int k = 999;
1494             int j = 0;
1495             double time1 = GetTimemS();
1496             
1497             for (j = 0; j < 5 && k != 0; j++) {
1498                 k = theApp.MyKLink1.RunRemoteReq(nDst, nPort,nChildId, ReqUpdateFirm,0,i, sendsize, Appbuf + i);
1499                 Sleep(10);
1500                 if (k) {
1501                     double time2 = GetTimemS();
1502                     s1.Format(_T("Wr@ %x %dB = %d  re %d %.2fmS %s "), i, sendsize, k, j, time2 - time1, theApp.MyKLink1.m_resultStr);
1503                     SysLog(s1);
1504                     myLogger1.UpdateLogDisplay();
1505                     Sleep(30);
1506                 }
1507             }
1508             double time2 = GetTimemS();
1509             s1.Format(_T("Wr@ %x %dB = %d  re %d %.2fmS"), i, sendsize, k, j, time2 - time1);
1510             SysLog(s1);
1511             myLogger1.UpdateLogDisplay();
1512             if (k) {   // 出现错误; 
1513                 bError = 1;
1514                 CString sErr;
1515                 sErr = KLink1::GetErrDescStr(k);
1516                 s1.Format(_T("下载出错 %d, %s"), k, sErr);
1517                 int r3 = AfxMessageBox(s1);
1518                 break;  //跳出 
1519             }
1520         }
1521         if (bError == 0) {
1522             NewAppInfo.Length = AppLength;
1523             NewAppInfo.Sign = 0x55AA;
1524             NewAppInfo.Version = 0x109;
1525             NewAppInfo.nCRC = crc2;
1526
1527             int k = theApp.MyKLink1.RunRemoteReq(nDst, nPort, nChildId, ReqUpdateFirmInfo, 0, 0,sizeof(NewAppInfo), (UCHAR*)&NewAppInfo);
1528             s1.Format(_T("Write InfoBlock %d bytes = %d"), sizeof(NewAppInfo), k);
1529             SysLog(s1);
1530             Sleep(100);
1531             int r5 = theApp.MyKLink1.RunRemoteReq(nDst, nPort, nChildId, ReqReset, 0);
1532             s1.Format(_T("ResetRemoteDevice Port %d Child %d = %d"), nPort, nChildId, r5);
1533             SysLog(s1);
1534 /*
1535             int r6 = theApp.MyKLink1.ResetDevice(nDst, 0);
1536             s1.Format(_T("ResetDevice  = %d"), r6);
1537             SysLog(s1);
1538             for (int i = 0; i < 8; i++) {
1539                 bPortRegsUpdated[i] = 0;
1540             }
1541             for (int i = 0; i < 16; i++) {
1542                 bPortDeviceUpdated[i] = 0;
1543             }
1544             myLogger1.UpdateLogDisplay();
1545 */
1546         //    Sleep(2000);
1547         }
1548     //    theApp.MyKLink1.DisConnect();;
1549     //    OnMenuNavConnect();
1550     }
1551 }
418cb3 1552