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

📄 usb.cpp

📁 F2812实现电机控制源程序.rar
💻 CPP
字号:
#include "windows.h"
#include "winioctl.h"
#include "ezusbsys.h"
#include "usb.h"

#define	VR_Reset     	0xb0
#define	VR_Intr         0xb1

//---------------------------------------------------------------------------
TUSB::TUSB()
{
    hDevice = INVALID_HANDLE_VALUE;
}

//---------------------------------------------------------------------------
bool TUSB::OpenDevice()
{
    hDevice = CreateFile("\\\\.\\Ezusb-0",
                        GENERIC_WRITE,
                        FILE_SHARE_WRITE,
                        NULL,
                        OPEN_EXISTING,
                        0,
                        0);

    if (hDevice == INVALID_HANDLE_VALUE) {
        hDevice = CreateFile("\\\\.\\Ezusb-1",
                        GENERIC_WRITE,
                        FILE_SHARE_WRITE,
                        NULL,
                        OPEN_EXISTING,
                        0,
                        0);
        if (hDevice == INVALID_HANDLE_VALUE)
            return false;
        else
            return true;
    }
    else
        return true;
}

//---------------------------------------------------------------------------
bool TUSB::CloseDevice()
{
  if (hDevice != INVALID_HANDLE_VALUE)
  {

        CloseHandle(hDevice);
        hDevice = INVALID_HANDLE_VALUE;
        return true;
  }
  else
        return false;
}

//---------------------------------------------------------------------------
bool TUSB::Reset()
{
    bool                                    Success ;
    VENDOR_OR_CLASS_REQUEST_CONTROL         VR;
    unsigned long                           nBytes;

    if (hDevice != INVALID_HANDLE_VALUE )
    {
      VR.direction = 0;   // (0=host to device, 1=device to host)
      VR.requestType = 2;
      VR.recepient = 0;
      VR.request = VR_Reset;
      VR.requestTypeReservedBits = 0;

      Success = DeviceIoControl (hDevice,
                    IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST,
                    &VR,
                    sizeof(VENDOR_OR_CLASS_REQUEST_CONTROL),
                    NULL,
                    0,
                    &nBytes,
                    NULL);
    }
    return Success;
}

//---------------------------------------------------------------------------
bool TUSB::ReadData(unsigned char *buf)
{
    bool                                    Success ;
    BULK_TRANSFER_CONTROL                   BR;
    unsigned long                           nBytes;

    if (hDevice != INVALID_HANDLE_VALUE )
    {
      BR.pipeNum = 2;

      Success = DeviceIoControl (hDevice,
	         IOCTL_EZUSB_BULK_READ,
	         &BR,
	         sizeof(BULK_TRANSFER_CONTROL),
	         &buf[0],
             32768,
	         &nBytes,
	         NULL);

      Success = DeviceIoControl (hDevice,
	         IOCTL_EZUSB_BULK_READ,
	         &BR,
	         sizeof(BULK_TRANSFER_CONTROL),
	         &buf[32768],
             32768,
	         &nBytes,
	         NULL);
    }

    return Success;
}

//---------------------------------------------------------------------------
bool TUSB::WriteData(unsigned char *buf)
{
    bool                                    Success ;
    BULK_TRANSFER_CONTROL                   BR;
    unsigned long                           nBytes;

    if (hDevice != INVALID_HANDLE_VALUE )
    {
      BR.pipeNum = 2;

      Success = DeviceIoControl (hDevice,
	         IOCTL_EZUSB_BULK_WRITE,
	         &BR,
	         sizeof(BULK_TRANSFER_CONTROL),
	         &buf[0],
             32768,
	         &nBytes,
	         NULL);

      Success = DeviceIoControl (hDevice,
	         IOCTL_EZUSB_BULK_WRITE,
	         &BR,
	         sizeof(BULK_TRANSFER_CONTROL),
	         &buf[32768],
             32768,
	         &nBytes,
	         NULL);
    }

    return Success;
}

//---------------------------------------------------------------------------
bool TUSB::SendCommand(unsigned char *buf)
{
    bool                                    Success ;
    VENDOR_OR_CLASS_REQUEST_CONTROL         VR;
    BULK_TRANSFER_CONTROL                   BR;
    unsigned long                           nBytes;

    if (hDevice != INVALID_HANDLE_VALUE )
    {
      BR.pipeNum = 0;

      Success = DeviceIoControl (hDevice,
	         IOCTL_EZUSB_BULK_WRITE,
	         &BR,
	         sizeof(BULK_TRANSFER_CONTROL),
	         &buf[0],
             512,
	         &nBytes,
	         NULL);
    }

    if (hDevice != INVALID_HANDLE_VALUE )
    {
      VR.direction = 0;   // (0=host to device, 1=device to host)
      VR.requestType = 2;
      VR.recepient = 0;
      VR.request = VR_Intr;
      VR.requestTypeReservedBits = 0;

      Success = DeviceIoControl (hDevice,
                    IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST,
                    &VR,
                    sizeof(VENDOR_OR_CLASS_REQUEST_CONTROL),
                    NULL,
                    0,
                    &nBytes,
                    NULL);
    }

    return Success;
}

//---------------------------------------------------------------------------
bool TUSB::GetResponse(unsigned char *buf)
{
    bool                                    Success ;
    BULK_TRANSFER_CONTROL                   BR;
    unsigned long                           nBytes;


    if (hDevice != INVALID_HANDLE_VALUE )
    {
      BR.pipeNum = 2;

      Success = DeviceIoControl (hDevice,
	         IOCTL_EZUSB_BULK_READ,
	         &BR,
	         sizeof(BULK_TRANSFER_CONTROL),
	         &buf[0],
             512,
	         &nBytes,
	         NULL);
    }

    return Success;
}

⌨️ 快捷键说明

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