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

📄 awuser.pas

📁 测试用例
💻 PAS
📖 第 1 页 / 共 5 页
字号:
      end;
    finally
      LeaveCriticalSection(DataSection);
    end;

    if ((DCB.BaudRate = NewBaudRate) and
        (DCB.Parity = NewParity) and
        (DCB.ByteSize = NewByteSize) and
        (DCB.StopBits = NewStopBits)) then
      Exit;

    { wait for the chars to be transmitted, don't want to change line }
    { settings while chars are pending }
    WaitTxSent;

    EnterCriticalSection(DataSection);
    try
      {Get current DCB parameters}
      GetComState(DCB);

      {Change the parameters}
      DCB.BaudRate := NewBaudRate;
      DCB.Parity   := NewParity;
      DCB.ByteSize := NewByteSize;
      DCB.StopBits := NewStopBits;

      {Set line parameters}
      Result := SetCommStateFix(DCB);
    finally
      LeaveCriticalSection(DataSection);
    end;
  end;

  procedure TApdBaseDispatcher.GetLine(
                    var Baud : LongInt;
                    var Parity : Word;
                    var DataBits : TDatabits;
                    var StopBits : TStopbits);
    {-Return line parameters}
  begin
    EnterCriticalSection(DataSection);
    try
      {Get current DCB parameters}
      GetComState(DCB);

      {Return the line parameters}
      Baud := ActualBaud(DCB.Baudrate);

      Parity := DCB.Parity;
      DataBits := DCB.ByteSize;
      if DCB.StopBits = OneStopBit then
        StopBits := 1
      else
        StopBits := 2;
    finally
      LeaveCriticalSection(DataSection);
    end;
  end;

  function TApdBaseDispatcher.SetModem(Dtr, Rts : Boolean) : Integer;
    {-Set modem control lines, Dtr and Rts}
  begin
    Result := SetDtr(Dtr);
    if Result = ecOK then
      Result := SetRts(Rts);                                             {!!.02}
  end;

  function TApdBaseDispatcher.SetDtr(OnOff : Boolean) : Integer;
    {-Set DTR modem control line}
  begin
    if DtrAuto then begin
      { We can't change DTR if we're controlling it automatically }
      Result := 1;
      Exit;
    end;

    if (OnOff = True) then
      Result := EscapeComFunction(WinTypes.SETDTR)
    else
      Result := EscapeComFunction(WinTypes.CLRDTR);

    if (Result < ecOK) then
      Result := ecBadArgument;
    DTRState := OnOff;
  end;

  function TApdBaseDispatcher.SetRts(OnOff : Boolean) : Integer;
    {-Set RTS modem control line}
  begin
    if RtsAuto then begin
      { We can't change RTS if we're controlling it automatically }
      Result := 1;
      Exit;
    end;

    if (OnOff = True) then
      Result := EscapeComFunction(WinTypes.SETRTS)
    else
      Result := EscapeComFunction(WinTypes.CLRRTS);

    if (Result < ecOK) then
      Result := ecBadArgument;
    RTSState := OnOff;
  end;

  function TApdBaseDispatcher.GetModemStatusPrim(ClearMask : Byte) : Byte;
    {-Primitive to return the modem status and clear mask}
  var
    Data : DWORD;
  begin
    EnterCriticalSection(DataSection);
    try

      {Get the new absolute values}
      GetCommModemStatus(CidEx, Data);
      ModemStatus := (ModemStatus and $0F) or Byte(Data);

      {Special case, transfer RI bit to TERI bit}
      if RingFlag then begin
        RingFlag := False;
        ModemStatus := ModemStatus or $04;
      end;

      {Return the current ModemStatus value}
      Result := Lo(ModemStatus);

      {Clear specified delta bits}
      ModemStatus := ModemStatus and Clearmask;

    finally
      LeaveCriticalSection(DataSection);
    end;
  end;

  function TApdBaseDispatcher.GetModemStatus : Byte;
    {-Return the modem status byte and clear the delta bits}
  begin
    Result := GetModemStatusPrim(ClearDelta);
  end;

  function TApdBaseDispatcher.CheckCTS : Boolean;
    {-Returns True if CTS is high}
  begin
    Result := GetModemStatusPrim(ClearDeltaCTS) and CTSMask = CTSMask;
  end;

  function TApdBaseDispatcher.CheckDSR : Boolean;
    {-Returns True if DSR is high}
  begin
    Result := GetModemStatusPrim(ClearDeltaDSR) and DSRMask = DSRMask;
  end;

  function TApdBaseDispatcher.CheckRI : Boolean;
    {-Returns True if RI is high}
  begin
    Result := GetModemStatusPrim(ClearDeltaRI) and RIMask = RIMask;
  end;

  function TApdBaseDispatcher.CheckDCD : Boolean;
    {-Returns True if DCD is high}
  begin
    Result := GetModemStatusPrim(ClearDeltaDCD) and DCDMask = DCDMask;
  end;

  function TApdBaseDispatcher.CheckDeltaCTS : Boolean;
    {-Returns True if DeltaCTS is high}
  begin
    Result := GetModemStatusPrim(ClearDeltaCTS) and DeltaCTSMask = DeltaCTSMask;
  end;

  function TApdBaseDispatcher.CheckDeltaDSR : Boolean;
    {-Returns True if DeltaDSR is high}
  begin
    Result := GetModemStatusPrim(ClearDeltaDSR) and DeltaDSRMask = DeltaDSRMask;
  end;

  function TApdBaseDispatcher.CheckDeltaRI : Boolean;
    {-Returns True if DeltaRI is high}
  begin
    Result := GetModemStatusPrim(ClearDeltaRI) and DeltaRIMask = DeltaRIMask;
  end;

  function TApdBaseDispatcher.CheckDeltaDCD : Boolean;
    {-Returns True if DeltaDCD is high}
  begin
    Result := GetModemStatusPrim(ClearDeltaDCD) and DeltaDCDMask = DeltaDCDMask;
  end;

  function TApdBaseDispatcher.GetLineError : Integer;
    {-Return current line errors}
  const
    AllErrorMask = ce_RxOver +
                   ce_Overrun + ce_RxParity + ce_Frame;
  var
    GotError : Boolean;
  begin
    EnterCriticalSection(DataSection);
    try
      GotError := True;
      if FlagIsSet(LastError, ce_RxOver) then
        Result := leBuffer
      else if FlagIsSet(LastError, ce_Overrun) then
        Result := leOverrun
      else if FlagIsSet(LastError, ce_RxParity) then
        Result := leParity
      else if FlagIsSet(LastError, ce_Frame) then
        Result := leFraming
      else if FlagIsSet(LastError, ce_Break) then
        Result := leBreak
      else begin
        GotError := False;
        Result := leNoError;
      end;

      {Clear all error flags}
      if GotError then
        LastError := LastError and not AllErrorMask;
    finally
      LeaveCriticalSection(DataSection);
    end;
  end;

  function TApdBaseDispatcher.CheckLineBreak : Boolean;
  begin
    EnterCriticalSection(DataSection);
    try
      Result := FlagIsSet(LastError, ce_Break);
      LastError := LastError and not ce_Break;
    finally
      LeaveCriticalSection(DataSection);
    end;
  end;

  procedure TApdBaseDispatcher.SendBreak(Ticks : Cardinal; Yield : Boolean);
    {Send a line break of Ticks ticks, with yields}
  begin
    { raise RTS for RS485 mode }
    if RS485Mode then                                                    {!!.01}
      SetRTS(True);                                                      {!!.01}
    SetCommBreak(CidEx);
    DelayTicks(Ticks, Yield);
    ClearCommBreak(CidEx);
    { lower RTS only if the output buffer is empty }
    if RS485Mode and (OutBuffUsed = 0) then                              {!!.01}
      SetRTS(False);                                                     {!!.01}
  end;

  procedure TApdBaseDispatcher.SetBreak(BreakOn: Boolean);
    {Sets or clears line break condition}
  begin
    if BreakOn then begin                                                {!!.01}
      if RS485Mode then                                                  {!!.01}
        SetRTS(True);                                                    {!!.01}
      SetCommBreak(CidEx)
    end else begin                                                       {!!.01}
      ClearCommBreak(CidEx);
      if RS485Mode and (OutBuffUsed = 0) then                            {!!.01}
        SetRTS(False);                                                   {!!.01}
    end;                                                                 {!!.01}                                                                     
  end;

  function TApdBaseDispatcher.CharReady : Boolean;
    {-Return True if at least one character is ready at the device driver}
  var
    NewTail : Cardinal;
  begin
    EnterCriticalSection(DispSection);
    try
      if InAvailMessage then begin
        NewTail := DBufTail + GetCount;
        if NewTail >= DispatchBufferSize then
          Dec(NewTail, DispatchBufferSize);
        Result := (DBufHead <> NewTail)
          or (DispatchFull and (GetCount < DispatchBufferSize));
      end else
        Result := (DBufHead <> DBufTail) or DispatchFull;
    finally
      LeaveCriticalSection(DispSection);
    end;
  end;

  function TApdBaseDispatcher.PeekCharPrim(var C : Char; Count : Cardinal) : Integer;
    {-Return the Count'th character but don't remove it from the buffer}
  var
    NewTail : Cardinal;
    InCount : Cardinal;
  begin
    Result := ecOK;
    EnterCriticalSection(DispSection);
    try
      if DBufHead > DBufTail then
        InCount := DBufHead-DBufTail
      else if DBufHead <> DBufTail then
        InCount := ((DBufHead+DispatchBufferSize)-DBufTail)
      else if DispatchFull then
        InCount := DispatchBufferSize
      else
        InCount := 0;

      if InCount >= Count then begin
        {Calculate index of requested character}
        NewTail := DBufTail + (Count - 1);
        if NewTail >= DispatchBufferSize then
          NewTail := (NewTail - DispatchBufferSize);
        C := DBuffer^[NewTail];
      end else
        Result := ecBufferIsEmpty;
    finally
      LeaveCriticalSection(DispSection);
    end;
  end;

  function TApdBaseDispatcher.PeekChar(var C : Char; Count : Cardinal) : Integer;
    {-Return the Count'th character but don't remove it from the buffer}
    {-Account for GetCount}
  begin
    EnterCriticalSection(DispSection);
    try
      if InAvailMessage then
        Inc(Count, GetCount);
      Result := PeekCharPrim(C, Count);
    finally
      LeaveCriticalSection(DispSection);
    end;
  end;

  function TApdBaseDispatcher.GetChar(var C : Char) : Integer;
    {-Return next char and remove it from buffer}
  begin
    EnterCriticalSection(DispSection);
    try
      {If within an apw_TriggerAvail message then do not physically      }
      {extract the character. It will be removed by the dispatcher after }
      {all trigger handlers have seen it. If not within an               }
      {apw_TriggerAvail message then physically extract the character    }

      if InAvailMessage then begin
        Inc(GetCount);
        Result := PeekCharPrim(C, GetCount);
        if Result < ecOK then begin
          Dec(GetCount);
          Exit;
        end;
      end else begin
        Result := PeekCharPrim(C, 1);
        if Result >= ecOK then begin
          {Increment the tail index}
          Inc(DBufTail);
          if DBufTail = DispatchBufferSize then
            DBufTail := 0;
          DispatchFull := False;
        end;
      end;

      if TracingOn
      and (Result >= ecOK) then
        AddTraceEntry('R', C);
    finally
      LeaveCriticalSection(DispSection);
    end;
  end;

  function TApdBaseDispatcher.PeekBlockPrim(Block : PChar;
    Offset : Cardinal; Len : Cardinal; var NewTail : Cardinal) : Integer;
    {-Return Block from ComPort, return new tail value}
  var
    Count : Cardinal;
    EndCount : Cardinal;
    BeginCount : Cardinal;
  begin
    EnterCriticalSection(DispSection);
    try
      {Get count}
      Count := BuffCount(DBufHead, DBufTail, DispatchFull);

      {Set new tail value}
      NewTail := DBufTail + Offset;
      if NewTail >= DispatchBufferSize then
        Dec(NewTail, DispatchBufferSize);

      if Count >= Len then begin
        {Set begin/end buffer counts}
        if NewTail+Len < DispatchBufferSize then begin
          EndCount := Len;
          BeginCount := 0;
        end else begin
          EndCount := (DispatchBufferSize-NewTail);
          BeginCount := Len-EndCount;
        end;

        if EndCount <> 0 then begin

⌨️ 快捷键说明

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