⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 u_sms.~pas

📁 串口发短信的例子
💻 ~PAS
字号:
unit U_sms;

interface
 uses SysUtils,StrUtils,Classes,Inifiles;

function  Mobile_change(Mobile:string):string ;
function  SMS_head_change(csca_mobile,Mobile:string):string ;
function  SMS_body_change(sms_content:string):string ;
function  GBToUcs2(s:string):string ;
function  Read_inifile(filename,readstring_first,readstring_second:string;scount:integer):TStringList;
function  save_inifile(filename,readstring_first,readstring_second,setup_str:string):boolean;
function  inport_no(txt_path,hm_type:string;var in_text:TStringList):boolean;
function  send_msg_1(csca_mobile_tem,mobile_tem,msg_tem:string;var msg_usc2:string):integer;


implementation


function Mobile_change(Mobile:string):string ;
var
    m_len,i,j:integer;
    output_mobile:string;
begin
  m_len:=length(Mobile);
  if  m_len mod 2<>0 then
   begin
    Mobile:=Mobile+'F';
    m_len:=m_len+1
   end;

  for i:=1 to m_len do
 begin
  if i mod 2=1 then j:=i+1 else j:=i-1;
  output_mobile:=output_mobile+Mobile[j]
 end;

 result:=output_mobile;
end;

function GBToUcs2(s:string):string ;
var
  i,len,cur:Integer;
  swidestring:widestring;
  t,Result_remp:string;
begin
  swidestring:=trim(s);   //转化为中文字符
  len:=Length(swidestring);
  i:=1;
 while i<=len do
  begin
  cur:=ord(swidestring[i]);
  FmtStr(t,'%4.4X',[cur]);
  Result_remp:=Result_remp+t;
  inc(i);
  end;
  Result:=Result_remp;
end;

function  SMS_head_change(csca_mobile,Mobile:string):string ;
  var  m_len:integer;
 begin
    Mobile:=Mobile_change(Mobile);
    m_len:=length(Mobile);

    if length(trim(csca_mobile))>13 then
    csca_mobile:=rightstr(csca_mobile,13); //除去+
    
     if (m_len=12)and  (leftstr(Mobile,2)='31')then
 begin
  Mobile:='9168'+Mobile;
  m_len:=m_len+2;
 end
 else
  Mobile:='81'+Mobile;

  if  AnsiContainsStr(Mobile,'F') then
   m_len:=m_len-1;
  
 if  length(trim(csca_mobile))<>13  then
  result:='001100'+format('%2.2X',[m_len])+Mobile
 else
  result:='0891'+Mobile_change(csca_mobile)+'1100'+format('%2.2X',[m_len])+Mobile;

  end;

    function  SMS_body_change(sms_content:string):string ;
  var content:string;    // ,AToText
      //i,pos_int,len_int:integer;
  begin
  content:=GBToUcs2(sms_content);
  result:='000800'+inttohex((length(content) div 2),2)+content;
  end;


  function  Read_inifile(filename,readstring_first,readstring_second:string;scount:integer):TStringList;
var
  SysIni:TInifile;
  all_list:TStringList ;
  i:integer;
begin
 all_list:=TStringList.Create;
 SysIni := TIniFile.Create(filename);
 for i:=0 to scount do
 begin
 all_list.Add(SysIni.ReadString(readstring_first,readstring_second+inttostr(i),''));
 end;
 SysIni.Free;
 result:= all_list;
end;

 function  save_inifile(filename,readstring_first,readstring_second,setup_str:string):boolean;
var
  SysIni:TInifile;
begin
 SysIni:=TIniFile.Create(filename);
      try
        SysIni.WriteString(readstring_first,readstring_second,setup_str);
      except
      
      end;
  SysIni.Free;
  result:=true;
 end;

 function  inport_no(txt_path,hm_type:string;var in_text:TStringList):boolean;
 var
    F:TextFile;
    s:string;
    s1:widestring;
 begin
  If FileExists(txt_path) then
    begin
    AssignFile(F,txt_path);
    Reset(F);
    while not Eof(F) do
      begin
        Readln(F,s);
        s:=trim(s);
        s1:=s;
        if hm_type='0' then //移动
         begin
          if  (Trim(s)<>'') then in_text.Add(Trim(s));
         end
        else if hm_type='1' then  //联通
         begin
          if (Trim(s)<>'') and (length(s1)=length(s)) and ((leftstr(s,3))>'129')and((leftstr(s,3))<'134') then in_text.Add(Trim(s));
         end
        else   if hm_type='2' then //移动
         begin
          if  (Trim(s)<>'') and (length(s1)=length(s)) and ((leftstr(s,3))>'133')and((leftstr(s,3))<'160') then in_text.Add(Trim(s));
         end

      end;
    CloseFile(F);
    result:=true;
    end
    else
    result:=false;


 end;


function send_msg_1(csca_mobile_tem,mobile_tem,msg_tem:string;var msg_usc2:string):integer;
var
  dest_temp:string;
  len_send:integer;
begin
  len_send:=0;
  msg_usc2:='';
  
  if   pos('###',msg_tem)>0 then
  begin
    // dest_temp:=push_head_change(csca_mobile_tem,mobile_tem);
    // msg_tem:=push_body_change(msg_tem);
  end
  else
  begin
     dest_temp:=SMS_head_change(csca_mobile_tem,mobile_tem);
     msg_tem:=SMS_body_change(msg_tem);
  end;
  
 // 长度是除去SMSC段(0891683108100005F0),从"11"开始算,除以2即得。
  if  leftstr(dest_temp,2)='00' then
  len_send:=(length(dest_temp+msg_tem) div 2)-1
  else if  leftstr(dest_temp,4)='0891' then
  len_send:=(length(dest_temp+msg_tem) div 2)-9;

   msg_usc2:=dest_temp+msg_tem;
   result:=len_send;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -