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

📄 fc_gsm.pas

📁 GSM MODEM的收发短信息
💻 PAS
字号:
unit fc_gsm;

interface
uses SysUtils, dialogs, Classes, Math;

function ConverInfoCenter(CenterNum: widestring): widestring;
function ConverMobileNum(MobileNum: widestring; Flag: integer): widestring;
function ContentToUnicode(ContentStr: widestring): widestring;
function codeaddcode(MobileNum, ContentStr: widestring): widestring;
function inttoBin(i: integer): widestring;
function EncodeChinese(Input: widestring): widestring;
function DecodeChinese(SRC: widestring): widestring;
function InsertLogfile(log_str: widestring): Boolean;
function SplitString(Source, Deli: string): TStringList;
function SpString(SRC, Dl: string): TStringList;
function DecodeInfo(SRC: string): string;
function DecodeDate(SRC: string): string;
function HexToDec(HexData: Integer): Integer;
implementation
//

function ConverInfoCenter(CenterNum: widestring): widestring;
var
  j: integer;
  temp: widestring;
begin
  CenterNum := '91' + ConverMobileNum(CenterNum, 1);
  j := length(CenterNum) div 2;
  if j < 10 then
    CenterNum := '0' + inttostr(j) + CenterNum
  else
    CenterNum := inttostr(j) + CenterNum;
  result := CenterNum;
end;

function ConverMobileNum(MobileNum: widestring; Flag: integer): widestring;
var
  temp: widestring;
  i: integer;
begin
  if ((length(MobileNum) mod 2) = 1) then
    MobileNum := MobileNum + 'F';
  if flag = 1 then
    MobileNum := '86' + MobileNum;
  temp := '';
  i := 0;
  while (i < length(mobileNum)) do
  begin
    i := i + 2;
    temp := temp + copy(MobileNum, i, 1);
    temp := temp + copy(MobileNum, i - 1, 1);
  end;
  result := temp;
end;

function ContentToUnicode(ContentStr: widestring): widestring;
var
  i: integer;
  s4: widestring;
begin
  s4 := '';
  ContentStr := EncodeChinese(ContentStr);
  i := length(ContentStr) div 2;
  s4 := Format('%X', [i]);
  if length(s4) < 2 then
    s4 := '0' + s4;
  result := s4 + ContentStr;
end;

function codeaddcode(MobileNum, ContentStr: widestring): widestring;
var
  str: widestring;
  ml: string;
  molen: integer;
begin
  str := '1100';
  if (copy(MobileNum, length(MobileNum) - 1, 1) <> 'F') then
    molen := length(MobileNum)
  else
    molen := length(MobileNum) - 1;

  ml := Format('%X', [molen]);
  if length(ml) < 2 then ml := '0' + ml;
  str := str + ml;
  if (copy(MobileNum, 1, 2) <> '68') then str := str + '81' else str := str + '91';
  result := str + MobileNum + '000800' + ContentStr;
  //result := '11000D91' + MobileNum + '000800' + ContentStr;
end;

function inttoBin(i: integer): widestring;
begin
  while i <> 0 do
  begin //i mod 2取模,再使用format格式化
    result := Format('%d' + result, [i mod 2]);
    i := i div 2
  end
end;

function EncodeChinese(Input: widestring): widestring; //将信息内容进行PDU编码
var
  i, len: integer;
  cur: integer;
  t: string;
  x: widestring;
begin
  result := '';
  len := length(Input);
  i := 1;
  while i <= len do
  begin
    cur := ord(Input[i]);
    FmtStr(t, '%4.4X', [cur]); //BCD转换
    x := x + t;
    result := x;
    inc(i);
  end;
end;

function DecodeChinese(SRC: widestring): widestring; //中文解码
var i: integer;
  S: widestring;
  D: WideChar;
  ResultW: widestring;
begin
  for i := 1 to Round(length(SRC) / 4) do begin
    S := copy(SRC, (i - 1) * 4 + 1, 4);
    D := WideChar(StrToInt('$' + S)); //此处是重点,用delphi提供的widechar可以转换
    ResultW := ResultW + D;
  end;
  result := ResultW;
end;

function InsertLogfile(log_str: widestring): Boolean;
var
  F: TextFile;
  MyStr: widestring;
  sss: widestring;
  FileHandle: integer;
begin
  result := True;
  try
    sss := 'C:\' + FormatDateTime('yyyy-mm-dd', Now) + '.txt';
    if not FileExists(sss) then begin
      FileHandle := FileCreate(sss);
      FileClose(FileHandle);
    end;
    AssignFile(F, sss);
    Append(F);
    //if log_str = ' ' then MyStr := '' else
    MyStr := FormatDateTime('yyyy-mm-dd hh:mm:ss', Now) + chr(9) + log_str;
    Writeln(F, MyStr);
    Flush(F);
    CloseFile(F);
  except
    result := FALSE;
  end;
end;

function SplitString(Source, Deli: string): TStringList;
var
  EndOfCurrentString: Byte;
  StringList: TStringList;
  xx: string;
begin
  StringList := TStringList.Create;
  Result := StringList;
  if length(Source) < 20 then exit;
  while Pos(Deli, Source) > 0 do
  begin
    EndOfCurrentString := Pos(Deli, Source);
    xx := copy(Source, 1, EndOfCurrentString - 1);
    if length(xx) > 20 then
      StringList.add(copy(Source, 1, EndOfCurrentString - 1));
    Source := copy(Source, EndOfCurrentString + Length(Deli), Length(Source) - EndOfCurrentString);
  end;
  StringList.add(Source);
end;

function SpString(SRC, Dl: string): TStringList;
var
  x: TStringList;
  q: string;
  i: integer;
begin
  x := TStringList.Create;
  while pos(Dl, SRC) > 0 do
  begin
    i := pos(Dl, SRC);
    q := copy(SRC, 1, i - 1);
    x.Add(q);
    SRC := copy(SRC, i + length(Dl), length(SRC) - (i + 1));
  end;
  x.Add(SRC);
  Result := x;
end;

function DecodeInfo(SRC: string): string;
var
  Centerlen, Centerlen2: integer;
  stt, stt2, stt3, stt4: string;
begin
  Centerlen := (strtoint(copy(SRC, 1, 2)) * 2) + 2 + 2;
  stt := copy(SRC, Centerlen + 1, length(SRC) - Centerlen);
  Centerlen := StrToint('$' + copy(stt, 1, 2));
  if Centerlen mod 2 = 1 then Centerlen := Centerlen + 1;
  stt2 := ConverMobileNum(copy(stt, 5, Centerlen), 0);
  stt3 := DecodeDate(ConverMobileNum(copy(stt, 5 + length(stt2) + 4, 10), 0));
  Centerlen2 := (StrToint('$' + copy(stt, 23 + Centerlen, 2))) * 2;
  stt4 := DecodeChinese(copy(stt, 25 + Centerlen, Centerlen2));
  if (copy(stt2, 1, 2) = '13') or (copy(stt2, 1, 2) = '15') then stt2 := copy(stt2, 1, 11);
  result := stt2 + '|_|' + stt3 + '|_|' + stt4;
end;

function DecodeDate(SRC: string): string;
var
  temp: string;
begin
  temp := '20' + copy(SRC, 1, 2) + '-' + copy(SRC, 3, 2) + '-' + copy(SRC, 5, 2) + ' ' + copy(SRC, 7, 2) + ':' + copy(SRC, 9, 2);
  result := temp;
end;

function HexToDec(HexData: Integer): Integer;
var m: Integer;
begin
        //showmessage(inttostr(hexdata));
  Result := 0;
  m := Trunc(Power(10, Length(IntToStr(HexData)) - 1));
  while m > 0 do
  begin
    Result := (Result * 16 + HexData div m);
    HexData := HexData mod m;
    m := m div 10;
  end;
end;

end.

⌨️ 快捷键说明

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