Unit UpDnLoad;

{***************************************************************************}

{* SayL's need to be taken out.

{***************************************************************************}



interface

{***************************************************************************}

{***************************************************************************}

Uses

  Dos,

  Disk,

  Glob,

  IOPorts,

  LCTYMBat,

  LTXMKrnl,

  LTXModem,

  Data;



type

  ModeType = (XModem, YModem, YModemB);



Function DownLoad(Mode: ModeType; FileStr: string; Port: PortType): boolean;

Function UpLoad(Mode: ModeType; FileStr: string; Port: PortType): boolean;



implementation

{***************************************************************************}

{***************************************************************************}



Function DownLoad(Mode: ModeType; FileStr: string; Port: PortType): boolean;

{***************************************************************************}

type

  GetBuffType = array[1..1024] of byte;

var

  alpha: integer;

  Count: integer;

  GetBuff,

  OldBuff: GetBuffType;

  FirstTime: boolean;

  GetResult: XMResult;

  HandShake: byte;

  NulStart: integer;

  NumBytes: integer;

  SF: file of byte;

begin

  if FileStr = '' then

    begin

      for Count := 1 to 20 do

        Say(char(CAN));

      SayL(Beep);

      SayL(Beep + 'Reception of file cancelled.  Name of file not known.');

      DownLoad := false;

      exit;

    end;

  case Mode of

    XModem:  NumBytes := 128;

    YModem:  NumBytes := 1024;

    YModemB:  begin

                DownLoad := LctYMRecv(Port);

{* Dumps in default directory *}

                exit;

              end;

  end; {case}

  Assign(SF, FileStr);

  Rewrite(SF);

  HandShake := CRCREQ;

  FirstTime := true;

  repeat

    GetResult := LxmRrec(Port, GetBuff, DumInt, RTOUT, HandShake);

    case GetResult of

      Success :

        begin

          if not(FirstTime) then

            for alpha := 1 to NumBytes do

              write(SF, OldBuff[alpha]);

          OldBuff := GetBuff;

          FirstTime := false;

        end;

      Retry   : SayL('There was a RETRY error.');

      InitFail: SayL('There was an INITIALIZATION error.');

      Error   : SayL('There was an unidentifiable ERROR.');

      CanReq  : SayL('There was a CANCEL REQUEST.');

      EndFile :

        begin

          for alpha := 1 to NumBytes do

            write(SF, OldBuff[alpha]);

          SayL('END of FILE transmission.');

        end;

      TimeOut : SayL('There was a TIME OUT error.');

      DupBlk  : SayL('DUPLICATE BLOCK error.');

      SeqErr  : SayL('SEQUENCE ERROR.');

    end; {case}

  until (GetResult <> Success) and (GetResult <> DupBlk);

  if GetResult <> EndFile then

    Say(char(CAN));

  Close(SF);

  DownLoad := GetResult = EndFile;

end;



Function UpLoad(Mode: ModeType; FileStr: string; Port: PortType): boolean;

{***************************************************************************}

var

  Count: integer;

  NumBytes: integer;

  SendBuff: array[1..1024] of byte;

  SendResult: XMResult;

  SF: file of byte;

  Temp: boolean;

  BuffCount: integer;

begin

  if not(FileExists(FileStr)) then

    begin

      for Count := 1 to 20 do

        Say(char(CAN));

      SayL(Beep);

      SayL(Beep + 'Could not locate file.');

      UpLoad := false;

      exit;

    end;

  case Mode of

    XModem:  NumBytes := 128;

    YModem:  NumBytes := 1024;

    YModemB:  begin

                Temp := LctYMSend(Port, FileStr);

                if not(Temp) then

                  begin

                    Say(char(CAN));

                    SayL(Beep);

                    SayL('YModem Batch transmission cancelled.');

                  end

                else

                  SayL(Beep + 'YModem Batch transmission successful.');

                Upload := Temp;

                exit;

              end;

  end; {case}

  UseYModem(Port, Mode = YModem);

  Assign(SF, FileStr);

  Reset(SF);

  while not(EOF(SF)) do

    begin

      BuffCount := 0;

      while not(EOF(SF)) and (BuffCount < NumBytes) do

        begin

          inc(BuffCount);

          read(SF, SendBuff[BuffCount]);

{*          if SendBuff[BuffCount] = 26 then

{* what is #26 ? *}

{*            SendBuff[BuffCount] := 0;

*}

        end;

      while BuffCount < NumBytes do

        begin

          inc(BuffCount);

          SendBuff[BuffCount] := 0;

        end;

      SendResult := LxmTrec(Port, SendBuff);

      case SendResult of

        Success:  SayL('Success');

        Retry  :  SayL('Retry');

        InitCan:  SayL('InitCan');

        CanReq :  SayL('CanReq');

      end; {case}

      if SendResult <> Success then

        begin

          SayL('Cancelling upload of "'+FileStr+'".');

          UpLoad := false;

          exit;

        end;

    end;

  Close(SF);

  SendResult := LxmTeot(Port);

  if SendResult = Success then

    SayL('Upload successfully terminated.')

  else

    SayL('Upload UNsuccessfully terminated.');

  UpLoad := SendResult = Success;

end;



{***************************************************************************}

{***************************************************************************}

{***************************************************************************}

begin

end.