Y
y1573
Unregistered / Unconfirmed
GUEST, unregistred user!
刚开始学习Dephi,现在做一个串口通信程序,我用的是SPComm控件,按照网上下载的一些教程编辑,但无论如何都不能收到信息,用串口精灵调试一切OK,不知为何?望哪位高手告之,谢谢!源代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, jpeg, ExtCtrls, StdCtrls, SPComm;
type
TForm1 = class(TForm)
Image1: TImage;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
ComboBox1: TComboBox;
Timer1: TTimer;
Edit1: TEdit;
Edit2: TEdit;
ListBox1: TListBox;
Label4: TLabel;
PComm: TComm;
procedure Timer1Timer(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure PCommModemStateChange(Sender: TObject; ModemEvent: Cardinal);
procedure PCommReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
ID_num: Integer;
rbuf : array[1..2] of byte;
implementation
{$R *.dfm}
procedure TForm1.Button4Click(Sender: TObject);
begin
{ if inttostr(combobox1.itemindex)='Ñ¡Ôñ¶Ë¿Ú' then
showmessage('´ò¿ª´®¿Ú´íÎó')
else
begin }
PComm.commname := 'com'+inttostr(combobox1.itemindex) ;
PComm.baudrate := 19200 ;
If Form1.Button4.Caption='´ò¿ª´®¿Ú' then
Form1.Button4.Caption:='¹Ø±Õ´®¿Ú'
Else Form1.Button4.Caption:='´ò¿ª´®¿Ú';
If Form1.Button4.Caption='¹Ø±Õ´®¿Ú' then
begin
button1.enabled := true;
button2.enabled := true;
PComm.StartComm;
end
else
begin
button1.enabled := false;
button2.enabled := false;
PComm.stopComm ;
end;
end;
//end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
DateSeparator:='-';
ShortDateFormat :='yyyy/mm/dd';
Edit1.Text :=DateTimeToStr(Now);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Form1.Close;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Form1.ListBox1.Clear;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
If Form1.Button1.Caption='¿ªÊ¼¼àÊÓ' then
Form1.Button1.Caption:='¹Ø±Õ¼àÊÓ'
Else Form1.Button1.Caption:='¿ªÊ¼¼àÊÓ';
If Form1.Button1.Caption='¹Ø±Õ¼àÊÓ' then
If (ID_num>=0) And (ID_num<10)then
begin
Edit2.Text :='0'+Inttostr(ID_num);
end
Else
Edit2.Text :=Inttostr(ID_num)
Else Edit2.Text :='00' ;
end;
procedure TForm1.PCommModemStateChange(Sender: TObject;
ModemEvent: Cardinal);
begin
end;
//&frac12;&Oacute;&Ecirc;&Uuml;&ordm;&macr;&Ecirc;&yacute;
procedure TForm1.PCommReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
begin
If Form1.Button4.Caption='&sup1;&Oslash;±&Otilde;&acute;&reg;&iquest;&Uacute;' then
If Form1.Button1.Caption='&sup1;&Oslash;±&Otilde;&frac14;à&Ecirc;&Oacute;' then
begin
move(buffer^,pchar(@rbuf)^,bufferlength);
ID_num:=rbuf[1+rbuf[2];//例子而已
//If ID_num<>0 then
ListBox1.Items.Add(DateTimeToStr(Now)+' ID: '+Inttostr(ID_num));
end;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, jpeg, ExtCtrls, StdCtrls, SPComm;
type
TForm1 = class(TForm)
Image1: TImage;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
ComboBox1: TComboBox;
Timer1: TTimer;
Edit1: TEdit;
Edit2: TEdit;
ListBox1: TListBox;
Label4: TLabel;
PComm: TComm;
procedure Timer1Timer(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure PCommModemStateChange(Sender: TObject; ModemEvent: Cardinal);
procedure PCommReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
ID_num: Integer;
rbuf : array[1..2] of byte;
implementation
{$R *.dfm}
procedure TForm1.Button4Click(Sender: TObject);
begin
{ if inttostr(combobox1.itemindex)='&Ntilde;&iexcl;&Ocirc;&ntilde;&para;&Euml;&iquest;&Uacute;' then
showmessage('&acute;ò&iquest;&ordf;&acute;&reg;&iquest;&Uacute;&acute;í&Icirc;ó')
else
begin }
PComm.commname := 'com'+inttostr(combobox1.itemindex) ;
PComm.baudrate := 19200 ;
If Form1.Button4.Caption='&acute;ò&iquest;&ordf;&acute;&reg;&iquest;&Uacute;' then
Form1.Button4.Caption:='&sup1;&Oslash;±&Otilde;&acute;&reg;&iquest;&Uacute;'
Else Form1.Button4.Caption:='&acute;ò&iquest;&ordf;&acute;&reg;&iquest;&Uacute;';
If Form1.Button4.Caption='&sup1;&Oslash;±&Otilde;&acute;&reg;&iquest;&Uacute;' then
begin
button1.enabled := true;
button2.enabled := true;
PComm.StartComm;
end
else
begin
button1.enabled := false;
button2.enabled := false;
PComm.stopComm ;
end;
end;
//end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
DateSeparator:='-';
ShortDateFormat :='yyyy/mm/dd';
Edit1.Text :=DateTimeToStr(Now);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Form1.Close;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Form1.ListBox1.Clear;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
If Form1.Button1.Caption='&iquest;&ordf;&Ecirc;&frac14;&frac14;à&Ecirc;&Oacute;' then
Form1.Button1.Caption:='&sup1;&Oslash;±&Otilde;&frac14;à&Ecirc;&Oacute;'
Else Form1.Button1.Caption:='&iquest;&ordf;&Ecirc;&frac14;&frac14;à&Ecirc;&Oacute;';
If Form1.Button1.Caption='&sup1;&Oslash;±&Otilde;&frac14;à&Ecirc;&Oacute;' then
If (ID_num>=0) And (ID_num<10)then
begin
Edit2.Text :='0'+Inttostr(ID_num);
end
Else
Edit2.Text :=Inttostr(ID_num)
Else Edit2.Text :='00' ;
end;
procedure TForm1.PCommModemStateChange(Sender: TObject;
ModemEvent: Cardinal);
begin
end;
//&frac12;&Oacute;&Ecirc;&Uuml;&ordm;&macr;&Ecirc;&yacute;
procedure TForm1.PCommReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
begin
If Form1.Button4.Caption='&sup1;&Oslash;±&Otilde;&acute;&reg;&iquest;&Uacute;' then
If Form1.Button1.Caption='&sup1;&Oslash;±&Otilde;&frac14;à&Ecirc;&Oacute;' then
begin
move(buffer^,pchar(@rbuf)^,bufferlength);
ID_num:=rbuf[1+rbuf[2];//例子而已
//If ID_num<>0 then
ListBox1.Items.Add(DateTimeToStr(Now)+' ID: '+Inttostr(ID_num));
end;
end;
end.