📄 rxasy.cpp
字号:
/************************************************************************
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
* PURPOSE.
************************************************************************/
/************************************************************************
*
* Module: rxasy.cpp
* Long name: VHPD1394 asynchronous receive sample
* Description: This sample demonstrates how to use the VHPD1394
* device driver to receive asynchronous data at high
* bandwidth.
* This sample is based on the VHPDLib C++ class library.
* It mainly demonstrates the usage of the class
* CVhpdDataSlave.
*
* Runtime Env.: implemented as Win32 console application
* Used link libraries:
* vhpdlib.lib, setupapi.lib, user32.lib
* Author(s): Frank Senf
* Company: Thesycon GmbH, Ilmenau
************************************************************************/
// standard includes
#include <stdio.h>
#include <conio.h>
// definitions of used classes
#include "AsyncReceiver.h"
// print prefixes
#define PFX "RXASY: "
#define PFXERR "RXASY Error: "
// standard help message
static const char g_UseHelp[] = "For help, use RXASY -h.";
// GLOBAL VARIABLES
// asynchronous receiver instance
CAsyncReceiver g_AsyncReceiver;
// zero-based index of the peer device within the Windows-internal
// device list
// not related to the device's node ID, see the "SCAN" example for
// further details
// (default 0)
int g_DevNumber =0;
// IEEE 1394 start address of address space used to receive the data
// (no default, always has to be specified)
__int64 g_StartAddress;
// size, in bytes, of the address space used for reception
// (default 200*1024)
// NOTE: do not set to a multiple of 64*1024 bytes (see also "Problems.txt")
unsigned long g_BufferSize = 200*1024;
// parameters of the buffer pool used for reception
// number of buffers in pool
unsigned long g_NumberOfBuffers =4;
// buffers will be directly mapped to address space used for reception
// thus, buffer size must be equal to size of address space
// name of the output file (optional)
const char* g_OutFileName =NULL;
/*******************************************************************/
// support functions
/*******************************************************************/
//
// display usage information
//
void
PrintHelp(void)
{
fprintf(stdout,
"\n"
"usage: RXASY Address <Options>\n"
"\n"
" Address (hexadecimal, required)\n"
" IEEE 1394 address of memory used for reception\n"
"\n"
" Options:\n"
" -dDevNumber: zero-based index of the peer device that transmits the data\n"
" (use the SCAN example to display a list of available devices)\n"
" (optional, default %d)\n"
" -sBufferSize: size of the memory used for reception, in bytes\n"
" (do not set to a multiple of 64*1024 bytes, see problems.txt\n"
" for details)\n"
" (optional, default %d)\n"
" -nBufferCount: number of buffers used for data reception\n"
" (optional, default %d)\n"
" -fOutputFile: file that will be used to store the received data\n"
" Data will not be stored if this parameter is omitted\n"
" The file will be OVERWRITTEN WITHOUT ANY WARNING!\n"
,g_DevNumber, g_BufferSize, g_NumberOfBuffers);
fprintf(stdout,"\nPress any key to continue\n");
getch();
} // PrintHelp
/*******************************************************************/
// main function
/*******************************************************************/
int __cdecl main(int argc, char *argv[])
{
/*******************************************************************/
// fixed command line argument
// check for required arguments
if ( argc < 2 ) {
// at least Address has to be specified
PrintHelp();
return 1;
}
// store values for required arguments
__int64 val64;
if ( 1==sscanf(argv[1]," %I64x ",&val64) ) {
// store value
g_StartAddress = val64;
} else {
// invalid Address parameter, we cannot continue
fprintf(stderr, PFXERR"Invalid Address argument '%s'\n",argv[1]);
return 4;
}
/*******************************************************************/
// optional command line options
int i;
int val;
char* p;
for ( i=1; i<argc; i++ ) {
p = argv[i];
if ( (*p) == '-' ) {
p++;
switch ( *p ) {
case 'h':
case 'H':
case '?':
// help
PrintHelp();
return 0;
// device number
case 'd':
// read number
if ( 1==sscanf(p+1," %i ",&val) ) {
if ( val>=0 && val<=62 ) {
// store value
g_DevNumber = val;
} else {
// invalid device number, ignore it
fprintf(stderr, PFXERR"Invalid device number %d ignored\n",val);
}
} else {
// invalid option format, ignore it
fprintf(stderr, PFXERR"Invalid argument '%s' ignored\n",argv[i]);
}
break;
// buffer size (in bytes)
case 's':
// read number
if ( 1==sscanf(p+1," %i ",&val) ) {
// check for a multiple of 64*1024 bytes, this will cause problems
if ( val % (64*1024) == 0 ) {
fprintf(stderr, PFXERR"Invalid argument '%s'\n",argv[i]);
fprintf(stderr, PFXERR"Multiples of 64*1024 bytes will cause serious problems\n");
return 5;
} else {
// store value
g_BufferSize = val;
}
} else {
// invalid option format, ignore it
fprintf(stderr, PFXERR"Invalid argument '%s' ignored\n",argv[i]);
}
break;
// buffer count
case 'n':
// read number
if ( 1==sscanf(p+1," %i ",&val) ) {
// store value
g_NumberOfBuffers = val;
} else {
// invalid option format, ignore it
fprintf(stderr, PFXERR"Invalid argument '%s' ignored\n",argv[i]);
}
break;
// output file name
case 'f':
if ( *(p+1) != 0 ) {
// save string pointer
g_OutFileName = p+1;
} else {
// invalid filename
fprintf(stderr, PFXERR"Invalid argument '%s' ignored\n",argv[i]);
}
break;
// unknown options
default:
fprintf(stderr, PFXERR"Unrecognized option '%s' ignored. %s\n",argv[i],g_UseHelp);
break;
} // switch
}
} // for
/*******************************************************************/
// setup g_AsyncReceiver
unsigned long Status;
// open the output file if a file name was given
if ( g_OutFileName != NULL ) {
Status = g_AsyncReceiver.OpenFile(g_OutFileName);
if ( Status != 0 ) {
// ERROR !!!
fprintf(stderr, PFXERR"Open output file '%s' failed (0x%08X)\n",g_OutFileName,Status);
return 10;
}
}
// prepare an OS-internal device list used for the open call
HDEVINFO DevList; DevList = NULL;
// device list will contain devices that provide the VHPD_IID interface
// please refer to to the documentation (chapter 7.4) for details on how
// to define your private interface (strongly recommended)
const GUID VhpdDefaultIID = VHPD_IID;
DevList = CVhpd::CreateDeviceList(&VhpdDefaultIID);
if ( DevList == NULL ) {
// ERROR !!!
fprintf(stderr, PFXERR"CreateDeviceList failed\n");
goto Exit;
}
// open a handle to a device, we use device zero for now
Status = g_AsyncReceiver.Open(g_DevNumber,DevList,&VhpdDefaultIID);
if ( Status != VHPD_STATUS_SUCCESS ) {
// ERROR !!!
fprintf(stderr, PFXERR"Failed to open the device (0x%08X)\n",Status);
goto Exit;
}
// device opened, destroy the device list, we don't need it anymore
CVhpd::DestroyDeviceList(DevList);
DevList = NULL;
// initialize address space
VHPD_UINT64 StartAddress;
StartAddress.QuadPart = g_StartAddress;
// trigger address is set to last byte of address space
VHPD_UINT64 TriggerAddress;
TriggerAddress.QuadPart = g_StartAddress + g_BufferSize - 1;
Status = g_AsyncReceiver.Setup(
StartAddress, // start address of address space
g_BufferSize, // size in bytes of address space
TriggerAddress, // trigger address
VHPD_FLAG_ADRRNG_ACCESS_WRITE, // allowed access options
VHPD_FLAG_ADRRNG_NOTIFY_WRITE, // requested notification options
g_NumberOfBuffers, // number of reception buffers
g_BufferSize // size in bytes of a reception buffer
);
if ( Status != VHPD_STATUS_SUCCESS ) {
// ERROR !!!
fprintf(stderr, PFXERR"Failed to initialize AsyncReceiver (0x%08X)\n",Status);
goto Exit;
}
// start worker thread of g_AsyncReceiver
// this worker thread continuously submits all buffers of the g_AsyncReceiver pool
// to the driver and delivers completed buffers for processing
if ( !g_AsyncReceiver.StartThread() ) {
// ERROR !!!
fprintf(stderr, PFXERR"Failed to start worker thread\n");
goto Exit;
}
/*******************************************************************/
// now loop until we are told to exit
fprintf(stdout, PFX"Receiving asynchronous data ... press any key to exit\n");
for(;;) {
// query current data rate transported via this file handle
VHPD_QUERY_RATE_COUNTER RateCount;
memset(&RateCount,0x0,sizeof(RateCount));
Status = g_AsyncReceiver.QueryRateCounter(&RateCount);
if (Status != VHPD_STATUS_SUCCESS) {
// ERROR !!!
fprintf(stderr, "\n"PFXERR"Failed to query data rate (0x%08X)",Status);
break;
}
// convert to bytes per second
__int64 r;
r = RateCount.CurrentMeanValue/*bytes*/;
r = (1000*r) / RateCount.FilterDelay/*ms*/;
// display state of data reception
fprintf(stdout, PFX"Avg. rate is %I64d Bytes/s %d buffer errors (last 0x%08X) \r",
(__int64)r, g_AsyncReceiver.GetErrorCount(), g_AsyncReceiver.GetLastError());
// check if thread is still running
// worker thread may terminate itself, even if ShutdownThread was not called
// (e.g. because PostProcessBuffer returns false or an internal error was detected)
if ( g_AsyncReceiver.ThreadExited() ) {
fprintf(stdout,"\n"PFX"Worker thread has terminated ... aborting.\n");
break;
}
// check for user break
if ( kbhit() ) {
getch();
break;
}
// suspend some time
Sleep(500);
} // for()
fprintf(stdout, "\n"PFX"Exiting ...\n");
/*******************************************************************/
// ERROR!!! or normal exit
// release claimed resources
Exit:
if ( !g_AsyncReceiver.ShutdownThread() ) {
// ERROR !!!
fprintf(stderr, PFXERR"FATAL: Failed to stop worker thread\n");
}
g_AsyncReceiver.CloseFile();
g_AsyncReceiver.DeleteSlave();
g_AsyncReceiver.Close();
return 0;
} // main
/*************************** EOF **************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -