📄 ybk_usb.~cpp
字号:
//---------------------------------------------------------------------------
#include <windows.h>
#include "stdio.h"
#include "winioctl.h"
#include "ezusbsys.h"
#include "main.h"
//---------------------------------------------------------------------------
// Important note about DLL memory management when your DLL uses the
// static version of the RunTime Library:
//
// If your DLL exports any functions that pass String objects (or structs/
// classes containing nested Strings) as parameter or function results,
// you will need to add the library MEMMGR.LIB to both the DLL project and
// any other projects that use the DLL. You will also need to use MEMMGR.LIB
// if any other projects which use the DLL will be performing new or delete
// operations on any non-TObject-derived classes which are exported from the
// DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
// EXE's to use the BORLNDMM.DLL as their memory manager. In these cases,
// the file BORLNDMM.DLL should be deployed along with your DLL.
//
// To avoid using BORLNDMM.DLL, pass string information using "char *" or
// ShortString parameters.
//
// If your DLL uses the dynamic version of the RTL, you do not need to
// explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------
#pragma argsused
extern "C" __declspec(dllexport) HANDLE __stdcall OpenDevice(UCHAR DeviceIndex);
int SetDeviceInterface(HANDLE DeviceHandle);
extern "C" __declspec(dllexport) void __stdcall CloseDevice(HANDLE DeviceHandle);
extern "C" __declspec(dllexport) int __stdcall BulKWrite(HANDLE DeviceHandle,
int PipeIndex,
UCHAR *Pdata,
int length);
extern "C" __declspec(dllexport) int __stdcall BulKRead(HANDLE DeviceHandle,
int PipeIndex,
UCHAR *Pdata,
int length);
extern "C" __declspec(dllexport) int __stdcall IsoRead(HANDLE DeviceHandle,
ULONG PipeIndex,
ULONG PacketSize,
ULONG PacketCount,
ULONG FramesPerBuffer,
ULONG BufferCount,
UCHAR *Pdata);
/*向指定的同步管道中写入定长数据;
输入:DeviceHandle 设备句柄;
PipeIndex 写入管道号;
Pdata 输出数据的指针;
length 长度,字节数;
返回:成功:TRUE 失败:FALSE:*/
extern "C" __declspec(dllexport) int __stdcall Isowrite(HANDLE DeviceHandle,
ULONG PipeIndex,
ULONG PacketSize,
ULONG PacketCount,
ULONG FramesPerBuffer,
ULONG BufferCount,
UCHAR *Pdata);
extern "C" __declspec(dllexport) int __stdcall StartIsoStream(HANDLE DeviceHandle,
ULONG PipeIndex,
ULONG PacketSize,
ULONG PacketCount,
ULONG FramesPerBuffer,
ULONG BufferCount);
extern "C" __declspec(dllexport) int __stdcall StopIsoStream(HANDLE DeviceHandle);
extern "C" __declspec(dllexport) int __stdcall ReadIsoBuffer(HANDLE DeviceHandle,
ULONG PipeIndex,
ULONG PacketSize,
ULONG PacketCount,
ULONG FramesPerBuffer,
ULONG BufferCount,
UCHAR *Pdata,
int *Readlength);
/*指定设备发送GET_DESCRIPTOR 的USB标准设备请求;
输入:DeviceHandle 设备句柄;
Pdata 输出数据的指针;
返回:成功:TRUE 失败:FALSE:*/
extern "C" __declspec(dllexport) int __stdcall GetDescriptor(HANDLE DeviceHandle,UCHAR *Pdata);
extern "C" __declspec(dllexport) int __stdcall LoadDownCode(HANDLE DeviceHandle,int codeoffset,UCHAR *Pdata,int codesize);
extern "C" __declspec(dllexport) int __stdcall GetPipeInf(HANDLE DeviceHandle,UCHAR *Pdata);
extern "C" __declspec(dllexport) int __stdcall ResetPipe(HANDLE DeviceHandle,ULONG Pipeindex);
extern "C" __declspec(dllexport) int __stdcall AbortPipe(HANDLE DeviceHandle,ULONG Pipeindex);
extern "C" __declspec(dllexport) int __stdcall GetConfiguration(HANDLE DeviceHandle,UCHAR *Pdata);
extern "C" __declspec(dllexport) int __stdcall GetStrDescriptor(HANDLE DeviceHandle,UCHAR *Pdata);
extern "C" __declspec(dllexport) int __stdcall CpuReset(HANDLE DeviceHandle,int HoldOrReset);
extern "C" __declspec(dllexport) int __stdcall PortReset(HANDLE DeviceHandle);
extern "C" __declspec(dllexport) int __stdcall GetUsbLastError(HANDLE DeviceHandle,char *Pdata);
extern "C" __declspec(dllexport) int __stdcall GetFrameNum(HANDLE DeviceHandle,ULONG frameNum);
void GetURBErrString(ULONG urbStatus, char* pStr);
void delay();
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
{
return 1;
}
//---------------------------------------------------------------------------
#define BYTES_PER_LINE 0x10
ULONG nbytes=0;
int success;
ULONG datasize;
extern "C" __declspec(dllexport) HANDLE __stdcall OpenDevice(UCHAR DeviceIndex)
{
char completeDeviceName[64] = "";
char pcMsg[64] = "";
HANDLE phDeviceHandle;
char devname[2]="";
int tempint;
devname[0]=char(DeviceIndex+48);
strcat (completeDeviceName,
"\\\\.\\Ezusb-"
);
strcat (completeDeviceName,
devname
);
phDeviceHandle = CreateFile(completeDeviceName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (phDeviceHandle != INVALID_HANDLE_VALUE)
tempint=SetDeviceInterface(phDeviceHandle);
if (tempint!=0)
return (phDeviceHandle);
else return 0 ;
}
int SetDeviceInterface(HANDLE DeviceHandle)
{
SET_INTERFACE_IN interinface;
interinface.interfaceNum=0;
interinface.alternateSetting=1;
success=DeviceIoControl(DeviceHandle,
IOCTL_Ezusb_SETINTERFACE,
&interinface,
sizeof(SET_INTERFACE_IN),
NULL,
0,
&nbytes,
NULL);
return(success);
}
extern "C" _declspec(dllexport) void __stdcall CloseDevice(HANDLE DeviceHandle)
{
CloseHandle (DeviceHandle);
}
extern "C" __declspec(dllexport) int __stdcall BulKWrite(HANDLE DeviceHandle,
int PipeIndex,
UCHAR *Pdata,
int length)
{
BULK_TRANSFER_CONTROL bulkcontrol;
bulkcontrol.pipeNum=PipeIndex;
success=DeviceIoControl(DeviceHandle,
IOCTL_EZUSB_BULK_WRITE,
&bulkcontrol,
sizeof(BULK_TRANSFER_CONTROL),
Pdata,
length,
&nbytes,
NULL);
return(success);
}
extern "C" __declspec(dllexport) int __stdcall BulKRead(HANDLE DeviceHandle,
int PipeIndex,
UCHAR *Pdata,
int length)
{ BULK_TRANSFER_CONTROL bulkcontrol;
bulkcontrol.pipeNum=PipeIndex;
success=DeviceIoControl(DeviceHandle,
IOCTL_EZUSB_BULK_READ,
&bulkcontrol,
sizeof(BULK_TRANSFER_CONTROL),
Pdata,
length,
&nbytes,
NULL);
return(success);
}
extern "C" __declspec(dllexport) int __stdcall IsoRead(HANDLE DeviceHandle,
ULONG PipeIndex,
ULONG PacketSize,
ULONG PacketCount,
ULONG FramesPerBuffer,
ULONG BufferCount,
UCHAR *Pdata)
{
ISO_TRANSFER_CONTROL IsoControl;
IsoControl.PipeNum=PipeIndex;
IsoControl.PacketSize=PacketSize;
IsoControl.PacketCount=PacketCount;
IsoControl.FramesPerBuffer=FramesPerBuffer;
IsoControl.BufferCount=BufferCount;
datasize=PacketCount*(PacketSize+sizeof(USBD_ISO_PACKET_DESCRIPTOR));
if (ResetPipe(DeviceHandle,PipeIndex))
{
success=DeviceIoControl(DeviceHandle,
IOCTL_EZUSB_ISO_READ,
&IsoControl,
sizeof(ISO_TRANSFER_CONTROL),
Pdata,
datasize,
&nbytes,
NULL);
}
else success=0;
return success;
}
extern "C" __declspec(dllexport) int __stdcall Isowrite(HANDLE DeviceHandle,
ULONG PipeIndex,
ULONG PacketSize,
ULONG PacketCount,
ULONG FramesPerBuffer,
ULONG BufferCount,
UCHAR *Pdata)
{
ISO_TRANSFER_CONTROL IsoControl;
IsoControl.PipeNum=PipeIndex;
IsoControl.PacketSize=PacketSize;
IsoControl.PacketCount=PacketCount;
IsoControl.FramesPerBuffer=FramesPerBuffer;
IsoControl.BufferCount=BufferCount;
datasize=PacketCount*(PacketSize+sizeof(USBD_ISO_PACKET_DESCRIPTOR));
if (ResetPipe(DeviceHandle,PipeIndex))
{
success=DeviceIoControl(DeviceHandle,
IOCTL_EZUSB_ISO_WRITE,
&IsoControl,
sizeof(ISO_TRANSFER_CONTROL),
Pdata,
datasize,
&nbytes,
NULL);
}
else success=0;
return success;
}
extern "C" __declspec(dllexport) int __stdcall StartIsoStream(HANDLE DeviceHandle,
ULONG PipeIndex,
ULONG PacketSize,
ULONG PacketCount,
ULONG FramesPerBuffer,
ULONG BufferCount)
{
ISO_TRANSFER_CONTROL IsoControl;
IsoControl.PipeNum=PipeIndex;
IsoControl.PacketSize=PacketSize;
IsoControl.PacketCount=PacketCount;
IsoControl.FramesPerBuffer=FramesPerBuffer;
IsoControl.BufferCount=BufferCount;
if (ResetPipe(DeviceHandle,PipeIndex))
{
success=DeviceIoControl (DeviceHandle,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -