📄 mainform.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MainForm.h"
#include "PMConfigForm.h"
#include "SCConfigForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmMain *frmMain;
ULONG lDeviceNum;
LONG hDeviceHandle;
USHORT HexToInt(String sVal)
{
int i,k;
USHORT iRet;
char cVal;
iRet = 0;
//Convert every valid character
for( i=1; i<= sVal.Length();i++)
{
cVal = sVal[i];
if( (cVal >= '0') && (cVal <= '9') )
k = Byte(cVal) - Byte('0');
else if( (cVal >= 'a') && (cVal <= 'f') )
k = Byte(cVal) - Byte('a') + 10;
else if( (cVal >= 'A') && (cVal <= 'F') )
k = Byte(cVal) - Byte('A') + 10 ;
else
break;
/// {Accumulate convert value}
iRet = iRet*16 + k;
}
return iRet;
}
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::cmdSelectDeviceClick(TObject *Sender)
{
char szDescription[80];
//This function is for getting the device number
DRV_SelectDevice(Handle, True, (ULONG*)&lDeviceNum, szDescription);
txtDeviceName->Text = AnsiString(szDescription);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::cmdPatternMatchClick(TObject *Sender)
{
frmPMConfig->usPA0Mask = HexToInt(txtPA0Mask->Text) ;
frmPMConfig->usPA0Value =HexToInt (txtPA0Value->Text);
frmPMConfig->usPA4Mask = HexToInt(txtPA4Mask->Text);
frmPMConfig->usPA4Value =HexToInt (txtPA4Value->Text);
if(mrOk == frmPMConfig->ShowModal())
{
txtPA0Mask->Text=IntToHex( frmPMConfig->usPA0Mask, 2 ) ;
txtPA0Value->Text=IntToHex( frmPMConfig->usPA0Value, 2 );
txtPA4Mask->Text=IntToHex( frmPMConfig->usPA4Mask, 2 ) ;
txtPA4Value->Text=IntToHex( frmPMConfig->usPA4Value, 2 );
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::cmdExitClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::cmdStatusChangeClick(TObject *Sender)
{
frmSCConfig->usPB0Value = HexToInt(txtPB0Mask->Text);
frmSCConfig->usPB4Value = HexToInt(txtPB4Mask->Text);
if( mrOk == frmSCConfig->ShowModal() )
{
txtPB0Mask->Text = IntToHex(frmSCConfig->usPB0Value,2 );
txtPB4Mask->Text = IntToHex(frmSCConfig->usPB4Value,2 );
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::cmdStartClick(TObject *Sender)
{
BOOL bStartEvent = false;
USHORT iTmp;
//1. Open device
lErrCde = DRV_DeviceOpen(lDeviceNum, &hDeviceHandle);
if(lErrCde != SUCCESS)
{
strcpy(szErrMsg,"Device open error !");
Application->MessageBoxA((char*)szErrMsg,"Device Open");
return;
}
//2. If want Enable Pattern Match event feature, enable it
iTmp = HexToInt(txtPA0Mask->Text)+ HexToInt(txtPA4Mask->Text)*256;
if (iTmp != 0 )
{
txtPA0Mask->Tag = iTmp; // {Record that has enable this function}
//2.1 Fill Pattern match feature needs data
ptEnableEventEx.Pattern.usEventType = ADS_EVT_PATTERNMATCH;
ptEnableEventEx.Pattern.usEventEnable = 1;
ptEnableEventEx.Pattern.usCount = 1;
ptEnableEventEx.Pattern.usEnable = iTmp;
ptEnableEventEx.Pattern.usValue = HexToInt(txtPA0Value->Text)+HexToInt(txtPA4Value->Text)*256;
//2.2 Start up Event match function
lErrCde = DRV_EnableEventEx(hDeviceHandle, &ptEnableEventEx);
if(lErrCde != SUCCESS)
{
strcpy(szErrMsg,"Device open error !");
DRV_GetErrorMessage(lErrCde, (LPSTR)szErrMsg);
Application->MessageBoxA((char*)szErrMsg,"Device Open");
DRV_DeviceClose(&hDeviceHandle);
return;
}
bStartEvent = true;
}
//3. Enable Status Change event feature
iTmp = HexToInt(txtPB0Mask->Text) + HexToInt(txtPB4Mask->Text) * 256;
if (iTmp != 0 )
{
txtPB0Mask->Tag = iTmp; //Record that has start this function
//3.1 Fill status change need data/
ptEnableEventEx.Status.usEventType = ADS_EVT_STATUSCHANGE;
ptEnableEventEx.Status.usEventEnable = 1;
ptEnableEventEx.Status.usCount = 1;
ptEnableEventEx.Status.usEnable = iTmp;
//3.2 start function
lErrCde = DRV_EnableEventEx(hDeviceHandle, &ptEnableEventEx);
if(lErrCde != SUCCESS)
{
strcpy(szErrMsg,"Device open error !");
DRV_GetErrorMessage(lErrCde, (LPSTR)szErrMsg);
Application->MessageBoxA((char*)szErrMsg,"Device Open");
DRV_DeviceClose(&hDeviceHandle);
return;
}
bStartEvent = true;
}//End of starting Status change
//4. Enable Interrupt function
if(radPCEnable->Checked )
{
//4.1 Fill table for interrupt function
ptEnableEvent.EventType = ADS_EVT_INTERRUPT;
ptEnableEvent.Enabled = 1;
ptEnableEvent.Count = 1;
//4.2 Start Interrupt function
lErrCde = DRV_EnableEvent( hDeviceHandle, &ptEnableEvent);
if(lErrCde != SUCCESS)
{
strcpy(szErrMsg,"Device open error !");
DRV_GetErrorMessage(lErrCde, (LPSTR)szErrMsg);
Application->MessageBoxA((char*)szErrMsg,"Device Open");
DRV_DeviceClose(&hDeviceHandle);
return;
}
bStartEvent = true;
} //End of if radPCEnable.Checked
//5. Create thread
usSCCount = 0;
usPMCount = 0;
usIntCount = 0;
if(bStartEvent )
pThread = new CheckThread(false);
//6. Start timer for display message
tmrScan->Enabled = true;
//7. Manage user interface
cmdStart->Enabled = false;
cmdStop->Enabled = true;
cmdExit->Enabled = false;
grpConfig->Enabled = false;
txtPMCount->Text = "";
txtSCCount->Text = "";
txtINTCount->Text = "";
txtData->Text = "";
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::tmrScanTimer(TObject *Sender)
{
//Read DIO data
static PT_DioReadPortByte ptDioReadPortByte;
static USHORT DIValue;
ptDioReadPortByte.value = &DIValue;
ptDioReadPortByte.port = StrToInt(txtChannel->Text);
lErrCde = DRV_DioReadPortByte(hDeviceHandle, &ptDioReadPortByte);
if(lErrCde != SUCCESS)
{
strcpy(szErrMsg,"Device open error !");
DRV_GetErrorMessage(lErrCde, (LPSTR)szErrMsg);
Application->MessageBoxA((char*)szErrMsg,"Device Open");
DRV_DeviceClose(&hDeviceHandle);
return;
}
txtData->Text = IntToHex(DIValue, 2);
}
//---------------------------------------------------------------------------
void TfrmMain::CheckEvent()
{
//TODO: Add your source code here
ptCheckEvent.EventType= &usEventType;;
ptCheckEvent.Milliseconds = 5000;
lErrCde = DRV_CheckEvent(hDeviceHandle, &ptCheckEvent);
if(lErrCde != SUCCESS)
{
strcpy(szErrMsg,"Device open error !");
DRV_GetErrorMessage(lErrCde, (LPSTR)szErrMsg);
Application->MessageBoxA((char*)szErrMsg,"Device Open");
DRV_DeviceClose(&hDeviceHandle);
return;
}
if( usEventType == ADS_EVT_INTERRUPT )
{
usIntCount = usIntCount + 1; // Increase the event count
txtINTCount->Text = IntToStr(usIntCount); //display on screen
}//End of ADS_EVT_INTERRUPT
if( usEventType == ADS_EVT_PATTERNMATCH )
{
usPMCount = usPMCount + 1;
txtPMCount->Text = IntToStr(usPMCount); //display on screen
}//End of ADS_EVT_PATTERNMATCH
if( usEventType == ADS_EVT_STATUSCHANGE )
{
usSCCount = usSCCount + 1;
txtSCCount->Text = IntToStr(usSCCount); //display on screen
}//End of ADS_EVT_STATUSCHANGE
}
void __fastcall TfrmMain::cmdStopClick(TObject *Sender)
{
//1. Stop thread
if(pThread!=NULL)
pThread->Terminate();
if (hDeviceHandle != 0)
{
//2. Stop Pattern match and Status change function
if ((txtPB0Mask->Tag != 0 ) || (txtPA0Mask->Tag != 0))
{
//2.1 fill table
ptEnableEventEx.Status.usEventType = 0;
ptEnableEventEx.Status.usEventEnable = 0;
//2.2 stop this function
lErrCde = DRV_EnableEventEx(hDeviceHandle, &ptEnableEventEx);
if(lErrCde != SUCCESS)
{
strcpy(szErrMsg,"Device open error !");
DRV_GetErrorMessage(lErrCde, (LPSTR)szErrMsg);
Application->MessageBoxA((char*)szErrMsg,"Device Open");
DRV_DeviceClose(&hDeviceHandle);
return;
}
txtPA0Mask->Tag = 0; //Record stop success
txtPB0Mask->Tag = 0;
}
//3. Stop Interrupt functions
if (radPCEnable->Checked)
{
//3.1 fill table
ptEnableEvent.EventType = ADS_EVT_INTERRUPT;
ptEnableEvent.Enabled = 0;
ptEnableEvent.Count = 1;
//3.2 Disable Interrupt function
lErrCde = DRV_EnableEvent(hDeviceHandle, &ptEnableEvent);
if(lErrCde != SUCCESS)
{
strcpy(szErrMsg,"Device open error !");
DRV_GetErrorMessage(lErrCde, (LPSTR)szErrMsg);
Application->MessageBoxA((char*)szErrMsg,"Device Open");
DRV_DeviceClose(&hDeviceHandle);
return;
}
}
//4. Stop scan time
tmrScan->Enabled = false;
//5. Close device
DRV_DeviceClose(&hDeviceHandle);
hDeviceHandle = 0;
}//End if hDeviceHandle
//6. Manage user interface
cmdStop->Enabled = false;
cmdStart->Enabled = true;
cmdExit->Enabled = true;
grpConfig->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
cmdSelectDeviceClick(Sender);
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -