Sunteți pe pagina 1din 6

unit uSMSGateway; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls,

OoMisc, AdPort; type TForm1 = class(TForm) GroupBox1: TGroupBox; Button1: TButton; Label1: TLabel; ComboBox1: TComboBox; Label2: TLabel; ComboBox2: TComboBox; Button2: TButton; Edit1: TEdit; Memo1: TMemo; GroupBox2: TGroupBox; Label3: TLabel; Edit2: TEdit; Label4: TLabel; Memo2: TMemo; Button3: TButton; StatusBar1: TStatusBar; ApdComPort1: TApdComPort; GroupBox3: TGroupBox; Label5: TLabel; Label6: TLabel; Edit3: TEdit; Memo3: TMemo; Edit4: TEdit; Label7: TLabel; procedure FormCreate(Sender: TObject); procedure ComboBox1DropDown(Sender: TObject); procedure Button1Click(Sender: TObject); procedure ApdComPort1PortClose(Sender: TObject); procedure ApdComPort1PortOpen(Sender: TObject); procedure Button2Click(Sender: TObject); procedure ApdComPort1TriggerAvail(CP: TObject; Count: Word); procedure Button3Click(Sender: TObject); private { Private declarations } Port_State: boolean; thedata,MessageStr:string; Command_Status: boolean; SMSReceive: boolean; procedure EnumComPorts(Ports: TStrings); procedure SendSerial(TheData:string;LineFeed:boolean); function GetTimeOut(TimeOut:integer):boolean; public { Public declarations } end; var Form1: TForm1; implementation

{$R *.dfm} procedure TForm1.ApdComPort1PortClose(Sender: TObject); begin Port_State:=false; Button1.Caption:='Open'; end; procedure TForm1.ApdComPort1PortOpen(Sender: TObject); begin Port_State:=true; Button1.Caption:='Close'; end; procedure TForm1.ApdComPort1TriggerAvail(CP: TObject; Count: Word); var thechar:char; buffer1:string; PhoneNumber, MsgDate:string; i,j:integer; begin for I :=Count downto 1 do begin thechar:=ApdComPort1.GetChar; thedata:=thedata+thechar; if thechar=chr(10) then begin //buffer1:=copy(thedata,1,4); if trim(thedata)<>'' then begin Memo1.Lines.Add(trim(thedata)+#13#10); Command_Status:=false; if trim(thedata)='OK' then begin Command_Status:=true; if SMSReceive then begin SMSReceive:=false; Memo3.Text:=MessageStr; MessageBox(Handle,'You got a message',pchar(Application.Title), MB_OK or MB_ICONINFORMATION); end; end; if SMSReceive then MessageStr:=MessageStr+thedata; thedata:=trim(thedata); if thedata ='ERROR' then Command_Status:=false; if Length(thedata)>5 then begin buffer1:=copy(thedata,1,5); if buffer1='+CMTI' then begin buffer1:=copy(thedata,13,Length(thedata)-1); SendSerial('AT+CMGR='+buffer1,true);

end else if buffer1='+CMGR' then begin PhoneNumber:=''; for j := 22 to Length(thedata) do begin if thedata[j]='"' then break; PhoneNumber:=PhoneNumber+thedata[j]; end; MsgDate:=''; for j := j+4 to Length(thedata) do begin if thedata[j]='"' then break; MsgDate:=MsgDate+thedata[j]; end; MessageStr:=''; for j := j+1 to Length(thedata) - 1 do begin //if thedata[j]=chr(10) then break; MessageStr:=MessageStr+thedata[j]; end; Edit3.Text:=PhoneNumber; Edit4.Text:=MsgDate; SMSReceive:=true; //Memo3.Text:=thedata; end; end; thedata:=''; end; end; end; end; procedure TForm1.Button1Click(Sender: TObject); var ComNo: string; begin if ComboBox1.ItemIndex=-1 then begin MessageBox(Handle,'Please select a com',pchar(Application.Title), MB_OK or MB_ICONERROR); exit; end; ComNo:=copy(ComboBox1.Text,4,length(ComboBox1.Text)-1); if not Port_State then begin with ApdComPort1 do begin Baud:=StrToInt(ComboBox2.Text); ComNumber:=StrToInt(ComNo); try Open:=true; Except MessageBox(Handle,'Can not open COM',pchar(Application.Title), MB_OK or MB_ICONERROR); exit; end;

