Skip to content

Instantly share code, notes, and snippets.

@stijnsanders
Created March 3, 2017 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stijnsanders/bf0e7014b5824720f50e2f25a8d6efaf to your computer and use it in GitHub Desktop.
Save stijnsanders/bf0e7014b5824720f50e2f25a8d6efaf to your computer and use it in GitHub Desktop.
Send SMS over 'USB modem' connected mobile phone
unit SendSMS;
interface
procedure DoSendSMS(const ToNr,Msg:string);
implementation
uses Windows, SysUtils, Classes, Registry, SmartPort;
const
ComPrefix='\Device\QCUSB_COM';
procedure DoSendSMS(const ToNr,Msg:string);
var
a,s:string;
r:TRegistry;
sl:TStringList;
i:integer;
p:TSmartPort;
ok:boolean;
tc:cardinal;
begin
r:=TRegistry.Create;
sl:=TStringList.Create;
try
r.RootKey:=HKEY_LOCAL_MACHINE;
r.OpenKeyReadOnly('\HARDWARE\DEVICEMAP\SERIALCOMM');
r.GetValueNames(sl);
i:=0;
while (i<sl.Count) and (Copy(sl[i],1,Length(ComPrefix))<>ComPrefix) do inc(i);
if i=sl.Count then
raise Exception.Create('Modem not found');
a:=r.ReadString(sl[i]);
finally
r.Free;
sl.Free;
end;
p:=TSmartPort.Create;
sl:=TStringList.Create;
try
p.PortName:=a;
p.SetPortParameters(115200,8,ppParityNone,psStopBitOne,$1000,$1000);//calls open
{
p.Send('AT+CPIN="0000"'#13);
sl.Text:=p.Get;
ok:=false;
for i:=0 to sl.Count-1 do
begin
s:=Trim(sl[i]);
if (s<>'') and (s[1]<>'^') and (Copy(s,1,3)<>'AT+') then
if s='OK' then ok:=true else
raise Exception.Create('F1[ERROR]'+s);
end;
if not ok then raise Exception.Create('F1[NO RESPONSE]');
}
p.Send('AT+CMGF=1'#13);
sl.Text:=p.Get;
ok:=false;
for i:=0 to sl.Count-1 do
begin
s:=Trim(sl[i]);
if (s<>'') and (s[1]<>'^') and (Copy(s,1,3)<>'AT+') then
if s='OK' then ok:=true else
raise Exception.Create('F1[ERROR]'+s);
end;
if not ok then raise Exception.Create('F1[NO RESPONSE]');
{
//p.Send('AT+CSMP=17,167,0,0'#13);
p.Send('AT+CSMP=1,,0,0'#13);
sl.Text:=p.Get;
ok:=false;
for i:=0 to sl.Count-1 do
begin
s:=Trim(sl[i]);
if (s<>'') and (s[1]<>'^') and (Copy(s,1,3)<>'AT+') then
if s='OK' then ok:=true else
raise Exception.Create('F1[ERROR]'+s);
end;
if not ok then raise Exception.Create('F1[NO RESPONSE]');
}
p.Send('AT+CMGS="+'+ToNr+'"'#13);
sl.Text:=p.Get;
ok:=false;
for i:=0 to sl.Count-1 do
begin
s:=Trim(sl[i]);
if (s<>'') and (s[1]<>'^') and (Copy(s,1,3)<>'AT+') then
if s='>' then ok:=true else
raise Exception.Create('S[ERROR]'+s);
end;
if not ok then raise Exception.Create('S[NO RESPONSE]');
p.Send(Msg+#26);
tc:=GetTickCount;
ok:=false;
while not(ok) and (cardinal(GetTickCount-tc)<30000) do
begin
sl.Text:=p.Get;
for i:=0 to sl.Count-1 do
begin
s:=Trim(sl[i]);
if (s<>'') and (s[1]<>'^') then
if (Copy(s,1,6)='+CMGS:') then ok:=true
else if s='OK' then //ok:=true
else raise Exception.Create('M[ERROR]'+s);
end;
end;
if not ok then raise Exception.Create('[TIMED OUT]');
finally
p.Free;
sl.Free;
end;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment