program ChnCom_Test4;

uses
  ChnVirt,
  ChnCom,
  Crt,
  Dos,
  uString,
  NumToStr;

const
  PStr  = 'NAM=COM COM=2 IRQ=3 BD=9600 BIT=8 PAR=E STOP=2 LRB=101 REC=OFF RS2=2';

var
 Com      : pChnCom;
 b        : Byte;
 w        : tChResult;

const
  UvString : String = #$1e+
                      ' test terminalu '+
                      '     Term01     '+
                      ' zadavej znaky: ';

function TstSendReady: Boolean;
begin
  TstSendReady:=false;
  while Com^.ChSendReady<>CHS_SendReady do
  begin
    if KeyPressed then
      if ReadKey=#$1B{Esc} then
      begin
        TstSendReady:=true;
        Break;
      end;
  end;
end;

function TstRecReady: Boolean;
begin
  TstRecReady:=false;
  while Com^.ChReceiveReady<>CHS_ReceiveReady do
  begin
    if KeyPressed then
      if ReadKey=#$1B{Esc} then
      begin
        TstRecReady:=true;
        Break;
      end;
  end;
end;

Begin
  DirectVideo:=false;
  WriteLn;
  WriteLn('T E S T - jednotky ChnCom');

  { inicializace Com }
  Com:=New(pChnCom,Init);
  if Com=nil then RunError(203);
  with Com^ do
  begin
    WriteLn(PStr);
    ChSetParam(PStr);
    WriteLn(ChGetParam(PStr));
    ChOpen;
    ChConnect;

    if not TstSendReady then
    begin
      ChSend(@UvString[1],Length(UvString));
      if not TstSendReady then
      begin
        Delay(100);
        repeat
          if TstRecReady then Break;
          b:=ChReceiveChar;
          if b in [$20..$7e] then Write(Char(b));
          w:=ChReceiveResult;
          if w<>res_Ok then Write('<'+WordStrHex(w)+'>');
          if TstSendReady then Break;
          ChSend(@b,1);
          if TstSendReady then Break;
        until false;
        WriteLn;
      end;
    end;
    ChDisConnect;
    ChClose;
    Done;
  end;

  WriteLn;
  Write  ('     --- K O N E C ---');
  ReadLn;
End.
