📄 formmain.pas
字号:
unit FormMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Driver;
type
TfrmMain = class(TForm)
grpSelDev: TGroupBox;
btnSelectDevice: TButton;
labDeviceName: TLabel;
grpPWMConfiguration: TGroupBox;
Label1: TLabel;
Label2: TLabel;
txtPeriod0: TEdit;
txtHiPeriod0: TEdit;
btnRun: TButton;
btnStop: TButton;
btnExit: TButton;
Label3: TLabel;
cmbChannel: TComboBox;
Label4: TLabel;
Label5: TLabel;
btnSet: TButton;
Label6: TLabel;
txtOutCount: TEdit;
procedure btnSelectDeviceClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnExitClick(Sender: TObject);
procedure btnRunClick(Sender: TObject);
procedure btnStopClick(Sender: TObject);
procedure btnSetClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
lDeviceNum : Longint;
lDeviceHandle : Longint;
szDescription : array[0..81] of Char;
ErrorNum: Longint;
dwBoardID: DWORD;
usMaxCntNum: Integer;
ptDevFeatures: DevFeatures; { structure for device features }
ptDevGetFeatures: PT_DeviceGetFeatures;
ptCounterPWMSetting: PT_CounterPWMSetting;
end;
var
frmMain: TfrmMain;
implementation
{$R *.DFM}
{*************************************************************
* Function: Handle the error code. If the input error code > 0,
* it means some error apperent. This function can
* show the error message to a message box and stop
* this application.
* Input: The error code.
* return: none
************************************************************* }
Function DoesErr(var lErrCode: LongInt): integer;
var
szErrMsg : string[100];
pszErrMsg : PChar;
begin
{Check the pressed error code}
If (lErrCode <> 0) Then
Begin
pszErrMsg := @szErrMsg;
DRV_GetErrorMessage(lErrCode, pszErrMsg);
Application.MessageBox(pszErrMsg, 'Error!!', MB_OK);
DoesErr := 1;
End
Else
DoesErr := 0;
end;
procedure TfrmMain.btnSelectDeviceClick(Sender: TObject);
begin
FormCreate(Sender);
end;
procedure TfrmMain.FormCreate(Sender: TObject);
var
i: Integer;
begin
//1.Select Device
DRV_SelectDevice(Handle, True, lDeviceNum, szDescription);
labDeviceName.Caption := szDescription;
//2. Open Device
ErrorNum := DRV_DeviceOpen(lDeviceNum, lDeviceHandle);
If ( DoesErr(ErrorNum) = 1 ) Then
Exit;
//3. Get Device Features
ptDevGetFeatures.buffer := @ptDevFeatures;
ptDevGetFeatures.size := sizeof(ptDevFeatures);
ErrorNum := DRV_DeviceGetFeatures(lDeviceHandle, ptDevGetFeatures);
if ( DoesErr(ErrorNum) = 1 ) Then
Begin
DRV_DeviceClose(lDeviceHandle);
Exit;
End;
//4. Close Device
DRV_DeviceClose(lDeviceHandle);
//5. Record some device specific parameters
dwBoardID := ptDevFeatures.dwBoardID;
usMaxCntNum := ptDevFeatures.usMaxTimerChl;
//6. Update UI
cmbChannel.Clear;
for i:=0 to usMaxCntNum-1 do
cmbChannel.Items.Add(IntToStr(i));
cmbChannel.ItemIndex := 0;
end;
procedure TfrmMain.btnExitClick(Sender: TObject);
begin
Close;
end;
procedure TfrmMain.btnRunClick(Sender: TObject);
var
code: Integer;
begin
ErrorNum := DRV_DeviceOpen(lDeviceNum, lDeviceHandle);
If ( DoesErr(ErrorNum) = 1 ) Then
Exit;
ptDevGetFeatures.buffer := @ptDevFeatures;
ptDevGetFeatures.size := sizeof(ptDevFeatures);
ErrorNum := DRV_DeviceGetFeatures(lDeviceHandle, ptDevGetFeatures);
If ( DoesErr(ErrorNum) = 1 ) Then
Begin
DRV_DeviceClose(lDeviceHandle);
Exit;
End;
{ check number of counter channels }
If ptDevFeatures.usMaxTimerChl = 0 Then
Begin
MessageBox(Handle, 'No Counter Channel', 'Driver Message', IDOK);
DRV_DeviceClose(lDeviceHandle);
Exit;
End;
{ Enable channel }
ptCounterPWMSetting.Port := StrToInt(cmbChannel.Text);
Val(txtPeriod0.Text, ptCounterPWMSetting.Period, code);
Val(txtHiPeriod0.Text, ptCounterPWMSetting.HiPeriod, code);
ptCounterPWMSetting.GateMode := 0;
ptCounterPWMSetting.OutCount := StrToInt(txtOutCount.Text);
ErrorNum := DRV_CounterPWMSetting(lDeviceHandle, ptCounterPWMSetting);
If ( DoesErr(ErrorNum) = 1 ) Then
Begin
Exit
End;
{ Start Channel PWM }
ErrorNum := DRV_CounterPWMEnable(lDeviceHandle, StrToInt(cmbChannel.Text));
If ( DoesErr(ErrorNum) = 1 ) Then
Begin
Exit;
End;
grpSelDev.Enabled := False;
btnRun.Enabled := False;
btnStop.Enabled := True;
btnExit.Enabled := False;
btnSet.Enabled := True;
txtOutCount.Enabled :=False;
end;
procedure TfrmMain.btnStopClick(Sender: TObject);
begin
{ Stop ChannelPWM }
ErrorNum := DRV_CounterReset(lDeviceHandle, StrToInt(cmbChannel.Text));
{ Stop Channel 1 PWM }
DRV_DeviceClose(lDeviceHandle);
grpSelDev.Enabled := True;
btnRun.Enabled := True;
btnStop.Enabled := False;
btnExit.Enabled := True;
btnSet.Enabled := False;
txtOutCount.Enabled :=True;
end;
procedure TfrmMain.btnSetClick(Sender: TObject);
var
code: Integer;
begin
if (ptCounterPWMSetting.OutCount <> 0) then
begin
Application.MessageBox('Can''t change setting in noncyclic mode', 'Error!!', MB_OK);
Exit
end ;
ptCounterPWMSetting.Port := StrToInt(cmbChannel.Text);
Val(txtPeriod0.Text, ptCounterPWMSetting.Period, code);
Val(txtHiPeriod0.Text, ptCounterPWMSetting.HiPeriod, code);
ptCounterPWMSetting.GateMode := 0;
ptCounterPWMSetting.OutCount := StrToInt(txtOutCount.Text); //no use here
ErrorNum := DRV_CounterPWMSetting(lDeviceHandle, ptCounterPWMSetting);
If ( DoesErr(ErrorNum) = 1 ) Then
Begin
Exit
End;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -