
program comlink;
uses yasync, crt, dos;

const
  incoming_modem_port = 2;   { These are defined here! }
  outgoing_modem_port = 1;

label
  the_loop, done;

var
  lf,cr,one_char : char;
  print_to_screen_flag : boolean;


{------------------------------------}
procedure send_to_modem(the_port : integer; the_string : string);
var a_char : char;
temp : integer;
begin
for temp := 1 to length(the_string) do
  begin
  a_char := the_string[temp];
  if not sendport(the_port,a_char) then
    begin
    writeln('Error sending string to modem on port',the_port,'.');
    halt(1);
    end;
  end;
end;

procedure wait(seconds : integer);
label loop;
var hh,hh2,mm,mm2,ss,ss2,hu,hu2 : word;
begin
gettime(hh,mm,ss,hu);  { Get the original time. }
hh2 := hh;
mm2 := mm;
ss2 := ss + seconds;
hu2 := hu;
while ss2 > 59 do
  begin
  ss2 := ss2 - 60;
  mm2 := mm2 + 1;
  end;
while mm2 > 59 do
  begin
  mm2 := mm2 - 60;
  hh2 := hh2 + 1;
  end;
if hh2 > 23 then
  hh2 := hh2 - 24;
gettime(hh,mm,ss,hu);
while (hh2 = 0) and (hh > 0) do { If we crossed a day, then wait till midnight.}
  gettime(hh,mm,ss,hu);
loop:
gettime(hh,mm,ss,hu);
if hh > hh2 then exit;
if (hh = hh2) and (mm > mm2) then exit;
if (hh = hh2) and (mm = mm2) and (ss > ss2) then exit;
if (hh = hh2) and (mm = mm2) and (ss = ss2) and (hu > hu2) then exit;
goto loop;
end;

{----------------------------------------------------------}
begin

lf := chr(10);
cr := chr(13);
print_to_screen_flag := FALSE;
writeln('Ctrl-A to end; Ctrl-B to toggle screen logging....');
writeln('Screen logging is now OFF.');

asyncport[incoming_modem_port].rtshand := TRUE;
if not openport(incoming_modem_port,19200,8,1,'N') then
  begin
  writeln('??? ERROR OPENING COM',incoming_modem_port,': ',portopenerror);
  halt(1);
  end;
asyncport[outgoing_modem_port].rtshand := TRUE;
if not openport(outgoing_modem_port,19200,8,1,'N') then
  begin
  writeln('??? ERROR OPENING COM',outgoing_modem_port,': ',portopenerror);
  halt(1);
  end;
setdtr(outgoing_modem_port,TRUE);    { Make sure we can use the outdialing modem. }
send_to_modem(incoming_modem_port,cr+lf+'Wait a second while I initialize the modem.'+cr+lf);
send_to_modem(outgoing_modem_port,cr);
wait(1);
send_to_modem(outgoing_modem_port,cr);
wait(1);
send_to_modem(outgoing_modem_port,'ATZ'+cr);
wait(1);
send_to_modem(outgoing_modem_port,'ATTE1'+cr);
send_to_modem(incoming_modem_port,'You are now connected to Al''s modem.  Hang up to quit.'+cr+lf+lf);

the_loop:
if keypressed then
  begin
  one_char := readkey;
  if one_char = ^A then
    goto done;
  if one_char = ^B then
    begin
    if print_to_screen_flag then
      begin
      print_to_screen_flag := FALSE;
      writeln('Screen logging is now OFF.');
      end
    else
      begin
      print_to_screen_flag := TRUE;
      writeln('Screen logging is now ON.');
      end;
    end
  else
  if not sendport(outgoing_modem_port,one_char) then
    begin
    writeln('Error sending keyboard input to outgoing port.');
    halt(1);
    end;
  end;
if readport(incoming_modem_port,one_char) then
  if not sendport(outgoing_modem_port,one_char) then
    begin
    writeln('Error sending to outgoing_modem_port.');
    halt(1);
    end;
if readport(outgoing_modem_port,one_char) then
  begin
  if not sendport(incoming_modem_port,one_char) then
    begin
    writeln('Error sending to incoming modem.');
    halt(1);
    end;
  if print_to_screen_flag then
      write(one_char);
  end;
goto the_loop;

done:
closeport(incoming_modem_port);
closeport(outgoing_modem_port);
setdtr(outgoing_modem_port,FALSE);

end.

