📄 disoftports.cpp
字号:
//#############################################################################
//*****************************************************************************
//
// Copyright (c) 2005 Xi'an R&D Center Advantech Automation Corp.
//
// This is An Unpublished Work Containing Confidential And Proprietary
// Information Which Is The Property Of Advantech Automation Corp.
//
// Any Disclosure, Use, Or Reproduction, Without Written Authorization From
// Advantech Automation Corp., Is Strictly Prohibit.
//
//*****************************************************************************
//#############################################################################
//
// File Name : DiSoftPorts.CPP
// Revision : 1.0
//
// Created : 05/30/2005
//
// Description :
// This examples performs the operation for DI ports .
//
//-----------------------------------------------------------------------------
//
// FUNCTIONS:
//
// Main() - Process entry.
//
// ShowDevList() - Show the list of installed devices.
//
// SelectDevice() - Prompt user to select a device and return the handle.
//
// SelectPort() - Prompt user to choose the start port and port count for
// data output.
//
// ReadPorts() - Read the data from the selected ports.
//
// ErrReport() - Report the error returned from driver.
//
// ClearStream() - Clear the get stream.
//
//-----------------------------------------------------------------------------
//
// API Used:
//
// DRV_DeviceOpen,
// DRV_DeviceClose,
// DRV_GetErrorMessage,
// DRV_DeviceGetProperty,
// DRV_DeviceGetNumOfList,
// DRV_DeviceGetList,
// AdxDioReadDiPorts,
//
//-----------------------------------------------------------------------------
#include "DiSoftPorts.h"
#include <assert.h> //For assert statement
//----------------------------------------------------------------------------
//The maximum count of devices.
#define MAX_ENTRIES 100
//The maximum of the length of all of the numbers. In fact, you can only input
//a number that lesses than 999 and larges than 0.
#define MAX_NUMLONG 4
//----------------------------------------------------------------------------
int main(int argc, char* argv[])
{
//Useful variables
LONG lDevHandle = 0; //Driver handle
BOOL bKeepRun = TRUE;
cout << "*************************************************************" << endl;
cout << "Welcome to the DI ports DEMO program!" << endl;
cout << "*************************************************************" << endl;
while ( bKeepRun )
{
BYTE btOption = 0; //Seletive option
cout << "Please Select what you want to do: " << endl;
cout << "input 'R' for Running the program. or 'E' for Exiting." << endl;
cin.width( 1 );
cin >> btOption;
ClearStream();
switch ( btOption )
{
case 'r':
case 'R':
{
cout << "Please select a device according to the device list: " << endl;
//Firstly, select device
DWORD * pDevNumList = NULL;
SHORT sDevCount = 0;
if ( !ShowDevList( &pDevNumList, sDevCount ) )
{
cout << "Can not show the device list!" << endl;
if ( pDevNumList != NULL )
{
delete[] pDevNumList;
pDevNumList = NULL;
}
break;
}
//Secondly, open the device. If the device has openned, close it.
if ( lDevHandle != 0 )
{
DRV_DeviceClose( &lDevHandle );
}
lDevHandle = SelectDevice( sDevCount );
if ( 0 == lDevHandle )
{
cout << "Can not open the device!" << endl;
delete[] pDevNumList;
pDevNumList = NULL;
break;
}
//Thirdly, Get count of DI ports
ULONG lDataLen = sizeof( LONG );
LONG lData = 0;
LONG lErrCde = DRV_DeviceGetProperty(
lDevHandle,
CFG_DiPortCount,
&lData,
&lDataLen);
if ( lErrCde != SUCCESS )
{
cout << "Can not get the DI port count!" << endl;
ErrReport( lErrCde );
delete[] pDevNumList;
pDevNumList = NULL;
break;
}
cout << "DI port count: " << lData << endl;
//Fouthly, select ports and read the selected ports
DWORD lPortStart = 0;
DWORD lPortCount = 0;
SelectPort( lPortStart, lPortCount, lData );
if ( !ReadPorts( lDevHandle, lPortStart, lPortCount) )
{
cout << "Can not read the ports!" << endl;
delete[] pDevNumList;
pDevNumList = NULL;
break;
}
//Finally, release memory.
delete[] pDevNumList;
pDevNumList = NULL;
}
break;
case 'e':
case 'E':
{
bKeepRun = FALSE;
}
break;
default:
{
cout << "Invalid params. Please input again." << endl;
}
break;
}
}
//Finally, close the device
if ( lDevHandle != 0 )
{
DRV_DeviceClose( &lDevHandle );
}
return 0;
}
// **************************************************************************
// Design Notes: Show the list of installed devices.
// Return TRUE for success or FALSE for failure.
// --------------------------------------------------------------------------
BOOL ShowDevList( DWORD * *dwpDevNumList, SHORT & sDevCount)
{
assert( dwpDevNumList != NULL );
DEVLIST * pDevList = NULL;
SHORT sDevNum = 0;
LONG lErrCde = SUCCESS;
//Firstly, get the count of installed devices and allocate memory.
lErrCde = DRV_DeviceGetNumOfList( &sDevNum );
if ( lErrCde != SUCCESS )
{
ErrReport( lErrCde );
return FALSE;
}
pDevList = new DEVLIST[ sDevNum ];
if ( NULL == pDevList )
{
cout << "System Error: " << "There are no enough memory!" << endl;
return FALSE;
}
memset( pDevList, 0, sizeof(DEVLIST) * sDevNum );
*dwpDevNumList = new DWORD[ sDevNum ];
if ( NULL == dwpDevNumList )
{
cout << "System Error: " << "There are no enough memory!" << endl;
delete[] pDevList;
pDevList = NULL;
return FALSE;
}
memset( *dwpDevNumList, 0, sizeof(DWORD) * sDevNum );
//Secondly, get the device list.
SHORT sOutEntries = 0;
lErrCde = DRV_DeviceGetList( pDevList, sDevNum, &sOutEntries );
if ( lErrCde != SUCCESS )
{
ErrReport( lErrCde );
return FALSE;
}
//Thirdly, show the devlist.
for ( SHORT i = 0; i < sOutEntries; i++ )
{
cout << "Devcie" << i << ": " << pDevList[i].szDeviceName << endl;
(*dwpDevNumList)[i] = pDevList[i].dwDeviceNum;
}
//Finally, release memory.
if ( pDevList != NULL )
{
delete[] pDevList;
pDevList = NULL;
}
//How many devices we get really.
sDevCount = sOutEntries;
return TRUE;
}
// **************************************************************************
// Design Notes: Select a device according to the number user input.
// Return the device handle for success or 0 for failure.
// --------------------------------------------------------------------------
LONG SelectDevice( SHORT sDevCount )
{
cout << "Please input the number of the device you want to open: " << endl;
cout << "Device: ";
DWORD dwDevNum = 0;
//Firstly, get the input number
InputNumStream( dwDevNum );
if ( (SHORT)dwDevNum >= sDevCount )
{
cout << "Invalid Device number, please confirm it." << endl;
return 0L;
}
//Secondly, open the device
LONG lErrCde = SUCCESS;
LONG DevHandle = 0;
lErrCde = DRV_DeviceOpen( dwDevNum, &DevHandle );
if ( lErrCde != SUCCESS )
{
ErrReport( lErrCde );
return 0L;
}
return DevHandle;
}
// **************************************************************************
// Design Notes: Let user input the start port and port count for operation.
// --------------------------------------------------------------------------
void SelectPort( DWORD & lStartPort, DWORD & lPortCount, DWORD lMaxCount )
{
//Start port
while ( 1 )
{
cout << "Please input the start port: " ;
InputNumStream( lStartPort );
if ( lStartPort >= lMaxCount || lStartPort < 0 )
{
cout << "Please input a appropriate number!" << endl;
continue;
}
break;
}
//Port count
while( 1 )
{
cout << "Please input the port count: ";
InputNumStream( lPortCount );
if ( ( lStartPort + lPortCount ) > lMaxCount || lPortCount <= 0 )
{
cout << "Please input a appropriate number!" << endl;
continue;
}
break;
}
}
// **************************************************************************
// Design Notes: Read the selected ports.
// TRUE for success, FALSE for failure.
// --------------------------------------------------------------------------
BOOL ReadPorts( LONG lDevHandle, DWORD lPortStart, DWORD lPortCount )
{
if ( 0 == lDevHandle )
{
cout << "Invalid params! Please open a device." << endl;
return FALSE;
}
LONG lErrCde = SUCCESS;
//Firstly, allocate memory for data buffer
BYTE * pBuf = new BYTE[ lPortCount ];
if ( NULL == pBuf )
{
cout << "System Error: " << "No enough memory!" << endl;
return FALSE;
}
memset( pBuf, 0, sizeof(BYTE) * lPortCount );
//Secondly, read the data
lErrCde = AdxDioReadDiPorts( lDevHandle, lPortStart, lPortCount, pBuf );
if ( lErrCde != SUCCESS )
{
ErrReport( lErrCde );
return FALSE;
}
cout << "**************************Data(Hex)*********************" << endl;
char szText[ 10 ];
for ( DWORD i = 0; i < lPortCount; i++ )
{
if ( pBuf[ i ] <= 0xF )
{
wsprintf( szText, "0%X ", pBuf[ i ] );
}
else
{
wsprintf( szText, "%X ", pBuf[ i ] );
}
cout << "Port " << lPortStart + i << ": " << szText << endl;
}
cout << "***************************End**************************" << endl;
//Finally, release memory
if ( pBuf != NULL )
{
delete[] pBuf;
pBuf = NULL;
}
return TRUE;
}
// **************************************************************************
// Design Notes: Clear the get stream by advancing the get pointer of streambuf
// --------------------------------------------------------------------------
inline
void ClearStream()
{
int nTemp = cin.rdbuf()->in_avail();
cin.ignore( nTemp - 1 );
}
// **************************************************************************
// Design Notes: Get the input number.
// --------------------------------------------------------------------------
inline
void InputNumStream( DWORD & dwNum )
{
char btTemp[ MAX_NUMLONG ] = { 0, 0, 0, 0 };
cin.width( MAX_NUMLONG );
cin >> btTemp;
//clear the stream
ClearStream();
//Get the number
dwNum = atoi( btTemp );
}
//---------------------------------------------------------------------------
inline
void ErrReport( LRESULT lErrCde )
{
char szErrMsg[255];
DRV_GetErrorMessage( lErrCde, szErrMsg );
cout << "Driver Error: " << szErrMsg << endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -