📄 setup.pas
字号:
unit Setup;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, Spin, INIFiles;
type
TSetupValue = record
AutoOpenComFlag: Boolean; //是否自动打开串口
PortIndex: Integer; //串口号
SpeedIndex: Integer; //串口速度
DataIndex: Integer; //数据位
StopIndex: Integer; //停止位
TimeOut: Integer; //超时时间
SendFlag: Boolean; //true:发送方;false:接收方
end;
TfrmSetup = class(TForm)
cbAutoOpen: TCheckBox;
cbPort: TComboBox;
Label1: TLabel;
Label2: TLabel;
cbSpeed: TComboBox;
Label3: TLabel;
cbData: TComboBox;
Label6: TLabel;
cbStop: TComboBox;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
Label4: TLabel;
sedTimeOut: TSpinEdit;
rbSend: TRadioButton;
rbReceive: TRadioButton;
Label5: TLabel;
private
{ Private declarations }
FSetupValue: TSetupValue;
procedure SaveSetupValue(AValue: TSetupValue);
procedure RefreshValue;
public
{ Public declarations }
class function Setup(out AValue: TSetupValue): Boolean;
class function GetSetupValue: TSetupValue;
end;
var
frmSetup: TfrmSetup;
implementation
{$R *.dfm}
{ TfrmSetup }
class function TfrmSetup.GetSetupValue: TSetupValue;
var
f: TINIFile;
s: string;
begin
s := ExtractFilePath(Application.ExeName) + '\setup.dat';
f := TIniFile.Create(s);
try
Result.AutoOpenComFlag := f.ReadBool('SYS', 'autoopen', true);
Result.PortIndex := f.ReadInteger('COM', 'port', 0);
Result.SpeedIndex := f.ReadInteger('COM', 'speed', 2);
Result.DataIndex := f.ReadInteger('COM', 'data', 3);
Result.StopIndex := f.ReadInteger('COM', 'stop', 0);
Result.TimeOut := f.ReadInteger('SYS', 'timeout', 900000); //默认15分钟
Result.SendFlag := f.ReadBool('SYS', 'sendflag', True);
finally
f.Free;
end;
end;
procedure TfrmSetup.RefreshValue;
begin
cbAutoOpen.Checked := FSetupValue.AutoOpenComFlag;
cbPort.ItemIndex := FSetupValue.PortIndex;
cbSpeed.ItemIndex := FSetupValue.SpeedIndex;
cbData.ItemIndex := FSetupValue.DataIndex;
cbStop.ItemIndex := FSetupValue.StopIndex;
sedTimeOut.Value := FSetupValue.TimeOut;
if FSetupValue.SendFlag then
rbSend.Checked := true
else
rbReceive.Checked := true;
end;
procedure TfrmSetup.SaveSetupValue(AValue: TSetupValue);
var
f: TINIFile;
s: string;
begin
s := ExtractFilePath(Application.ExeName) + '\setup.dat';
f := TIniFile.Create(s);
try
f.WriteBool('SYS', 'autoopen', AValue.AutoOpenComFlag);
f.WriteInteger('COM', 'port', AValue.PortIndex);
f.WriteInteger('COM', 'speed', AValue.SpeedIndex);
f.WriteInteger('COM', 'data', AValue.DataIndex);
f.WriteInteger('COM', 'stop', AValue.StopIndex);
f.WriteInteger('SYS', 'timeout', AValue.TimeOut);
f.WriteBool('SYS', 'sendflag', AValue.SendFlag);
finally
f.Free;
end;
end;
class function TfrmSetup.Setup(out AValue: TSetupValue): Boolean;
var
FSetupForm: TfrmSetup;
begin
Result := false;
FSetupForm := TfrmSetup.Create(Application);
try
with FSetupForm do
begin
FSetupValue := GetSetupValue;
RefreshValue;
ShowModal;
if ModalResult = mrOK then
begin
FSetupValue.AutoOpenComFlag := cbAutoOpen.Checked;
FSetupValue.PortIndex := cbPort.ItemIndex;
FSetupValue.SpeedIndex := cbSpeed.ItemIndex;
FSetupValue.DataIndex := cbData.ItemIndex;
FSetupValue.StopIndex := cbStop.ItemIndex;
FSetupValue.TimeOut := sedTimeOut.Value;
FSetupValue.SendFlag := rbSend.Checked;
SaveSetupValue(FSetupValue);
Result := true;
AValue := FSetupValue;
end;
end;
finally
FreeAndNil(FSetupForm);
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -