📄 mainp.~pas
字号:
unit mainp;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, nrterminal, nrclasses, nrcomm;
type
pbyte=^byte;
TForm1 = class(TForm)
comm: TnrComm;
nrTerminal1: TnrTerminal;
Button1: TButton;
Label1: TLabel;
phonenum: TEdit;
Memo1: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function StrtoPDU(CentreNO,RcvNO: string): string;
function InCodePDUSMS(Mess: WideString): String;
function numtopdu(no:string):string;
var
Form1: TForm1;
implementation
{$R *.dfm}
function numtopdu(no:string):string;
var
i:integer;
s:string;
o:string;
begin
s:=no;
o:='';
for I := 1 to (length(s) div 2) do
begin
o:=o+s[i*2]+s[i*2-1];
end;
result:='0011000D9168'+o+'000801';
end;
function StrtoPDU(CentreNO,RcvNO: string): string;
var
StrTemp,PDURcvNo:String;
i,m,StrLen:integer;
begin
StrTemp:='';
PDURcvNo:='';
//解析短消息中中心号码
StrLen:=Length(CentreNO);
if (StrLen mod 2)<>0 then //如果是奇数
begin
CentreNO:=CentreNO+'F';
StrLen:=StrLen+1;
end;
i:=1;
while i<=Strlen do
begin
StrTemp:=StrTemp+CentreNO[i+1]+CentreNO[i]; // 交换奇数位和偶数位
inc(i,2);
end;
StrTemp:='91'+StrTemp; //将短信息中心号码前面加上字符91,91是国际化的意思
StrLen:=Length(StrTemp);
m:=(StrLen div 2);
StrTemp:=inttohex(m,2)+StrTemp;
//解析完短消息中中心号码
//解析收信人号码
if copy(RcvNo,1,2)='13' then RcvNo:='86'+RcvNo; //手机号前面加13
if copy(RcvNo,1,1)='0' then RcvNo:='106'+RcvNo; //小灵通前面加106
if copy(RcvNo,1,2)='86' then StrTemp := StrTemp + '11000D91'; //手机91, 0D表示电话长度(13位),用16进制表示。
if copy(RcvNo,1,3)='106' then
begin
if Length(RcvNo)=15 then StrTemp := StrTemp + '11000F81'; //小灵通81, 0F表示电话长度(15位),用16进制表示。
if Length(RcvNo)=14 then StrTemp := StrTemp + '11000E81';
end;
StrLen:=Length(RcvNO);
if (StrLen mod 2)<>0 then //如果是奇数
begin
RcvNO:=RcvNO+'F';
StrLen:=StrLen+1;
end;
i:=1;
while i<=Strlen do
begin
PDURcvNo:=PDURcvNo+RcvNO[i+1]+RcvNO[i]; // 交换奇数位和偶数位
inc(i,2);
end;
PDURcvNo:=PDURcvNO+'0008A7';
Result:=StrTemp+PDURcvNo;
end;
function InCodePDUSMS(Mess: WideString): String;
var
sLen,cur,i:integer;
strTmp:string;
begin
result := '';
sLen := length(Mess);
i := 1;
while i <= sLen do
begin
cur := ord(Mess[i]); //先返回序数值
FmtStr(strTmp,'%4.4X',[cur]); //格式化序数值(BCD转换)
result := result + strTmp;
inc(i);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
pdumess:string;
messlen:string;
tpdu:string;
numcode:string;
begin
PDUMess:=InCodePDUSMS(WideString(Memo1.Text));
MessLen:=InttoHex(length(PDUMess) div 2,2) ;
if length(phonenum.Text)<>11 then
begin
showmessage('手机号码错误!');
end;
numcode:=numtopdu(phonenum.Text+'F');
TPDU:=numcode+MessLen+PDUMess;
//TPDU:=MessLen+PDUMess;
try
comm.Active:=true;
except
showmessage('打开端口错误');
exit;
end;
{用PDU方式发送短信不用AT指令设置短消息中心号码也可以发送成功,设置短信中心,返加Error,也能发送成功}
// comm.SendString('AT+CSCA="008613800290500"'+char(13)); //设置短信中心号码
// sleep(300);
comm.SendString('AT+CMGF=0'+char(13)); //设置为PDU模式
sleep(600);
// comm.SendString('AT+CSCS="UCS2"'+char(13));
//设置短消息中心时应使用PDU的UCS2编码方式,WaveCom是HEX,否则ERROR!
// sleep(600);
// comm.SendString('AT'+char(13)); //用#13#10,对WaveCom不行。
//sleep(300);
comm.SendString('AT+CMGS=0'+inttostr(length(PDUMess) div 2+15)+char(13));
sleep(600);
comm.SendString(TPDU+char(26));
sleep(600);
// comm.Active:=false;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
comm.Active:=true;
comm.SendString('ATZ'+char(13));
sleep(600);
// comm.SendString('AT+CSCA="+8613800290500"'+char(13));
// sleep(600);
//comm.SendString('AT+CSAS'+char(13));
//sleep(600);
comm.SendString('ATZ'+char(13));
sleep(600);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm.Active:=false;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -