哪位能给我一个用spcom控件通讯的例子?E-mail:huu@263.net(100分)

  • 主题发起人 主题发起人 gateway
  • 开始时间 开始时间
这种例子很多,已经给你发了一个
 
张国芳姐姐(或哥哥):我也想要一个。xiximing@263.net
 
本人有一个,相信一定对你友帮助:
unit AlarmMsg;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, SPComm,Registry;

type
TForm1 = class(TForm)
Memo1: TMemo;
SMSPort: TComm;
btnDial: TButton;
btnAutoAnswer: TButton;
btnHangUp: TButton;
procedure SMSPortReceiveData(Sender: TObject; Buffer: Pointer;BufferLength: Word);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure btnDialClick(Sender: TObject);
procedure btnAutoAnswerClick(Sender: TObject);
procedure btnHangUpClick(Sender: TObject);
private
{ Private declarations }
public
procedure SetCom;
end;
const
Cmd1: array[0..6] of Char ='ATDT#2'+#$0D;
Cmd2: array[0..3] of Char ='ATA'+#$0D;
Cmd3: array[0..6] of Char ='ATS0=2'+#$0D;
Cmd4: array[0..8] of Char ='+++ATH0'+#$0D+#$0A;
Cmd5: array[0..5] of Char ='ATH0'+#$0D+#$0A;
Cmd6: array[0..4] of Char ='ATY1'+#$0D;
Cmd7: array[0..5] of Char ='ATZ0'+#$0D+#$0A;


var
Form1: TForm1;
DCount:Integer;
implementation

{$R *.DFM}

procedure TForm1.SetCom;
var
vReg:TRegistry;
begin
//生成一个注册表对象
vReg:=TRegistry.Create;
try
// SMSPort.CommName := vReg.ReadString(COMMSEC, 'DownCom','COM1');
// SMSPort.BaudRate := vReg.ReadInteger(COMMSEC,'DownBaudRate',1200);
finally
//关闭注册表,保存对注册表的修改
vReg.CloseKey;
vReg.Free;
end;
end;


//窗体创建事件,初始化并打开串口
procedure TForm1.FormCreate(Sender: TObject);
begin
SMSPort.CommName:= '//./COM1';
SMSPort.StartComm;
Sleep(1000);
SMSPort.WriteCommData(Cmd3,SizeOf(Cmd3));
DCount:=0;
end;


//窗体关闭事件
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
SMSPort.StopComm; //关闭窗体时,将comm1控件关闭。
end;



procedure TForm1.SMSPortReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word);
type
IArr = ^integer;
var
RevP : array [1..2048] of Byte;
i: Integer;
CommRevStr : ShortString;
Msg :String;
begin
SetLength(CommRevStr,BufferLength);
Move(IArr(Buffer)^,RevP,BufferLength);
for i:=1 to BufferLength do
begin
CommrevStr := Char(RevP);
end;
Memo1.Lines.Add(CommRevStr);
//ShowMessage('sfsadfdsafds');
if Pos('CONNECT',CommRevStr)<>0 then
Begin
DCount:=DCount+1;
Msg:='ABCDEFGEND'+IntToStr(DCount);
SMSPort.WriteCommData(PChar(Msg),Length(Msg));
End;
end;




procedure TForm1.btnDialClick(Sender: TObject);
begin
SMSPort.WriteCommData(Cmd1,SizeOf(Cmd1));
end;

procedure TForm1.btnAutoAnswerClick(Sender: TObject);
begin
SMSPort.WriteCommData(Cmd2,SizeOf(Cmd2));
end;

procedure TForm1.btnHangUpClick(Sender: TObject);
begin
// Sleep(1000);
// SMSPort.WriteCommData(Cmd4,SizeOf(Cmd4));
// Sleep(1000);
SMSPort.WriteCommData(Cmd4,SizeOf(Cmd4));
end;

end.
 
多人接受答案了。
 
后退
顶部