请贴一段简单的发送短信的例子代码.
(注:请发广告贴的不要跟,不要影响大家阅读我的贴子)非常感谢.

解决方案 »

  1.   

    我用at先做个简单的测试为何也不行:
    procedure Tsmsfrm.Button1Click(Sender: TObject);
    var
       sat:string;
    begin
       try
          Comm1.BaudRate:=9600;
          Comm1.ByteSize:=8;
          Comm1.ParityCheck:=1;
          Comm1.CommName:='com1';
          Comm1.startcomm;
       except
          messagedlg('打开串口时失败,可能该串口已被占用或不存在,请换另一个串口!',mterror,[mbyes],0);
       end;sat:='at'+Char(13)+Char(10);
    if Comm1.WriteCommData(Pchar(sat),Length(sat)) then
          showmessage('发送指令成功')
    else
         messagedlg('发送指令失败!',mterror,[mbyes],0);
    end;错误提示是:发送指令失败.
    我用的是spcomm控件,请问应该如何写发送最简单的at这个指令的程序.
    请高手看一下,我的这个发送程序错在哪了,谢谢.
      

  2.   

    我用at先做个简单的测试为何也不行:
    procedure Tsmsfrm.Button1Click(Sender: TObject);
    var
       sat:string;
    begin
       try
          Comm1.BaudRate:=9600;
          Comm1.ByteSize:=8;
          Comm1.ParityCheck:=1;
          Comm1.CommName:='com1';
          Comm1.startcomm;
       except
          messagedlg('打开串口时失败,可能该串口已被占用或不存在,请换另一个串口!',mterror,[mbyes],0);
       end;sat:='at'+Char(13)+Char(10);
    if Comm1.WriteCommData(Pchar(sat),Length(sat)) then
          showmessage('发送指令成功')
    else
         messagedlg('发送指令失败!',mterror,[mbyes],0);
    end;错误提示是:发送指令失败.
    我用的是spcomm控件,请问应该如何写发送最简单的at这个指令的程序.
    请高手看一下,我的这个发送程序错在哪了,谢谢.
      

  3.   

    procedure TForm1.Sendinfo(StrPhone:String;StrInfo:string);//发送短信息
    Var
      r,s,s2,s3,s4,s5:string;
      cmdlong,tmp:integer;
      //msgs:WideString;
      Stmp:string;
      strcmdlong:string;
    begin
      // SysDelay(5000);     ///延时
       try
          //s:='0031000D9168';
          s:='0891683108200405F011000D9168';
          //PDU编码属性,这种方法是不需要设置短信中心号码的,因为现的手机SIM卡已经写好了
          s2:=SEncodeMobNO(StrPhone);//对手机号码进行PDU编码
          //s3:='0008A7';
          s3:='000800';
          s4:='';
          s5:=EnCodeChinese(StrInfo);
          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);     ///延时
           Comm1.WriteCommData(pchar(r),length(r));//发送短信。
           SysDelay(10);     ///延时
           send_ok:=1;
           Ssend_ok:='发送成功';
          // Application.MessageBox('发送成功!','提示',mb_ok+mb_iconinformation);
        except
           send_ok:=0;
           Ssend_ok:='发送失败';
          // Application.MessageBox('发送失败!','提示',mb_ok+mb_iconinformation);
        end;
    end;Sendinfo('13889825689','dddd');///发送信息
      

  4.   

    多谢楼上朋友。
    请再将SEncodeMobNO以及EnCodeChinese两个函数的代码贴出,
    不胜感激。
      

  5.   

    function TForm1.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 TForm1.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;