spcomm控件
procedure TForm1.FormCreate(Sender: TObject);
var
IniFileName:string;
begin
IniFileName:=ExtractFilePath(ParamStr(0))+'comsetup.ini';
IniFile:=TIniFile.Create(IniFileName);
SendTimer.Enabled :=false;
LoadSettings;
try
try
Comm1.startcomm;//创建窗体时,将comm1控件打开。
SendBuffer[0]:=$aa; SendBuffer[1]:=$07;
SendBuffer[2]:=$01; SendBuffer[3]:=$88;
SendBuffer[4]:=not(SendBuffer[0]+SendBuffer[1]+
SendBuffer[2]+SendBuffer[3]);
SendBufferLength:=5;
Sendtimer.Enabled :=true;
Except
on E:Exception do
showmessage(E.Message );
//comm1.StopComm ;
end;
finally
showmessage('form2.show-Finally');
// comm1.StopComm ;
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.StopComm;//关闭窗体时,将comm1控件关闭。
end;
procedure TForm1.SendTimerTimer(Sender: TObject);
var
viewstring:string;
i:integer;
begin
comm1.WritecommData(pchar(@SendBuffer),SendBufferLength);
for i:=0 to SendBufferLength-1 do
viewstring:=viewstring+inttohex(SendBuffer,2)+' ';
viewstring:='发送'+viewstring;
Memo1.Lines.Add(viewstring);
memo1.Lines.Add ('');
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
viewstring:string;
i:integer;
begin
// SetLength(S, BufferLength); //接收RS232的数据并显示Memo1上。
Move(Buffer^,pchar(@ReceiveBuffer)^, BufferLength);
for i:=0 to bufferlength-1 do
viewstring:=viewstring+inttohex(ReceiveBuffer,2)+' ';
viewstring:='接收'+viewstring;
Memo1.Lines.Add(viewstring);
Memo1.Lines.Add('');
Memo1.Invalidate;
if memo1.Lines.Count >1000 then
memo1.Clear ;
// edit2.Text :=floattostr(sqrt(2));
edit2.Text :=floattostr(((ReceiveBuffer[0]*256+ReceiveBuffer[1])-2048)/2048*115*sqrt(2));
end;
例子