{-----------------------------------------------------------------}
{ Priklad na odesilani a prijem ethernetovych ramcu primo pomoci  }
{ ovladace ethernetove karty IOETH01                              }
{-----------------------------------------------------------------}

program EthSample;

uses
  CoBase,
  CoEthDef,
  CoEth01;

const
  { Cilova adresa odesilaneho ramce }
  SAddress : TEthAddress =
    (
      Size     : SizeOf( TEthAddress );
      Mac      : (Bytes: ($FF, $FF, $FF, $FF, $FF, $FF));  { Broadcast  }
      Format   : ETH_FORMAT_ETHII;          { Format ramce Ethernet II  }
      Protocol : $4000
    );

var
  Status    : TCoStatus;              { Stav vysledku operace           }
  Dev       : PCoDevice;              { Ukazatel na instanci zarizeni   }
  FrameData : array[0..1023] of Byte; { Data ramce                      }
  RecvLen   : Word;                   { Delka prijateho ramce           }
  RAddress  : TEthAddress;            { Zdrojova adresa prijateho ramce }

  { Procedura pripravujici data odesilaneho ramce }
  procedure ProduceData;
  begin
    { ... }
  end;

  { Procedura zpracovavajici data prijateho ramce }
  procedure ConsumeData;
  begin
    { ... }
  end;

begin

  { Vytvoreni zarizeni ovladace ethernetove karty }
  { Bazova adresa radice v I/O prostoru je $2300  }
  Status := CoCreateDevice( 'ETH01', '', 'IOBASE=$2300',  nil, Dev );

  if Status <> CST_SUCCESS then
  begin
    WriteLn( 'CoCreateDevice() failed: ', CoStatusToStr( Status ) );
  end
  else begin

    { Zarizeni TCoEth01 bylo uspesne vytvoreno }
    Status := Dev^.CoBind( nil, 0, nil );

    if Status = CST_SUCCESS then
    begin

      { Zarizeni TCoEth01 bylo uspesne inicializovano,
        MAC adresa byla nastavena podle obsahuje EEPROM }

      { Vyplneni dat ramce }
      ProduceData;

      { Odeslani ramce }
      Status := Dev^.CoSendBufferTo( @FrameData, SizeOf( FrameData ), 0,
                  @SAddress, nil );

      if Status <> CST_SUCCESS then
      begin
        WriteLn( 'CoSendBufferTo() failed: ', Dev^.CoStatusToStr( Status ) );
      end;

      { Prijem ramce }
      Status := Dev^.CoRecvBufferFrom( @FrameData, SizeOf( FrameData ),
                  @RecvLen, 0, @RAddress, nil );

      if Status <> CST_SUCCESS then
      begin
        WriteLn( 'CoSendBufferFrom() failed: ', Dev^.CoStatusToStr( Status ) );
      end
      else begin
        ConsumeData;
      end;

      { Deinicializace zarizeni }
      Status := Dev^.CoUnbind( 0, nil );

      if Status <> CST_SUCCESS then
      begin
        WriteLn( 'CoUnbind() failed: ', Dev^.CoStatusToStr( Status ) );
      end;

    end
    else begin
      WriteLn( 'CoBind() failed: ', Dev^.CoStatusToStr( Status ) );
    end;

    { Uvolneni ukazatele na zarizeni, zruseni instance zarizeni }
    CoReleaseDevicePtr( Dev );
  end;

end.
