Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2003537
  • 博文数量: 356
  • 博客积分: 8284
  • 博客等级: 中将
  • 技术积分: 4580
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-15 20:25
个人简介

天行健,君子以自强不息

文章分类

全部博文(356)

文章存档

2018年(1)

2016年(4)

2015年(13)

2014年(14)

2013年(2)

2012年(25)

2011年(43)

2010年(65)

2009年(189)

分类: C/C++

2015-12-19 19:13:01


点击(此处)折叠或打开

  1. // testDlg.cpp : implementation file
  2. //

  3. #include "stdafx.h"
  4. #include "afxmt.h"
  5. #include "test.h"
  6. #include "testDlg.h"
  7. #include "ControlCAN.h"
  8. #include "crc32.h"
  9. #include "windows.h"


  10. #define mhtonl(x)    ( (x&0x000000ff)<<24 ) |( (x&0x0000ff00)<<8 ) | ( ( x&0x00ff0000)>>8 ) | ( (x&0xff000000)>>24 )

  11. #define     AI_BD_BIG            0x051eU //大信号对应的理论输出值4.8ma    
  12. #define     AI_BD_SMALL            0x6147U    //小信号对应的理论输出值19.2ma

  13. CEvent CTestDlg::m_evtSearch(FALSE,FALSE);
  14. CEvent CTestDlg::m_evtSearchCancel(FALSE,FALSE);
  15. CEvent CTestDlg::m_evtBD(FALSE,FALSE);
  16. CEvent CTestDlg::m_evtCancelBD(FALSE,FALSE);
  17. CEvent CTestDlg::m_evtReadKB(FALSE,FALSE);
  18. CEvent CTestDlg::m_evtWriteKB(FALSE,FALSE);

  19. CEvent CTestDlg::m_evtUpdateDisplay(FALSE,FALSE);

  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif

  25. //数据类型定义
  26. typedef unsigned int    uint32;
  27. typedef unsigned char    uint8;
  28. typedef int                int32;
  29. typedef char            int8;
  30. void OnRegWrite();
  31. void OnRegRead();
  32. void delayus(long int microsecond);//以微秒延时函数

  33. //类中的静态成员变量的定义,必须加
  34. int        CTestDlg::m_cannum;
  35. DWORD    CTestDlg::m_devind;
  36. int        CTestDlg::m_devtype;
  37. int        CTestDlg::m_connect;

  38. S_NODE    g_node;

  39. CEvent CTestDlg::m_evtUpdate(FALSE,FALSE);
  40. //DWORD WINAPI CanSendBinFile(LPVOID lpparameter);

  41. /////////////////////////////////////////////////////////////////////////////
  42. // CAboutDlg dialog used for App About

  43. class CAboutDlg : public CDialog
  44. {
  45. public:
  46.     CAboutDlg();

  47. // Dialog Data
  48.     //{{AFX_DATA(CAboutDlg)
  49.     enum { IDD = IDD_ABOUTBOX };
  50.     //}}AFX_DATA

  51.     // ClassWizard generated virtual function overrides
  52.     //{{AFX_VIRTUAL(CAboutDlg)
  53.     protected:
  54.     virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  55.     //}}AFX_VIRTUAL

  56. // Implementation
  57. protected:
  58.     //{{AFX_MSG(CAboutDlg)
  59.     //}}AFX_MSG
  60.     DECLARE_MESSAGE_MAP()
  61. };

  62. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  63. {
  64.     //{{AFX_DATA_INIT(CAboutDlg)
  65.     //}}AFX_DATA_INIT
  66. }

  67. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  68. {
  69.     CDialog::DoDataExchange(pDX);
  70.     //{{AFX_DATA_MAP(CAboutDlg)
  71.     //}}AFX_DATA_MAP
  72. }

  73. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  74.     //{{AFX_MSG_MAP(CAboutDlg)
  75.         // No message handlers
  76.     //}}AFX_MSG_MAP
  77. END_MESSAGE_MAP()

  78. /////////////////////////////////////////////////////////////////////////////
  79. // CTestDlg dialog

  80. CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
  81.     : CDialog(CTestDlg::IDD, pParent)
  82. {
  83.     //{{AFX_DATA_INIT(CTestDlg)
  84.     m_EditSendData = _T("");
  85.     m_EditSendFrmID = _T("");
  86.     m_EditCode = _T("");
  87.     m_EditMask = _T("");
  88.     m_EditTiming0 = _T("");
  89.     m_EditTiming1 = _T("");
  90.     //m_show = 0;
  91.     //}}AFX_DATA_INIT
  92.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  93.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  94.     m_connect=0;
  95.     m_cannum=0;
  96.     m_devtype=VCI_USBCAN1;
  97. }

  98. void CTestDlg::DoDataExchange(CDataExchange* pDX)
  99. {
  100.     CDialog::DoDataExchange(pDX);
  101.     //{{AFX_DATA_MAP(CTestDlg)
  102.     DDX_Control(pDX, IDC_BUTTON_IOBD, m_ButtonIOBD);
  103.     DDX_Control(pDX, IDC_STATIC_NODEID, m_StaticNodeID);
  104.     DDX_Control(pDX, IDC_BUTTON_SEARCH, m_ButtonSearch);
  105.     DDX_Control(pDX, IDC_LIST_BD, m_Grid);
  106.     DDX_Control(pDX, IDC_COMBO_CANBAUD, m_CanBaud);
  107.     DDX_Control(pDX, IDC_COMBO_MODE, m_ComboMode);
  108.     DDX_Control(pDX, IDC_COMBO_FILTERTYPE, m_ComboFilterType);
  109.     DDX_Control(pDX, IDC_COMBO_CANIND, m_ComboCANInd);
  110.     DDX_Control(pDX, IDC_COMBO_INDEX, m_ComboIndex);
  111.     DDX_Control(pDX, IDC_LIST_INFO, m_ListInfo);
  112.     DDX_Control(pDX, IDC_COMBO_SENDTYPE, m_ComboSendType);
  113.     DDX_Control(pDX, IDC_COMBO_SENDFRAMETYPE, m_ComboSendFrmType);
  114.     DDX_Control(pDX, IDC_COMBO_SENDFRAMEFORMAT, m_ComboSendFrmFmt);
  115.     DDX_Text(pDX, IDC_EDIT_SENDDATA, m_EditSendData);
  116.     DDX_Text(pDX, IDC_EDIT_SENDFRAMEID, m_EditSendFrmID);
  117.     DDX_Text(pDX, IDC_EDIT_CODE, m_EditCode);
  118.     DDX_Text(pDX, IDC_EDIT_MASK, m_EditMask);
  119.     DDX_Text(pDX, IDC_EDIT_TIMING0, m_EditTiming0);
  120.     DDX_Text(pDX, IDC_EDIT_TIMING1, m_EditTiming1);
  121.     //}}AFX_DATA_MAP
  122. }

  123. BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
  124.     //{{AFX_MSG_MAP(CTestDlg)
  125.     ON_WM_SYSCOMMAND()
  126.     ON_WM_PAINT()
  127.     ON_WM_QUERYDRAGICON()
  128.     ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
  129.     ON_BN_CLICKED(IDC_BUTTON_STARTCAN, OnButtonStartcan)
  130.     ON_BN_CLICKED(IDC_BUTTON_RESETCAN, OnButtonResetcan)
  131.     ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
  132.     ON_COMMAND(ID_MENU_REFRESH, OnMenuRefresh)
  133.     ON_CBN_EDITCHANGE(IDC_COMBO_CANBAUD, OnEditchangeComboCanbaud)
  134.     ON_BN_CLICKED(IDC_BUTTON_OPENFILE, OnButtonOpenfile)
  135.     ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
  136.     ON_MESSAGE(ON_WM_SEND,OnSendFile)
  137.     ON_BN_CLICKED(IDC_BUTTON_SEARCH, OnButtonSearch)
  138.     ON_BN_CLICKED(IDC_BUTTON_WRITE, OnButtonWrite)
  139.     ON_BN_CLICKED(IDC_BUTTON_CALC, OnButtonCalc)
  140.     ON_BN_CLICKED(IDC_BUTTON_BDBIG, OnButtonBdbig)
  141.     ON_BN_CLICKED(IDC_BUTTON_BDSMALL, OnButtonBdsmall)
  142.     ON_BN_CLICKED(IDC_BUTTON_IOBD, OnButtonIobd)
  143.     ON_NOTIFY(NM_CLICK, IDC_LIST_BD, OnClickListBd)
  144.     //}}AFX_MSG_MAP
  145. END_MESSAGE_MAP()

  146. /////////////////////////////////////////////////////////////////////////////
  147. // CTestDlg message handlers

  148. BOOL CTestDlg::OnInitDialog()
  149. {
  150.     CDialog::OnInitDialog();

  151.     // Add "About..." menu item to system menu.

  152.     // IDM_ABOUTBOX must be in the system command range.
  153.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  154.     ASSERT(IDM_ABOUTBOX < 0xF000);

  155.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  156.     if (pSysMenu != NULL)
  157.     {
  158.         CString strAboutMenu;
  159.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  160.         if (!strAboutMenu.IsEmpty())
  161.         {
  162.             pSysMenu->AppendMenu(MF_SEPARATOR);
  163.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  164.         }
  165.     }

  166.     // Set the icon for this dialog. The framework does this automatically
  167.     // when the application's main window is not a dialog
  168.     SetIcon(m_hIcon, TRUE);            // Set big icon
  169.     SetIcon(m_hIcon, FALSE);        // Set small icon

  170.     // TODO: Add extra initialization here
  171.     m_ComboSendType.SetCurSel(2);
  172.     m_ComboSendFrmType.SetCurSel(1);
  173.     m_ComboSendFrmFmt.SetCurSel(0);
  174.     
  175.     m_EditSendFrmID="00000080";
  176.     m_EditSendData="01 02 03 04 05 06 07 08 ";

  177.     DWORD dwid;
  178.     m_hSendThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)SendThreadProc,this,0,&dwid);

  179.     HANDLE hthread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)DisplayThreadProc,this,0,&dwid);

  180.     CString str;
  181.     
  182.     for(int i=0;i<8;i++)
  183.     {
  184.         str.Format("%d",i);
  185.         m_ComboIndex.AddString(str);
  186.     }
  187.     for(i=0;i<1;i++)
  188.     {
  189.         str.Format("%d",i);
  190.         m_ComboCANInd.AddString(str);
  191.     }
  192.     
  193.     m_EditCode="00000000";
  194.     m_EditMask="ffffffff";
  195.     m_EditTiming0="01";//can baud set timer0
  196.     m_EditTiming1="1c";//can baud set timer1
  197.     m_ComboIndex.SetCurSel(0);
  198.     m_ComboCANInd.SetCurSel(0);
  199.     m_ComboFilterType.SetCurSel(0);
  200.     m_ComboMode.SetCurSel(0);
  201.     
  202.     //添加combobox的每条内容
  203.     ((CComboBox *)GetDlgItem(IDC_COMBO_CANBAUD))->InsertString(0,"请选择");
  204.     ((CComboBox *)GetDlgItem(IDC_COMBO_CANBAUD))->InsertString(1,"50");
  205.     ((CComboBox *)GetDlgItem(IDC_COMBO_CANBAUD))->InsertString(2,"100");
  206.     ((CComboBox *)GetDlgItem(IDC_COMBO_CANBAUD))->InsertString(3,"125");
  207.     ((CComboBox *)GetDlgItem(IDC_COMBO_CANBAUD))->InsertString(4,"250");
  208.     ((CComboBox *)GetDlgItem(IDC_COMBO_CANBAUD))->InsertString(5,"500");
  209.     ((CComboBox *)GetDlgItem(IDC_COMBO_CANBAUD))->InsertString(6,"800");
  210.     ((CComboBox *)GetDlgItem(IDC_COMBO_CANBAUD))->InsertString(7,"1000");
  211.     //设置combobox当前显示的条目
  212.     ((CComboBox *)GetDlgItem(IDC_COMBO_CANBAUD))->SetCurSel(3);

  213.     InitGrid();//初始化控制列表框
  214.     UpdateData(false);
  215.     InitializeCriticalSection(&m_Section);
  216.     
  217.     return TRUE; // return TRUE unless you set the focus to a control
  218. }
  219. UINT CTestDlg::SendThreadProc(LPVOID lpRarameter)
  220. {
  221.     CTestDlg *dlg=(CTestDlg *)lpRarameter;
  222.     int ret;
  223.     while (TRUE)
  224.     {
  225.         ret=WaitForSingleObject(CTestDlg::m_evtUpdate.m_hObject,INFINITE);
  226.         if (ret==WAIT_OBJECT_0)
  227.         {
  228.             
  229.             dlg->OnFileRead();
  230.         }
  231.         Sleep(10);
  232.                 
  233.     }
  234. }

  235. void CTestDlg::OnSendFile(WPARAM wParam,LPARAM lPram)
  236. {
  237.     OnFileRead();
  238. }
  239. DWORD WINAPI CanSendBinFile(LPVOID lpparameter)
  240. {
  241.     return 0;
  242. }

  243. void CTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
  244. {
  245.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  246.     {
  247.         CAboutDlg dlgAbout;
  248.         dlgAbout.DoModal();
  249.     }
  250.     else
  251.     {
  252.         CDialog::OnSysCommand(nID, lParam);
  253.     }
  254. }

  255. // If you add a minimize button to your dialog, you will need the code below
  256. // to draw the icon. For MFC applications using the document/view model,
  257. // this is automatically done for you by the framework.

  258. void CTestDlg::OnPaint()
  259. {
  260.     if (IsIconic())
  261.     {
  262.         CPaintDC dc(this); // device context for painting

  263.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

  264.         // Center icon in client rectangle
  265.         int cxIcon = GetSystemMetrics(SM_CXICON);
  266.         int cyIcon = GetSystemMetrics(SM_CYICON);
  267.         CRect rect;
  268.         GetClientRect(&rect);
  269.         int x = (rect.Width() - cxIcon + 1) / 2;
  270.         int y = (rect.Height() - cyIcon + 1) / 2;

  271.         // Draw the icon
  272.         dc.DrawIcon(x, y, m_hIcon);
  273.     }
  274.     else
  275.     {
  276.         CDialog::OnPaint();
  277.     }
  278. }

  279. // The system calls this to obtain the cursor to display while the user drags
  280. // the minimized window.
  281. HCURSOR CTestDlg::OnQueryDragIcon()
  282. {
  283.     return (HCURSOR) m_hIcon;
  284. }

  285. void CTestDlg::OnCancel()
  286. {
  287.     // TODO: Add extra cleanup here
  288.     int connect=m_connect;
  289.     m_connect=0;
  290.     if(connect)
  291.     {
  292.         Sleep(500);        
  293.         VCI_CloseDevice(m_devtype,m_devind);
  294.     }
  295.     DeleteCriticalSection(&m_Section);
  296.     CDialog::OnCancel();
  297. }

  298. void CTestDlg::OnOK()
  299. {
  300.     // TODO: Add extra validation here
  301.     int connect=m_connect;
  302.     m_connect=0;
  303.     Sleep(100);
  304.     if(connect)
  305.         VCI_CloseDevice(m_devtype,m_devind);
  306.     
  307.     DeleteCriticalSection(&m_Section);
  308.     CDialog::OnOK();
  309. }

  310. void CTestDlg::OnButtonConnect()
  311. {
  312.     // TODO: Add your control notification handler code here
  313.     if(m_connect==1)
  314.     {
  315.         m_connect=0;
  316.         Sleep(500);
  317.         GetDlgItem(IDC_BUTTON_CONNECT)->SetWindowText("连接");
  318.         VCI_CloseDevice(m_devtype,m_devind);
  319.         return;
  320.     }

  321.     VCI_INIT_CONFIG init_config;
  322.     int index,filtertype,mode,cannum;
  323.     CString strcode,strmask,strtiming0,strtiming1,strtmp;
  324.     char szcode[20],szmask[20],sztiming0[20],sztiming1[20];
  325.     unsigned char sztmp[4];
  326.     DWORD code,mask,timing0,timing1;
  327.     
  328.     UpdateData(true);
  329.     index=m_ComboIndex.GetCurSel();
  330.     filtertype=m_ComboFilterType.GetCurSel();
  331.     mode=m_ComboMode.GetCurSel();
  332.     cannum=m_ComboCANInd.GetCurSel();
  333.     strcode=m_EditCode;
  334.     strmask=m_EditMask;
  335.     
  336.     
  337.     CString can_baud;
  338.     GetDlgItemText(IDC_COMBO_CANBAUD,can_baud);

  339.     if (!strcmp(can_baud,"50"))
  340.     {
  341.         strtiming0="09";
  342.         strtiming1="1c";    
  343.     }
  344.     if (!strcmp(can_baud,"100"))
  345.     {
  346.         strtiming0="04";
  347.         strtiming1="1c";
  348.     }
  349.     if(!strcmp(can_baud,"125"))
  350.     {
  351.         strtiming0="03";
  352.         strtiming1="1c";
  353.     }
  354.     if (!strcmp(can_baud,"250"))
  355.     {
  356.         strtiming0="01";
  357.         strtiming1="1c";    
  358.     }
  359.     if (!strcmp(can_baud,"500"))
  360.     {
  361.         strtiming0="00";
  362.         strtiming1="1c";    
  363.     }
  364.     if(!strcmp(can_baud,"800"))
  365.     {
  366.         strtiming0="00";
  367.         strtiming1="16";    
  368.     }
  369.     if (!strcmp(can_baud,"1000"))
  370.     {
  371.         strtiming0="00";
  372.         strtiming1="14";    
  373.     }
  374.     
  375.     m_EditTiming0=strtiming0;
  376.     m_EditTiming1=strtiming1;

  377.     //strtiming0=m_EditTiming0;
  378.     //strtiming1=m_EditTiming1;
  379.     UpdateData(false);
  380.     
  381.     strtmp=strcode;
  382.     strcode.Format("%08s",strtmp);
  383.     strtmp=strmask;
  384.     strmask.Format("%08s",strtmp);
  385.     strtmp=strtiming0;
  386.     strtiming0.Format("%02s",strtmp);
  387.     strtmp=strtiming1;
  388.     strtiming1.Format("%02s",strtmp);
  389.     
  390.     strcpy(szcode,(LPCTSTR)strcode);
  391.     strcpy(szmask,(LPCTSTR)strmask);
  392.     strcpy(sztiming0,(LPCTSTR)strtiming0);
  393.     strcpy(sztiming1,(LPCTSTR)strtiming1);
  394.     
  395.     if(strtodata((unsigned char*)szcode,sztmp,4,0)!=0)
  396.     {
  397.         MessageBox("验收码数据格式不对!","警告",MB_OK|MB_ICONQUESTION);
  398.         return;
  399.     }
  400.     code=(((DWORD)sztmp[0])<<24)+(((DWORD)sztmp[1])<<16)+(((DWORD)sztmp[2])<<8)+
  401.         ((DWORD)sztmp[3]);
  402.     
  403.     if(strtodata((unsigned char*)szmask,sztmp,4,0)!=0)
  404.     {
  405.         MessageBox("屏蔽码数据格式不对!","警告",MB_OK|MB_ICONQUESTION);
  406.         return;
  407.     }
  408.     mask=(((DWORD)sztmp[0])<<24)+(((DWORD)sztmp[1])<<16)+(((DWORD)sztmp[2])<<8)+
  409.         ((DWORD)sztmp[3]);
  410.     
  411.     if(strtodata((unsigned char*)sztiming0,sztmp,1,0)!=0)
  412.     {
  413.         MessageBox("定时器0数据格式不对!","警告",MB_OK|MB_ICONQUESTION);
  414.         return;
  415.     }
  416.     timing0=((DWORD)sztmp[0]);
  417.     
  418.     if(strtodata((unsigned char*)sztiming1,sztmp,1,0)!=0)
  419.     {
  420.         MessageBox("定时器1数据格式不对!","警告",MB_OK|MB_ICONQUESTION);
  421.         return;
  422.     }
  423.     timing1=((DWORD)sztmp[0]);
  424.     
  425.     init_config.AccCode=code;
  426.     init_config.AccMask=mask;
  427.     init_config.Filter=filtertype;
  428.     init_config.Mode=mode;
  429.     init_config.Timing0=(BYTE)timing0;
  430.     init_config.Timing1=(BYTE)timing1;
  431.     
  432.     if(VCI_OpenDevice(m_devtype,index,0)!=STATUS_OK)
  433.     {
  434.         MessageBox("打开设备失败!","警告",MB_OK|MB_ICONQUESTION);
  435.         return;
  436.     }
  437.     if(VCI_InitCAN(m_devtype,index,cannum,&init_config)!=STATUS_OK)
  438.     {
  439.         MessageBox("初始化CAN失败!","警告",MB_OK|MB_ICONQUESTION);
  440.         VCI_CloseDevice(m_devtype,index);
  441.         return;
  442.     }
  443.     m_connect=1;
  444.     m_devind=index;
  445.     m_cannum=cannum;
  446.     GetDlgItem(IDC_BUTTON_CONNECT)->SetWindowText("断开");
  447.     AfxBeginThread(ReceiveThread,this);
  448. }

  449. void CTestDlg::OnButtonStartcan()
  450. {
  451.     // TODO: Add your control notification handler code here
  452.     if(m_connect==0)
  453.         return;
  454.     if(VCI_StartCAN(m_devtype,m_devind,m_cannum)==1)
  455.     {
  456.         ShowInfo("启动成功",0);        
  457.     }
  458.     else
  459.     {
  460.         CString str;
  461.         str="启动失败";
  462.         ShowInfo(str,2);
  463.     }

  464. }

  465. void CTestDlg::OnButtonResetcan()
  466. {
  467.     // TODO: Add your control notification handler code here
  468.     if(m_connect==0)
  469.         return;
  470.     if(VCI_ResetCAN(m_devtype,m_devind,m_cannum)==1)
  471.     {
  472.         //GetDlgItem(IDC_BUTTON_STARTCAN)->EnableWindow(true);
  473.         ShowInfo("复位成功",0);        
  474.     }
  475.     else
  476.     {
  477.         CString str;
  478.         str="复位失败";
  479.         ShowInfo(str,2);
  480.     }
  481.     
  482. }

  483. void CTestDlg::OnButtonSend()
  484. {
  485.     // TODO: Add your control notification handler code here
  486.     if(m_connect==0)
  487.         return;
  488.     VCI_CAN_OBJ frameinfo;
  489.     char szFrameID[9];
  490.     unsigned char FrameID[4]={0,0,0,0};
  491.     memset(szFrameID,'0',9);
  492.     unsigned char Data[8];
  493.     char szData[25];
  494.     BYTE datalen=0;
  495.     
  496.     UpdateData(true);
  497.     if(m_EditSendFrmID.GetLength()==0||
  498.         (m_EditSendData.GetLength()==0&&m_ComboSendFrmType.GetCurSel()==0))
  499.     {
  500.         ShowInfo("请输入数据",1);
  501.         return;
  502.     }
  503.     
  504.     if(m_EditSendFrmID.GetLength()>8)
  505.     {
  506.         ShowInfo("ID值超过范围",1);
  507.         return;
  508.     }
  509.     if(m_EditSendData.GetLength()>24)
  510.     {
  511.         ShowInfo("数据长度超过范围,最大为8个字节",1);
  512.         return;
  513.     }
  514.     if(m_ComboSendFrmType.GetCurSel()==0)
  515.     {
  516.         if(m_EditSendData.GetLength()%3==1)
  517.         {
  518.             ShowInfo("数据格式不对,请重新输入",1);
  519.             return;        
  520.         }        
  521.     }
  522.     memcpy(&szFrameID[8-m_EditSendFrmID.GetLength()],(LPCTSTR)m_EditSendFrmID,m_EditSendFrmID.GetLength());
  523.     strtodata((unsigned char*)szFrameID,FrameID,4,0);

  524.     datalen=(m_EditSendData.GetLength()+1)/3;
  525.     strcpy(szData,(LPCTSTR)m_EditSendData);
  526.     strtodata((unsigned char*)szData,Data,datalen,1);


  527.     UpdateData(false);
  528.     
  529.     frameinfo.DataLen=datalen;
  530.     memcpy(&frameinfo.Data,Data,datalen);

  531.     frameinfo.RemoteFlag=m_ComboSendFrmFmt.GetCurSel();
  532.     frameinfo.ExternFlag=m_ComboSendFrmType.GetCurSel();
  533.     if(frameinfo.ExternFlag==1)
  534.     {
  535.         frameinfo.ID=((DWORD)FrameID[0]<<24)+((DWORD)FrameID[1]<<16)+((DWORD)FrameID[2]<<8)+
  536.             ((DWORD)FrameID[3]);
  537.     }
  538.     else
  539.     {
  540.         frameinfo.ID=((DWORD)FrameID[2]<<8)+((DWORD)FrameID[3]);        
  541.     }
  542.     frameinfo.SendType=m_ComboSendType.GetCurSel();

  543.     if(VCI_Transmit(m_devtype,m_devind,m_cannum,&frameinfo,1)==1)
  544.     {
  545.         ShowInfo("写入成功",0);        
  546.     }
  547.     else
  548.     {
  549.         ShowInfo("写入失败",2);        
  550.     }
  551.     
  552. }

  553. void CTestDlg::ShowInfo(CString str, int code)
  554. {
  555.     m_ListInfo.InsertString(m_ListInfo.GetCount(),str);
  556.     m_ListInfo.SetCurSel(m_ListInfo.GetCount()-1);
  557. }

  558. //-----------------------------------------------------
  559. //参数:
  560. //str:要转换的字符串
  561. //data:储存转换过来的数据串
  562. //len:数据长度
  563. //函数功能:字符串转换为数据串
  564. //-----------------------------------------------------
  565. int CTestDlg::strtodata(unsigned char *str, unsigned char *data,int len,int flag)
  566. {
  567.     unsigned char cTmp=0;
  568.     int i=0;
  569.     for(int j=0;j<len;j++)
  570.     {
  571.         if(chartoint(str[i++],&cTmp))
  572.             return 1;
  573.         data[j]=cTmp;
  574.         if(chartoint(str[i++],&cTmp))
  575.             return 1;
  576.         data[j]=(data[j]<<4)+cTmp;
  577.         if(flag==1)
  578.             i++;
  579.     }
  580.     return 0;
  581. }

  582. //----------------------------------------------]-------
  583. //参数:
  584. //chr:要转换的字符
  585. //cint:储存转换过来的数据
  586. //函数功能:字符转换为数据
  587. //-----------------------------------------------------
  588. int CTestDlg::chartoint(unsigned char chr, unsigned char *cint)
  589. {
  590.     unsigned char cTmp;
  591.     cTmp=chr-48;
  592.     if(cTmp>=0&&cTmp<=9)
  593.     {
  594.         *cint=cTmp;
  595.         return 0;
  596.     }
  597.     cTmp=chr-65;
  598.     if(cTmp>=0&&cTmp<=5)
  599.     {
  600.         *cint=(cTmp+10);
  601.         return 0;
  602.     }
  603.     cTmp=chr-97;
  604.     if(cTmp>=0&&cTmp<=5)
  605.     {
  606.         *cint=(cTmp+10);
  607.         return 0;
  608.     }
  609.     return 1;
  610. }

  611. UINT CTestDlg::ReceiveThread(void *param)
  612. {
  613.     CTestDlg *dlg=(CTestDlg*)param;
  614.     CListBox *box=(CListBox *)dlg->GetDlgItem(IDC_LIST_INFO);
  615.     VCI_CAN_OBJ frameinfo[50];
  616.     VCI_ERR_INFO errinfo;
  617.     int len=1;
  618.     int i=0;
  619.     CString str,tmpstr;
  620.     while(1)
  621.     {
  622.         Sleep(1);
  623.         if(dlg->m_connect==0)
  624.             break;
  625.         len=VCI_Receive(dlg->m_devtype,dlg->m_devind,dlg->m_cannum,frameinfo,50,200);
  626.         if(len<=0)
  627.         {
  628.             //注意:如果没有读到数据则必须调用此函数来读取出当前的错误码,
  629.             //千万不能省略这一步(即使你可能不想知道错误码是什么)
  630.             VCI_ReadErrInfo(dlg->m_devtype,dlg->m_devind,dlg->m_cannum,&errinfo);
  631.         }
  632.         else
  633.         {
  634.             for(i=0;i<len;i++)
  635.             {
  636.                 str="接收: ";
  637.                 if(frameinfo[i].TimeFlag==0)
  638.                     tmpstr=":无 ";
  639.                 else
  640.                     tmpstr.Format("时间标识:%08x ",frameinfo[i].TimeStamp);
  641.                 str+=tmpstr;
  642.                 tmpstr.Format("ID:%08x ",frameinfo[i].ID);
  643.                 str+=tmpstr;
  644.                 str+="-";
  645.                 if(frameinfo[i].RemoteFlag==0)
  646.                     tmpstr="数据帧 ";
  647.                 else
  648.                     tmpstr="远程帧 ";
  649.                 str+=tmpstr;
  650.                 str+="-";
  651.                 if(frameinfo[i].ExternFlag==0)
  652.                     tmpstr="标准帧 ";
  653.                 else
  654.                     tmpstr="扩展帧 ";
  655.                 str+=tmpstr;
  656.             
  657.                 if(frameinfo[i].RemoteFlag==0)//data frame
  658.                 {
  659.                     str="数据:";
  660.                     if(frameinfo[i].DataLen>8)
  661.                         frameinfo[i].DataLen=8;
  662.                     for(int j=0;j<frameinfo[i].DataLen;j++)
  663.                     {
  664.                         tmpstr.Format("%02x ",frameinfo[i].Data[j]);
  665.                         str+=tmpstr;
  666.                     }
  667.                     if ( (frameinfo[i].ID>0x700) && (frameinfo[i].ID<=0x77f))//HEARTBEAT    
  668.                     {
  669.                         g_node.id=frameinfo[i].ID-0x700;
  670.                     }
  671.                     if ( (frameinfo[i].ID>0x580) && (frameinfo[i].ID<=0x5ff) )//SDO
  672.                     {
  673.                         BYTE ch;
  674.                         BYTE type;
  675.                         BYTE cmd;
  676.                         WORD index;
  677.                         BYTE subindex;
  678.                         DWORD tmp;
  679.                         //tmp=MEM_DW(frameinfo[i].Data);
  680.                         //cmd=DW_H4(tmp);
  681.                         cmd        =MEM_B(frameinfo[i].Data);
  682.                         index    =MEM_W(frameinfo[i].Data+1);
  683.                         subindex=MEM_B(frameinfo[i].Data+3);
  684.                         ch=subindex>>4;
  685.                         type=subindex&0x0f;    
  686.                         
  687.                         if (cmd==CMD_RD)
  688.                         {
  689.                             if(index==INDEX_RDAI)
  690.                             {
  691.                                 g_node.wAIValue[ch][type]=MEM_W(frameinfo[i].Data+4);
  692.                                 //AfxMessageBox("ff");
  693.                                 m_evtUpdateDisplay.SetEvent();
  694.                             }
  695.                             if (index==INDEX_K)
  696.                             {
  697.                                 g_node.readkb[ch].fk=MEM_FLOAT(frameinfo[i].Data+4);
  698.                                 m_evtUpdateDisplay.SetEvent();
  699.                             }
  700.                             if (INDEX_K==INDEX_B)
  701.                             {
  702.                                 g_node.readkb[ch].fb=MEM_FLOAT(frameinfo[i].Data+4);
  703.                                 m_evtUpdateDisplay.SetEvent();
  704.                             }
  705.                         }
  706.                         
  707.                     }
  708.                     
  709.                     
  710.                     if (frameinfo[i].ID==0xff)
  711.                     if (frameinfo[i].Data[0]==0x88 && frameinfo[i].Data[1]==0x88 && frameinfo[i].Data[2]==0x88&&frameinfo[i].Data[3]==0x88)
  712.                     {
  713.                         str+="下载成功!";
  714.                     }
  715.                     else if (frameinfo[i].Data[0]==0x11 && frameinfo[i].Data[1]==0x33 && frameinfo[i].Data[2]==0x44&&frameinfo[i].Data[3]==0x55)
  716.                     {
  717.                         str+="下载失败! 丢帧";
  718.                     }
  719.                     else if (frameinfo[i].Data[0]==0x11 && frameinfo[i].Data[1]==0x22 && frameinfo[i].Data[2]==0x11&&frameinfo[i].Data[3]==0x23)
  720.                     {
  721.                         str+="下载失败! 写入比较不匹配";
  722.                     }
  723.                     else if (frameinfo[i].Data[0]==0xc0 && frameinfo[i].Data[1]==0xc0 && frameinfo[i].Data[2]==0xc0&&frameinfo[i].Data[3]==0xc0)
  724.                     {
  725.                         str+="下载失败!CRC校验错误!";
  726.                     }
  727.                     
  728.                 
  729.                     //EnterCriticalSection(&(dlg->m_Section));
  730.                     //LeaveCriticalSection(&(dlg->m_Section));
  731.                     box->InsertString(box->GetCount(),str);
  732.                 }                
  733.             }
  734.             box->SetCurSel(box->GetCount()-1);
  735.         }
  736.     }
  737.     return 0;
  738. }

  739. void CTestDlg::OnMenuRefresh()
  740. {
  741.     // TODO: Add your command handler code here
  742. }

  743. void CTestDlg::OnEditchangeComboCanbaud()
  744. {
  745.     // TODO: Add your control notification handler code here
  746.     UpdateData(true);
  747.     int index;
  748.     index=((CComboBox *)GetDlgItem(IDC_COMBO_CANBAUD))->GetCurSel();
  749.     
  750.     UpdateData(false);
  751.     
  752. }
  753. //CRC校验函数,对已经写入程序的FLASH进行CRC校验
  754. unsigned int crc_32( unsigned char *p, unsigned int len)
  755. {
  756.   unsigned int ii=0;
  757.   unsigned int crc;
  758.   unsigned int m_crc = 0xFFFFFFFF; //寄存器中预置初始值
  759.   for(ii=0; ii <len; ii++)
  760.   {
  761.   m_crc = crc32_table[( m_crc^(*(p+ii)) ) & 0xff] ^ (m_crc >> 8); //计算
  762.   //gdelay(2000);
  763.   }
  764.   crc= ~m_crc; //取反
  765.   return crc;
  766. }
  767. void CTestDlg::OnFileRead()
  768. {
  769.     uint32 pkt_num=0;
  770.     uint32 last_len=0;
  771.     uint32 CMD_LAST_FRAME=0xffffff;
  772.     uint8 BUF_LAST_FRAME[8]={0,0,0,0,0,0,0,0};
  773.     uint32 CRC_BINFILE;
  774.     CFile file;
  775.     unsigned char *pbuf;
  776.     CString path;
  777.     DWORD dwFileLen;    
  778.     long lvalue;
  779.     RegQueryValue(HKEY_LOCAL_MACHINE,"software\\CanIAP\\admin",NULL,&lvalue);

  780.     char *path_temp=new char[lvalue];

  781.     RegQueryValue(HKEY_LOCAL_MACHINE,"software\\CanIAP\\admin",path_temp,&lvalue);

  782.     path=(LPCTSTR)path_temp;

  783.     file.Open(path,CFile::modeRead | CFile::typeBinary);
  784.         
  785.     dwFileLen=file.GetLength();
  786.     
  787.     pbuf=new unsigned char[dwFileLen];
  788.         if (dwFileLen<64*1024)//如果bin文件小于64k
  789.         {
  790.             file.Read(pbuf,dwFileLen);
  791.         }
  792.         else
  793.         {
  794.             file.ReadHuge(pbuf,dwFileLen);
  795.         }
  796.         CRC_BINFILE=crc_32(&pbuf[0],dwFileLen);
  797.         
  798.             
  799.         //显示bin文件路径
  800.         GetDlgItem(IDC_STATIC_FILEPATH)->SetWindowText(path_temp);
  801.         CString STR_TMP;
  802.         //显示bin文件大小
  803.         STR_TMP.Format("%d",dwFileLen);
  804.         GetDlgItem(IDC_STATIC_FILESIZE)->SetWindowText(STR_TMP);
  805.         //显示bin文件crc32值
  806.         STR_TMP.Format("%x",CRC_BINFILE);
  807.         GetDlgItem(IDC_STATIC_FILECRC)->SetWindowText(STR_TMP);

  808.     

  809.         memcpy(&BUF_LAST_FRAME[0],(unsigned char *)(&CRC_BINFILE),4);
  810.         memcpy(&BUF_LAST_FRAME[4],(unsigned char *)(&dwFileLen),4);

  811.         if(dwFileLen%8==0)
  812.         {
  813.             pkt_num=dwFileLen/8;
  814.         }
  815.         else
  816.         {
  817.             pkt_num=dwFileLen/8+1;
  818.         }
  819.         ((CProgressCtrl *)GetDlgItem(IDC_PROGRESS_IAP))->SetRange(0,pkt_num);
  820.         ((CProgressCtrl *)GetDlgItem(IDC_PROGRESS_IAP))->SetStep(1);
  821.         ((CProgressCtrl *)GetDlgItem(IDC_PROGRESS_IAP))->SetPos(0);

  822.         for (uint32 iLp=0;iLp<pkt_num-1;iLp++)
  823.         {
  824.             if (Data_CanSend(iLp,0,1,(uint8 *)(pbuf+8*iLp),8)!=0)
  825.             {
  826.                 ShowInfo("写入失败",2);
  827.                 break;
  828.             
  829.             }

  830.             ((CProgressCtrl *)GetDlgItem(IDC_PROGRESS_IAP))->StepIt();

  831.             if ((iLp==31) || (iLp==(4095+32)))
  832.             {
  833.                 Sleep(300);
  834.                 //delayus(200000);
  835.             }
  836.             else if (iLp==(8191+32))
  837.             {
  838.                 Sleep(300);
  839.                 //delayus(300000);
  840.             }
  841.             else
  842.             {
  843.                 if ((iLp+1)%32==0)
  844.                 {
  845.                     
  846.                     //delayus(20);
  847.                     Sleep(1);
  848.                 }
  849.                 else
  850.                 {
  851.                     //Sleep(1);
  852.                 }
  853.                     
  854.             }
  855.                 
  856.         }
  857.             Sleep(3);

  858.         //发送最后一个can报文
  859.         if (dwFileLen%8==0)
  860.         {
  861.             if(Data_CanSend(iLp,0,1,(uint8 *)(pbuf+8*iLp),8)!=0)
  862.             {
  863.                 ShowInfo("写入失败",2);
  864.             }
  865.         }
  866.         else
  867.         {
  868.             last_len=dwFileLen%8;
  869.             if(Data_CanSend(iLp,0,1,(uint8 *)(pbuf+8*iLp),last_len)!=0)
  870.             {
  871.                 ShowInfo("写入失败",2);
  872.             }
  873.         }
  874.         Sleep(3);

  875.         //发送最后帧数据指令:
  876.         if(Data_CanSend(CMD_LAST_FRAME,0,1,&BUF_LAST_FRAME[0],8)!=0)
  877.         {
  878.             ShowInfo("写入失败",2);
  879.         }

  880.         file.Close();
  881.         delete(pbuf);
  882.         delete(path_temp);

  883. }
  884. //打开文件,选择需要下载的bin文件
  885. void CTestDlg::OnButtonOpenfile()
  886. {
  887.     // TODO: Add your control notification handler code here
  888.     CFileDialog filedlg(true);
  889.     filedlg.m_ofn.lpstrTitle="请选择目标文件";
  890.     filedlg.m_ofn.lpstrFilter="Bin Files(*.bin)\0 *.bin\0\0";
  891.     if(IDOK==filedlg.DoModal())
  892.     {
  893.         CFile file(filedlg.GetFileName(),CFile::modeRead | CFile::typeBinary);
  894.         CString path;    
  895.         HKEY hkey;
  896.         
  897.         //获取选择bin文件的路径,并写入注册表
  898.         path=file.GetFilePath();
  899.         RegCreateKey(HKEY_LOCAL_MACHINE,"software\\CanIAP\\admin",&hkey);
  900.         RegSetValue(hkey,NULL,REG_SZ,path,strlen(path));
  901.         RegCloseKey(hkey);

  902.         //file.Close();
  903.     }    
  904.     
  905. }

  906. //can帧发送函数
  907. int CTestDlg::Data_CanSend(uint32 ID,UINT8 RTR,UINT8 IDE,UINT8 *DATA,UINT8 LEN)
  908. {
  909.     VCI_CAN_OBJ frameinfo;
  910.     //int m_cannum=0;
  911.     //int m_devtype=0;

  912.     frameinfo.SendType=0;//正常发送
  913.     frameinfo.ID=ID;
  914.     frameinfo.RemoteFlag=RTR;
  915.     frameinfo.ExternFlag=IDE;
  916.     frameinfo.DataLen=LEN;
  917.  
  918.     memcpy(&frameinfo.Data,DATA,LEN);

  919.     if(VCI_Transmit(m_devtype,m_devind,m_cannum,&frameinfo,1)==1)
  920.     {
  921.         //ShowInfo("写入成功",0);
  922.         return 0;
  923.     }
  924.     else
  925.     {
  926.         //ShowInfo("写入失败",2);    
  927.         return -1;
  928.     }

  929.     return 0;
  930. }
  931. //开始下载,将选中的bin文件打开并发送出去
  932. void CTestDlg::OnButtonStart()
  933. {
  934.     //HWND hwnd;
  935.     SetEvent(m_evtUpdate);
  936.     //::PostMessage(hwnd,ON_WM_SEND,0,NULL);
  937.     //OnFileRead();
  938.     //((CComboBox *)GetDlgItem(IDC_COMBO_CANBAUD))->InsertString(0,"请选择");


  939. }

  940. void OnRegWrite()
  941. {
  942.     HKEY hkey;
  943.     RegCreateKey(HKEY_LOCAL_MACHINE,"software\\CanIAP\\admin",&hkey);
  944.     RegSetValue(hkey,NULL,REG_SZ,"ZHANGSAN",strlen("ZHANGSAN"));
  945.     RegCloseKey(hkey);
  946. }

  947. void OnRegRead()
  948. {
  949.     long lvalue;
  950.     RegQueryValue(HKEY_LOCAL_MACHINE,"software\\CanIAP\\admin",NULL,&lvalue);

  951.     char *pbuf=new char[lvalue];

  952.     RegQueryValue(HKEY_LOCAL_MACHINE,"software\\CanIAP\\admin",pbuf,&lvalue);
  953.     
  954.     AfxMessageBox(pbuf);
  955.     delete(pbuf);//??????
  956.     
  957. }
  958. void delayus(long int microsecond) //以微秒延时函数
  959. { _LARGE_INTEGER litmp;

  960.     LONGLONG QPart;
  961.     double dfFreq;
  962.     long dfTim;
  963.     
  964.     QueryPerformanceFrequency(&litmp);
  965.     dfFreq = (double)litmp.QuadPart / 1e6 ; // 获得计数器的时钟频率,以微秒为单位
  966.     QueryPerformanceCounter(&litmp);
  967.     QPart = litmp.QuadPart;                  // 获得时间初始值
  968.     do {
  969.           QueryPerformanceCounter(&litmp);
  970.           dfTim = (long )((double)(litmp.QuadPart-QPart) / dfFreq) ; // 获得对应的时间值,单位为微秒
  971.        }while(dfTim< microsecond);
  972.     return ;
  973. }

  974. void CTestDlg::InitGrid()
  975. {
  976.     //
  977.     m_Grid.SetExtendedStyle(LVS_EX_FLATSB
  978.         |LVS_EX_FULLROWSELECT
  979.         |LVS_EX_HEADERDRAGDROP
  980.         |LVS_EX_ONECLICKACTIVATE
  981.         |LVS_EX_GRIDLINES);
  982.     
  983.     m_Grid.InsertColumn(0,"ch",        LVCFMT_LEFT,50,0);
  984.     m_Grid.InsertColumn(1,"bV",        LVCFMT_LEFT,50,1);
  985.     m_Grid.InsertColumn(2,"sV",        LVCFMT_LEFT,50,2);
  986.     m_Grid.InsertColumn(3,"k",        LVCFMT_LEFT,50,3);
  987.     m_Grid.InsertColumn(4,"b",        LVCFMT_LEFT,50,4);

  988.     m_Grid.InsertColumn(5,"mA",        LVCFMT_LEFT,50,6);
  989.     m_Grid.InsertColumn(6,"ok",        LVCFMT_LEFT,50,5);
  990.     
  991.     for(int i=0;i<=7;i++)
  992.     m_Grid.InsertItem(i,"");
  993. }
  994. int CTestDlg::SearchThreadProc(LPVOID lpparameter)
  995. {
  996.     CTestDlg *dlg=(CTestDlg *)lpparameter;

  997.     dlg->m_ButtonSearch.EnableWindow(FALSE);
  998.     SearchPLC();
  999.     dlg->m_ButtonSearch.EnableWindow(TRUE);

  1000.     return 0;
  1001. }
  1002. int CTestDlg::CAN_PushMessage(CAN_MSG *msg)
  1003. {
  1004.     VCI_CAN_OBJ frameinfo;

  1005.     frameinfo.SendType=0;//正常发送
  1006.     frameinfo.ID=msg->ID;
  1007.     frameinfo.RemoteFlag=msg->RTR;
  1008.     frameinfo.ExternFlag=0;
  1009.     frameinfo.DataLen=msg->LEN;
  1010.     
  1011.     if (msg->LEN>0)
  1012.     memcpy(&frameinfo.Data,msg->BUF,msg->LEN);

  1013.     if(VCI_Transmit(m_devtype,m_devind,m_cannum,&frameinfo,1)==1)
  1014.     {
  1015.         //ShowInfo("写入成功",0);
  1016.         return 0;
  1017.     }
  1018.     else
  1019.     {
  1020.         //ShowInfo("写入失败",2);    
  1021.         return -1;
  1022.     }

  1023. }
  1024. int CTestDlg::SearchPLC(void)
  1025. {
  1026.     CAN_MSG msg;
  1027.     msg.LEN=0;
  1028.     msg.RTR=1;

  1029.     for (UINT8 i=1;i<64;i++)
  1030.     {    
  1031.         msg.ID=i+0x700;
  1032.         CAN_PushMessage(&msg);
  1033.         Sleep(1);
  1034.     }
  1035.     return 0;
  1036. }
  1037. int CTestDlg::ReadAI(BYTE byid,BYTE mtype)
  1038. {
  1039.     CAN_MSG msg;
  1040.     DWORD tmp;

  1041.     BYTE subindex;
  1042.     BYTE ch,type;


  1043.     if(byid==0)
  1044.         return -1;
  1045.     if (mtype>1)
  1046.     {
  1047.         return -1;
  1048.     }
  1049.     type=mtype;
  1050.     msg.ID=byid+0x600;
  1051.     msg.LEN=8;
  1052.     msg.RTR=0;
  1053.     subindex=1;
  1054.     //进标定
  1055.     tmp=(CMD_WR<<24) | (INDEX_INBD<<8) | (subindex);
  1056.     MEM_DW(msg.BUF)=mhtonl(tmp);
  1057.     MEM_DW(msg.BUF+4)=0;
  1058.     CAN_PushMessage(&msg);

  1059.     for (ch=0;ch<=7;ch++)
  1060.     {
  1061.         //for (type=0;type<=1;type++)
  1062.         {
  1063.                 subindex=(BYTE)(ch<<4) | (BYTE)(type);
  1064.                 tmp=(CMD_RD<<24) | (INDEX_RDAI<<8) | (subindex);
  1065.                 MEM_DW(msg.BUF)=mhtonl(tmp);
  1066.                 MEM_DW(msg.BUF+4)=0;
  1067.                 CAN_PushMessage(&msg);
  1068.                 Sleep(100);
  1069.         }
  1070.     }

  1071.     return 0;
  1072. }


  1073. //搜索按钮消息响应函数
  1074. void CTestDlg::OnButtonSearch()
  1075. {
  1076.     // TODO: Add your control notification handler code here
  1077.     DWORD dwid;
  1078.     HANDLE hthread;
  1079.     if (m_connect!=1)
  1080.     {
  1081.         return;
  1082.     }
  1083.     hthread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)SearchThreadProc,this,0,&dwid);
  1084. }

  1085. //写k、b按钮消息响应函数
  1086. void CTestDlg::OnButtonWrite()
  1087. {
  1088.     // TODO: Add your control notification handler code here
  1089.     if (m_connect!=1)    
  1090.     {
  1091.         return ;
  1092.     }
  1093. }
  1094. //标定子线程
  1095. int CTestDlg::BDThreadProc(LPVOID lpparameter)
  1096. {
  1097.     BYTE type=*(BYTE *)lpparameter;
  1098.     if(m_connect!=1)    return -1;
  1099.     ReadAI(g_node.id,type);
  1100.     return 0;
  1101. }
  1102. //显示子线程
  1103. int CTestDlg::DisplayThreadProc(LPVOID lpparameter)
  1104. {
  1105.     CTestDlg *dlg=(CTestDlg *)AfxGetMainWnd();
  1106.     CString strch,strbv,strsv,strfk,strfb,strbok,strmA,strAICalcOut;
  1107.     while(TRUE)
  1108.     {
  1109.         if (m_connect!=1)
  1110.         {
  1111.             Sleep(10);
  1112.             continue;
  1113.         }

  1114.         DWORD result=WaitForSingleObject(m_evtUpdateDisplay.m_hObject,INFINITE);
  1115.         if (result==WAIT_OBJECT_0)
  1116.         {
  1117.             CString str;
  1118.             str.Format("0x%02x",g_node.id);
  1119.             dlg->m_StaticNodeID.SetWindowText(str);
  1120.             
  1121.             for (int ch=0;ch<=7;ch++)
  1122.             {
  1123.                     
  1124.                 strch.Format(    "%d",    ch+1);                        //channel
  1125.                 strbv.Format(    "%x",    g_node.wAIValue[ch][1]);    //big
  1126.                 strsv.Format(    "%x",    g_node.wAIValue[ch][0]);    //small
  1127.                 strfk.Format(    "%f",    g_node.calckb[ch].fk);        //fk
  1128.                 strfb.Format(    "%f",    g_node.calckb[ch].fb);        //fb
  1129.                 strbok.Format(    "%d",    g_node.bBDOK);                //bd is finished suc
  1130.                 strmA.Format(    "%f",    g_node.fCurrent);            //ma
  1131.                 
  1132.                 //WORD wAIHuiJian=g_node.calckb[ch].fk*

  1133.                 dlg->m_Grid.SetItemText(ch,0,strch);
  1134.                 dlg->m_Grid.SetItemText(ch,1,strbv);
  1135.                 dlg->m_Grid.SetItemText(ch,2,strsv);
  1136.                 dlg->m_Grid.SetItemText(ch,3,strfk);
  1137.                 dlg->m_Grid.SetItemText(ch,4,strfb);
  1138.                 dlg->m_Grid.SetItemText(ch,5,strbok);
  1139.                 dlg->m_Grid.SetItemText(ch,6,strmA);
  1140.             }
  1141.             
  1142.         }
  1143.     
  1144.         Sleep(1);
  1145.     }
  1146.     //m_connect
  1147.     return 0;
  1148. }

  1149. void CTestDlg::OnButtonCalc()
  1150. {
  1151.     // TODO: Add your control notification handler code here
  1152.     float fk[8],fb[8];
  1153.     
  1154.     CalcKB(g_node.wAIValue,fk,fb);
  1155.     

  1156.     for (int i=0;i<8;i++)
  1157.     {
  1158.         memcpy(&g_node.calckb[i].fk,&fk+i,4);
  1159.         memcpy(&g_node.calckb[i].fb,&fb+i,4);
  1160.     }
  1161.     m_evtUpdateDisplay.SetEvent();
  1162.     
  1163. }



  1164. int CTestDlg::CalcKB(uint16 (*stAIValue)[2], float *fk, float *fb)
  1165. {
  1166.     int i;
  1167.     WORD wbig=AI_BD_BIG;
  1168.     WORD wsmall=AI_BD_SMALL;
  1169.     for(i=0;i<8;i++)
  1170.     {
  1171.         *(fk+i)=(wbig-wsmall)*1.0/( *(stAIValue[i]+1)-*(stAIValue[i]+0) );//calc k
  1172.         *(fb+i)=wsmall-*(fk+i)**(stAIValue[i]+0);
  1173.     }
  1174.     return 0;
  1175. }

  1176. void CTestDlg::OnButtonBdbig()
  1177. {
  1178.     // TODO: Add your control notification handler code here
  1179.     HANDLE hthread;
  1180.     DWORD dwid;
  1181.     BYTE type=1;    
  1182.     if (m_connect!=1)//not connect
  1183.     {
  1184.         return;
  1185.     }
  1186.     hthread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)BDThreadProc,&type,0,&dwid);
  1187. }

  1188. void CTestDlg::OnButtonBdsmall()
  1189. {
  1190.     // TODO: Add your control notification handler code here
  1191.     HANDLE hthread;
  1192.     DWORD dwid;
  1193.     BYTE type=0;    
  1194.     if (m_connect!=1)//not connect
  1195.     {
  1196.         return;
  1197.     }
  1198.     hthread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)BDThreadProc,&type,0,&dwid);
  1199.     
  1200. }

  1201. void CTestDlg::OnButtonIobd()
  1202. {
  1203.     // TODO: Add your control notification handler code here
  1204.     CAN_MSG msg;
  1205.     msg.ID=0x600+g_node.id;
  1206.     msg.RTR=0;
  1207.     msg.LEN=8;
  1208.     
  1209.     
  1210.     DWORD tmp;


  1211.     static bflag=0;
  1212.     if (m_connect!=1)
  1213.     {
  1214.         return;
  1215.     }
  1216.     if (bflag==1)
  1217.     {
  1218.         //退标定
  1219.         bflag=0;
  1220.         tmp=(CMD_WR<<24) | (INDEX_INBD<<8) | (0);
  1221.         MEM_DW(msg.BUF)=mhtonl(tmp);
  1222.         MEM_DW(msg.BUF+4)=0;
  1223.         CAN_PushMessage(&msg);
  1224.         m_ButtonIOBD.SetWindowText("进标定");
  1225.     }
  1226.     else
  1227.     {
  1228.         //进标定
  1229.         bflag=1;
  1230.         tmp=(CMD_WR<<24) | (INDEX_INBD<<8) | (1);
  1231.         MEM_DW(msg.BUF)=mhtonl(tmp);
  1232.         MEM_DW(msg.BUF+4)=0;
  1233.         CAN_PushMessage(&msg);
  1234.         m_ButtonIOBD.SetWindowText("退标定");
  1235.     }

  1236. }

  1237. void CTestDlg::OnClickListBd(NMHDR* pNMHDR, LRESULT* pResult)
  1238. {
  1239.     // TODO: Add your control notification handler code here
  1240.     POSITION pos=m_Grid.GetFirstSelectedItemPosition();;
  1241.     CAN_MSG msg;
  1242.     DWORD tmp;
  1243.     msg.ID=0x600+g_node.id;
  1244.     msg.RTR=0;
  1245.     msg.LEN=8;

  1246.     if (pos == NULL)
  1247.            TRACE0("No items were selected!\n");
  1248.     else
  1249.     {
  1250.        while (pos)
  1251.        {
  1252.              int nItem = m_Grid.GetNextSelectedItem(pos);
  1253.              tmp=(CMD_WR<<24) | (INDEX_K<<8) |( (BYTE)nItem);
  1254.              MEM_DW(msg.BUF)=tmp;
  1255.              MEM_DW(msg.BUF+4)=g_node.calckb[nItem].fk;
  1256.              CAN_PushMessage(&msg);
  1257.              Sleep(50);
  1258.              tmp=(CMD_WR<<24) | (INDEX_B<<8) |( (BYTE)nItem);
  1259.              MEM_DW(msg.BUF)=tmp;
  1260.              MEM_DW(msg.BUF+4)=g_node.calckb[nItem].fb;
  1261.              CAN_PushMessage(&msg);
  1262.             
  1263.        }
  1264.     }
  1265.     *pResult = 0;
  1266. }

阅读(1815) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~