📄 dio_soft_ports.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 : DIO_SOFT_PORTS.CPP
// Revision : 1.0
//
// Created : 03/03/2005
//
// Description :
// This examples performs the DIO ports operation.
//
//-----------------------------------------------------------------------------
//
// FUNCTIONS:
//
// DioSoftDlgProc() - Main dialog box procedure
//
// InitDlg() - Initialize the dialog box
//
// InitDeviceListComboBox() - initialize the device list ( combobox )
//
// OnMsgOpenDevice() - Process the dialog box messages for open device
//
// OnMsgReadDiPorts() - Precess the dialog box message for read di ports.
//
// OnMsgWriteDoPorts() - Precess the dialog box message for write do ports.
//
// OnMsgGetDoPortsState() - Precess the dialog box message for get do ports state.
//-----------------------------------------------------------------------------
#include <windows.h> /* required for all Windows applications */
#include <assert.h>
#include "..\..\..\include\driver.h"
#include "resource.h"
//--------------------------------------------
// macro & struct definition
//--------------------------------------------
typedef struct _DIO_SOFT_CTX{
LONG DevHandle;
ULONG nDiPortCount;
ULONG nDoPortCount;
}DIO_SOFT_CTX,*PDIO_SOFT_CTX,FAR *LPDIO_SOFT_CTX;
//--------------------------------------------
// Forward declaration
//--------------------------------------------
BOOL CALLBACK DioSoftDlgProc (
HWND hwndDlg,
UINT uMsg,
WPARAM wParam,
LPARAM lParam );
BOOL InitDlg( HWND hDlg );
BOOL InitDeviceListComboBox( HWND hDlg );
void OnMsgOpenDevice( HWND hDlg );
void OnMsgReadDiPorts( HWND hDlg );
void OnMsgWriteDoPorts( HWND hDlg );
void OnMsgGetDoPortsState( HWND hDlg );
BYTE CharToHex( char c );
//--------------------------------------------
// Entry point
//--------------------------------------------
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
DialogBox( hInstance, MAKEINTRESOURCE(IDD_DIO_SOFT_PORTS), NULL, DioSoftDlgProc );
return 0;
}
//--------------------------------------------
// Dialog Proc
//--------------------------------------------
BOOL CALLBACK DioSoftDlgProc (
HWND hDlg,
UINT uMsg,
WPARAM wParam,
LPARAM lParam )
{
switch ( uMsg )
{
case WM_INITDIALOG :
{
if ( !InitDlg( hDlg ) )
{
MessageBox(
NULL,
"Error occured while initialize the dialog box! Program aborted!",
"Error",
MB_OK | MB_ICONERROR );
PostQuitMessage( -1 );
}
return TRUE;
}
case WM_COMMAND :
{
switch (LOWORD(wParam))
{
case IDOK:
case IDCANCEL:
{
EndDialog(hDlg, 0);
}
break;
case IDC_BTN_OPEN_DEVICE:
{
OnMsgOpenDevice( hDlg );
}
break;
case IDC_BTN_READ_PORTS:
{
OnMsgReadDiPorts( hDlg );
}
break;
case IDC_BTN_WRITE_PORTS:
{
OnMsgWriteDoPorts( hDlg );
}
break;
case IDC_BTN_GET_DO_PORTS:
{
OnMsgGetDoPortsState( hDlg );
}
break;
default:
return FALSE;
}
}
break;
case WM_DESTROY:
{
PDIO_SOFT_CTX pCtx = (PDIO_SOFT_CTX)GetWindowLong( hDlg, GWL_USERDATA );
if ( pCtx != NULL )
{
//Close device!
if ( pCtx->DevHandle != NULL )
{
DRV_DeviceClose( &pCtx->DevHandle );
}
HeapFree( GetProcessHeap(), 0, pCtx );
}
}
break;
default:
return FALSE;
}
return TRUE ;
}
//--------------------------------------------
// InitDlg
//--------------------------------------------
BOOL InitDlg( HWND hDlg )
{
//allocate a context struct
PDIO_SOFT_CTX pCtx = (PDIO_SOFT_CTX)HeapAlloc(
GetProcessHeap(),
HEAP_ZERO_MEMORY,
sizeof(DIO_SOFT_CTX) );
if ( pCtx == NULL )
{
MessageBox( NULL, "Not enough memory!", "Error", MB_OK | MB_ICONERROR );
return FALSE;
}
SetWindowLong( hDlg, GWL_USERDATA, (LONG)pCtx );
//Init controls
//Init device combox list
if ( !InitDeviceListComboBox( hDlg ) )
{
MessageBox( NULL,"Get device list failed!","Error", MB_OK | MB_ICONERROR );
return FALSE;
}
//Read ports
SetDlgItemInt( hDlg, IDC_EDIT_READPORTS_START_PORT, 0, FALSE );
SetDlgItemInt( hDlg, IDC_EDIT_READPORTS_PORT_COUNT, 1, FALSE );
//Write ports
SetDlgItemInt( hDlg, IDC_EDIT_WRITEPORTS_START_PORT, 0, FALSE );
SetDlgItemInt( hDlg, IDC_EDIT_WRITEPORTS_PORT_COUNT, 1, FALSE );
//Get DO State
SetDlgItemInt( hDlg, IDC_EDIT_GETSTATE_START_PORT, 0, FALSE );
SetDlgItemInt( hDlg, IDC_EDIT_GETSTATE_PORT_COUNT, 1, FALSE );
return TRUE;
}
void OnMsgOpenDevice( HWND hDlg )
{
ULONG dwDevNum;
LRESULT lErrCde;
PDIO_SOFT_CTX pCtx;
ULONG dwBufLen;
int nSelItem;
pCtx = (PDIO_SOFT_CTX)GetWindowLong( hDlg, GWL_USERDATA );
assert( pCtx != NULL );
//Close the device we had opened.
if ( pCtx->DevHandle != NULL )
{
DRV_DeviceClose( &pCtx->DevHandle );
pCtx->DevHandle = NULL;
SetDlgItemInt( hDlg, IDC_EDIT_DI_PORT_COUNT, 0, FALSE );
EnableWindow( GetDlgItem( hDlg, IDC_BTN_READ_PORTS), FALSE );
SetDlgItemInt( hDlg, IDC_EDIT_DO_PORT_COUNT, 0, FALSE );
EnableWindow( GetDlgItem( hDlg, IDC_BTN_WRITE_PORTS), FALSE );
EnableWindow( GetDlgItem( hDlg, IDC_BTN_GET_DO_PORTS ), FALSE );
}
//Get user input device number
nSelItem = SendDlgItemMessage( hDlg, IDC_COMBO_DEV_LIST, CB_GETCURSEL, 0, 0 );
dwDevNum = SendDlgItemMessage( hDlg, IDC_COMBO_DEV_LIST, CB_GETITEMDATA, nSelItem, 0 );
if ( dwDevNum == CB_ERR )
{
MessageBox(NULL, "Can't get the device number !", "Error", MB_OK | MB_ICONERROR );
return;
}
//Open device
lErrCde = DRV_DeviceOpen( dwDevNum, &pCtx->DevHandle );
if ( lErrCde != SUCCESS )
{
char msg[255];
DRV_GetErrorMessage( lErrCde, msg );
MessageBox(NULL,msg,"Error", MB_ICONERROR | MB_OK );
return;
}
//Get Device Information
//Get DI Ports information
dwBufLen = sizeof( LONG );
lErrCde = DRV_DeviceGetProperty( pCtx->DevHandle, CFG_DiPortCount, &pCtx->nDiPortCount, &dwBufLen );
if ( lErrCde == SUCCESS )
{
SetDlgItemInt( hDlg, IDC_EDIT_DI_PORT_COUNT, pCtx->nDiPortCount, FALSE );
if ( pCtx->nDiPortCount > 0 )
{
EnableWindow( GetDlgItem( hDlg, IDC_BTN_READ_PORTS), TRUE );
}
}
//Get DO Ports information
dwBufLen = sizeof( LONG );
lErrCde = DRV_DeviceGetProperty( pCtx->DevHandle, CFG_DoPortCount, &pCtx->nDoPortCount, &dwBufLen );
if ( lErrCde == SUCCESS )
{
SetDlgItemInt( hDlg, IDC_EDIT_DO_PORT_COUNT, pCtx->nDoPortCount, FALSE );
if ( pCtx->nDiPortCount > 0 )
{
EnableWindow( GetDlgItem( hDlg, IDC_BTN_WRITE_PORTS), TRUE );
EnableWindow( GetDlgItem( hDlg, IDC_BTN_GET_DO_PORTS ), TRUE );
}
}
}
BOOL InitDeviceListComboBox( HWND hDlg )
{
SHORT usDevNum;
SHORT usRetrieved;
LRESULT lErrCde;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -