www.pudn.com > at.rar > ShortSMSActiveImpl1.pas


unit ShortSMSActiveImpl1; 
 
{$WARN SYMBOL_PLATFORM OFF} 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
  ActiveX, AxCtrls, ShortSMS_TLB, StdVcl, StdCtrls, SPComm, StrUtils, IniFiles, 
  ExtCtrls; 
 
type 
  TShortSMSActiveX = class(TActiveForm, IShortSMSActiveX) 
    Comm1: TComm; 
    Image1: TImage; 
    procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer; 
      BufferLength: Word); 
    procedure ActiveFormCreate(Sender: TObject); 
  private 
    { Private declarations } 
    FEvents: IShortSMSActiveXEvents; 
    procedure ActivateEvent(Sender: TObject); 
    procedure ClickEvent(Sender: TObject); 
    procedure CreateEvent(Sender: TObject); 
    procedure DblClickEvent(Sender: TObject); 
    procedure DeactivateEvent(Sender: TObject); 
    procedure DestroyEvent(Sender: TObject); 
    procedure KeyPressEvent(Sender: TObject; var Key: Char); 
    procedure PaintEvent(Sender: TObject); 
 
    procedure DoEvents();//实现与VB的DoEvents功能 
    procedure FindCMTI();//查找新信息的索引号 
//    function  GetSysDir():string;  //返回系统目录 
//    function  GetSysReg(): Boolean; //得到系统是否已注册 
  protected 
    //变量 
    ReceiveData: string;                   //从模块中收到的数据 
    CMTIIndex  : string;                   //新信息的索引,三位为一组 
    IfExecCmd  : WordBool;                 //模块是否执行了AT指令 
 
    IMEINO     : string;                   //模块生产序列号 
    IfReg      : Bool;                     //是否已注册 
    UseDays    : Byte;                     //使用天数 
    UseTimes   : Word;                     //使用次数 
 
    { Protected declarations } 
 
    procedure DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage); override; 
    procedure EventSinkChanged(const EventSink: IUnknown); override; 
    function Get_Active: WordBool; safecall; 
    function Get_AlignDisabled: WordBool; safecall; 
    function Get_AutoScroll: WordBool; safecall; 
    function Get_AutoSize: WordBool; safecall; 
    function Get_AxBorderStyle: TxActiveFormBorderStyle; safecall; 
    function Get_Caption: WideString; safecall; 
    function Get_Color: OLE_COLOR; safecall; 
    function Get_DoubleBuffered: WordBool; safecall; 
    function Get_DropTarget: WordBool; safecall; 
    function Get_Enabled: WordBool; safecall; 
    function Get_Font: IFontDisp; safecall; 
    function Get_HelpFile: WideString; safecall; 
    function Get_KeyPreview: WordBool; safecall; 
    function Get_PixelsPerInch: Integer; safecall; 
    function Get_PrintScale: TxPrintScale; safecall; 
    function Get_Scaled: WordBool; safecall; 
    function Get_ScreenSnap: WordBool; safecall; 
    function Get_SnapBuffer: Integer; safecall; 
    function Get_Visible: WordBool; safecall; 
    function Get_VisibleDockClientCount: Integer; safecall; 
    procedure _Set_Font(var Value: IFontDisp); safecall; 
    procedure AboutBox; safecall; 
    procedure Set_AutoScroll(Value: WordBool); safecall; 
    procedure Set_AutoSize(Value: WordBool); safecall; 
    procedure Set_AxBorderStyle(Value: TxActiveFormBorderStyle); safecall; 
    procedure Set_Caption(const Value: WideString); safecall; 
    procedure Set_Color(Value: OLE_COLOR); safecall; 
    procedure Set_DoubleBuffered(Value: WordBool); safecall; 
    procedure Set_DropTarget(Value: WordBool); safecall; 
    procedure Set_Enabled(Value: WordBool); safecall; 
    procedure Set_Font(const Value: IFontDisp); safecall; 
    procedure Set_HelpFile(const Value: WideString); safecall; 
    procedure Set_KeyPreview(Value: WordBool); safecall; 
    procedure Set_PixelsPerInch(Value: Integer); safecall; 
    procedure Set_PrintScale(Value: TxPrintScale); safecall; 
    procedure Set_Scaled(Value: WordBool); safecall; 
    procedure Set_ScreenSnap(Value: WordBool); safecall; 
    procedure Set_SnapBuffer(Value: Integer); safecall; 
    procedure Set_Visible(Value: WordBool); safecall; 
    function OpenComm(CommNum: Word; BaudRate: Integer; Parity: TSMSParity; 
      DataBit: TSMSDataBit; StopBit: TSMSStopBit): Smallint; safecall; 
    function CloseComm: Smallint; safecall; 
    function GetModemCSQ: Smallint; safecall; 
    function GetModemCREG: Smallint; safecall; 
    function GetModemCPIN: Smallint; safecall; 
    function SetModemCSCA(const Value: WideString): Smallint; safecall; 
    function SetModemCMGF(Value: TSMSCMGF): Smallint; safecall; 
    function SendInfo(const HandNum, Info: WideString): Smallint; safecall; 
    function ReadInfo(Index: Word; var HandNum, Info, 
      SendTime: WideString): Smallint; safecall; 
    function ReadNewInfo(var HandNum, Info, SendTime: WideString): Smallint; 
      safecall; 
    function DelInfo(Index: Word): Smallint; safecall; 
    function GetModemIMEINO: WideString; safecall; 
    function SetModemATZ: Smallint; safecall; 
    function SetModemCNMI: Smallint; safecall; 
  public 
    { Public declarations } 
    procedure Initialize; override; 
  end; 
 
implementation 
 
uses ComObj, ComServ, About1, PunFun;//, BWCRT; 
 
{$R *.DFM} 
 
{ TShortSMSActiveX } 
 
procedure TShortSMSActiveX.DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage); 
begin 
  { Define property pages here.  Property pages are defined by calling 
    DefinePropertyPage with the class id of the page.  For example, 
      DefinePropertyPage(Class_ShortSMSActiveXPage); } 
end; 
 
procedure TShortSMSActiveX.EventSinkChanged(const EventSink: IUnknown); 
begin 
  FEvents := EventSink as IShortSMSActiveXEvents; 
  inherited EventSinkChanged(EventSink); 
end; 
 
procedure TShortSMSActiveX.Initialize; 
begin 
  inherited Initialize; 
  OnActivate := ActivateEvent; 
  OnClick := ClickEvent; 
  OnCreate := CreateEvent; 
  OnDblClick := DblClickEvent; 
  OnDeactivate := DeactivateEvent; 
  OnDestroy := DestroyEvent; 
  OnKeyPress := KeyPressEvent; 
  OnPaint := PaintEvent; 
end; 
 
function TShortSMSActiveX.Get_Active: WordBool; 
begin 
  Result := Active; 
end; 
 
function TShortSMSActiveX.Get_AlignDisabled: WordBool; 
begin 
  Result := AlignDisabled; 
end; 
 
function TShortSMSActiveX.Get_AutoScroll: WordBool; 
begin 
  Result := AutoScroll; 
end; 
 
function TShortSMSActiveX.Get_AutoSize: WordBool; 
begin 
  Result := AutoSize; 
end; 
 
function TShortSMSActiveX.Get_AxBorderStyle: TxActiveFormBorderStyle; 
begin 
  Result := Ord(AxBorderStyle); 
end; 
 
function TShortSMSActiveX.Get_Caption: WideString; 
begin 
  Result := WideString(Caption); 
end; 
 
function TShortSMSActiveX.Get_Color: OLE_COLOR; 
begin 
  Result := OLE_COLOR(Color); 
end; 
 
function TShortSMSActiveX.Get_DoubleBuffered: WordBool; 
begin 
  Result := DoubleBuffered; 
end; 
 
function TShortSMSActiveX.Get_DropTarget: WordBool; 
begin 
  Result := DropTarget; 
end; 
 
function TShortSMSActiveX.Get_Enabled: WordBool; 
begin 
  Result := Enabled; 
end; 
 
function TShortSMSActiveX.Get_Font: IFontDisp; 
begin 
  GetOleFont(Font, Result); 
end; 
 
function TShortSMSActiveX.Get_HelpFile: WideString; 
begin 
  Result := WideString(HelpFile); 
end; 
 
function TShortSMSActiveX.Get_KeyPreview: WordBool; 
begin 
  Result := KeyPreview; 
end; 
 
function TShortSMSActiveX.Get_PixelsPerInch: Integer; 
begin 
  Result := PixelsPerInch; 
end; 
 
function TShortSMSActiveX.Get_PrintScale: TxPrintScale; 
begin 
  Result := Ord(PrintScale); 
end; 
 
function TShortSMSActiveX.Get_Scaled: WordBool; 
begin 
  Result := Scaled; 
end; 
 
function TShortSMSActiveX.Get_ScreenSnap: WordBool; 
begin 
  Result := ScreenSnap; 
end; 
 
function TShortSMSActiveX.Get_SnapBuffer: Integer; 
begin 
  Result := SnapBuffer; 
end; 
 
function TShortSMSActiveX.Get_Visible: WordBool; 
begin 
  Result := Visible; 
end; 
 
function TShortSMSActiveX.Get_VisibleDockClientCount: Integer; 
begin 
  Result := VisibleDockClientCount; 
end; 
 
procedure TShortSMSActiveX._Set_Font(var Value: IFontDisp); 
begin 
  SetOleFont(Font, Value); 
end; 
 
procedure TShortSMSActiveX.AboutBox; 
begin 
  ShowShortSMSActiveXAbout; 
end; 
 
procedure TShortSMSActiveX.ActivateEvent(Sender: TObject); 
begin 
  if FEvents <> nil then FEvents.OnActivate; 
end; 
 
procedure TShortSMSActiveX.ClickEvent(Sender: TObject); 
begin 
  if FEvents <> nil then FEvents.OnClick; 
end; 
 
procedure TShortSMSActiveX.CreateEvent(Sender: TObject); 
begin 
  if FEvents <> nil then FEvents.OnCreate; 
end; 
 
procedure TShortSMSActiveX.DblClickEvent(Sender: TObject); 
begin 
  if FEvents <> nil then FEvents.OnDblClick; 
end; 
 
procedure TShortSMSActiveX.DeactivateEvent(Sender: TObject); 
begin 
  if FEvents <> nil then FEvents.OnDeactivate; 
end; 
 
procedure TShortSMSActiveX.DestroyEvent(Sender: TObject); 
begin 
  if FEvents <> nil then FEvents.OnDestroy; 
end; 
 
procedure TShortSMSActiveX.KeyPressEvent(Sender: TObject; var Key: Char); 
var 
  TempKey: Smallint; 
begin 
  TempKey := Smallint(Key); 
  if FEvents <> nil then FEvents.OnKeyPress(TempKey); 
  Key := Char(TempKey); 
end; 
 
procedure TShortSMSActiveX.PaintEvent(Sender: TObject); 
begin 
  if FEvents <> nil then FEvents.OnPaint; 
end; 
 
procedure TShortSMSActiveX.Set_AutoScroll(Value: WordBool); 
begin 
  AutoScroll := Value; 
end; 
 
procedure TShortSMSActiveX.Set_AutoSize(Value: WordBool); 
begin 
  AutoSize := Value; 
end; 
 
procedure TShortSMSActiveX.Set_AxBorderStyle( 
  Value: TxActiveFormBorderStyle); 
begin 
  AxBorderStyle := TActiveFormBorderStyle(Value); 
end; 
 
procedure TShortSMSActiveX.Set_Caption(const Value: WideString); 
begin 
  Caption := TCaption(Value); 
end; 
 
procedure TShortSMSActiveX.Set_Color(Value: OLE_COLOR); 
begin 
  Color := TColor(Value); 
end; 
 
procedure TShortSMSActiveX.Set_DoubleBuffered(Value: WordBool); 
begin 
  DoubleBuffered := Value; 
end; 
 
procedure TShortSMSActiveX.Set_DropTarget(Value: WordBool); 
begin 
  DropTarget := Value; 
end; 
 
procedure TShortSMSActiveX.Set_Enabled(Value: WordBool); 
begin 
  Enabled := Value; 
end; 
 
procedure TShortSMSActiveX.Set_Font(const Value: IFontDisp); 
begin 
  SetOleFont(Font, Value); 
end; 
 
procedure TShortSMSActiveX.Set_HelpFile(const Value: WideString); 
begin 
  HelpFile := String(Value); 
end; 
 
procedure TShortSMSActiveX.Set_KeyPreview(Value: WordBool); 
begin 
  KeyPreview := Value; 
end; 
 
procedure TShortSMSActiveX.Set_PixelsPerInch(Value: Integer); 
begin 
  PixelsPerInch := Value; 
end; 
 
procedure TShortSMSActiveX.Set_PrintScale(Value: TxPrintScale); 
begin 
  PrintScale := TPrintScale(Value); 
end; 
 
procedure TShortSMSActiveX.Set_Scaled(Value: WordBool); 
begin 
  Scaled := Value; 
end; 
 
procedure TShortSMSActiveX.Set_ScreenSnap(Value: WordBool); 
begin 
  ScreenSnap := Value; 
end; 
 
procedure TShortSMSActiveX.Set_SnapBuffer(Value: Integer); 
begin 
  SnapBuffer := Value; 
end; 
 
procedure TShortSMSActiveX.Set_Visible(Value: WordBool); 
begin 
  Visible := Value; 
end; 
 
///////////////////////////////////////////////////////////// 
//说  明:此过程是用来判断是否已来到新短信息 
//函数名:FindCMTI 
//参  数:无 
//返回值:无 
//作  者:卞伟    2003年6月 
///////////////////////////////////////////////////////////// 
procedure TShortSMSActiveX.FindCMTI(); 
var 
  Temp: string; 
  CMTINum1,CMTINum2: Integer; 
  NumTemp: Integer; 
  Temp1: string; 
