📄 dosoftports.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 : DoSoftPorts.CPP
// Revision : 1.0
//
// Created : 05/30/2005
//
// Description :
// This examples performs the operation for DO and get DO state of 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.
//
// WritePorts() - Write the data to the selected ports.
//
// GetPortsState() - Get state of the selected ports to indicate the writing
// operation is right or not.
//
// CharToHex() - Change the input characters to apprppriate numbers.
//
// 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,
// AdxDioWriteDoPorts,
// AdxDioGetCurrentDoPortsState.
//
//-----------------------------------------------------------------------------
#include "DoSoftPorts.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 >> 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_DoPortCount,
&lData,
&lDataLen);
if ( lErrCde != SUCCESS )
{
cout << "Can not get the DI port count!" << endl;
ErrReport( lErrCde );
delete[] pDevNumList;
pDevNumList = NULL;
break;
}
cout << "DO port count: " << lData << endl;
//Fouthly, select ports and write data to the selected ports
DWORD lPortStart = 0;
DWORD lPortCount = 0;
SelectPort( lPortStart, lPortCount, lData );
if ( !WritePorts( lDevHandle, lPortStart, lPortCount) )
{
cout << "Can not read the ports!" << endl;
delete[] pDevNumList;
pDevNumList = NULL;
break;
}
//Fifthly, Get DO ports state
cout << "******************* Get DO Ports State *****************" << endl;
SelectPort( lPortStart, lPortCount, lData);
if ( !GetStateOfPorts( 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: Change an character to a hex number.
// Return the changed value if success, otherwise return 0.
// --------------------------------------------------------------------------
BYTE CharToHex( char c )
{
if ( c >= '0' && c <= '9' )
{
return ( c - '0' );
}
c = (char)tolower( c );
if ( c >= 'a' && c <= 'f' )
{
return ( c - 'a' + 0xa );
}
return 0;
}
// **************************************************************************
// Design Notes: Write data to the selected ports.
// TRUE for success, FALSE for failure.
// --------------------------------------------------------------------------
BOOL WritePorts( 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 text and data buffer
char pBufText[255];
BYTE * pBufData = new BYTE[ lPortCount ];
if ( NULL == pBufData )
{
cout << "System Error: " << "No enough memory!" << endl;
return FALSE;
}
//Secondly, have the data in
cout << "Please input the data you want to output:" << endl;
cout << "*********************** Data(Hex) ***********************" << endl;
cin.getline( pBufText, lPortCount * 2 + 1);
//Thirdly, change the characters to numbers
DWORD i = 0;
DWORD j = 0;
for ( ; i < lPortCount; i++, j += 2 )
{
pBufData[ i ] = ( CharToHex( pBufText[ j ] ) << 4 ) |
( CharToHex( pBufText[ j + 1 ] ) & 0x0f );
}
lErrCde = AdxDioWriteDoPorts( lDevHandle, lPortStart, lPortCount, pBufData );
if ( lErrCde != SUCCESS )
{
ErrReport( lErrCde );
return FALSE;
}
cout << "The data have been output are: " << endl;
char szText[ 10 ];
for ( i = 0; i < lPortCount; i++ )
{
if ( pBufData[ i ] <= 0xF )
{
wsprintf( szText, "0%X ", pBufData[ i ] );
}
else
{
wsprintf( szText, "%X ", pBufData[ i ] );
}
cout << "Port " << lPortStart + i << ": " << szText << endl;
}
cout << "******************* Completed! *************************" << endl;
//Finally, release memory.
if ( pBufData != NULL )
{
delete[] pBufData;
pBufData = NULL;
}
return TRUE;
}
// **************************************************************************
// Design Notes: Get state of the selected ports.
// TRUE for success, FALSE for fail.
// --------------------------------------------------------------------------
BOOL GetStateOfPorts( 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 = AdxDioGetCurrentDoPortsState(
lDevHandle,
lPortStart,
lPortCount,
pBuf );
if ( lErrCde != SUCCESS )
{
ErrReport( lErrCde );
return FALSE;
}
cout << "******************* DO State 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 );
}
// **************************************************************************
// 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 );
}
//---------------------------------------------------------------------------
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 + -