MFCAppDlg.cpp
上传用户:lyfy_2008
上传日期:2014-07-13
资源大小:3016k
文件大小:12k
- // MFCAppDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "MFCApp.h"
- #include "MFCAppDlg.h"
- #include <windows.h>
- #include <winioctl.h>
- #include "ForUsbioctl.h"
- #include "ForUsbDeviceinterface.h" // Has class GUID definition
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // This function is found in module OpenByIntf.cpp
- HANDLE OpenByInterface(GUID* pClassGuid, DWORD instance, PDWORD pError);
- GUID ClassGuid = ForUsbDevice_CLASS_GUID;
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMFCAppDlg dialog
- CMFCAppDlg::CMFCAppDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CMFCAppDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CMFCAppDlg)
- m_Mes = _T("");
- m_key0 = FALSE;
- m_key1 = FALSE;
- m_key2 = FALSE;
- m_key3 = FALSE;
- m_key4 = FALSE;
- m_key5 = FALSE;
- m_key6 = FALSE;
- m_key7 = FALSE;
- m_buzzer = FALSE;
- m_lcd1 = _T("");
- m_left = _T("");
- m_right = _T("");
- m_lcd2 = _T("");
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CMFCAppDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CMFCAppDlg)
- DDX_Text(pDX, IDC_EDIT_Mes, m_Mes);
- DDX_Check(pDX, IDC_CHECK1, m_key0);
- DDX_Check(pDX, IDC_CHECK2, m_key1);
- DDX_Check(pDX, IDC_CHECK3, m_key2);
- DDX_Check(pDX, IDC_CHECK4, m_key3);
- DDX_Check(pDX, IDC_CHECK5, m_key4);
- DDX_Check(pDX, IDC_CHECK6, m_key5);
- DDX_Check(pDX, IDC_CHECK7, m_key6);
- DDX_Check(pDX, IDC_CHECK8, m_key7);
- DDX_Check(pDX, IDC_CHECK9, m_buzzer);
- DDX_Text(pDX, IDC_EDIT3, m_lcd1);
- DDX_Text(pDX, IDC_EDIT1, m_left);
- DDX_Text(pDX, IDC_EDIT2, m_right);
- DDX_Text(pDX, IDC_EDIT4, m_lcd2);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CMFCAppDlg, CDialog)
- //{{AFX_MSG_MAP(CMFCAppDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- // ON_BN_CLICKED(IDC_BUTTON1, OnLed)
- ON_BN_CLICKED(IDC_BUTTON2, OnLed0)
- ON_BN_CLICKED(IDC_BUTTON3, OnLed1)
- ON_BN_CLICKED(IDC_BUTTON4, OnLed2)
- ON_BN_CLICKED(IDC_BUTTON8, OnLed3)
- ON_BN_CLICKED(IDC_BUTTON9, OnLed4)
- ON_BN_CLICKED(IDC_BUTTON5, OnLed5)
- ON_BN_CLICKED(IDC_BUTTON6, OnLed6)
- ON_BN_CLICKED(IDC_BUTTON7, OnLed7)
- ON_WM_TIMER()
- ON_BN_CLICKED(IDC_CHECK9, OnBuzzer)
- ON_EN_CHANGE(IDC_EDIT1, OnChangeLeft)
- ON_EN_CHANGE(IDC_EDIT2, OnChangeRight)
- ON_EN_CHANGE(IDC_EDIT3, OnChangelcd1)
- ON_EN_CHANGE(IDC_EDIT4, OnChangeLcd2)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMFCAppDlg message handlers
- BOOL CMFCAppDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
-
- // TODO: Add extra initialization here
- led=0xff;
- hDevice = INVALID_HANDLE_VALUE;
-
- m_Mes="Test application Test_ForUsb starting...n";
- UpdateData(FALSE);
-
- DWORD Error;
- hDevice = OpenByInterface( &ClassGuid, 0, &Error);
- if (hDevice == INVALID_HANDLE_VALUE)
- {
- m_Mes="ERROR opening device: returned from CreateFilen" ;
- // m_Mes+=GetLastError();
- }
- else
- {
- m_Mes="Found TX-1B USB Device, handle open.";
- }
- UpdateData(FALSE);
- SetTimer(1,80,0);
- CEdit *pedit=(CEdit *)GetDlgItem(IDC_EDIT1);
- pedit->SetLimitText(1);
- pedit=(CEdit *)GetDlgItem(IDC_EDIT2);
- pedit->SetLimitText(1);
- pedit=(CEdit *)GetDlgItem(IDC_EDIT3);
- pedit->SetLimitText(16);
- pedit=(CEdit *)GetDlgItem(IDC_EDIT4);
- pedit->SetLimitText(16);
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CMFCAppDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CMFCAppDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CMFCAppDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CMFCAppDlg::OnLed()
- {
- // TODO: Add your control notification handler code here
- char buff[16];
- CHAR bufOutput[16]; // Output from device
- ULONG nOutput;
- buff[0]=0x01;
- buff[1]=led;
- if (!DeviceIoControl(hDevice,
- FORUSB_IOCTL_Write,
- buff,
- 16,
- bufOutput,
- 0,
- &nOutput,
- NULL)
- )
- {
- m_Mes="ERROR: DeviceIoControl returns .";
- UpdateData(FALSE);
- }
- }
- CMFCAppDlg::~CMFCAppDlg()
- {
- // KillTimer(1);
- if (hDevice != INVALID_HANDLE_VALUE)
- {
- // Close the handle to the driver
- if (!CloseHandle(hDevice))
- {
- m_Mes="ERROR: CloseHandle returns %0x.n";
- UpdateData(FALSE);
- }
- hDevice = INVALID_HANDLE_VALUE;
- }
- //
- }
- void CMFCAppDlg::OnLed0()
- {
- // TODO: Add your control notification handler code here
- if(led&0x01)
- led&=0xfe;
- else
- led|=0x01;
- OnLed();
- }
- void CMFCAppDlg::OnLed1()
- {
- if(led&0x02)
- led&=0xfd;
- else
- led|=0x02;
- OnLed();
- }
- void CMFCAppDlg::OnLed2()
- {
- if(led&0x04)
- led&=0xfb;
- else
- led|=0x04;
- OnLed();
- }
- void CMFCAppDlg::OnLed3()
- {
- if(led&0x08)
- led&=0xf7;
- else
- led|=0x08;
- OnLed();
- }
- void CMFCAppDlg::OnLed4()
- {
- // TODO: Add your control notification handler code here
- if(led&0x10)
- led&=0xef;
- else
- led|=0x10;
- OnLed();
- }
- void CMFCAppDlg::OnLed5()
- {
- // TODO: Add your control notification handler code here
- if(led&0x20)
- led&=0xdf;
- else
- led|=0x20;
- OnLed();
- }
- void CMFCAppDlg::OnLed6()
- {
- // TODO: Add your control notification handler code here
- if(led&0x40)
- led&=0xbf;
- else
- led|=0x40;
- OnLed();
- }
- void CMFCAppDlg::OnLed7()
- {
- // TODO: Add your control notification handler code here
- if(led&0x80)
- led&=0x7f;
- else
- led|=0x80;
- OnLed();
- }
- void CMFCAppDlg::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- char Key; // Output from device
- ULONG nOutput;
- if (!DeviceIoControl(hDevice,
- FORUSB_IOCTL_Read,
- NULL,
- 0,
- &Key,
- 1,
- &nOutput,
- NULL)
- )
- {
- m_Mes="ERROR: DeviceIoControl returns .";
- UpdateData(FALSE);
- }
- if(Key&0x01)m_key0=0;else m_key0=1;
- if(Key&0x02)m_key1=0;else m_key1=1;
- if(Key&0x04)m_key2=0;else m_key2=1;
- if(Key&0x08)m_key3=0;else m_key3=1;
- if(Key&0x10)m_key4=0;else m_key4=1;
- if(Key&0x20)m_key5=0;else m_key5=1;
- if(Key&0x40)m_key6=0;else m_key6=1;
- if(Key&0x80)m_key7=0;else m_key7=1;
- UpdateData(FALSE);
- CDialog::OnTimer(nIDEvent);
- }
- //DEL void CMFCAppDlg::OnRadio1()
- //DEL {
- //DEL // TODO: Add your control notification handler code here
- //DEL
- //DEL }
- void CMFCAppDlg::OnBuzzer()
- {
- // TODO: Add your control notification handler code here
- UpdateData(TRUE);
- char buff[16];
- ULONG nOutput;
- buff[0]=0x03;
- buff[1]=!m_buzzer;
- if (!DeviceIoControl(hDevice,
- FORUSB_IOCTL_Write,
- buff,
- 16,
- NULL,
- 0,
- &nOutput,
- NULL)
- )
- {
- m_Mes="ERROR: DeviceIoControl returns .";
- UpdateData(FALSE);
- }
- }
- void CMFCAppDlg::OnChangeLeft()
- {
- // TODO: If this is a RICHEDIT control, the control will not
- // send this notification unless you override the CDialog::OnInitDialog()
- // function and call CRichEditCtrl().SetEventMask()
- // with the ENM_CHANGE flag ORed into the mask.
- UpdateData();
- if(m_left.GetLength()==0||m_right.GetLength()==0)return ;
- char buff[16];
- ULONG nOutput;
- buff[0]=0x02;
- buff[1]=m_left.GetAt(0)-'0';
- buff[2]=m_right.GetAt(0)-'0';
- if (!DeviceIoControl(hDevice,
- FORUSB_IOCTL_Write,
- buff,
- 16,
- NULL,
- 0,
- &nOutput,
- NULL)
- )
- {
- m_Mes="ERROR: DeviceIoControl returns .";
- UpdateData(FALSE);
- }
- // TODO: Add your control notification handler code here
-
- }
- void CMFCAppDlg::OnChangeRight()
- {
- // TODO: If this is a RICHEDIT control, the control will not
- // send this notification unless you override the CDialog::OnInitDialog()
- // function and call CRichEditCtrl().SetEventMask()
- // with the ENM_CHANGE flag ORed into the mask.
- UpdateData();
- if(m_left.GetLength()==0||m_right.GetLength()==0)return ;
- char buff[16];
- ULONG nOutput;
- buff[0]=0x02;
- buff[1]=m_left.GetAt(0)-'0';
- buff[2]=m_right.GetAt(0)-'0';
- if (!DeviceIoControl(hDevice,
- FORUSB_IOCTL_Write,
- buff,
- 16,
- NULL,
- 0,
- &nOutput,
- NULL)
- )
- {
- m_Mes="ERROR: DeviceIoControl returns .";
- UpdateData(FALSE);
- }
- // TODO: Add your control notification handler code here
-
- }
- void CMFCAppDlg::OnChangelcd1()
- {
- // TODO: If this is a RICHEDIT control, the control will not
- // send this notification unless you override the CDialog::OnInitDialog()
- // function and call CRichEditCtrl().SetEventMask()
- // with the ENM_CHANGE flag ORed into the mask.
- UpdateData();
- // if(m_lcd1.GetLength()==0)return;
- if(!IsE(&m_lcd1))
- {
- MessageBox("只能输入英文字符");
- m_lcd1="";
- UpdateData(FALSE);
- }
- char buff[18];
- ULONG nWritten;
- memset(buff,' ',18*sizeof(char));
- buff[0]=1;
- buff[1]=1;
- for(int i=0;i!=m_lcd1.GetLength();i++)buff[i+2]=m_lcd1.GetAt(i);
- WriteFile(hDevice, buff, 18, &nWritten, NULL);
- // TODO: Add your control notification handler code here
-
- }
- BOOL CMFCAppDlg::IsE(CString * str)
- {
- for(int i=0;i!=str->GetLength();i++)
- if(str->GetAt(i)>255||str->GetAt(i)<0)return FALSE;
- return TRUE;
- }
- void CMFCAppDlg::OnChangeLcd2()
- {
- // TODO: If this is a RICHEDIT control, the control will not
- // send this notification unless you override the CDialog::OnInitDialog()
- // function and call CRichEditCtrl().SetEventMask()
- // with the ENM_CHANGE flag ORed into the mask.
- UpdateData();
- // if(m_lcd1.GetLength()==0)return;
- if(!IsE(&m_lcd2))
- {
- MessageBox("只能输入英文字符");
- m_lcd2="";
- UpdateData(FALSE);
- }
- char buff[18];
- ULONG nWritten;
- memset(buff,' ',18*sizeof(char));
- buff[0]=1;
- buff[1]=2;
- for(int i=0;i!=m_lcd2.GetLength();i++)buff[i+2]=m_lcd2.GetAt(i);
- WriteFile(hDevice, buff, 18, &nWritten, NULL);
- // TODO: Add your control notification handler code here
-
- }