end; end else begin ApdComPort1.Open:=false; end; end; procedure TForm1.Button2Click(Sender: TObject); begin if not Port_State then exit; Memo1.Lines.Add(Edit1.Text); ApdComPort1.Output:=Edit1.Text+#13#10; end; procedure TForm1.Button3Click(Sender: TObject); begin if not Port_State then exit; Command_Status:=false; with ApdComPort1 do begin SendSerial('AT',true); if not GetTimeOut(1000) then begin MessageBox(Handle,'Modem no reply or command error',pchar(Application.T itle), MB_OK or MB_ICONERROR); exit; end; SendSerial('AT+CMGS="'+Edit2.Text+'"'#13,false); GetTimeOut(2000); Output:=Memo2.Text; Memo1.Lines.Add(Memo2.Text); Output:=chr(26); if not GetTimeOut(100) then begin MessageBox(Handle,'SMS send error',pchar(Application.Title), MB_OK or MB_ICONERROR); exit; end else MessageBox(Handle,'SMS send OK',pchar(Application.Title), MB_OK); end; end; procedure TForm1.ComboBox1DropDown(Sender: TObject); begin EnumComPorts(ComboBox1.Items); end; procedure TForm1.EnumComPorts(Ports: TStrings); var KeyHandle: HKEY; ErrCode, Index: Integer; ValueName, Data: string; ValueLen, DataLen, ValueType: DWORD; TmpPorts: TStringList; begin

ErrCode := RegOpenKeyEx( HKEY_LOCAL_MACHINE, 'HARDWARE\DEVICEMAP\SERIALCOMM', 0, KEY_READ, KeyHandle); if ErrCode <> ERROR_SUCCESS then begin //raise EComPortExt.Create('EnumComPorts',CError_RegError, ErrCode); MessageBox(Handle,'Registry Error',pchar(Application.Title),MB_OK or MB_ICON ERROR); exit; end; TmpPorts := TStringList.Create; try Index := 0; repeat ValueLen := 256; DataLen := 256; SetLength(ValueName, ValueLen); SetLength(Data, DataLen); ErrCode := RegEnumValue( KeyHandle, Index, PChar(ValueName), {$IFDEF DELPHI_4_OR_HIGHER} Cardinal(ValueLen), {$ELSE} ValueLen, {$ENDIF} nil, @ValueType, PByte(PChar(Data)), @DataLen); if ErrCode = ERROR_SUCCESS then begin SetLength(Data, DataLen); TmpPorts.Add(Data); Inc(Index); end else if ErrCode <> ERROR_NO_MORE_ITEMS then MessageBox(Handle,'Registry error',pchar(Application.Title),MB_OK or MB_ICON ERROR); until (ErrCode <> ERROR_SUCCESS) ; TmpPorts.Sort; Ports.Assign(TmpPorts); finally RegCloseKey(KeyHandle); TmpPorts.Free; end; end; procedure TForm1.FormCreate(Sender: TObject); begin Caption:='SMS Gateway';

Application.Title:='SMS Gateway'; StatusBar1.Panels[1].Text:='Blogna Kang U-2 Man'; EnumComPorts(ComboBox1.Items); if ComboBox1.Items.Count>0 then ComboBox1.ItemIndex:=0; end; function TForm1.GetTimeOut(TimeOut: integer): boolean; begin Result:=false; while TimeOut>0 do begin Sleep(1); Application.ProcessMessages; dec(TimeOut); end; Result:=Command_Status; end; procedure TForm1.SendSerial(TheData: string;LineFeed:boolean); begin Memo1.Lines.Add(trim(TheData)); if LineFeed then TheData:=TheData+#13#10; ApdComPort1.Output:=TheData; end; end.