📄 digoutform.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "DigoutForm.h"
#include "..\..\..\include\driver.h"
#include "DirunFrom.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmDigout *frmDigout;
//---------------------------------------------------------------------------
const SHORT MAX_DEVICES = 100;
LONG lDriverHandle; //Driver handle
PT_DeviceGetFeatures ptDevFeatures;
DEVFEATURES DevFeatures; //struct to store the device features
PT_DioWritePortByte ptDioWritePortByte; // DioWriteByte table
PT_DioGetCurrentDOByte ptDioGetCurrentDOByte;
char szErrMsg[80]; // used for MESSAGEBOX function
LRESULT ErrCde; // return error code
DEVLIST DeviceList[MAX_DEVICES]; //Device list array
DEVLIST SubDeviceList[MAX_DEVICES]; //Sub Device list array
SHORT gnNumOfDevices, gnNumOfSubdevices; //number of devices, number of sub devices
SHORT OutEntries;
USHORT uDoReadValue = 0x00;
//---------------------------------------------------------------------------
__fastcall TfrmDigout::TfrmDigout(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmDigout::FormCreate(TObject *Sender)
{
//Get the list of current Advantech I/O Devices in your system
bOpen = False;
ErrCde = DRV_DeviceGetNumOfList(&gnNumOfDevices);
if(ErrCde != SUCCESS){
DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
Application->MessageBoxA((LPCSTR)szErrMsg, "Driver Message");
return;
}
if(gnNumOfDevices > MAX_DEVICES){
gnNumOfDevices = MAX_DEVICES;
}
ErrCde = DRV_DeviceGetList((DEVLIST far *)DeviceList, gnNumOfDevices, &OutEntries);
if(ErrCde != SUCCESS){
DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
Application->MessageBoxA((LPCSTR)szErrMsg, "Driver Message");
return;
}
for(int i=0; i<gnNumOfDevices; i++){
lstDevice->Items->Add(AnsiString(DeviceList[i].szDeviceName));
}
lstDevice->ItemIndex = 0;
lstDeviceClick(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TfrmDigout::lstDeviceClick(TObject *Sender)
{
//add by mining
PT_DioWriteBit ptDioWriteBit;
ptDioWriteBit.port=0;
ptDioWriteBit.bit=0;
//end by mining
//Get the port of each device
AnsiString strPort("Port: ");
lstModule->Clear();
lstChannel->Clear();
gnNumOfSubdevices = DeviceList[lstDevice->ItemIndex].nNumOfSubdevices;
if(gnNumOfSubdevices > MAX_DEVICES){
gnNumOfSubdevices = MAX_DEVICES;
}
if(gnNumOfSubdevices >0){
lstModule->Enabled = True;
ErrCde = DRV_DeviceGetSubList(DeviceList[lstDevice->ItemIndex].dwDeviceNum, (DEVLIST far*)SubDeviceList,gnNumOfSubdevices, &OutEntries);
if (ErrCde != SUCCESS){
DRV_GetErrorMessage(ErrCde, (LPSTR)szErrMsg);
Application->MessageBoxA((LPCSTR)szErrMsg, "Driver Message");
return;
}
for(int j=0; j<gnNumOfSubdevices; j++){
lstModule->Items->Add(AnsiString(SubDeviceList[j].szDeviceName));
}
}else{
lstModule->Enabled = False;
}
if(0 == gnNumOfSubdevices){
lstChannel->Enabled = True;
if(True == bOpen){
DRV_DeviceClose((LONG far* )&lDriverHandle); //close the old opened Device
bOpen = False;
}
ErrCde = DRV_DeviceOpen(DeviceList[lstDevice->ItemIndex].dwDeviceNum, (LONG far*)&lDriverHandle);
if(ErrCde != SUCCESS){
DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
return;
}
bOpen = True;
ptDevFeatures.buffer = (LPDEVFEATURES)&DevFeatures;
ptDevFeatures.size = sizeof(DEVFEATURES);
if ((ErrCde = DRV_DeviceGetFeatures(lDriverHandle,
(LPT_DeviceGetFeatures)&ptDevFeatures)) != SUCCESS){
DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
DRV_DeviceClose((LONG far *)&lDriverHandle);
bOpen = False;
Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
return;
}
//add by mining
//judge whether this device supports DO function
if(ptDevFeatures.buffer->usMaxDOChl==0)
{
ErrCde = DRV_DioWriteBit(lDriverHandle,&ptDioWriteBit);
DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
DRV_DeviceClose((LONG far *)&lDriverHandle);
bOpen = False;
cmdRun->Enabled=false;
Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
return;
}
else
{
cmdRun->Enabled=true;
} //end by mining
for(int i=0; i<(DevFeatures.usMaxDOChl+7)/8; i++){
strPort = "Port: ";
strPort = strPort + AnsiString(i);
lstChannel->Items->Add(strPort);
}
lstChannel->ItemIndex = 0;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmDigout::lstModuleClick(TObject *Sender)
{
//Get the port of each sub device
AnsiString strPort("Port:");
lstChannel->Clear();
if(True == bOpen){
DRV_DeviceClose((LONG far* )&lDriverHandle);
bOpen = False;
}
ErrCde = DRV_DeviceOpen(SubDeviceList[lstModule->ItemIndex].dwDeviceNum, (LONG far *)&lDriverHandle);
if(ErrCde != SUCCESS){
DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
return;
}
bOpen = True;
ptDevFeatures.buffer = (LPDEVFEATURES)&DevFeatures;
ptDevFeatures.size = sizeof(DEVFEATURES);
if ((ErrCde = DRV_DeviceGetFeatures(lDriverHandle,
(LPT_DeviceGetFeatures)&ptDevFeatures)) != SUCCESS){
DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
DRV_DeviceClose((LONG far *)&lDriverHandle);
bOpen = False;
Application->MessageBoxA((char *)szErrMsg, "Driver Message", MB_OK);
return;
}
for(int i=0; i<DevFeatures.usMaxDOChl/8; i++){
strPort = "Port: ";
strPort = strPort + AnsiString(i);
lstChannel->Items->Add(strPort);
}
lstChannel->ItemIndex = 0;
}
//---------------------------------------------------------------------------
void __fastcall TfrmDigout::cmdRunClick(TObject *Sender)
{
ptDioWritePortByte.port = lstChannel->ItemIndex;
ptDioWritePortByte.mask = StrToInt64Def(txtMask->Text,0xff);
ptDioGetCurrentDOByte.port= lstChannel->ItemIndex;
//modified by mining
ptDioGetCurrentDOByte.value = &uDoReadValue;
if((ErrCde = DRV_DioGetCurrentDOByte(lDriverHandle, (LPT_DioGetCurrentDOByte)&ptDioGetCurrentDOByte)) != SUCCESS){
DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
Application->MessageBoxA((char* )szErrMsg, "Driver Message");
return;
} //end minng
frmDiorun->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TfrmDigout::cmdExitClick(TObject *Sender)
{
if(True == bOpen){
DRV_DeviceClose((LONG far*)&lDriverHandle);
bOpen = False;
}
Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TfrmDigout::txtMaskExit(TObject *Sender)
{
AnsiString S;
if(txtMask->Text.SubString(1,2) != "0x"){
txtMask->Text = txtMask->Text.Insert("0x",1);
}
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -