📄 dio_soft_ports.cpp
字号:
DEVLIST * pDevList;
char szErrMsg[255];
int nItemIndex;
int i;
// --------------------------------
// Initialize Device List Combobox
// --------------------------------
// get number of the installed devices
lErrCde = DRV_DeviceGetNumOfList(&usDevNum);
if ( lErrCde != SUCCESS)
{
DRV_GetErrorMessage( lErrCde, (LPSTR)szErrMsg );
MessageBox(NULL, szErrMsg, "Driver Message", MB_OK | MB_ICONERROR);
return FALSE;
}
if ( usDevNum == 0 )
{
MessageBox( NULL, "Cannot find any device installed on this machine!","Error", MB_OK );
return FALSE;
}
//Allocate memory for Device List
pDevList = (DEVLIST*)HeapAlloc(
GetProcessHeap(),
HEAP_ZERO_MEMORY,
usDevNum * sizeof(DEVLIST) );
if ( pDevList == NULL )
{
MessageBox(NULL,"Not enough Memory!","Error", MB_OK | MB_ICONERROR );
return FALSE;
}
// retrieve the information of all installed devices
lErrCde = DRV_DeviceGetList( pDevList, usDevNum, &usRetrieved );
if ( lErrCde != (LONG)SUCCESS)
{
DRV_GetErrorMessage( lErrCde,(LPSTR)szErrMsg);
MessageBox(NULL,szErrMsg,"Driver Message",MB_OK | MB_ICONERROR);
return FALSE;
}
// initialize the Device List Combobox with the retrieved information
for (i = 0; i < usRetrieved; i ++)
{
nItemIndex = SendDlgItemMessage(
hDlg,
IDC_COMBO_DEV_LIST,
CB_ADDSTRING,
0,
(LPARAM)((LPSTR)pDevList[i].szDeviceName));
SendDlgItemMessage(
hDlg,
IDC_COMBO_DEV_LIST,
CB_SETITEMDATA,
nItemIndex,
pDevList[i].dwDeviceNum );
}
SendDlgItemMessage( hDlg, IDC_COMBO_DEV_LIST, CB_SETCURSEL, (WPARAM)0, (LPARAM)0);
HeapFree( GetProcessHeap(), 0, pDevList );
return TRUE;
}
void OnMsgReadDiPorts( HWND hDlg )
{
PDIO_SOFT_CTX pCtx;
PBYTE pDiData;
ULONG dwPortStart;
ULONG dwPortCount;
BOOL bTranslated;
LRESULT lErrCde;
char szTxtBuf[255]={0};
pCtx = (PDIO_SOFT_CTX)GetWindowLong( hDlg, GWL_USERDATA );
assert( pCtx != NULL );
if ( pCtx->DevHandle == NULL )
{
MessageBox(NULL,"Please select a device at first!","Error", MB_OK );
return;
}
//Get start port
dwPortStart = GetDlgItemInt( hDlg, IDC_EDIT_READPORTS_START_PORT, &bTranslated, FALSE );
if ( !bTranslated || dwPortStart >= pCtx->nDiPortCount )
{
MessageBox(NULL,"Please input a valid 'start port'!", "Error", MB_OK | MB_ICONERROR );
return;
}
//Get port count
dwPortCount = GetDlgItemInt( hDlg, IDC_EDIT_READPORTS_PORT_COUNT, &bTranslated, FALSE );
if ( !bTranslated || dwPortCount == 0 || ( dwPortStart + dwPortCount > pCtx->nDiPortCount ) )
{
MessageBox(NULL,"Please input a valid 'port count'!", "Error", MB_OK | MB_ICONERROR );
return;
}
//Allocate memory for DI data
pDiData = (BYTE*)HeapAlloc( GetProcessHeap(), 0, dwPortCount );
if ( pDiData == NULL )
{
MessageBox(NULL,"Not enough memory!","Error", MB_OK | MB_ICONERROR );
return;
}
//Read DI ports
lErrCde = AdxDioReadDiPorts( pCtx->DevHandle, dwPortStart, dwPortCount, pDiData );
if ( lErrCde != SUCCESS )
{
DRV_GetErrorMessage( lErrCde, szTxtBuf );
MessageBox(NULL, szTxtBuf, "Driver Error", MB_OK );
} else {
int i;
int count = 0;
for ( i = dwPortCount - 1; i >= 0; --i )
{
count += wsprintf( &szTxtBuf[count], "%x ", pDiData[i] );
}
SetDlgItemText( hDlg, IDC_EDIT_READPORTS_DATA, szTxtBuf );
}
HeapFree( GetProcessHeap(), 0, pDiData );
}
void OnMsgWriteDoPorts( HWND hDlg )
{
PDIO_SOFT_CTX pCtx;
ULONG dwPortStart;
ULONG dwPortCount;
BOOL bTranslated;
LRESULT lErrCde;
char * pTxtBuf;
PBYTE pDoData;
int i,j;
pCtx = (PDIO_SOFT_CTX)GetWindowLong( hDlg, GWL_USERDATA );
assert( pCtx != NULL );
if ( pCtx->DevHandle == NULL )
{
MessageBox(NULL,"Please select a device at first!","Error", MB_OK );
return;
}
//Get start port
dwPortStart = GetDlgItemInt( hDlg, IDC_EDIT_WRITEPORTS_START_PORT, &bTranslated, FALSE );
if ( !bTranslated || dwPortStart >= pCtx->nDoPortCount )
{
MessageBox(NULL,"Please input a valid 'start port'!", "Error", MB_OK | MB_ICONERROR );
return;
}
//Get port count
dwPortCount = GetDlgItemInt( hDlg, IDC_EDIT_WRITEPORTS_PORT_COUNT, &bTranslated, FALSE );
if ( !bTranslated || dwPortCount == 0 || ( dwPortStart + dwPortCount > pCtx->nDoPortCount ) )
{
MessageBox(NULL,"Please input a valid 'port count'!", "Error", MB_OK | MB_ICONERROR );
return;
}
//Allocate memory for DI data
//two character for one port.
pTxtBuf = (char*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 2 * dwPortCount + 1 );
if ( pTxtBuf == NULL )
{
MessageBox(NULL,"Not enough memory!","Error", MB_OK | MB_ICONERROR );
return;
}
pDoData = (BYTE*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwPortCount );
if ( pDoData == NULL )
{
HeapFree( GetProcessHeap(), 0, pTxtBuf );
MessageBox(NULL,"Not enough memory!","Error", MB_OK | MB_ICONERROR );
return;
}
//Get user input DO Data
if ( 0 == GetDlgItemText( hDlg, IDC_EDIT_WRITEPORTS_DATA, pTxtBuf, dwPortCount * 2 + 1) )
{
HeapFree( GetProcessHeap(), 0, pTxtBuf );
HeapFree( GetProcessHeap(), 0, pDoData );
MessageBox(NULL, "Please input DO data to output!", "Error", MB_OK | MB_ICONERROR );
return;
}
//Convert character to integer
for( i = strlen(pTxtBuf) - 1, j = 0; i >= 0; i -= 2, ++j )
{
pDoData[ j ] = ( CharToHex( pTxtBuf[i-1] ) << 4 ) | ( CharToHex( pTxtBuf[ i ] ) & 0x0f ) ;
}
//Write Do ports
lErrCde = AdxDioWriteDoPorts( pCtx->DevHandle, dwPortStart, dwPortCount, pDoData );
if ( lErrCde != SUCCESS )
{
char szTxtBuf[255];
DRV_GetErrorMessage( lErrCde, szTxtBuf );
MessageBox(NULL, szTxtBuf, "Driver Error", MB_OK );
}
HeapFree( GetProcessHeap(), 0, pTxtBuf );
HeapFree( GetProcessHeap(), 0, pDoData );
}
void OnMsgGetDoPortsState( HWND hDlg )
{
PDIO_SOFT_CTX pCtx;
ULONG dwPortStart;
ULONG dwPortCount;
BOOL bTranslated;
LRESULT lErrCde;
char szTxtBuf[255]={0};
PBYTE pDoData;
pCtx = (PDIO_SOFT_CTX)GetWindowLong( hDlg, GWL_USERDATA );
assert( pCtx != NULL );
if ( pCtx->DevHandle == NULL )
{
MessageBox(NULL,"Please select a device at first!","Error", MB_OK );
return;
}
//Get start port
dwPortStart = GetDlgItemInt( hDlg, IDC_EDIT_GETSTATE_START_PORT, &bTranslated, FALSE );
if ( !bTranslated || dwPortStart >= pCtx->nDoPortCount )
{
MessageBox(NULL,"Please input a valid 'start port'!", "Error", MB_OK | MB_ICONERROR );
return;
}
//Get port count
dwPortCount = GetDlgItemInt( hDlg, IDC_EDIT_GETSTATE_PORT_COUNT, &bTranslated, FALSE );
if ( !bTranslated || dwPortCount == 0 || ( dwPortStart + dwPortCount > pCtx->nDoPortCount ) )
{
MessageBox(NULL,"Please input a valid 'port count'!", "Error", MB_OK | MB_ICONERROR );
return;
}
//Allocate memory for DO data
pDoData = (BYTE*)HeapAlloc( GetProcessHeap(), 0, dwPortCount );
if ( pDoData == NULL )
{
MessageBox(NULL,"Not enough memory!","Error", MB_OK | MB_ICONERROR );
return;
}
//Get DO ports state
lErrCde = AdxDioGetCurrentDoPortsState( pCtx->DevHandle, dwPortStart, dwPortCount, pDoData );
if ( lErrCde != SUCCESS )
{
DRV_GetErrorMessage( lErrCde, szTxtBuf );
MessageBox(NULL, szTxtBuf, "Driver Error", MB_OK );
} else {
int i;
int count = 0;
for ( i = dwPortCount - 1; i >= 0; --i )
{
count += wsprintf( &szTxtBuf[count], "%x ", pDoData[i] );
}
SetDlgItemText( hDlg, IDC_EDIT_GETSTATE_DATA, szTxtBuf );
}
HeapFree( GetProcessHeap(), 0, pDoData );
}
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -