program PokKlic;

{$define VerPC}
uses
  ChnVirt,
  ChnCom,
  ChnItch1,
  NumToStr,
  Crt,
  uString;

var
  Chn     : PChnVirt; {ukazatel na komunikacni objekt}
  KlicBuff: tRecBuff; {prijimaci buffer}
  PomStr  : string;   {pomocny retezec pro precteni parametru kanalu}
  Delka   : word;     {delka prijate zpravy}
  i       : byte;     {pomocna pro FOR cykly}

const
 {parametry komunikacniho kanalu, jako fyzicka vrstva je zvolen standardni COM
  napr. na desce IOPCOM s bazovou adresou $300 ($2300 na KitV40/386) }
 {$ifdef VerPC}
  ParamStr:tParamStr='NAM=ITCH NAM=COM ADD=$310 IRQ=5 BD=4800 BIT=8 PAR=E STO=1 LRB=300';
 {$else}
  ParamStr:tParamStr='NAM=ITCH NAM=COM ADD=$2310 IRQ=3 BD=4800 BIT=8 PAR=E STO=1 LRB=300';
 {$endif}

begin
      {vytvoreni instance Chn a nastaveni parametru komunikace}
  Chn:=new(pChnITch,ChInitParam(ParamStr));
  with Chn^ do
  begin
    if ChResult<>res_Ok
    then WriteLn('inicializace chybna')
    else WriteLn('inicializace OK');

    PomStr:=ChGetParam('');
    WriteLn(PomStr);

    ChOpen;
    repeat
      if ChResult<>res_Ok then WriteLn('Chyba open');
    until ChReady=CHS_Open;
    if ChResult<>res_Ok then WriteLn('Chyba open');

    ChConnect;
    repeat
      if ChResult<>res_Ok then WriteLn('Chyba connect');
    until ChReady=CHS_Connect;
    if ChResult<>res_Ok then WriteLn('Chyba connect');

      {definovani mista, kam se ma prijata zprava ulozit}
    ChReceiveBuffer(@KlicBuff,SizeOf(KlicBuff));
    if ChReceiveResult<>res_Ok then WriteLn('Chyba nastaveni bufferu');

    repeat
      if ChReceiveReady=CHS_ReceiveReady {cekani na prijem zpravy}
      then
        begin
          ChReceive(Delka); {prijem zpravy}

          if ChReceiveResult<>res_Ok
          then WriteLn('Chyba prijmu')
          else
            begin
              Write('Family code: '+ByteStrHex(KlicBuff[0])+', Serial number: ');
              for i:=1 to 6
              do  Write(ByteStrHex(KlicBuff[i]));
              WriteLn;
            end;
        end
      else
        begin
          if ChReceiveResult<>res_Ok  {test na chybu prijmu}
          then WriteLn('Chyba prijmu');
        end;
    until KeyPressed;


    { ukonceni }
    ChDisConnect;
    repeat
      if ChResult<>res_Ok then WriteLn('Chyba disconnect');
    until ChReady=CHS_DisConnect;
    if ChResult<>res_Ok then WriteLn('Chyba disconnect');
    ChClose;
    repeat
      if ChResult<>res_Ok then WriteLn('Chyba close');
    until ChReady=CHS_Close;
    if ChResult<>res_Ok then WriteLn('Chyba close');
    { zruseni instance Chn }
    Dispose(Chn,Done);
  end;
end.


