04-22-2013 08:08 AM
Hi I have to evaluate a large amount of data from a USB port an intended to do with the follown functions but I can get it to work.
Main()
{
if(EuroloopUSBPortOpen != 0)
{
if(OpenEuroloopUSBPort(errorMsg) != 0)
{
sprintf (&errorMsg[strlen(errorMsg)], "Error opening BTM USB port \n");
return FAIL;
}
}
nFileHandle = OpenFile (szEULfileName, VAL_READ_WRITE, VAL_APPEND, VAL_BINARY);
// Evaluate step 4 in TC2 specification
for(nFrameIndex = 0; nFrameIndex < nFramesToRead; nFrameIndex++)
{
ReadEuroloopUSBPort (nFileHandle, errorMsg);
}
GetFileInfo (szEULfileName, nFileSize);
nNrOfValidBytes = EvaluateResultFileTC2 (nFileHandle, nFileSize, nSSCodeAWG, pBERResult);
}
Function in same file as main
int EvaluateResultFileTC2(char nfileHadle,
int *nFileSize,
int nSSCode,
BERResult *pBERResult)
{
int i;
int nValidLoops;
char RxBufferVal;
char tmpRxBufferVal;
long int nCntValidBytes;
long int nOldCntValidBytes;
unsigned char *RxBuffer;
BOOL bIsFirstLoop;
nCntValidBytes = 0;
tmpRxBufferVal = 0;
bIsFirstLoop = TRUE;
RxBuffer = malloc ((int)nFileSize + 1);
ReadFile (nfileHadle, RxBuffer, (int)nFileSize);
RxBuffer[(int)nFileSize] = '\0';
for(i = 0; i < (int)nFileSize; i++)
{
if((unsigned char)RxBuffer[i] == EUL_TELEGRAM_HEADER)
{
if((unsigned char)(RxBuffer[i + 1] & 0x40))
{
/*** Shift annother bit ***/
tmpRxBufferVal = (tmpRxBufferVal << 1) + 1;
}
else
{
tmpRxBufferVal = (tmpRxBufferVal << 1);
}
RxBufferVal = (unsigned char)tmpRxBufferVal & 0x7F;
if((unsigned char)RxBufferVal == REPTESTPATTERN)
{
RxBufferVal = 0;
tmpRxBufferVal = 0;
nCntValidBytes = (unsigned char)RxBuffer[i + 3] << 8;
nCntValidBytes += (unsigned char)RxBuffer[i + 4];
if(bIsFirstLoop == TRUE)
{
nOldCntValidBytes = nCntValidBytes;
bIsFirstLoop = FALSE;
nValidLoops++;
}
// the pattern should be repeted each seven bytes.
if(nCntValidBytes == nOldCntValidBytes + 7)
{
/*** Count up if the pattern is consecutive ***/
nValidLoops++;
nOldCntValidBytes = nCntValidBytes;
if(nValidLoops >= nMinValidFrames && nValidLoops < nFramesToRead)
{
ResolveSScode (nCntValidBytes, nSSCode, nValidLoops, pBERResult);
}
}
else
{
nValidLoops = 0;
}
}
}
}
free (RxBuffer);
CloseFile (nfileHadle);
return nValidLoops;
}
Common function in common function files
int ReadEuroloopUSBPort(int fileHadle,
char *szTestMessage)
{
FT_STATUS ftStatus;
unsigned long EventDWord;
unsigned long TxBytes;
unsigned long RxBytes = 0;
unsigned long BytesReceived;
unsigned char RxBuffer[10000] = {0};
BOOL bThreadDone = FALSE;
BOOL bHeaderFounded = FALSE;
UINT i;
UINT portWaitLoop = 0;
if (!EuroloopUSBPortOpen())
{
sprintf(&szTestMessage[strlen(szTestMessage)],
"Error USB port is not open\n");
return -1;
}
do
{
ftStatus = FT_GetStatus(FTDIHandle,&RxBytes,&TxBytes,&EventDWord);
if (RxBytes > 0)
{
for(i = 0; i < RxBytes; i++)
{
/*** Read the header first one character at the time ***/
if (bHeaderFounded == FALSE)
{
ftStatus = FT_Read(FTDIHandle, RxBuffer, 1, &BytesReceived);
}
/*** Find the header ***/
if ( RxBuffer[0] == EUL_TELEGRAM_LENGHT)
{
bHeaderFounded = TRUE;
}
if(ftStatus == FT_OK && bHeaderFounded == TRUE)
{
/*** Read the rest of the message ***/
ftStatus = FT_Read(FTDIHandle, &RxBuffer[LENGHT_OF_EUL_HEADER],
(LENGTH_OF_EUL_FRAME - LENGHT_OF_EUL_HEADER), &BytesReceived);
if(ftStatus == FT_OK)
{
if(BytesReceived == LENGTH_OF_EUL_FRAME - LENGHT_OF_EUL_HEADER)
{
WriteFile(fileHadle,(const char*)RxBuffer, EUL_TELEGRAM_LENGHT);
}
else
{
sprintf(&szTestMessage[strlen(szTestMessage)],
"One frame is uncompleted \n");
}
}
}
}
bHeaderFounded = FALSE;
bThreadDone = TRUE;
}
else
{
/* Nothing to read and timeout*/
if (portWaitLoop == 10)
{
bThreadDone = TRUE;
}
}
portWaitLoop++;
}while (!bThreadDone);
return 0;
}
It goes rigth to read the USB port and save in to file in ReadEuroloopUSBPort but when i read back the data in EvaluateResultFileTC2 the
data pointer RxBuffer is empty.
I include windows.h before formatio.h i use the same handle for write and read.
Can someone point what i do wrong.
Regards
04-22-2013 08:20 AM
I forget to inform you that I use Labwindows/CVI for this application.
04-22-2013 09:59 AM
A quick look at your code makes me feel that you must insert a SetFilePtr (nfileHadle, 0, 0); call before ReadFile.
You are writing data to the file with WriteFile, thus movine the file pointer to the end of written data. Next you want to read back from the file, but the file pointer is already at EOF, so ReadFIle reads nothing. SetFilePtr returns the pointer to the beginning of file and ReadFile should be able to read back your data.