⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 一个关于usb编程的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*++

  
    Module Name:
    Main.c
    
   Note: it's ugly!                  
                    
--*/

#include <windows.h>
#include <malloc.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <process.h>

#include "resource.h"
#include "main.h"

#include <winioctl.h>
#include "..\..\..\..\drivers\ezusbdrv\ezusbsys.h"

HINSTANCE hGInstance = NULL;
UCHAR transfercount = 0;
BOOL CALLBACK bMainDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
int my_sscanf(LPCSTR cBuffer, DWORD *data);

void FillBuffer(PUCHAR buffer, PUCHAR pattern, int length);
BOOL BufferHasErrors(PUCHAR buffer, PUCHAR pattern, int length, BOOL inEqualsNotOut);

HWND    ghOutputBox                      = NULL;
BOOLEAN StopTest = FALSE;

char *build_time = __TIME__;
char *build_date = __DATE__;


/*******************************
*WinMain: Windows Entry point  *
********************************/
int PASCAL WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR     lpCmdLine,
                   int       nCmdShow)
{
   hGInstance=hInstance;
   
   if(DialogBox(hInstance,"MAIN_DIALOG",NULL,(DLGPROC)bMainDlgProc)==-1)
      MessageBox(NULL,"Unable to create root dialog!","DialogBox failure",MB_ICONSTOP);
   
   return 0;
}

void TestThread(
  LPVOID lpParameter   // thread data
)
{
   HWND hDlg = (HWND) lpParameter;
   HWND hOutputBox;
   HANDLE  hDevice = NULL;
   char    pcDriverName[MAX_DRIVER_NAME]   = "";
   DWORD pass = 0;
   char    tempbuff[256];
   int     nItems                          = 0;
   ULONG       i;

   UCHAR outPattern, inPattern;
   WORD  outPacketSize, inPacketSize;
   UCHAR outBuffer[64], inBuffer[64];
   BULK_TRANSFER_CONTROL bulkControl;
   BOOL ERRORS = FALSE;
   BOOLEAN bResult                         = FALSE;
   int     nBytes                          = 0;

   hOutputBox = GetDlgItem (hDlg, IDC_OUTPUT_BOX);

   SetDlgItemInt(hDlg,IDC_TESTPASS,0,FALSE);

   //
   // get handles to the device
   //
   GetDlgItemText (hDlg, IDC_DRIVER_NAME, pcDriverName, MAX_DRIVER_NAME);
   if (bOpenDriver (&hDevice, pcDriverName) != TRUE)
   {
      SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Failed to Open Device");
      return;
   }

   outPattern = inPattern = GetDlgItemInt (hDlg, IDC_PATTERN, NULL, FALSE);
   outPacketSize = inPacketSize = GetDlgItemInt (hDlg, IDC_PACKETSIZE, NULL, FALSE);
   if (outPacketSize > 64)
   {
      SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Error: Invalid Packet Size");
      StopTest = TRUE;
   }



   while (!StopTest  && !ERRORS)
   {

      //
      // Write to all of the OUT pipes
      //
      for (i = 0; i < 7; i++)
      {
         FillBuffer(&outBuffer[0], &outPattern, outPacketSize);

         bulkControl.pipeNum = i;

         // Perform the BULK OUT
         bResult = DeviceIoControl (hDevice,
                     IOCTL_EZUSB_BULK_WRITE,
                     &bulkControl,
                     sizeof(BULK_TRANSFER_CONTROL),
                     &outBuffer[0],
                     outPacketSize,
                     &nBytes,
                     NULL);

         if (bResult != TRUE)
         {
            MessageBox(hDlg, "bulk OUT failed","Error",MB_ICONINFORMATION);
            ERRORS = TRUE;
            break;
         }

         if (IsDlgButtonChecked(hDlg,IDC_VERBOSE) == BST_CHECKED)
         {
            wsprintf (tempbuff, "Wrote %d bytes to Pipe %d",outPacketSize,i);
            SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)tempbuff);
         }

         // bump the packet size for the next transfer
         // packet size cycles from 1 to 64 bytes
         if (IsDlgButtonChecked(hDlg,IDC_INCREMENT) == BST_CHECKED)
         {
            outPacketSize++;
            if (outPacketSize == 65)
               outPacketSize = 1;
         }
      }


      //
      // Read from each IN pipe and verify the data
      //
      for (i = 7; i < 14; i++)
      {
         bulkControl.pipeNum = i;

         // Perform the BULK OUT
         bResult = DeviceIoControl (hDevice,
                     IOCTL_EZUSB_BULK_READ,
                     &bulkControl,
                     sizeof(BULK_TRANSFER_CONTROL),
                     &inBuffer[0],
                     inPacketSize,
                     &nBytes,
                     NULL);

         if (bResult != TRUE)
         {
            MessageBox(hDlg, "bulk IN failed","Error",MB_ICONINFORMATION);
            ERRORS = TRUE;
            break;
         }

         if (IsDlgButtonChecked(hDlg,IDC_VERBOSE) == BST_CHECKED)
         {
            wsprintf (tempbuff, "Read %d bytes from Pipe %d",nBytes,i);
            SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)tempbuff);
         }

         if (nBytes != inPacketSize)
         {
            MessageBox(hDlg, "IN returned wrong number of packets","Error",MB_ICONINFORMATION);
            ERRORS = TRUE;
            break;
         }

         if (BufferHasErrors(&inBuffer[0], &inPattern, inPacketSize, IsDlgButtonChecked(hDlg,IDC_IN_EQUALS_NOT_OUT) == BST_CHECKED))
         {
            MessageBox(hDlg, "IN returned wrong data","Error",MB_ICONINFORMATION);
            ERRORS = TRUE;
            break;
         }


         // bump the packet size for the next transfer
         // packet size cycles from 1 to 64 bytes
         if (IsDlgButtonChecked(hDlg,IDC_INCREMENT) == BST_CHECKED)
         {
            inPacketSize++;
            if (inPacketSize == 65)
               inPacketSize = 1;
         }

      }

      pass++;

      if (!(pass % 10))
      {
         SetDlgItemInt(hDlg,IDC_TESTPASS,pass,FALSE);
      }


      MAINTAIN_OUTPUT_BOX (hOutputBox, nItems);

  }
   CloseHandle(hDevice);

   MessageBox(hDlg, NULL,"Exiting Test",MB_ICONINFORMATION);

}


/**************************************************
* Main Dialog proc                               *
**************************************************/

BOOL CALLBACK bMainDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
   HWND    hOutputBox                      = NULL;
   HANDLE  hDevice                         = NULL;
   char    pcDriverName[MAX_DRIVER_NAME]   = "";
   BOOLEAN bResult                         = FALSE;
   int     nBytes                          = 0;
   PVOID   pvBuffer                        = 0;
   int     nItems                          = 0;
   HFONT   hFont                           = NULL;
   ULONG   ulLength                         = 0;
   char    tempbuff[256];
   
   // Get a handle to the output box
   ghOutputBox = hOutputBox = GetDlgItem (hDlg, IDC_OUTPUT_BOX);
   
   MAINTAIN_OUTPUT_BOX (hOutputBox, nItems);
   
   switch(message)
   {
   case WM_LBUTTONDOWN:
      hOutputBox = GetDlgItem (hDlg, IDC_OUTPUT_BOX);
      
      MAINTAIN_OUTPUT_BOX (hOutputBox, nItems);
      
      SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"you clicked");
      break;
      
   case WM_INITDIALOG:
      // Get a handle to the output box
      hOutputBox = GetDlgItem (hDlg, IDC_OUTPUT_BOX);
      
      // Setup the std system font
      hFont = GetStockObject(SYSTEM_FONT);
      SendMessage (hOutputBox, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE,0));
      sprintf(tempbuff,"EZ-USB Bulk Test - built %s %s",build_time,build_date);
      SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)tempbuff);
      
      // Setup the default symbolic name for the device driver           
      SetDlgItemText (hDlg, IDC_DRIVER_NAME, "Ezusb-0");
      SetDlgItemInt (hDlg, IDC_PACKETSIZE, 1, FALSE);
      SetDlgItemInt (hDlg, IDC_PATTERN, 0, FALSE);
      CheckDlgButton(hDlg,IDC_INCREMENT,BST_CHECKED);
      CheckDlgButton(hDlg,IDC_IN_EQUALS_NOT_OUT,BST_CHECKED);
      
      break; /*end WM_INITDIALOG case*/
      
   case WM_COMMAND:
      switch(LOWORD(wParam))
      {
       
         case IDC_ABOUT:
            sprintf(tempbuff,"EZ-USB Bulk Endpoint Test - built %s %s",build_time,build_date);
            MessageBox(hDlg, tempbuff,"About EZ-USB",MB_ICONINFORMATION);
            break;
       
         case IDOK:
         case IDCANCEL:
            EndDialog(hDlg,0);
            break;
       
         case IDC_CLEAR:
         {
            hOutputBox = GetDlgItem (hDlg, IDC_OUTPUT_BOX);

            SendMessage (hOutputBox, LB_RESETCONTENT, 0, 0);
         }
         break;

         case IDC_STARTBULKTEST:
            StopTest = FALSE;
            _beginthread(TestThread,0,hDlg);
         break;

         case IDC_STOPBULKTEST:
            StopTest = TRUE;
         break;

         case IDC_RESET_HLD:
         case IDC_RESET_RLS:
         {
            VENDOR_REQUEST_IN	myRequest;

            // Get a handle to the output box
            hOutputBox = GetDlgItem (hDlg, IDC_OUTPUT_BOX);

            MAINTAIN_OUTPUT_BOX (hOutputBox, nItems);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -