描述:
目的就是通过usb2.0设备和pc进行高速数据交换。买那个usb外设板子的时候提供有事例程序,但是是往设备写和读一次的。现在为了测试其速度到底是不是达到usb2.0高速标准,我将其改为:
// TODO: Add your command handler code here
//BULK数据的写操作
int i = 0;
char *buffer;
char senddat[512] = {0};
ULONG length;
BULK_TRANSFER_CONTROL bulkControl;
int recnBytes = 0;
/*写Endpoint2*/
bulkControl.pipeNum = 0; //0,1管道为写
for(i = 0;i<512;i++)
{
senddat[i] = i;
}
buffer = &senddat[0];
length =theApp.m_USBbufferlong; //usb2.0最大数据包512bytes
DWORD dwStartTime = ::timeGetTime(); //计算传输时间用
for(int n = 0;n < 100000;n++) //读写总数据为100m
{
bulkControl.pipeNum = 0;
buffer = &senddat[0];
Sx2BulkdataTrans( &bulkControl,
buffer,
length,
&recnBytes);
/*读回Endpoint6,并显示*/
bulkControl.pipeNum = 2; //2,3管道为度
for(i = 0;i<512;i++)
{
senddat[i] = 0;
}
buffer = &senddat[0];
Sx2BulkdataTrans( &bulkControl,
buffer,
length,
&recnBytes);
}
DWORD dwElapsedTime = ::timeGetTime() - dwStartTime;
theApp.Out("%d\n",dwElapsedTime);
Sx2BulkdataTrans()函数原型如下:
BOOLEAN Sx2BulkdataTrans(PVOID bulkControl,
char *buffer,
int bufferSize,
int *recnBytes)
{
int nBytes = 0;
bool bResult = FALSE;
PUSBD_PIPE_INFORMATION pPipe = pSx2InterfaceInfo->Pipes;
//管道信息,打开设备时已得到
//执行读写操作的判断
DWORD ioctl_val = IOCTL_EZUSB_BULK_WRITE;
if(pPipe[((PBULK_TRANSFER_CONTROL)bulkControl)->pipeNum].EndpointAddress >> 7)
{
ioctl_val = IOCTL_EZUSB_BULK_READ;
}
//执行读写操作的判断
bResult = DeviceIoControl ( hDevice,
ioctl_val,
bulkControl,
sizeof (BULK_TRANSFER_CONTROL),
buffer,
bufferSize,
(unsigned long *)&nBytes,
NULL);
if(bResult == FALSE)
{
return FALSE;
}
*recnBytes = nBytes;
return TRUE;
}
如此测试下来用的时间为65秒多,也就是说每秒才1.Xm的速度,对于一个usb2.0的设备不可能有这么慢吧,是不是有什么没有配置阿?还望各位高手指点
解决方案1:
学习
解决方案2: 在这里,只要你的PC驱动没有错误,就不会这么慢的。
你还是看一下USB设备端是怎么处理的吧。
每次发512bytes已经很快了,一般情况下你的USB设备的Buffer一般只有1K或更少,所以,
你最多每次发送1K数据,但是在你发下一个1K数据之前,你的设备还没有处理完,
那么PC端只有等待。
还有可能就是你的PC主板不支持USB2.0。
倒,怎么用DeviceIoControl,可以用ReadFile或者WriteFile直接读写bulk pipe阿。