如何动态显示buffer中收到的数据
现在我用FPGA想CYPRESS FX2LP芯片发送数据,设置为每16个字节发送一次,但是PC端的程序是直到收集满512个字节之后才在电脑上显示结果,如何让它实时显示呢?最近几天才刚接触这个东西,求各位帮忙。下面是源代码:
#include "CyUSBHost.h"
#define SISUSBPID 0x1003
typedef struct _USBContext
{
CCyUSBDevice * USBDevice ;
CCyUSBEndPoint * InEndpt ;
CCyUSBEndPoint * OutEndpt ;
USHORT VendorPid ;
HANDLE TestThread ;
}USBCONTEXT;
DWORD WINAPI XferLoop ( LPVOID Param ) ;
int main ( int argn, char ** argv )
{
USBCONTEXT * context = new USBCONTEXT ;
context -> USBDevice = new CCyUSBDevice ;
context -> VendorPid = SISUSBPID ;
int DevCnt = context -> USBDevice ->DeviceCount() ;
int i = 0 ;
for( i = 0; i < DevCnt ; i ++ )
{
USB_CONFIGURATION_DESCRIPTOR ConfDesc;
if ( context -> USBDevice->Open( i ) )
{
CCyUSBEndPoint *endpt = 0;
int epts = context -> USBDevice->EndPointCount();
context -> USBDevice->GetConfigDescriptor(&ConfDesc);
printf( "USB Dev %s PD 0x%X \n", context -> USBDevice->DeviceName , context->USBDevice->ProductID);
for( int j = 0; j < epts ; j ++ )
{
endpt = context -> USBDevice->EndPoints[j] ;
printf(" Ep 0x%02X, Att %d ", endpt->Address, endpt->Attributes );
if ( endpt->Attributes == 2 ) // bulk mode
{
if (endpt->Address & 0x80)
{
printf( " IN " ) ;
context -> InEndpt = endpt ;
}
else
{
context -> OutEndpt = endpt ;
printf ( " OUT " ) ;
}
}
printf ( "\n") ;
}
printf ("\n") ;
if ( context->USBDevice->ProductID == context->VendorPid )
{
context -> TestThread = CreateThread( 0, 0, XferLoop, context, 0, 0 ) ;
break;
}
}
}
WaitForSingleObject( context -> TestThread , INFINITE ) ;
CloseHandle( context -> TestThread ) ;
delete context -> USBDevice ;
delete context ;
return 0;
}
DWORD WINAPI XferLoop ( LPVOID Param )
{
USBCONTEXT * context = ( USBCONTEXT * ) Param ;
bool success;
LONG nSuccess = 0;
LONG nFailure = 0;
OVERLAPPED outOvLap, inOvLap;
char s[24]; ZeroMemory( s, 24 );
LONG xfer = 512;
PUCHAR data = new UCHAR[xfer]; ZeroMemory(data,xfer);
PUCHAR inData = new UCHAR[xfer]; ZeroMemory(inData,xfer);
if( !context )
return 0 ;
outOvLap.hEvent = CreateEvent(NULL, false, false, 0);
inOvLap.hEvent = CreateEvent(NULL, false, false, 0);
context->OutEndpt->TimeOut = 2000;
context->InEndpt->TimeOut = 2000;
bool bLoop = true ;
for ( ; bLoop ; )
{
LONG outlen,inlen,len;
outlen = inlen = len = xfer; // Use temp var because XferData can change the value of len
UCHAR *outContext = context->OutEndpt->BeginDataXfer(data,outlen,&outOvLap);
UCHAR *inContext = context->InEndpt->BeginDataXfer(inData,inlen,&inOvLap);
context->OutEndpt->WaitForXfer(&outOvLap,2000);
context->InEndpt->WaitForXfer(&inOvLap,2000);
success = context->OutEndpt->FinishDataXfer(data, outlen, &outOvLap,outContext);
success = context->InEndpt->FinishDataXfer(inData,inlen, &inOvLap,inContext);[/color]
if ( success )
{
int i = 0;
printf ( "OUT : ") ;
for( i = 0; i < xfer ; i ++ )
printf("%d ", data[i] ++ );
printf("\n");
printf ( "IN : ") ;
for( i = 0; i < xfer ; i ++ )
printf("%d ", inData[i]);
printf("\n");
nSuccess++;
Sleep ( 2000 ) ;
}
else
nFailure++;
if ((!success) )
bLoop = false;
}
CloseHandle(outOvLap.hEvent);
CloseHandle(inOvLap.hEvent);
delete [] data;
delete [] inData;
context->USBDevice->Close();
return 0;
}