采用了虚拟串口软件 (已经上传到CSDN了)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm, TeEngine, Series, ExtCtrls, TeeProcs, Chart;
type
TForm1 = class(TForm)
Comm1: TComm;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
ComboBox3: TComboBox;
ComboBox4: TComboBox;
ComboBox5: TComboBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Button1: TButton;
Memo1: TMemo;
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Viewstring:string;
i:integer;
rbuf,sbuf:array[1..6] of byte;
implementation
{$R *.dfm}
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
begin
viewstring:=' ';
move(buffer^,pchar(@rbuf)^,bufferlength);
for i:=1 to bufferlength do
viewstring:=viewstring+inttohex(rbuf[i],2)+' ';
viewstring:='接受'+viewstring;
memo1.lines.add(viewstring);
memo1.lines.add(' ');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
//判断按键的状态可以便面打开串口出错时,要按两次按键
if Button1.Caption = '打开串口' then
begin
// 串口初始化
//EnumComPorts(ComboBox1.Items);//得到串口列表
Comm1.CommName := ComboBox1.Text;
Comm1.BaudRate := StrToInt(ComboBox2.Text);
Comm1.Parity := None; //ComboBox3.Text
Comm1.ByteSize := _8; //ComboBox4.Text
Comm1.StopBits := _1; //ComboBox5.Text
//###########################################
Comm1.StartComm;//打开串口
Button1.Caption := '关闭串口';
ComboBox1.Enabled := false;
ComboBox2.Enabled := false;
ComboBox3.Enabled := false;
ComboBox4.Enabled := false;
ComboBox5.Enabled := false;
//btnSend.Enabled := true;
//ImageOff.Visible := false;
//ImageOn.Visible := true;
end
else
begin
//##############################################
Comm1.StopComm; // 关闭串口
Button1.Caption := '打开串口';
ComboBox1.Enabled := true;
ComboBox2.Enabled := true;
ComboBox3.Enabled := true;
ComboBox4.Enabled := true;
ComboBox5.Enabled := true;
//btnSend.Enabled := false;
//ImageOn.Visible := false;
//ImageOff.Visible := true;
end;
end;
end.