用控件spcomm写的上位机程序 Delphi / Windows SDK/APIhttp://www.delphi2007.net/DelphiNetwork/html/delphi_20061125143154259.html
我先用上位机给单片机发数据,单片机接收到数据后在发给上位机并显示出来.问什么上位机接收不到单片机发给的数据,怎么知道上位机已经给单片机发数据了.我的单片机程序好用我已经用串口助手调试过了,谢谢
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
ComboBox1: TComboBox;
Label1: TLabel;
Button6: TButton;
Comm1: TComm;
Edit1: TEdit;
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject;Buffer:pointer;BufferLength:Word);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Comm1ReceiveData(Sender: TObject;Buffer:pointer;BufferLength:Word);
var
data:string;
begin //接收数据
SetLength(data,BufferLength);
move(Buffer^,pchar(data)^,Bufferlength);
Memo1.lines.Add(data);
Memo1.Invalidate;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Form1.Close;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
comm1.StopComm ;
button1.Enabled:=true ;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
try
//comm1.CommName:='com'+inttostr(combobox1.ItemIndex); //选择端口号
comm1.CommName=com1;
comm1.BaudRate:=9600;
comm1.StartComm;
button1.Enabled:=false;
button2.Enabled:=true;
button3.Enabled:=true;
button4.Enabled:=true;
button5.Enabled:=true;
except
ShowMessage('打开串口错误!');
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
var
str:string;
begin //发送数据
str:= Edit1.Text;
form1.comm1.writecommdata(pchar(str),length(str));
end;
end.
用双串口或虚拟串口调试
另外你的spcomm控件的属性值设置正确了没?
spcomm的所有bool属性都设成false 试试