我给你别人写的两段代码,第一段是称重显示仪设置, 第一段是过磅数据采集,
你看吧:unit c_ChengZhongYi;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, OleCtrls, MSCommLib_TLB, StdCtrls, Grids, DBGrids, Db, DBTables,
Buttons, DBCtrls;
type
TChengZhongYi = class(TForm)
Label4: TLabel;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Timer1: TTimer;
Button3: TButton;
DataSource1: TDataSource;
BitBtn1: TBitBtn;
Bevel2: TBevel;
Bevel3: TBevel;
GroupBoxCom: TGroupBox;
Rcom1: TRadioButton;
Rcom2: TRadioButton;
Label1: TLabel;
Edit2: TEdit;
MSComm1: TMSComm;
DBGrid1: TDBGrid;
GroupBox1: TGroupBox;
verify1: TRadioButton;
verify2: TRadioButton;
verify3: TRadioButton;
verify4: TRadioButton;
GroupBox2: TGroupBox;
B1200: TRadioButton;
B2400: TRadioButton;
B4800: TRadioButton;
B9600: TRadioButton;
procedure Button3Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
ChengZhongYi: TChengZhongYi;
implementation
uses PublicValues,c_DataM;
var
dk01:string[1];
verify01 :string[1];
Baud01 :string[4];
zhong01:string;
zhong02:Integer;
{$R *.DFM}
procedure TChengZhongYi.Button3Click(Sender: TObject);
begin
Close;
end;
procedure TChengZhongYi.Timer1Timer(Sender: TObject);
begin
zhong01 :=MSComm1.input;
Edit1.text :=zhong01;
zhong02 :=pos(verify01,zhong01);
if zhong02>0 then
edit2.text :=' '+IntTostr(StrToInt(copy(zhong01,zhong02+4,5)))
Else
Edit2.Text :='设置错误,无法读取数据!';
end;
procedure TChengZhongYi.Button1Click(Sender: TObject);
begin
if Button1.Caption='停 止' then
begin
Timer1.Interval :=0;
MSComm1.PortOpen :=False;
Edit2.Text :='';
Button1.Caption :='测 试';
Exit;
end;
if verify1.Checked then
Verify01 :='*'
else if verify2.Checked then
Verify01 :=')'
else if verify3.Checked then
Verify01 :='0'
else if verify4.Checked then
Verify01 :='2';
if B1200.Checked then
Baud01 :='1200'
else if B2400.Checked then
Baud01 :='2400'
else if B4800.Checked then
Baud01 :='4800'
else if B9600.Checked then
Baud01 :='9600';
if Rcom1.Checked=True Then
dk01 :='1'
Else
dk01 :='2';
if MSComm1.PortOpen=True Then
MSComm1.PortOpen :=False;
MSComm1.CommPort :=StrToInt(dk01);
MSComm1.Settings :=Baud01+',e,7,1';
MSComm1.PortOpen :=True;
Timer1.Interval :=500;
Button1.Caption :='停 止';
end;
procedure TChengZhongYi.Button2Click(Sender: TObject);
begin
if verify1.Checked then
Verify01 :='*'
else if verify2.Checked then
Verify01 :=')'
else if verify3.Checked then
Verify01 :='0'
else if verify4.Checked then
Verify01 :='2';
if B1200.Checked then
Baud01 :='1200'
else if B2400.Checked then
Baud01 :='2400'
else if B4800.Checked then
Baud01 :='4800'
else if B9600.Checked then
Baud01 :='9600';
if not gbData.TBSheZhi.FindKey(['Verify']) then
gbData.TBSheZhi.Append;
gbData.TBSheZhi.edit;
gbData.TBSheZhi.FieldByName('Name').AsString:='Verify';
gbData.TBSheZhi.FieldByName('Values').AsString:=Verify01;
gbData.TBSheZhi.Post;
gbData.TBSheZhi.FlushBuffers;
if not gbData.TBSheZhi.FindKey(['Baud']) then
gbData.TBSheZhi.Append;
gbData.TBSheZhi.edit;
gbData.TBSheZhi.FieldByName('Name').AsString:='Baud';
gbData.TBSheZhi.FieldByName('Values').AsString:=baud01;
gbData.TBSheZhi.Post;
gbData.TBSheZhi.FlushBuffers;
Application.MessageBox('已经保存当前设置,重新启动本系统后设开始生效 !','系统信息',mb_ok+mb_IconInformation);
end;
procedure TChengZhongYi.BitBtn1Click(Sender: TObject);
begin
Application.HelpContext(103);
end;
procedure TChengZhongYi.FormCreate(Sender: TObject);
var verify01,baud01:string;
begin
if gbData.TBSheZhi.FindKey(['Verify']) then
verify01:=gbData.TBSheZhi.FieldByName('values').AsString;
if gbData.TBSheZhi.FindKey(['Baud']) then
baud01:=gbData.TBSheZhi.FieldByName('values').AsString;
if verify01='*' then
verify1.Checked:=true
else if verify01=')' then
verify2.Checked:=true
else if verify01='0' then
verify3.Checked:=true
else if verify01='2' then
verify4.Checked:=true;
if baud01='1200' then
B1200.Checked:=true
else if baud01='2400' then
B2400.Checked:=true
else if baud01='4800' then
B4800.Checked:=true
else if baud01='9600' then
B9600.Checked:=true;
end;
end.