📄 mainform.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 : DIOSoftPorts.CPP
// Revision : 1.0
//
// Created : 05/23/2005
//
// Description :
// This examples performs the operation for DIO ports .
//
//-----------------------------------------------------------------------------
//
// API Used:
//
// DRV_DeviceOpen,
// DRV_DeviceClose,
// DRV_GetErrorMessage,
// DRV_DeviceGetProperty,
// DRV_DeviceGetNumOfList,
// DRV_DeviceGetList,
// AdxDioWriteDoPorts,
// AdxDioReadDiPorts,
// AdxDioGetCurrentDoPortsState.
//
//-----------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <assert.h> //For assert statement
#include "..\..\..\include\driver.h"
#include "MainForm.h"
#include <wstring.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
#define MAX_DEVNUM 100 //The max nums of device
#define MAX_ERRMSG_LEN 255 //THe max length of the string of error message
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
m_lDeviceHandle = (LONG)NULL;
m_pdwDeviceNum = NULL;
m_lDIPort = 0;
m_lDOPort = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate( TObject *Sender )
{
if ( !InitDeviceCombo() )
{
Application->MessageBox(
"Can not get the device list!",
"Error",
MB_OK | MB_ICONERROR);
return;
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//Design notes: Initial the combo box for device list
//---------------------------------------------------------------------------
BOOL __fastcall TForm1::InitDeviceCombo()
{
LONG lErrCde = SUCCESS;
UCHAR szErrMsg[MAX_ERRMSG_LEN];
//Firstly, get num of devices
SHORT sDevNum = 0;
lErrCde = DRV_DeviceGetNumOfList( &sDevNum );
if ( lErrCde != SUCCESS )
{
DRV_GetErrorMessage( lErrCde, (LPSTR)szErrMsg );
Application->MessageBox(
szErrMsg,
"Driver Message",
MB_OK | MB_ICONERROR );
return FALSE;
}
if ( sDevNum <= 0 )
{
Application->MessageBox(
"There are no device on your computer. Please install one at least.",
"Prompt",
MB_OK | MB_ICONINFORMATION );
return FALSE;
}
//Secondly, allocate memory for device list and device number
DEVLIST * pDevList = new DEVLIST[ sDevNum ];
if ( NULL == pDevList )
{
Application->MessageBox(
"Allocate memory failed!",
"System Message",
MB_OK | MB_ICONERROR );
return FALSE;
}
memset( pDevList, 0, sizeof(DEVLIST) * sDevNum );
if ( m_pdwDeviceNum != NULL )
{
delete[] m_pdwDeviceNum;
m_pdwDeviceNum = NULL;
}
m_pdwDeviceNum = new DWORD[ sDevNum ];
if ( NULL == m_pdwDeviceNum )
{
Application->MessageBox(
"Allocate memory failed!",
"System Message",
MB_OK | MB_ICONERROR );
delete[] pDevList;
pDevList = NULL;
return FALSE;
}
memset( m_pdwDeviceNum, 0, sizeof(DWORD) * sDevNum );
//Thirdly, get devlice list
SHORT sDevRealNum = 0;
lErrCde = DRV_DeviceGetList(
pDevList,
sDevNum,
&sDevRealNum );
if ( lErrCde != SUCCESS)
{
DRV_GetErrorMessage( lErrCde, (LPSTR)szErrMsg );
Application->MessageBox(
szErrMsg,
"Driver Message",
MB_OK | MB_ICONERROR );
//Release memory
delete[] pDevList;
pDevList = NULL;
return FALSE;
}
for ( SHORT i = 0; i < sDevRealNum; i++ )
{
if ( Combo_Device != NULL )
{
Combo_Device->Items->Add( pDevList[i].szDeviceName );
m_pdwDeviceNum[i] = pDevList[i].dwDeviceNum;
}
}
Combo_Device->ItemIndex = 0;
//Finally, rember to release the memory allocated
delete[] pDevList;
pDevList = NULL;
return TRUE;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy( TObject *Sender )
{
if ( m_pdwDeviceNum != NULL )
{
delete[] m_pdwDeviceNum;
m_pdwDeviceNum = NULL;
}
if ( m_lDeviceHandle != (LONG)NULL )
{
DRV_DeviceClose( &m_lDeviceHandle );
m_lDeviceHandle = (LONG)NULL;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BtnSelectDevClick(TObject *Sender)
{
UCHAR szErrMsg[MAX_ERRMSG_LEN]; //Error message
//Firstly, confirm the devnum buffer is not null
if ( NULL == m_pdwDeviceNum )
{
Application->MessageBoxA(
"Allocate memory failed!",
"System Error",
MB_OK | MB_ICONERROR );
return;
}
//Secondly, find the device num
int nSel = Combo_Device->ItemIndex;
if ( nSel < 0 )
{
Application->MessageBoxA(
"Please select a devie firstly!",
"Information",
MB_OK | MB_ICONINFORMATION );
return;
}
DWORD dwDevNum = m_pdwDeviceNum[nSel];
//Thirdly, open the device
//--Firstly, if the device has opened, close it
if ( m_lDeviceHandle != (LONG)NULL )
{
DRV_DeviceClose( &m_lDeviceHandle );
m_lDeviceHandle = (LONG)NULL;
}
//--Secondly, open the device
LONG lErrCde = DRV_DeviceOpen(dwDevNum, &m_lDeviceHandle);
if ( lErrCde != SUCCESS )
{
DRV_GetErrorMessage(lErrCde, szErrMsg);
Application->MessageBoxA(
szErrMsg,
"Driver Error",
MB_OK | MB_ICONERROR );
BtnWrite->Enabled = FALSE;
BtnGetDOState->Enabled = FALSE;
Btn_Read->Enabled = FALSE;
DIPortCount->Text = "";
DOPortCount->Text = "";
return;
}
//Fourthly, find the DIO ports count and fill the edits
//--Firstly, get DI ports count
ULONG lDataLen = sizeof(LONG);
//The maximum of the length of a number is 10.
char strText[10];
lErrCde = DRV_DeviceGetProperty(
m_lDeviceHandle,
CFG_DiPortCount,
&m_lDIPort,
&lDataLen );
if ( SUCCESS == lErrCde )
{
//--Secondly, fill the count in the edit
itoa(m_lDIPort, strText, 10);
DIPortCount->Text = strText;
//--Thirdly, enable or disenable the button Read
if ( m_lDIPort > 0 )
{
Btn_Read->Enabled = TRUE;
}
else
{
Btn_Read->Enabled = FALSE;
DIPortCount->Text = "";
}
}
else
{
Btn_Read->Enabled = FALSE;
DIPortCount->Text = "";
}
//--Then, find the DO ports count
lDataLen = sizeof( LONG );
lErrCde = DRV_DeviceGetProperty(
m_lDeviceHandle,
CFG_DoPortCount,
&m_lDOPort,
&lDataLen );
if ( SUCCESS == lErrCde )
{
itoa(m_lDOPort, strText, 10);
DOPortCount->Text = strText;
//--Finally, enable or disenable the button Write and State
if ( m_lDOPort > 0 )
{
BtnWrite->Enabled = TRUE;
BtnGetDOState->Enabled = TRUE;
}
else
{
BtnWrite->Enabled = FALSE;
BtnGetDOState->Enabled = FALSE;
DOPortCount->Text = "";
}
}
else
{
BtnWrite->Enabled = FALSE;
BtnGetDOState->Enabled = FALSE;
DOPortCount->Text = "";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Btn_ReadClick(TObject *Sender)
{
if ( (LONG)NULL == m_lDeviceHandle )
{
Application->MessageBoxA(
"The device has not been opened!",
"Error",
MB_OK | MB_ICONERROR );
return;
}
//Firstly, get start port and port count
AnsiString Temp = DIPort_Start->Text;
if ( Temp.IsEmpty() )
{
Application->MessageBoxA(
"Please input a appropriate number !",
"Prompt",
MB_OK | MB_ICONINFORMATION);
return;
}
DWORD lPortStart = (DWORD)atoi( Temp.c_str() );
if ( lPortStart >= m_lDIPort )
{
Application->MessageBoxA(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -