|
发表于 2012-4-20 11:35:25
|
显示全部楼层
C++的话就简单多了,就用windows.h里面定义的BYTE * 就行,BYTE 就是unsigned char。我现在就是用byte,没任何问题。我把我的库的头文件贴出来你看看就知道了。传进去的是一个BYTE *数组和一个长度。
#include "stdafx.h"
#include "windows.h"
#include "winioctl.h"
#include "malloc.h"
ULONG DEV_BULK_CONTROL_OUT_EDP = 0;
ULONG DEV_BULK_DATA_OUT_EDP = 1;
ULONG DEV_BULK_CONTROL_IN_EDP = 2;
ULONG DEV_BULK_DATA_IN_EDP = 3;
#define I2CProgram16KA0is0 0xF0
#define I2CProgram16KA0is1 0xF1
#define I2CRead16KA0is0 0xF2
#define I2CRead16KA0is1 0xF3
#define A0TOGGLE 0xF4
#define DATA_BLOCK_SIZE 256
typedef struct _VENDOR_REQUEST_IN
{
BYTE bRequest;
WORD wValue;
WORD wIndex;
WORD wLength;
BYTE direction;
BYTE bData;
} VENDOR_REQUEST_IN, *PVENDOR_REQUEST_IN;
typedef struct __usb_Dev_Descriptor__ {
UCHAR bLength;
UCHAR bDescriptorType;
USHORT bcdUSB;
UCHAR bDeviceClass;
UCHAR bDeviceSubClass;
UCHAR bDeviceProtocol;
UCHAR bMaxPacketSize0;
USHORT idVendor;
USHORT idProduct;
USHORT bcdDevice;
UCHAR iManufacturer;
UCHAR iProduct;
UCHAR iSerialNumber;
UCHAR bNumConfigurations;
} Usb_Device_Descriptor, *pUsb_Device_Descriptor;
typedef struct __usb_String_Descriptor__ {
UCHAR bLength;
UCHAR bDescriptorType;
WCHAR bString[1];
} Usb_String_Descriptor, *pUsb_String_Descriptor;
typedef PVOID USBD_PIPE_HANDLE;
typedef PVOID USBD_CONFIGURATION_HANDLE;
typedef PVOID USBD_INTERFACE_HANDLE;
typedef enum _USBD_PIPE_TYPE {
UsbdPipeTypeControl,
UsbdPipeTypeIsochronous,
UsbdPipeTypeBulk,
UsbdPipeTypeInterrupt
} USBD_PIPE_TYPE;
typedef struct _USBD_PIPE_INFORMATION {
USHORT MaximumPacketSize;
UCHAR EndpointAddress;
UCHAR Interval;
USBD_PIPE_TYPE PipeType;
USBD_PIPE_HANDLE PipeHandle;
ULONG MaximumTransferSize;
ULONG PipeFlags;
} USBD_PIPE_INFORMATION, *PUSBD_PIPE_INFORMATION;
typedef struct _USBD_INTERFACE_INFORMATION {
USHORT Length;
UCHAR InterfaceNumber;
UCHAR AlternateSetting;
UCHAR Class;
UCHAR SubClass;
UCHAR Protocol;
UCHAR Reserved;
USBD_INTERFACE_HANDLE InterfaceHandle;
ULONG NumberOfPipes;
USBD_PIPE_INFORMATION Pipes[1];
} USBD_INTERFACE_INFORMATION, *PUSBD_INTERFACE_INFORMATION;
typedef LONG USBD_STATUS;
UCHAR InterfaceInfo[1024];
PUSBD_INTERFACE_INFORMATION pInterface;
typedef struct _BULK_TRANSFER_CONTROL
{
ULONG pipeNum;
} BULK_TRANSFER_CONTROL, *PBULK_TRANSFER_CONTROL;
#define Ezusb_IOCTL_INDEX 0x0800
#define IOCTL_Ezusb_GET_PIPE_INFO CTL_CODE(FILE_DEVICE_UNKNOWN,Ezusb_IOCTL_INDEX + 0,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_Ezusb_GET_DEVICE_DESCRIPTOR CTL_CODE(FILE_DEVICE_UNKNOWN,Ezusb_IOCTL_INDEX + 1,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_EZUSB_BULK_READ CTL_CODE(FILE_DEVICE_UNKNOWN,Ezusb_IOCTL_INDEX + 19,METHOD_OUT_DIRECT,FILE_ANY_ACCESS)
#define IOCTL_EZUSB_BULK_WRITE CTL_CODE(FILE_DEVICE_UNKNOWN,Ezusb_IOCTL_INDEX + 20,METHOD_IN_DIRECT,FILE_ANY_ACCESS)
#define IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST CTL_CODE(FILE_DEVICE_UNKNOWN,Ezusb_IOCTL_INDEX+22,METHOD_IN_DIRECT,FILE_ANY_ACCESS)
#define IOCTL_Ezusb_VENDOR_REQUEST CTL_CODE(FILE_DEVICE_UNKNOWN,Ezusb_IOCTL_INDEX+5,METHOD_BUFFERED,FILE_ANY_ACCESS)
typedef struct _VENDOR_OR_CLASS_REQUEST_CONTROL
{
// transfer direction (0=host to device, 1=device to host)
UCHAR direction;
// request type (1=class, 2=vendor)
UCHAR requestType;
// recipient (0=device,1=interface,2=endpoint,3=other)
UCHAR recepient;
//
// see the USB Specification for an explanation of the
// following paramaters.
//
UCHAR requestTypeReservedBits;
UCHAR request;
USHORT value;
USHORT index;
} VENDOR_OR_CLASS_REQUEST_CONTROL, *PVENDOR_OR_CLASS_REQUEST_CONTROL;
char pcDriverName[] = "Ezusb-0";
extern "C" __declspec(dllexport) BOOL bOpenDriver (HANDLE & hDevice)
{
char completeDeviceName[64] = "";
strcat (completeDeviceName,"\\\\.\\");
strcat (completeDeviceName,pcDriverName);
hDevice = CreateFile(completeDeviceName,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
if (hDevice == INVALID_HANDLE_VALUE)
return (FALSE);
else
return (TRUE);
}
extern "C" __declspec(dllexport) int WriteBuf(int edpnum,BYTE * pBuf,int Length)
{
HANDLE hDevice;
if (bOpenDriver (hDevice) != TRUE)
{
MessageBox(NULL, "Please connect Hardware.",0,0);
return (-1);
}
BULK_TRANSFER_CONTROL bulkControl;
BOOL bResult = FALSE;
int nBytes = 0;
bulkControl.pipeNum = (ULONG)edpnum;
bResult = DeviceIoControl(hDevice,IOCTL_EZUSB_BULK_WRITE,&bulkControl,sizeof(BULK_TRANSFER_CONTROL),pBuf,Length,(unsigned long *)&nBytes,NULL);
if(!bResult)
{
MessageBox(NULL, "Failed To Write Specific Endpoint","Error",MB_ICONSTOP);
CloseHandle(hDevice);
return -1;
}
CloseHandle(hDevice);
return nBytes;
}
extern "C" __declspec(dllexport) int ReadBuf(int edpnum,BYTE * pBuf,int Length)
{
HANDLE hDevice;
if (bOpenDriver (hDevice) != TRUE)
{
MessageBox(NULL, "Please connect Hardware.",0,0);
return (-1);
}
BULK_TRANSFER_CONTROL bulkControl;
BOOL bResult = FALSE;
int nBytes = 0;
bulkControl.pipeNum = (ULONG)edpnum;
bResult = DeviceIoControl(hDevice,IOCTL_EZUSB_BULK_READ,&bulkControl,sizeof(BULK_TRANSFER_CONTROL),pBuf,Length,(unsigned long *)&nBytes,NULL);
if(!bResult)
{
MessageBox(NULL, "Failed To Read Specific Endpoint","Error",MB_ICONSTOP);
CloseHandle(hDevice);
return -1;
}
CloseHandle(hDevice);
return nBytes;
}
extern "C" __declspec(dllexport) BOOL Get_EZUSB_Device_Info()
{
HANDLE hDevice;
if (bOpenDriver (hDevice) != TRUE)
{
MessageBox(NULL, "Please connect Hardware.",0,0);
return (-1);
}
PVOID pvBuffer = 0;
PVOID pdBuffer = 0;
BOOL bResult = FALSE;
int nBytes = 0;
USHORT bVendor;
USHORT bProduct;
PUSBD_PIPE_INFORMATION pPipe;
pvBuffer = (void*)malloc(sizeof(Usb_String_Descriptor) + 128);
pdBuffer = (void*)malloc(sizeof(Usb_Device_Descriptor) + 128);
// Get VID/PID from Device Descriptor
bResult = DeviceIoControl(hDevice,IOCTL_Ezusb_GET_DEVICE_DESCRIPTOR,pdBuffer,sizeof (Usb_Device_Descriptor),pdBuffer,sizeof (Usb_Device_Descriptor),(unsigned long *)&nBytes,NULL);
bVendor = ((Usb_Device_Descriptor*)((void*)pdBuffer))->idVendor;
bProduct = ((Usb_Device_Descriptor*)((void*)pdBuffer))->idProduct;
if (bResult != TRUE)
{
MessageBox(NULL,"Get VID/PID Failed",0,0);
CloseHandle(hDevice);
return FALSE;
}
else if(bVendor != 0x0474 || bProduct != 0x1128)
{
MessageBox(NULL,"Specific Device Not Found (Ven_0474&Dev_1128)","Error",MB_ICONSTOP);
CloseHandle(hDevice);
return FALSE;
}
bResult = DeviceIoControl(hDevice,IOCTL_Ezusb_GET_PIPE_INFO,NULL,0,InterfaceInfo,sizeof(InterfaceInfo),(unsigned long *)&nBytes,NULL);
if (bResult == TRUE)
{
pInterface = (PUSBD_INTERFACE_INFORMATION) InterfaceInfo;
pPipe = pInterface->Pipes;
}
else
{
MessageBox(NULL,"Get Pipe Info Failed",0,0);
CloseHandle(hDevice);
return FALSE;
}
free (pvBuffer); // Free the memory
CloseHandle(hDevice);
return TRUE;
}
extern "C" __declspec(dllexport) int InitUSB()
{
HANDLE hDevice;
if (bOpenDriver (hDevice) != TRUE)
{
MessageBox(NULL, "Please connect Hardware.",0,0);
return (-1);
}
CloseHandle(hDevice);
if(!Get_EZUSB_Device_Info())
return -1;
if (pInterface->NumberOfPipes != 2)
{
MessageBox(NULL, "Specific Hardware not found.",0,0);
return (-1);
}
return 1;
}
extern "C" __declspec(dllexport) int Set_Device_Mode(int Value)
{
HANDLE hDevice;
if (bOpenDriver (hDevice) != TRUE)
{
MessageBox(NULL, "Please connect Hardware.",0,0);
return (-1);
}
VENDOR_OR_CLASS_REQUEST_CONTROL venderctrl;
venderctrl.direction=0;
venderctrl.requestType=2;
venderctrl.recepient=0;
venderctrl.request=0x80;
venderctrl.requestTypeReservedBits=0;
venderctrl.index=0;
venderctrl.value=(USHORT)Value;
BOOL bResult = FALSE;
int nBytes = 0;
bResult = DeviceIoControl(
hDevice,
IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST,
&venderctrl,
sizeof(venderctrl),
NULL,
0,
(unsigned long *)&nBytes,
NULL
);
if (bResult != TRUE)
{
MessageBox(NULL,"Operation Failed.",0,0);
CloseHandle(hDevice);
return -1;
}
CloseHandle(hDevice);
return 0;
}
extern "C" __declspec(dllexport) int RestartDevice()
{
HANDLE hDevice;
if (bOpenDriver (hDevice) != TRUE)
{
MessageBox(NULL, "Please connect Hardware.",0,0);
return (-1);
}
BOOL bResult = FALSE;
VENDOR_REQUEST_IN myRequest;
myRequest.bRequest = 0xA0;
myRequest.wValue = 0xE600; // using CPUCS.0 in FX2
myRequest.wIndex = 0x00;
myRequest.wLength = 0x01;
myRequest.bData = 1; // set RESET
myRequest.direction = 0x00;
int nBytes = 0;
bResult = DeviceIoControl (hDevice,IOCTL_Ezusb_VENDOR_REQUEST,&myRequest,sizeof(VENDOR_REQUEST_IN),NULL,0,(unsigned long *)&nBytes,NULL);
myRequest.bRequest = 0xA0;
myRequest.wValue = 0xE600; // using CPUCS.0 in FX2
myRequest.wIndex = 0x00;
myRequest.wLength = 0x01;
myRequest.bData = 0; // set RESET
myRequest.direction = 0x00;
bResult = DeviceIoControl (hDevice,IOCTL_Ezusb_VENDOR_REQUEST,&myRequest,sizeof(VENDOR_REQUEST_IN),NULL,0,(unsigned long *)&nBytes,NULL);
if(bResult != TRUE)
{
MessageBox(NULL,"Operation Failed.",0,0);
CloseHandle(hDevice);
return -1;
}
CloseHandle(hDevice);
return 0;
}
这个库改了之后目前可以支持多线程,HANDLE切记不能共享,要不然多个线程调用就锁了。 |
|