unit GBSetFrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,inifiles, ExtCtrls, SPComm, Buttons, ComCtrls, ToolWin,
ImgList;
type
TGBSetForm = class(TForm)
Panel1: TPanel;
Label3: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Bevel1: TBevel;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
z: TRadioButton;
q: TRadioButton;
Panel2: TPanel;
Label11: TLabel;
B: TRadioButton;
A: TRadioButton;
ImageList1: TImageList;
Edit1: TComboBox;
LB: TPanel;
ToolBar1: TToolBar;
ToolButton3: TToolButton;
ToolButton4: TToolButton;
ToolButton2: TToolButton;
ToolButton1: TToolButton;
ToolButton5: TToolButton;
ToolButton6: TToolButton;
Comm1: TComm;
Memo1: TMemo;
procedure WriteIni(Comm,CommName,BaudRate,Timeout,start,len,zt:String);
procedure ToolButton3Click(Sender: TObject);
procedure ToolButton1Click(Sender: TObject);
procedure ToolButton2Click(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure FormCreate(Sender: TObject);
procedure zClick(Sender: TObject);
procedure qClick(Sender: TObject);
procedure Edit3KeyPress(Sender: TObject; var Key: Char);
procedure Edit4KeyPress(Sender: TObject; var Key: Char);
procedure Edit5KeyPress(Sender: TObject; var Key: Char);
procedure Edit2KeyPress(Sender: TObject; var Key: Char);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure ToolButton6Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
GBSetForm: TGBSetForm;
Filename:string;
Comm,CommName,BaudRate,Timeout,start,len,zt:String;
implementation
var
CommSetIni:Tinifile;
{$R *.dfm}
procedure TGBSetForm.WriteIni(Comm,CommName,BaudRate,Timeout,start,len,zt:String);
begin
CommSetIni.writestring(Comm,'CommName',CommName);
CommSetIni.writestring(Comm,'BaudRate',BaudRate);
CommSetIni.writestring(Comm,'Timeout',Timeout);
CommSetIni.writestring(Comm,'start',start);
CommSetIni.writestring(Comm,'len',len);
CommSetIni.writestring(Comm,'zt',zt);
end;
function Codown(Str: string): String;
Var I: Integer;
S:string;
begin
for I:=Length(Trim(Str)) downto 1 do
begin
S:=copy(Trim(Str), i , 1);
Result:=Result+S;
end;
end;
procedure TGBSetForm.ToolButton3Click(Sender: TObject);
begin
IF Z.Checked then Comm:='Comm1' Else Comm:='Comm2';
CommName:=Trim(Edit1.Text) ;
BaudRate:=Trim(Edit2.Text );
Timeout:= Trim(Edit3.Text );
start:=Trim(Edit4.Text );
Len:=Trim(Edit5.Text );
IF A.Checked then zt:='0' Else Zt:='1';
WriteIni(Comm,CommName,BaudRate,Timeout,start,len,zt);
Application.MessageBox('保存成功!!!','提示',64+MB_OK);
end;
procedure TGBSetForm.ToolButton1Click(Sender: TObject);
begin
comm1.StopComm ;
end;
procedure TGBSetForm.ToolButton2Click(Sender: TObject);
begin
comm1.CommName:=trim(edit1.Text);
comm1.BaudRate:=strtoint(trim(edit2.Text));
comm1.ReadIntervalTimeout:=strtoint('0'+trim(edit3.Text));
comm1.StopComm ;
Try
comm1.StartComm ;
ExCept
comm1.StopComm ;
end;
end;
procedure TGBSetForm.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
type
IArr = ^Integer;
var
RevP : array [1..4096] of byte;
st,l,i : integer;
CommRevStr : ShortString;
Str : String;
begin
SetLength(CommRevStr,BufferLength);
Move(IArr(buffer)^,Revp,bufferLength);
For i:=1 to BufferLength do
begin
CommrevStr := Char(Revp);
end;
Memo1.Lines.Add(CommrevStr);
Memo1.Invalidate;
st:=strtoint('0'+trim(edit4.Text));
l:=strtoint('0'+trim(edit5.Text));
Str:=copy(CommrevStr,st,l);
IF length(Str)=l then
Begin
IF A.Checked then LB.Caption :=Str
Else LB.Caption:=CoDown(Str);
end;
end;
procedure TGBSetForm.FormCreate(Sender: TObject);
begin
FileName:=ExtractFilePath(paramstr(0))+'CommSet.DLL';
CommSetIni:=TInifile.Create(FileName);
IF Z.Checked then Comm:='Comm1' Else Comm:='Comm2';
Edit1.Text :=CommSetIni.ReadString (Comm,'CommName','COM1');
Edit2.Text :=CommSetIni.ReadString(Comm,'BaudRate','1200');
Edit3.Text :=CommSetIni.ReadString(Comm,'Timeout','50');
Edit4.Text :=CommSetIni.ReadString(Comm,'start','1');
Edit5.Text :=CommSetIni.ReadString(Comm,'len','6');
IF Trim(CommSetIni.ReadString(Comm,'zt','0'))='0' then A.Checked Else B.Checked ;
LB.Color :=clblack;
Application.Title := Trim('COM测试程序');
end;
procedure TGBSetForm.zClick(Sender: TObject);
begin
IF Z.Checked then Comm:='Comm1' Else Comm:='Comm2';
Edit1.Text :=CommSetIni.ReadString (Comm,'CommName','COM1');
Edit2.Text :=CommSetIni.ReadString(Comm,'BaudRate','1200');
Edit3.Text :=CommSetIni.ReadString(Comm,'Timeout','50');
Edit4.Text :=CommSetIni.ReadString(Comm,'start','1');
Edit5.Text :=CommSetIni.ReadString(Comm,'len','6');
IF Trim(CommSetIni.ReadString(Comm,'zt','0'))='0' then A.Checked:=True Else B.Checked:=True ;
end;
procedure TGBSetForm.qClick(Sender: TObject);
begin
IF Z.Checked then Comm:='Comm1' Else Comm:='Comm2';
Edit1.Text :=CommSetIni.ReadString (Comm,'CommName','COM1');
Edit2.Text :=CommSetIni.ReadString(Comm,'BaudRate','1200');
Edit3.Text :=CommSetIni.ReadString(Comm,'Timeout','50');
Edit4.Text :=CommSetIni.ReadString(Comm,'start','1');
Edit5.Text :=CommSetIni.ReadString(Comm,'len','6');
IF Trim(CommSetIni.ReadString(Comm,'zt','0'))='0' then A.Checked:=True Else B.Checked:=True ;
end;
procedure TGBSetForm.Edit3KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9',#8]) then
key:=#0;
end;
procedure TGBSetForm.Edit4KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9',#8]) then
key:=#0;
end;
procedure TGBSetForm.Edit5KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9',#8]) then
key:=#0;
end;
procedure TGBSetForm.Edit2KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9',#8]) then
key:=#0;
end;
procedure TGBSetForm.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9','C','O','M','c','o','m',#8]) then
key:=#0;
end;
procedure TGBSetForm.ToolButton6Click(Sender: TObject);
begin
Close;
end;
end.