{ adaptiert aus TPB - B.B. 31/10/88 }

const

{ Modem result codes }

  OKAY        = '0';                        { Command executed with no errors }
  connect300  = '1';                        { Carrier detect at 300 bps }
  RING        = '2';                        { Ring signal detected }
  NOCARRIER   = '3';                        { Carrier lost or never heard }
  ERROR       = '4';                        { Error in command execution }
  connect1200 = '5';                        { Carrier detect at 1200 bps }
  modem_answer = FALSE;
  init_str = 'AT X1 V0 E1 M0 S0=0 S2=3 |';



{ Modem routines}

function mdresult(secs : Integer) : Str3;
   {get result code from mdodem with timeout}

var
   count : Real;
   ch : Char;
   result : Str3;

begin
   result := '';
   count := secs/0.001;
   repeat
      repeat
         count := count-1.0;
         Delay(1);
      until (Ch_Inprdy) or (count <= 0);
      if count > 0 then
         begin
            ch := Chr(Ch_Inp);
            if ch in ['0'..'9'] then
               result := result+ch;
            if Length(result) > 2 then
               Delete(result, 1, 1);
         end;
   until ((ch = CR) and (Length(result) > 0)) or (count <= 0);
   if count > 0 then
      mdresult := result
   else
      mdresult := '';
end;



procedure mdsend(st : StrStd);
   { Send a command string to the modem  }

var
   i, n : Byte;

begin
   while Ch_Inprdy do
      i := Ch_Inp;            {clear buffer}
   for i := 1 to Length(st) do
      begin
         if st[i] = '~' then
            Delay(1000)
         else if st[i] = '|' then
            Ch_Out(Ord(CR))
         else
            Ch_Out(Ord(st[i]));
         Delay(100);
         if (Ch_Inprdy) then
            n := Ch_Inp;      {eat echo, if any}
      end;
end;



procedure mdhangup;
   { Hangup modem }

begin
   Ch_Off;
   Delay(1000);
   Ch_On
end;



procedure mdbusy;
   { Take modem off hook to present a busy signal to incoming callers }

begin
   mdsend('AT H1 |');      { Take modem off hook }
end;



function mdring : Boolean;
   { Determine if the phone is ringing }

var
   ans : Char;
   
begin
   ans := ' ';                {initialize}
   if Ch_Inprdy then
      ans := Chr(Ch_Inp);
   if ans = RING then
      begin
         ans := Chr(Ch_Inp);  {get CR from modem}
         mdring := True;
      end
   else
      mdring := False;
end;



procedure mdans;
   { Detect and set system to rate at which modem answered phone }

var
   bt : Byte;
   result : Str3;

begin
   Delay(100);
   if (not modem_answer) then
      begin
         mdsend('AT A |');  { Let the modem answer }
         Delay(100);
         result := mdresult(15); { first check for OK response }
         if (result = OKAY) or (result = '') then
            result := mdresult(40); { get actual speed code response}
      end
   else
      result := mdresult(40);
   if result = connect300 then
      Ch_Set(300)
   else if result = connect1200 then
      Ch_Set(1200)
   else
      mdhangup;
   Delay(500);                { Make sure carrier is stable }
   while ch_inprdy do bt := ch_inp;
end;



procedure mdinit;
   { Ensure the modem is hung up, initialized, and ready to wait for a ring. }

var
   dummy, bt, i : Byte;
   result : Str3;

begin
   bt := 0;
   Delay(500);
   Ch_Init;                   { re-initialize it}
   Ch_On;
   Ch_Set(1200);              { set baud }
   Ch_Out(Ord(CR));           { get modem's attention}
   repeat
      Delay(1000);
      bt := succ(bt);
      while ch_inprdy do dummy := ch_inp;            {clear buffer}
      mdsend('AT Z |');
      delay(1000);
      while ch_inprdy do dummy := ch_inp;            {clear buffer}
      mdsend(init_str);
   until (mdresult(5) = OKAY) or (bt > 4);
   if bt > 4 then
      begin
         putstat('Modem-Initialisierungsfehler ...');
         Delay(7500);
      end;
end;

