📄 read.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: read.cpp
* Long name: VHPD1394 asynchronous read example
* Description: This sample demonstrates how to use the VHPD1394
* device driver to read data from a peer device
* in asynchronous mode (asynchronous READ request).
* This sample is based on the VHPDLib C++ class library.
*
* 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 "CVhpd.h"
// print prefixes
#define PFX "READ: "
#define PFXERR "READ Error: "
// standard help message
static const char g_UseHelp[] = "For help, use READ -h.";
// GLOBAL VARIABLES
// 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 memory at peer device that will be read
// (no default, always has to be specified)
__int64 g_StartAddress;
// number of bytes to read from peer device (size of data block)
// (default 4)
unsigned long g_BytesToRead =4;
// number of bytes to read with a single request on bus
// if this value is smaller than g_BytesToRead the data block will be read
// using several asynchronous requests
// 0 for maximum request packet size
// (default 0)
unsigned long g_BytesPerRequest =0;
// buffer that will receive the bytes read
// will be allocated before initiating the read
unsigned char* g_OutputBuffer;
// name of the output file (optional)
const char* g_OutFileName =NULL;
/*******************************************************************/
// support functions
/*******************************************************************/
//
// display usage information
//
void
PrintHelp(void)
{
fprintf(stdout,
"\n"
"usage: READ Address <Options>\n"
"\n"
" Address (hexadecimal, required)\n"
" IEEE 1394 address of memory at peer device that will be read\n"
"\n"
" Options:\n"
" -dDevNumber: zero-based index of the peer device (optional, default %d)\n"
" (use the SCAN example to display a list of available devices)\n"
" -nNmbOfBytes: number of bytes to read from peer device (optional, default %d)\n"
" -bRequestSize: number of bytes to read with a single asynchronous request,\n"
" 0 for maximum allowed (optional, default %d)\n"
" -fOutputFile: file that will be used to store the data bytes\n"
" Data will be printed on screen if this parameter is omitted\n"
" The file will be OVERWRITTEN WITHOUT ANY WARNING!\n"
,g_DevNumber, g_BytesToRead, g_BytesPerRequest);
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;
// number of bytes to read
case 'n':
// read number
if ( 1==sscanf(p+1," %i ",&val) ) {
// store value
g_BytesToRead = val;
} else {
// invalid option format, ignore it
fprintf(stderr, PFXERR"Invalid argument '%s' ignored\n",argv[i]);
}
break;
// bytes per request packet
case 'b':
// read number
if ( 1==sscanf(p+1," %i ",&val) ) {
// store value
g_BytesPerRequest = 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
/*******************************************************************/
// open a device handle
// VHPD1394 device instance
CVhpd Device;
// 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");
return 10;
}
// open a handle to the device
unsigned long Status;
Status = Device.Open(g_DevNumber,DevList,&VhpdDefaultIID);
if ( Status != VHPD_STATUS_SUCCESS ) {
// ERROR !!!
fprintf(stderr, PFXERR"Failed to open device %d (0x%08X)\n",g_DevNumber,Status);
goto Exit;
}
// device opened, destroy the device list, we don't need it anymore
CVhpd::DestroyDeviceList(DevList);
DevList = NULL;
/*******************************************************************/
// issue the asynchronous read request(s)
// init input structure
VHPD_ASYNC_READ AsyncRead;
memset(&AsyncRead,0,sizeof(AsyncRead));
AsyncRead.DestinationAddress.QuadPart = g_StartAddress;
AsyncRead.BlockSize = g_BytesPerRequest;
// no additional options
AsyncRead.Flags = 0;
// output buffer
g_OutputBuffer = new unsigned char[g_BytesToRead];
memset(g_OutputBuffer,0,g_BytesToRead);
// helper variables
unsigned long BytesTransferred; BytesTransferred = 0;
fprintf(stdout, PFX"Reading %d bytes from address 0x%012I64X\n",g_BytesToRead,g_StartAddress);
// initiate the asynchronous read request
Status = Device.AsyncReadSync(
&AsyncRead, // VHPD_ASYNC_READ structure describing the read operation
g_OutputBuffer, // buffer that will receive the bytes read
g_BytesToRead, // number of bytes to read
&BytesTransferred, // on return: number of bytes read
INFINITE // timeout while waiting for the operation to complete
);
if ( Status != VHPD_STATUS_SUCCESS ) {
// ERROR !!!
fprintf(stderr, PFXERR"AsyncReadSync failed (0x%08X)\n",Status);
goto Exit;
}
if ( BytesTransferred != g_BytesToRead ) {
// ERROR !!!
fprintf(stderr, PFXERR"AsyncReadSync returned invalid number of bytes (Exp %d, Got %d)\n",
g_BytesToRead,BytesTransferred);
goto Exit;
}
fprintf(stdout, PFX"Read succeeded (%d bytes returned)\n",BytesTransferred);
if ( g_OutFileName != NULL ) {
// save data to the output file if a file name was given
FILE* file;
file = fopen(g_OutFileName, "wb");
if ( file != NULL ) {
fwrite(g_OutputBuffer, 1, g_BytesToRead, file);
fclose(file);
}
} else {
// no output file, print returned data to screen
fprintf(stdout, PFX"Data read from address 0x%012I64X:\n",g_StartAddress);
for (unsigned long i=0; i<BytesTransferred; i++) {
if ( (i%4) == 0 ) {
fprintf(stdout,"\n ");
}
fprintf(stdout,"0x%02X ",g_OutputBuffer[i]);
}
fprintf(stdout,"\n");
}
/*******************************************************************/
// ERROR!!! or normal exit
// release claimed resources
Exit:
delete[] g_OutputBuffer;
Device.Close();
return 0;
} // main
/*************************** EOF **************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -