procedure TForm1.FormShow(Sender: TObject);
begin
Comm1.StartComm;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.StopComm;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.Text:='';
memo2.Text:='';
end;procedure TForm1.Button2Click(Sender: TObject);
begin
comm1.CommName:=combobox1.text;
if combobox2.text='' then
showmessage('请设置波特率')
else
comm1.BaudRate:=strtoint(combobox2.text);
comm1.ByteSize:=_8; //TByteSize
comm1.ParityCheck:=True;
comm1.Parity:=Even; //TParity = ( None, Odd, Even, Mark, Space )
comm1.StopBits:=_1; //TStopBits = ( _1, _1_5, _2 )
try
Comm1.StartComm;
except
raise Exception.Create( '打开串口错误 ');
end; end;
function EncodeChinese(Input: WideString): string;
var
i: Integer;
begin
Result := ' ';
for i := 1 to Length(Input) do
Result := Result + Format( '%4.4X ', [ord(Input[i])]);end;
function SEncodeMobNO(SmobNO: string): string;
//要想发送中文短信必须使用Modem的PDU方式。这个函数是将手机号码进行PDU编码。
var
TempPchar: Pchar;
i: integer;
Str: string;
begin
if (copy(smobno, 1, 1) = '+ ') then //判断是否包含国家编码
SmobNO := copy(smobno, 2, length(smobno) - 1); //去掉手机号码中的’+’ if ((length(SmobNO) mod 2) = 1) then
SmobNO := SmobNO + 'F '; TempPchar := Pchar(SmobNO); //将字符串 Char数组化 i := 0;
Str := ' ';
while i < length(TempPchar) do begin
Str := Str + TempPchar[i + 1] + TempPchar[i];
i := i + 2;
end;
result := Str;end;
procedure SysDelay(aMs: Longint); //延迟
var
TickCount : LongInt;
begin
TickCount:=GetTickCount;
while GetTickCount - TickCount < aMs do Application.ProcessMessages;
end;procedure TForm1.Button1Click(Sender: TObject);
Var
r,s,s2,s3,s4,s5:string;
cmdlong,tmp:integer;
//msgs:WideString;
Stmp:string;
strcmdlong:string;
begin
if (edit2.Text = ' ') then
begin
Application.MessageBox( '手机号码不能为空,请填写! ', '提示 ',mb_ok+mb_iconinformation);
Exit;
end;
if (length(edit2.Text) <=10) or (length(edit2.Text)>=12) then
begin
Application.MessageBox( '手机号码位数不正确,请填写! ', '提示 ',mb_ok+mb_iconinformation);
Exit;
end;
if length(trim(Memo1.Text)) <=0 then
begin
Application.MessageBox( '信息不能为空,请填写! ', '提示 ',mb_ok+mb_iconinformation);
Exit;
end;
try
s:= '0891685112128822F811000D9168 ';
//PDU编码属性,这种方法是不需要设置短信中心号码的,因为现的手机SIM卡已经写好了
s2:=SEncodeMobNO(edit2.Text);//对手机号码进行PDU编码
s3:= '000800 ';
s4:= ' ';
s5:=EnCodeChinese(Memo1.Text);
tmp:=length(s5)div 2;
s4:=format( '%X ',[tmp]);
if length(s4) <2 then
s4:= '0 '+s4;
//计算PDU编码长度
r:=s+s2+s3+s4+s5+^Z;
strcmdlong:= '0031000D9168 '+s2+s3+s4+s5+^Z;
cmdlong:=(length(strcmdlong)-2) div 2;
Stmp:= 'AT+CMGF=0 '#13;//设置Modem为PDU模式
Comm1.WriteCommData(pchar(Stmp),length(Stmp));
SysDelay(7); ///延时
Stmp:= 'AT+CMGS= '+inttostr(cmdlong)+#13;//设置信息长度,这里应为PDU编码长度的1/2.
Comm1.WriteCommData(pchar(Stmp),length(Stmp));
SysDelay(7); ///延时
// r:= '0891683108200405F011000B81 '+s2+ '0000A70BE8329BFD06DDDF723619^Z ';
Comm1.WriteCommData(pchar(r),length(r));//发送短信。
SysDelay(9); ///延时 Application.MessageBox( '发送成功! ', '提示 ',mb_ok+mb_iconinformation);
except
Application.MessageBox( '发送失败! ', '提示 ',mb_ok+mb_iconinformation);
end; end;end.
begin
Comm1.StartComm;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.StopComm;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.Text:='';
memo2.Text:='';
end;procedure TForm1.Button2Click(Sender: TObject);
begin
comm1.CommName:=combobox1.text;
if combobox2.text='' then
showmessage('请设置波特率')
else
comm1.BaudRate:=strtoint(combobox2.text);
comm1.ByteSize:=_8; //TByteSize
comm1.ParityCheck:=True;
comm1.Parity:=Even; //TParity = ( None, Odd, Even, Mark, Space )
comm1.StopBits:=_1; //TStopBits = ( _1, _1_5, _2 )
try
Comm1.StartComm;
except
raise Exception.Create( '打开串口错误 ');
end; end;
function EncodeChinese(Input: WideString): string;
var
i: Integer;
begin
Result := ' ';
for i := 1 to Length(Input) do
Result := Result + Format( '%4.4X ', [ord(Input[i])]);end;
function SEncodeMobNO(SmobNO: string): string;
//要想发送中文短信必须使用Modem的PDU方式。这个函数是将手机号码进行PDU编码。
var
TempPchar: Pchar;
i: integer;
Str: string;
begin
if (copy(smobno, 1, 1) = '+ ') then //判断是否包含国家编码
SmobNO := copy(smobno, 2, length(smobno) - 1); //去掉手机号码中的’+’ if ((length(SmobNO) mod 2) = 1) then
SmobNO := SmobNO + 'F '; TempPchar := Pchar(SmobNO); //将字符串 Char数组化 i := 0;
Str := ' ';
while i < length(TempPchar) do begin
Str := Str + TempPchar[i + 1] + TempPchar[i];
i := i + 2;
end;
result := Str;end;
procedure SysDelay(aMs: Longint); //延迟
var
TickCount : LongInt;
begin
TickCount:=GetTickCount;
while GetTickCount - TickCount < aMs do Application.ProcessMessages;
end;procedure TForm1.Button1Click(Sender: TObject);
Var
r,s,s2,s3,s4,s5:string;
cmdlong,tmp:integer;
//msgs:WideString;
Stmp:string;
strcmdlong:string;
begin
if (edit2.Text = ' ') then
begin
Application.MessageBox( '手机号码不能为空,请填写! ', '提示 ',mb_ok+mb_iconinformation);
Exit;
end;
if (length(edit2.Text) <=10) or (length(edit2.Text)>=12) then
begin
Application.MessageBox( '手机号码位数不正确,请填写! ', '提示 ',mb_ok+mb_iconinformation);
Exit;
end;
if length(trim(Memo1.Text)) <=0 then
begin
Application.MessageBox( '信息不能为空,请填写! ', '提示 ',mb_ok+mb_iconinformation);
Exit;
end;
try
s:= '0891685112128822F811000D9168 ';
//PDU编码属性,这种方法是不需要设置短信中心号码的,因为现的手机SIM卡已经写好了
s2:=SEncodeMobNO(edit2.Text);//对手机号码进行PDU编码
s3:= '000800 ';
s4:= ' ';
s5:=EnCodeChinese(Memo1.Text);
tmp:=length(s5)div 2;
s4:=format( '%X ',[tmp]);
if length(s4) <2 then
s4:= '0 '+s4;
//计算PDU编码长度
r:=s+s2+s3+s4+s5+^Z;
strcmdlong:= '0031000D9168 '+s2+s3+s4+s5+^Z;
cmdlong:=(length(strcmdlong)-2) div 2;
Stmp:= 'AT+CMGF=0 '#13;//设置Modem为PDU模式
Comm1.WriteCommData(pchar(Stmp),length(Stmp));
SysDelay(7); ///延时
Stmp:= 'AT+CMGS= '+inttostr(cmdlong)+#13;//设置信息长度,这里应为PDU编码长度的1/2.
Comm1.WriteCommData(pchar(Stmp),length(Stmp));
SysDelay(7); ///延时
// r:= '0891683108200405F011000B81 '+s2+ '0000A70BE8329BFD06DDDF723619^Z ';
Comm1.WriteCommData(pchar(r),length(r));//发送短信。
SysDelay(9); ///延时 Application.MessageBox( '发送成功! ', '提示 ',mb_ok+mb_iconinformation);
except
Application.MessageBox( '发送失败! ', '提示 ',mb_ok+mb_iconinformation);
end; end;end.
帧数少吗