//  CMTITemp: Integer; 
begin 
  //判断是否已来到新短信息 
  while True do 
  begin 
    CMTINum1:= Pos(#13 + #10 + '+CMTI: "',ReceiveData); 
    CMTINum2:= Pos('+CMTI: "',ReceiveData); 
    if CMTINum1 > 0 then 
    begin 
      Temp:= RightBStr(ReceiveData,Length(ReceiveData) - CMTINum1 - 13); 
      NumTemp:= Pos(#13 + #10,Temp); 
      if (NumTemp > 0) and (NumTemp < 5) then 
      begin 
        CMTIIndex:= CMTIIndex + RightBStr('000' + LeftBStr(Temp,NumTemp -1),3); 
 
        Delete(ReceiveData,CMTINum1,15 + NumTemp); 
      end 
      else 
      begin 
        Temp1:= Trim(LeftBStr(Temp,2)); 
        if Length(Temp1) > 0 then 
        begin 
          try 
            CMTIIndex:= CMTIIndex + RightBStr('000' + IntToStr(StrToInt(Temp1)),3); 
            Delete(ReceiveData,CMTINum1,16); 
          except 
            try 
              CMTIIndex:= CMTIIndex + RightBStr('000' + IntToStr(StrToInt(LeftBStr(Temp1,1))),3); 
              Delete(ReceiveData,CMTINum1,15); 
            except 
              //CMGLAll:= CMGLAll + 1; 
            end; 
          end; 
        end 
        else 
          Break; 
      end; 
    end 
    else if CMTINum2 > 0 then 
    begin 
      Temp:= RightBStr(ReceiveData,Length(ReceiveData) - CMTINum2 - 11); 
      NumTemp:= Pos(#13 + #10,Temp); 
      if (NumTemp > 0) and (NumTemp < 5) then 
      begin 
        CMTIIndex:= CMTIIndex + RightBStr('000' + LeftBStr(Temp,NumTemp -1),3); 
 
        Delete(ReceiveData,CMTINum2,13 + NumTemp); 
      end 
      else 
      begin 
        Temp1:= Trim(LeftBStr(Temp,2)); 
        if Length(Temp1) > 0 then 
        begin 
          try 
            CMTIIndex:= CMTIIndex + RightBStr('000' + IntToStr(StrToInt(Temp1)),3); 
            Delete(ReceiveData,CMTINum1,16); 
          except 
            try 
              CMTIIndex:= CMTIIndex + RightBStr('000' + IntToStr(StrToInt(LeftBStr(Temp1,1))),3); 
              Delete(ReceiveData,CMTINum1,15); 
            except 
              //CMGLAll:= CMGLAll + 1; 
            end; 
          end; 
        end 
        else 
          Break; 
      end; 
    end 
    else 
      Break; 
  end; 
end; 
 
/////////////////////////////////////////////////////////////// 
//说  明:此过程是用来实现VB中的DoEvents功能 
//函数名:DoEvents 
//参  数:无 
//返回值:无 
//作  者:卞伟    2003年6月 
///////////////////////////////////////////////////////////// 
procedure TShortSMSActiveX.DoEvents(); 
begin 
  Application.ProcessMessages; 
end; 
 
/////////////////////////////////////////////////////////////// 
//说  明:此函数是用来返回系统目录 
//函数名:GetSysDir 
//参  数:无 
//返回值:目录名 
//作  者:卞伟    2003年6月 
///////////////////////////////////////////////////////////// 
{function  TShortSMSActiveX.GetSysDir():string;  //返回系统目录 
var 
  path: array [0..MAX_PATH] of Char; 
begin 
  // 取系统目录 
  GetSystemDirectory(path, MAX_PATH); 
  Result:= string(path); 
//  ShowMessage(string(path)); 
  // 取TEMP目录 
//  GetTempPath(MAX_PATH, path); 
//  ShowMessage(string(path)); 
end;} 
 
/////////////////////////////////////////////////////////////// 
//说  明:此函数是用来得到系统是否已注册 
//函数名:GetSysReg 
//参  数:无 
//返回值:Boolean 
//        True:已注册 
//        False:没有注册 
//作  者:卞伟    2003年6月 
///////////////////////////////////////////////////////////// 
{function  TShortSMSActiveX.GetSysReg(): Boolean; // 
const 
  cStartDate = 'StartDate'; 
  cLastDate = 'LastDateB'; 
  cUseDays = 'UseDaysWE'; 
  cUseTimes = 'UseTimes7' 
  cRegNum = 'RegNumIBI'; 
  Pass1= '*&%$BV@&^56#%^&JH98'; 
  Pass2= 'BianWei!(&*!)@!';  //BianWei19781021 
  Pass3= '$%sa&^as$s%dsf'; 
var 
  SetIni: Tinifile; 
  StartDateIdent,LastDateIdent,UseDaysIdent,UseTimesIdent,RegNumIdent: string; 
  StartDateValue,LastDateValue,UseDaysValue,UseTimesValue,RegNumValue: string; 
  eIMEINO: string; 
  temp:string; 
  tempI: word; 
begin 
  Result:= True; 
    IMEINO     : string;                   //模块生产序列号 
    IfReg      : Bool;                     //是否已注册 
    UseDays    : Byte;                     //使用天数 
    UseTimes   : Word;                     //使用次数 
 
  if IMEINO = '' then 
    IMEINO:= GetModemIMEINO; 
  if IMEINO = '' then 
  begin 
    Result:= True; 
    Exit; 
  end; 
 
  StartDateIdent:= Encrypt(cStartDate,Pass1); 
  LastDateIdent:= Encrypt(cLastDate,Pass1); 
  UseDaysIdent:= Encrypt(cUseDays,Pass1); 
  RegNumIdent:= Encrypt(cRegNum,Pass1); 
  UseTimesIdent:= Encrypt(cUseTimes,Pass1); 
  eIMEINO:= Encrypt(IMEINO,Pass1); 
 
  try 
    SetIni:= TiniFile.Create(GetSysDir + '\SysReg.dll'); 
    StartDateValue:= SetIni.ReadString(IMEINO,StartDateIdent,Encrypt('12' + FormatDateTime('YYYYMMDD',Date()),Pass3)); 
    LastDateValue:= SetIni.ReadString(IMEINO,LastDateIdent,Encrypt('123' + FormatDateTime('YYYYMMDD',Date()),Pass3)); 
    UseTimesValue:= SetIni.ReadString(IMEINO,UseDaysIdent,Encrypt('1234' + '000',Pass3)); 
    UseDaysValue:= SetIni.ReadString(IMEINO,UseDaysIdent,Encrypt('1234567' + '00',Pass3)); 
    RegNumValue:= SetIni.ReadString(IMEINO,RegNumIdent,Encrypt('12345678901234',Pass3)); 
 
    SmartDateValue:= MidBStr(Decrypt(StartDateValue,Pass3),3,8); 
    LastDateValue:= MidBStr(Decrypt(LastDateValue,Pass3),4,8); 
    UseTimesValue:= MidBStr(Decrypt(UseTimesValue,Pass3),5,3); 
    UseDaysValue:= MidBStr(Decrypt(UseDaysValue,Pass3),8,2); 
    RegNumValue:= MidBStr(Decrypt(RegNumValue,Pass3),2,15); 
    if StartDateValue = DateToStr(Date()) then 
    begin 
      StartDateValue:= FormatDateTime('YYYYMMDD',Date()); 
      Temp:= RandGetStr(6); 
      StartDateValue:= LeftBStr(Temp,2) + StartDateValue + RightBStr(Temp,4); 
    end; 
    if 
 
      LastDateValue:= FormatDateTime('YYYYMMDD',Date()); 
      Temp:= RandGetStr(6); 
      LastDateValue:= LeftBStr(Temp,3) + LastDateValue + RightBStr(Temp,3); 
      Temp:= RandGetStr(12); 
      UseDaysValue:= LeftBStr(Temp,5) + '01' + RightBStr(Temp,7); 
      Temp:= RandGetStr(10); 
      UseTimesValue:= LeftBStr(Temp,4) + '0001' + RightBStr(Temp,6); 
      RegNumValue:= RandGetStr(14); 
 
      SetIni.WriteString(IMEINO,StartDateIdent,Encrypt(StartDateValue,Pass3)); 
      SetIni.WriteString(IMEINO,LastDateIdent,Encrypt(LastDateValue,Pass3)); 
      SetIni.WriteString(IMEINO,UseDaysIdent,Encrypt(UseDaysValue,Pass3)); 
      SetIni.WriteString(IMEINO,UseTimesIdent,Encrypt(UseTimesValue,Pass3)); 
      SetIni.WriteString(IMEINO,RegNumIdent,Encrypt(RegNumValue,Pass3)); 
 
      Result:= False; 
    end 
    else if MidBStr(Decrypt(LastDateValue,Pass3),4,8) <> FormatDateTime('YYYYMMDD',Date()) then 
    begin 
      Temp:= RandGetStr(6); 
      LastDateValue:= LeftBStr(Temp,3) + FormatDateTime('YYYYMMDD',Date()) + RightBStr(Temp,3); 
 
      try 
        tempI:= StrToInt(MidBStr(UseDaysValue,5,2)); 
      except 
 
      end; 
    end; 
  except 
    Result:= True; 
  end; 
end;} 
 
//接收事件 
procedure TShortSMSActiveX.Comm1ReceiveData(Sender: TObject; 
  Buffer: Pointer; BufferLength: Word); 
var 
  Temp: string; 
begin 
  SetLength(Temp,BufferLength); 
  Move(Buffer^,PChar(Temp)^,BufferLength); 
  ReceiveData:= ReceiveData + Temp; 
 
//  memo1.Lines.add('《' + ReceiveData + '》《' + inttostr(Length(ReceiveData)) + '》'); 
 
  //判断是否已来到新短信息 
  FindCMTI(); 
 
  if (Trim(ReceiveData) = 'OK') Or (Trim(ReceiveData) = 'ERROR') then 
    ReceiveData:= ''; 
end; 
 
/////////////////////////////////////////////////////////////// 
//说  明:此函数是用来配置并打开串口 
//函数名:OpenComm 
//参  数:1、CommNum:Byte;  串口号    默认值为1 
//        2、BaudRate: Integer; 波特率  默认值为9600 
//        3、Parity: Byte;      奇偶检验位 默认值为0  None 
//                        值只能为0,1,2,3,4 : None, Odd, Even, Mark, Space; 
//        4、DataBit: Byte;     数据位     默认值为8 
//                        值只能为5,6,7,8 
//        5、StopBit: Byte;     停止位     默认值为0 
//                        值只能为0,1,2:1,1.5,2 
//返回值:Shortint 
//        >=0 打开串口成功 
//        -1  打开串口失败 
//        -2  串口号码错误,只能016) then 
  begin 
    Result:= -2; 
    Exit; 
  end; 
  if (BaudRate <1) or (BaudRate > 256000) then 
  begin 
    Result:= -3; 
    Exit; 
  end; 
{  case BaudRate of 
    110,300,600,1200,2400,9600,14400,19200,28800,38400,56000,128000,256000: 
  else 
    begin 
      Result:= -3; 
      Exit; 
    end; 
  end;} 
  case Parity of 
    0: TempParity:= None; 
    1: TempParity:= Odd; 
    2: TempParity:= Even; 
    3: TempParity:= Mark; 
    4: TempParity:= Space; 
  else 
    begin 
      Result:= -4; 
      Exit; 
    end; 
  end; 
  case DataBit of 
    5: TempDataBit:= _5; 
    6: TempDataBit:= _6; 
    7: TempDataBit:= _7; 
    8: TempDataBit:= _8; 
  else 
    begin 
      Result:= -5; 
      Exit; 
    end; 
  end; 
  case StopBit of 
    0: TempStopBit:= _1; 
    1: TempStopBit:= _1_5; 
    2: TempStopBit:= _2; 
  else 
    begin 
      Result:= -6; 
      Exit; 
    end; 
  end; 
 
  try 
    Comm1.CommName:= 'COM' + IntToStr(CommNum); 
    Comm1.BaudRate:= BaudRate; 
    Comm1.Parity:= TempParity; 
    Comm1.ByteSize:= TempDataBit; 
    Comm1.StopBits:= TempStopBit; 
    IfExecCmd:= False; 
    ReceiveData:= ''; 
    Comm1.StartComm; 
  except 
    Result:= -1; 
    Exit; 
  end; 
end; 
 
/////////////////////////////////////////////////////////////// 
//说  明:此函数是用来关闭串口 
//函数名:CloseComm 
//参  数:无 
//返回值:Integer 
//        >=0 关闭串口成功 
//        -1  关闭串口失败 
//作  者:卞伟    2003年6月 
///////////////////////////////////////////////////////////// 
function TShortSMSActiveX.CloseComm: Smallint; 
begin 
  try 
    Comm1.StopComm; 
    IfExecCmd:= False; 
    ReceiveData:= ''; 
    Result:= 0; 
  except 
    Result:= -1; 
  end; 
end; 
 
/////////////////////////////////////////////////////////////// 
//说  明:此函数是用来求出信号强度 
//函数名:GetModemCSQ 
//参  数:无 
//返回值:Integer 
//        0~99 信号强度 
//        -1  求信号强度失败 
//        -2  模块忙 
//        -3  串口可能没有打开 
//作  者:卞伟    2003年6月 
///////////////////////////////////////////////////////////// 
function TShortSMSActiveX.GetModemCSQ: Smallint; 
var 
  OutData: PChar; 
  CSQNum,OKNum: Integer; 
  ATNum:        Integer; 
  StartTime:    TDateTime; 
  ErrNum:     Integer; 
begin 
  Result:= -1; 
  try 
    OutData:= 'AT+CSQ' + #13 + #10; 
    if not IfExecCmd then 
    begin 
      IfExecCmd:= True; 
      if Comm1.WriteCommData(OutData,Length(OutData)) then 
      begin 
        StartTime:= Now(); 
 
        while Now() <= StartTime + 1 * (1/24/60/60) do 
        begin 
          DoEvents; 
          ATNum:= Pos('AT+CSQ',ReceiveData); 
          CSQNum:= Pos(#13 + #10 + '+CSQ: ',ReceiveData); 
          OKNum:= Pos(#13 + #10 + 'OK' + #13 + #10,ReceiveData); 
          ErrNum:= Pos(#13 + #10 + 'ERROR' + #13 + #10,ReceiveData); 
 
          if (ATNum > 0) and (CSQNum > ATNum) and (OKNum > CSQNum) then 
          begin 
            Result:= StrToInt(MidBStr(ReceiveData,CSQNum + 8,2)); 
            IfExecCmd:= False; 
            Delete(ReceiveData,ATNum,OKNum - ATNum + 6); 
            if Trim(ReceiveData) = '' then 
              ReceiveData:= ''; 
            Exit; 
          end 
          else if (ATNum > 0) and (ErrNum > ATNum) and (CSQNum < 1) then 
          begin 
            IfExecCmd:= False; 
            Delete(ReceiveData,ATNum,ErrNum - ATNum + 9); 
            if Trim(ReceiveData) = '' then 
              ReceiveData:= ''; 
            Exit; 
          end; 
        end; 
      end; 
 
      IfExecCmd:= False; 
      ReceiveData:= ''; 
    end 
    else 
      Result:= -2; 
  except 
    IfExecCmd:= False; 
    ReceiveData:= ''; 
    Result:= -3; 
  end; 
end; 
 
///////////////////////////////////////////////////////////// 
//说  明:此函数是用来判断模块是否登陆成功 
//函数名:GetModemCREG 
//参  数:无 
//返回值:ShortInt 
//        0:登陆成功 
//        -1:登陆失败 
//        -2:模块忙 
//        -3:串口可能没有打开 
//作  者:卞伟    2003年6月 
///////////////////////////////////////////////////////////// 
function TShortSMSActiveX.GetModemCREG: Smallint; 
var 
  OutData: PChar; 
  CREGNum,ATNum,OKNum: Integer; 
  StartTime: TDateTime; 
  ErrNum:    Integer; 
begin 
  Result:= -1; 
  try 
    OutData:= 'AT+CREG?' + #13 + #10; 
    if not IfExecCmd then 
    begin 
      IfExecCmd:= True; 
      if Comm1.WriteCommData(OutData,Length(OutData)) then 
      begin 
        StartTime:= Now(); 
 
        while Now() <= StartTime + 1 * (1/24/60/60) do 
        begin 
          DoEvents; 
          CREGNum:= Pos(#13 + #10 + '+CREG: 0,1',ReceiveData); 
          ATNum:= Pos('AT+CREG?',ReceiveData); 
          OKNum:= Pos(#13 + #10 + 'OK' + #13 + #10,ReceiveData); 
          ErrNum:= Pos(#13 + #10 + 'ERROR' + #13 + #10,ReceiveData); 
 
          if (ATNum > 0) and (CREGNum > ATNum) and (OKNum > CREGNum) then 
          begin 
            Result:= 0; 
            IfExecCmd:= False; 
            Delete(ReceiveData,ATNum,OKNum - ATNum + 6); 
            if Trim(ReceiveData) = '' then 
              ReceiveData:= ''; 
            Exit; 
          end 
          else if (ATNum > 0) and (ErrNum > ATNum) and (CREGNum < 1) then 
          begin 
            IfExecCmd:= False; 
            Delete(ReceiveData,ATNum,ErrNum - ATNum + 9); 
            if Trim(ReceiveData) = '' then 
              ReceiveData:= ''; 
            Exit; 
          end; 
        end; 
      end; 
 
      IfExecCmd:= False; 
      ReceiveData:= ''; 
    end 
    else 
      Result:= -2; 
  except 
    IfExecCmd:= False; 
    ReceiveData:= ''; 
    Result:= -3; 
  end; 
end; 
 
///////////////////////////////////////////////////////////// 
//说  明:此函数是用来判断模块是否准备就绪 
//函数名:GetModemCPIN 
//参  数:无 
//返回值:Shortint 
//        0:就绪 
//        -1:没有就绪 
//        -2:模块忙 
//        -3:串口可能没有打开 
//作  者:卞伟    2003年6月 
///////////////////////////////////////////////////////////// 
function TShortSMSActiveX.GetModemCPIN: Smallint; 
var 
  OutData: PChar; 
  CPINNum,ATNum: Integer; 
  StartTime: TDateTime; 
  ErrNum:    Integer; 
begin 
  Result:= -1; 
  try 
    OutData:= 'AT+CPIN?' + #13 + #10; 
    if not IfExecCmd then 
    begin 
      IfExecCmd:= True; 
      if Comm1.WriteCommData(OutData,Length(OutData)) then 
      begin 
        StartTime:= Now(); 
 
        while Now() <= StartTime + 1 * (1/24/60/60) do 
        begin 
          DoEvents; 
          CPINNum:= Pos(#13 + #10 + '+CPIN: READY' + #13 + #10,ReceiveData); 
          ATNum:= Pos('AT+CPIN?',ReceiveData); 
          ErrNum:= Pos(#13 + #10 + 'ERROR' + #13 + #10,ReceiveData); 
 
          if CPINNum > 0 then 
          begin 
            Result:= 0; 
            IfExecCmd:= False; 
            Delete(ReceiveData,ATNum,CPINNum - ATNum + 16); 
            if (Trim(ReceiveData) = '') or (Trim(ReceiveData) = 'OK') then 
              ReceiveData:= ''; 
            Exit; 
          end 
          else if (ATNum > 0) and (ErrNum > ATNum) and (CPINNum < 1) then 
          begin 
            IfExecCmd:= False; 
            Delete(ReceiveData,ATNum,ErrNum - ATNum + 9); 
            if Trim(ReceiveData) = '' then 
              ReceiveData:= ''; 
            Exit; 
          end; 
        end; 
      end; 
 
      IfExecCmd:= False; 
      ReceiveData:= ''; 
    end 
    else 
      Result:= -2; 
  except 
    IfExecCmd:= False; 
    ReceiveData:= ''; 
    Result:= -3; 
  end; 
end; 
 
///////////////////////////////////////////////////////////// 
//说  明:此函数是用来设置短信中心号码 
//函数名:SetModemCSCA 
//参  数:Value: WideString;  短信中心号码 
//返回值:ShortInt 
//        0:成功 
//        -1:失败 
//        -2:模块忙 
//        -3:串口可能没有打开 
//作  者:卞伟    2003年6月 
///////////////////////////////////////////////////////////// 
function TShortSMSActiveX.SetModemCSCA(const Value: WideString): Smallint; 
var 
  OutData: string; 
  ATNum,OKNum: Integer; 
  StartTime: TDateTime; 
  ErrNum:    Integer; 
begin 
  Result:= -1; 
  try 
    OutData:= 'AT+CSCA=' + Value + #13 + #10; 
    if not IfExecCmd then 
    begin 
      IfExecCmd:= True; 
      if Comm1.WriteCommData(PChar(OutData),Length(PChar(OutData))) then 
      begin 
        StartTime:= Now(); 
 
        while Now() <= StartTime + 1 * (1/24/60/60) do 
        begin 
          DoEvents; 
          OKNum:= Pos(#13 + #10 + 'OK' + #13 + #10,ReceiveData); 
          ATNum:= Pos('AT+CSCA=',ReceiveData); 
          ErrNum:= Pos(#13 + #10 + 'ERROR' + #13 + #10,ReceiveData); 
 
          if (ATNum > 0) and (OKNum > ATNum) then 
          begin 
            Result:= 0; 
            IfExecCmd:= False; 
            Delete(ReceiveData,ATNum,OKNum - ATNum + 6); 
            if Trim(ReceiveData) = '' then 
              ReceiveData:= ''; 
            Exit; 
          end 
          else if (ATNum > 0) and (ErrNum > ATNum) then 
          begin 
            IfExecCmd:= False; 
            Delete(ReceiveData,ATNum,ErrNum - ATNum + 9); 
            if Trim(ReceiveData) = '' then 
              ReceiveData:= ''; 
            Exit; 
          end; 
        end; 
      end; 
 
      IfExecCmd:= False; 
      ReceiveData:= ''; 
    end 
    else 
      Result:= -2; 
  except 
    IfExecCmd:= False; 
    ReceiveData:= ''; 
    Result:= -3; 
  end; 
end; 
 
///////////////////////////////////////////////////////////// 
//说  明:此函数是用来设置PDU格式 
//函数名:SetModemCMGF 
//参  数:Value: TSMSCMGF;  PDU格式 
//        1:TEXT格式 
//        0:PDU格式 
//返回值:Shortint 
//        0:成功 
//        -1:失败 
//        -2:模块忙 
//        -3:串口可能没有打开 
//作  者:卞伟    2003年6月 
///////////////////////////////////////////////////////////// 
function TShortSMSActiveX.SetModemCMGF(Value: TSMSCMGF): Smallint; 
var 
  OutData: string; 
  OKNum,ATNum: Integer; 
  StartTime: TDateTime; 
begin 
  Result:= -1; 
  try 
    OutData:= 'AT+CMGF=' + IntToStr(Value) + #13 + #10; 
    if not IfExecCmd then 
    begin 
      IfExecCmd:= True; 
      if Comm1.WriteCommData(PChar(OutData),Length(PChar(OutData))) then 
      begin 
        StartTime:= Now(); 
 
        while Now() <= StartTime + 1 * (1/24/60/60) do 
        begin 
          DoEvents; 
          OKNum:= Pos(#13 + #10 + 'OK' + #13 + #10,ReceiveData); 
          ATNum:= Pos('AT+CMGF=',ReceiveData); 
 
          if (ATNum > 0) and (OKNum > ATNum) then 
          begin 
            Result:= 0; 
            IfExecCmd:= False; 
            Delete(ReceiveData,ATNum,OKNum - ATNum + 6); 
            if Trim(ReceiveData) = '' then 
              ReceiveData:= ''; 
            Exit; 
          end 
        end; 
      end; 
 
      IfExecCmd:= False; 
      ReceiveData:= ''; 
    end 
    else 
      Result:= -2; 
  except 
    Result:= -3; 
    IfExecCmd:= False; 
    ReceiveData:= ''; 
  end; 
end; 
 
///////////////////////////////////////////////////////////// 
//说  明:此函数是用来发送短信 
//函数名:SendInfo 
//参  数:1、HandNum: WideString 对方手机号码 
//        2、MsgInfo: WideString 要发送的内容 
//返回值:Shortint 
//        0:成功 
//        -1:发送短信失败 
//        -2:模块忙 
//        -3:短信内容超长或为空 
//        -4:设置短信发送模式失败 
//        -5:发送失败,并且可能需要重新设置发送模式 
//        -6:发送短信结果没有返回,可能与模块的连接终止 
//作  者:卞伟    2003年2月 
///////////////////////////////////////////////////////////// 
function TShortSMSActiveX.SendInfo(const HandNum, 
  Info: WideString): Smallint; 
var 
  OutData: string; 
  TempHandNum: string; 
  TempInfo: string; 
 
  ATNum,OKNum,CMGSNum: Integer; 
  StartTime: TDateTime; 
  CMSErrNum,CMEErrNum,ErrNum: Integer; 
begin 
  if Pos('86',HandNum) = 1 then 
    TempHandNum:= '+' + HandNum 
  else if Pos('+86',HandNum) = 1 then 
    TempHandNum:= HandNum 
  else 
    TempHandNum:= '+86' + HandNum; 
 
  TempInfo:= Info; 
  if IfExecCmd then 
    //模块忙 
    Result:= -2 
{  else if ((Length(TempInfo) > Length(Info)) and (Length(Info) > 60)) or 
          ((Length(TempInfo) = Length(Info)) and (Length(Info) > 120)) or 
          (Length(Info) < 1) then 
    //短信内容超长或为空 
    Result:= -3} 
  else if ((Length(TempInfo) > Length(Info)) and (Length(Info) > 70)) or 
          (Length(Info) < 1) then 
    //短信内容超长或为空 
    Result:= -3 
  else 
  begin 
    try 
//      if Length(TempInfo) > Length(Info) then 
//      begin 
        //PDU格式 
//        if SetModemCMGF(0) < 0 then 
//        begin 
//          Result:= -4; 
//          IfExecCmd:= False; 
//          Exit; 
//        end; 
 
        IfExecCmd:= True; 
        TempInfo:= SMChangHex(TempHandNum,TempInfo + '-BW'); 
        OutData:= 'AT+CMGS=' + IntToStr(Length(TempInfo) div 2 - 1) + Chr(13) + Chr(10); 
        if not Comm1.WriteCommData(PChar(OutData),length(PChar(OutData))) then 
        begin 
          Result:= -1; 
          IfExecCmd:= False; 
          Exit; 
        end; 
{      end 
      else if Length(TempInfo) = Length(Info) then 
      begin 
        //TEXT格式 
        if SetModemCMGF(1) < 0 then 
        begin 
          Result:= -4; 
          IfExecCmd:= False; 
          Exit; 
        end; 
 
        IfExecCmd:= True; 
        OutData:= 'AT+CMGS=' + String(TempHandNum) + Chr(13) + Chr(10); 
        if not Comm1.WriteCommData(PChar(OutData),length(PChar(OutData))) then 
        begin 
          Result:= -1; 
          IfExecCmd:= False; 
          Exit; 
        end; 
      end; 
} 
      Sleep(10); 
      StartTime:= Now(); 
      while Now() <= StartTime + 1 * (1/24/60/60) do 
      begin 
        DoEvents; 
        if Length(ReceiveData) > (Length(OutData) + Pos(OutData,ReceiveData)) then 
          if Pos('>',ReceiveData) >= Length(OutData) + Pos(OutData,ReceiveData) then 
          begin 
            Result:= 0; 
            Break; 
          end; 
          if Pos('ERROR',ReceiveData) > (Length(OutData) + Pos(OutData,ReceiveData)) then 
          begin 
            //showmessage('error'); 
            Delete(ReceiveData,Pos(OutData,ReceiveData),Pos('ERROR',ReceiveData) - Pos(OutData,ReceiveData) + 6); 
            Result:= -5; 
            IfExecCmd:= False; 
            Exit; 
          end; 
 
        Result:= -1; 
      end; 
 
      if Result<> 0 then 
      begin 
        IfExecCmd:= False; 
        Exit; 
      end; 
 
{      if rightstr(trim(receivedata),1) = '>' then 
       showmessage('True') 
     else 
        showmessage(rightstr(trim(receivedata),1)); 
} 
      OutData:= TempInfo + Chr(26); 
      if not Comm1.WriteCommData(PChar(OutData),length(PChar(OutData))) then 
      begin 
        Result:= -1; 
        IfExecCmd:= False; 
        Exit; 
      end; 
      Sleep(10); 
 
      StartTime:= Now(); 
 
      while Now() <= StartTime + 60 * (1/24/60/60) do 
      begin 
        DoEvents; 
        OKNum:= Pos(#13 + #10 + 'OK' + #13 + #10,ReceiveData); 
        ATNum:= Pos('AT+CMGS=',ReceiveData); 
        CMGSNum:= Pos('+CMGS:',ReceiveData); 
        CMSErrNum:= Pos('+CMS ERROR:',ReceiveData); 
        CMEErrNum:= Pos('+CME ERROR:',ReceiveData); 
        ErrNum:= Pos('ERROR',ReceiveData); 
 
        if (CMGSNum > ATNum) and (OKNum > CMGSNum) then 
        //发送短信成功 
        begin 
          Delete(ReceiveData,ATNum,OKNum - ATNum + 6); 
          if Trim(ReceiveData) = '' then 
            ReceiveData:= ''; 
 
          IfExecCmd:= False; 
          Result:= 0; 
          Exit; 
        end 
        else if CMSErrNum > ATNum then 
        //发送短信失败 
        begin 
          Delete(ReceiveData,ATNum,CMSErrNum - ATNum + 12); 
          if Trim(ReceiveData) = '' then 
            ReceiveData:= ''; 
 
          Result:= -1;   //发送短信失败 
          IfExecCmd:= False; 
          Exit; 
        end 
        else if CMEErrNum > ATNum then 
        begin 
          Delete(ReceiveData,ATNum,CMEErrNum - ATNum + 12); 
          if Trim(ReceiveData) = '' then 
            ReceiveData:= ''; 
 
          Result:= -1;   //发送短信失败 
          IfExecCmd:= False; 
          Exit; 
        end 
        else if ErrNum > ATNum then 
        //发送短信失败,并且有可能需要重新设置发送模式及短信中心号码 
        begin 
          Delete(ReceiveData,ATNum,ErrNum - ATNum + 6); 
          if Trim(ReceiveData) = '' then 
            ReceiveData:= ''; 
 
            Result:= -5;   //发送失败,并且可能需要重新设置发送模式 
          IfExecCmd:= False; 
          Exit; 
        end 
        else 
        //发送短信结果没有返回,可能与模块的连接终止 
        begin 
          Result:= -6;   //发送短信结果没有返回,可能与模块的连接终止 
        end; 
      end; 
      IfExecCmd:= False; 
    except 
      Result:= -1; 
      IfExecCmd:= False; 
    end; 
  end; 
end; 
 
///////////////////////////////////////////////////////////// 
//说  明:此函数是用来从SIM卡中读出指定的短信息内容 
//函数名:ReadInfo 
//参  数:1、Index: 索引 
//        2、HandNum: WideString 发送方手机号码 
//        3、Info:WideString    读出的短信息内容 
//        4、SendTime: WideString 接收到的发送时间 
//返回值:Shortint 
//        0:读短信成功 
//        -1:读短信失败 
//        -2:模块忙 
//        -3:短信索引不能小于1 
//        -4:设置短信模式失败 
//        -5:短短信失败,并且可能需要重新设置短信模式 
//        -6:读短信结果没有返回,可能与模块的连接终止 
//作  者:卞伟    2003年6月 
///////////////////////////////////////////////////////////// 
function TShortSMSActiveX.ReadInfo(Index: Word; var HandNum, Info, 
  SendTime: WideString): Smallint; 
var 
  OutData: string; 
  OKNum: Integer; 
  CMGRNum: Integer; 
  ATNum,ErrNum: Integer; 
  StartTime: TDateTime; 
  MsgInfo: WideString; 
  TempHandNum,TempInfo,TempSendTime: string; 
begin 
  Result:= 0; 
  if IfExecCmd then 
  begin 
    //模块忙 
    Result:= -2; 
    Exit; 
  end; 
  if Index < 1 then 
  begin 
    //短信索引不能小于1 
    Result:= -3; 
    Exit; 
  end; 
  if SetModemCMGF(0) < 0 then 
  begin 
    //设置接收模式失败 
    Result:= -4; 
    IfExecCmd:= False; 
    Exit; 
  end; 
  try 
    IfExecCmd:= True; 
    OutData:= 'AT+CMGR=' + IntToStr(Index) + #13 + #10; 
    if Comm1.WriteCommData(PChar(OutData),Length(PChar(OutData))) then 
    begin 
      StartTime:= Now(); 
 
      while Now() <= StartTime + 30 * (1/24/60/60) do 
      begin 
        DoEvents; 
        ATNum:= Pos('AT+CMGR=',ReceiveData); 
        OKNum:= Postion(#13 + #10 + 'OK' + #13 + #10,ReceiveData,ATNum) + ATNum; 
        CMGRNum:= Postion(#13 + #10 + '+CMGR: ',ReceiveData,ATNum) + ATNum; 
        ErrNum:= Postion(#13 + #10 + 'ERROR' + #13 + #10,ReceiveData,ATNum) + ATNum; 
 
        if (CMGRNum > ATNum) and (OKNum > CMGRNum) then 
        begin 
          //读短信成功 
          MsgInfo:= MidBStr(ReceiveData,CMGRNum,OKNum - CMGRNum + 4); 
          Delete(ReceiveData,ATNum,OKNum - ATNum + 6); 
 
          SMSDecode(MsgInfo,TempHandNum,TempInfo,TempSendTime); 
          HandNum:= TempHandNum; 
          Info:= TempInfo; 
          SendTime:= TempSendTime; 
 
          if Pos(RightBStr('000' + IntToStr(Index),3),CMTIIndex) > 0 then 
            Delete(CMTIIndex,Pos(RightBStr('000' + IntToStr(Index),3),CMTIIndex),3); 
 
          IfExecCmd:= False; 
          Result:= 0; 
          Exit; 
        end; 
        if ErrNum > ATNum then 
        begin 
          //读短信失败 
          Delete(ReceiveData,ATNum,ErrNum - ATNum + 9); 
          Result:= -5; 
          IfExecCmd:= False; 
          Exit; 
        end; 
        Result:= -6; 
      end; 
      IfExecCmd:= False; 
      Exit; 
    end 
    else 
    begin 
      Result:= -1; 
      IfExecCmd:= False; 
      Exit; 
    end; 
  except 
    Result:= -1; 
    IfExecCmd:= False; 
    Exit; 
  end; 
end; 
 
///////////////////////////////////////////////////////////// 
//说  明:此函数是用来从SIM卡中读出接收到的新短信息 
//函数名:ReadNewInfo 
//参  数:1、HandNum: WideString 发送方手机号码 
//        2、Info:WideString    读出的短信息内容 
//        3、SendTime: WideString 接收到的发送时间 
//返回值:Shortint 
//        >0:返回信息的索引号 
//        0:读短信成功 
//        -1:读短信失败 
//        -2:模块忙 
//        -3:短信索引不能小于1 
//        -4:设置短信模式失败 
//        -5:短短信失败,并且可能需要重新设置短信模式 
//        -6:读短信结果没有返回,可能与模块的连接终止 
//作  者:卞伟    2003年6月 
///////////////////////////////////////////////////////////// 
function TShortSMSActiveX.ReadNewInfo(var HandNum, Info, 
  SendTime: WideString): Smallint; 
var 
  TempIndex: Byte; 
  Resu: ShortInt; 
begin 
  if Length(CMTIIndex) <= 0 then 
  begin 
    Result:= 0; 
    Exit; 
  end; 
 
  try 
    TempIndex:= StrToInt(LeftBStr(CMTIIndex,3)); 
    Resu:= ReadInfo(TempIndex,HandNum,Info,SendTime); 
    if Resu>= 0 then 
      Result:= TempIndex 
    else 
      Result:= Resu; 
  except 
    Result:= -1; 
  end; 
end; 
 
///////////////////////////////////////////////////////////// 
//说  明:此函数是用来把SIM卡中的短信删除掉 
//函数名:DelInfo 
//参  数:1、Index: 索引 
//返回值:Shortint 
//        0:删除短信成功 
//        -1:删除短信失败 
//        -2:模块忙 
//        -3:短信索引不能小于1 
//        -4:删除短信失败,并且可能需要重新设置短信模式 
//        -5:删除短信结果没有返回,可能与模块的连接终止 
//作  者:卞伟    2003年6月 
///////////////////////////////////////////////////////////// 
function TShortSMSActiveX.DelInfo(Index: Word): Smallint; 
var 
  OutData: string; 
  OKNum,ATNum: Integer; 
  StartTime: TDateTime; 
  ErrNum: Integer; 
begin 
  Result:= 0; 
  if IfExecCmd then 
  begin 
    Result:= -2; 
    Exit; 
  end; 
 
  if Index < 1 then 
  begin 
    Result:= -3; 
    Exit; 
  End; 
 
  OutData:= 'AT+CMGD=' + IntToStr(Index) + #13 + #10; 
 
  if Comm1.WriteCommData(PChar(OutData),Length(PChar(OutData))) then 
  begin 
    StartTime:= Now(); 
 
    while Now() <= StartTime + 6 * (1/24/60/60) do 
    begin 
      DoEvents; 
      ATNum:= Pos('AT+CMGD='+ IntToStr(Index),ReceiveData); 
      OKNum:= Postion(#13 + #10 + 'OK' + #13 + #10,ReceiveData,ATNum) + ATNum; 
      ErrNum:= Postion(#13 + #10 + 'ERROR' + #13 + #10,ReceiveData,ATNum) + ATNum; 
 
      if OKNum > ATNum then 
      begin 
        Result:= 0; 
        Delete(ReceiveData,ATNum,OKNum - ATNum + 6); 
        IfExecCmd:= False; 
        Exit; 
      end 
      else if (ErrNum > ATNum) then 
      begin 
        Result:= -1; 
        Delete(ReceiveData,ATNum,ErrNum - ATNum + 9); 
        IfExecCmd:= False; 
        Exit; 
      end 
      else 
        Result:= -5; 
    end; 
    IfExecCmd:= False; 
  end 
  else 
  begin 
    IfExecCmd:= False; 
    Result:= -1; 
  end; 
end; 
 
///////////////////////////////////////////////////////////// 
//说  明:此函数是用来得到模块的生产序号 
//函数名:GetModemIMEINO 
//参  数:无 
//返回值:WideString 
//        成功返回生产序号 
//        失败返回空值 
//作  者:卞伟    2003年6月 
///////////////////////////////////////////////////////////// 
function TShortSMSActiveX.GetModemIMEINO: WideString; 
var 
  OutData: string; 
  OKNum,ATNum: Integer; 
  ErrNum:      Integer; 
  StartTime: TDateTime; 
begin 
  if IfExecCmd then 
    Exit; 
 
  OutData:= 'AT+CGSN' + Chr(13) + Chr(10); 
  if not Comm1.WriteCommData(PChar(OutData),Length(PChar(OutData))) then 
    Exit; 
 
  StartTime:= Now(); 
 
  while Now() <= StartTime + 1 * (1/24/60/60) do 
  begin 
    DoEvents; 
    ATNum:= Pos('AT+CGSN',ReceiveData); 
    OKNum:= Postion(#13 + #10 + 'OK' + #13 + #10,ReceiveData,ATNum) + ATNum; 
    ErrNum:= Postion(#13 + #10 + 'ERROR' + #13 + #10,ReceiveData,ATNum) + ATNum; 
 
    if OKNum > ATNum then 
    begin 
      Result:= Trim(MidBStr(ReceiveData,ATNum + 7,OKnum - ATNum - 7)); 
      Delete(ReceiveData,ATNum,OKNum - ATNum + 6); 
      IfExecCmd:= False; 
      Exit; 
    end; 
    if ErrNum > ATNum then 
    begin 
      Delete(ReceiveData,ATNum,ErrNum - ATNum + 9); 
      IfExecCmd:= False; 
      Exit; 
    end; 
  end; 
end; 
 
///////////////////////////////////////////////////////////// 
//说  明:此函数是用来恢复模块的默认设置 
//函数名:SetModemATZ 
//参  数:无 
//返回值:ShortInt 
//        0:成功 
//        -1:失败 
//        -2:模块忙 
//        -3:没有返回成功或失败标志 
//作  者:卞伟    2003年6月 
///////////////////////////////////////////////////////////// 
function TShortSMSActiveX.SetModemATZ: Smallint; 
var 
  OutData: string; 
  OKNum,ATNum: Integer; 
  ErrNum:      Integer; 
  StartTime: TDateTime; 
begin 
  if IfExecCmd then 
  begin 
    Result:= -2; 
    Exit; 
  end; 
 
  OutData:= 'ATZ' + Chr(13) + Chr(10); 
  if not Comm1.WriteCommData(PChar(OutData),Length(PChar(OutData))) then 
  begin 
    Result:= -1; 
    Exit; 
  end; 
 
  StartTime:= Now(); 
 
  while Now() <= StartTime + 1 * (1/24/60/60) do 
  begin 
    DoEvents; 
    ATNum:= Pos('ATZ',ReceiveData); 
    OKNum:= Postion(#13 + #10 + 'OK' + #13 + #10,ReceiveData,ATNum) + ATNum; 
    ErrNum:= Postion(#13 + #10 + 'ERROR' + #13 + #10,ReceiveData,ATNum) + ATNum; 
 
    if OKNum > ATNum then 
    begin 
      Delete(ReceiveData,ATNum,OKNum - ATNum + 6); 
      Result:= 0; 
      IfExecCmd:= False; 
      Exit; 
    end; 
    if ErrNum > ATNum then 
    begin 
      Delete(ReceiveData,ATNum,ErrNum - ATNum + 9); 
      Result:= -1; 
      IfExecCmd:= False; 
      Exit; 
    end; 
    Result:= -3; 
  end; 
end; 
 
///////////////////////////////////////////////////////////// 
//说  明:此函数是用来设置新信息来到时的提示方式 
//函数名:SetModemCNMI 
//参  数:无 
//返回值:ShortInt 
//        0:成功 
//        -1:失败 
//        -2:模块忙 
//        -3:没有返回成功或失败标志 
//作  者:卞伟    2003年6月 
///////////////////////////////////////////////////////////// 
function TShortSMSActiveX.SetModemCNMI: Smallint; 
var 
  OutData: string; 
  OKNum,ATNum: Integer; 
  ErrNum:      Integer; 
  StartTime: TDateTime; 
begin 
  if IfExecCmd then 
  begin 
    Result:= -2; 
    Exit; 
  end; 
 
  OutData:= 'AT+CNMI=2,1,0,0,1' + Chr(13) + Chr(10); 
  if not Comm1.WriteCommData(PChar(OutData),Length(PChar(OutData))) then 
  begin 
    Result:= -1; 
    Exit; 
  end; 
 
  StartTime:= Now(); 
 
  while Now() <= StartTime + 2 * (1/24/60/60) do 
  begin 
    DoEvents; 
    ATNum:= Pos('AT+CNMI=2,1,0,0,1',ReceiveData); 
    OKNum:= Postion(#13 + #10 + 'OK' + #13 + #10,ReceiveData,ATNum) + ATNum; 
    ErrNum:= Postion(#13 + #10 + 'ERROR' + #13 + #10,ReceiveData,ATNum) + ATNum; 
 
    if OKNum > ATNum then 
    begin 
      Delete(ReceiveData,ATNum,OKNum - ATNum + 6); 
      Result:= 0; 
      IfExecCmd:= False; 
      Exit; 
    end; 
    if ErrNum > ATNum then 
    begin 
      Delete(ReceiveData,ATNum,ErrNum - ATNum + 9); 
      Result:= -1; 
      IfExecCmd:= False; 
      Exit; 
    end; 
    Result:= -3; 
  end; 
end; 
 
procedure TShortSMSActiveX.ActiveFormCreate(Sender: TObject); 
begin 
  Image1.Width:= 32; 
  Image1.Height:= 32; 
  Width:= 32; 
  Height:= 32; 
end; 
 
initialization 
  TActiveFormFactory.Create( 
    ComServer, 
    TActiveFormControl, 
    TShortSMSActiveX, 
    Class_ShortSMSActiveX, 
    1, 
    '', 
//    OLEMISC_SIMPLEFRAME or OLEMISC_ACTSLIKELABEL, 
    OLEMISC_INVISIBLEATRUNTIME or OLEMISC_ACTSLIKELABEL,// or OLEMISC_SIMPLEFRAME, 
    tmApartment); 
end.