home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / communic / tp55 / snd&rec.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-08-23  |  22.0 KB  |  994 lines

  1. program send_and_receive;
  2.  
  3. uses   dos,crt;
  4.  
  5. const  max_buffer  =4096;  { Grösse der Sende- und Empfangspuffer }
  6.        timeout_len = 900;  { 50 sek ist die TIMEOUT Zeit }
  7.  
  8.        intctrl     = $21;
  9.        rs8259      = $20;
  10.  
  11.        enable_IRQ4 = $EF;
  12.        enable_IRQ3 = $F7;
  13.  
  14.        ACK         = ^F;
  15.        NAK         = ^U;
  16.        XON         = ^Q;
  17.        XOFF        = ^S;
  18.  
  19.        ESC         = #27;
  20.        CR          = #13;
  21.        LF          = #10;
  22.        TAB         = #9;
  23.        BEL         = #7;
  24.        BLANK       = #32;
  25.        CARET       = #94;
  26.  
  27.        PROGID      = 'SND&REC V1.01 (c) 1992-1993  Peter Sieg';
  28.  
  29. type   buff_type   = array[0..max_buffer] of char;
  30.  
  31. var    time_out                                  : boolean;
  32.        com_open                                  : boolean;
  33.        intr_pointer                              : pointer;
  34.        send_buffer,receive_buffer                : buff_type;
  35.        filvar,protfil                            : text;
  36.        int_enable_reg,modem_read_i,
  37.        line_cntr_reg,modem_cntr_reg,pzeiger,
  38.        pkopf,dataport,modem_line_status          : word;
  39.  
  40.        int_com                                   : byte;
  41.        com_IRQ,com_BASE,com_INTR                 : array[1..2] of word;
  42.        timer                                     : longint absolute $0:$46C;
  43.        ch,ch1                                    : char;
  44.        PARFILE                                   : string;
  45.        EDITOR                                    : string;
  46.        filename                                  : string;
  47.        line                                      : string;
  48.        answer                                    : string;
  49.        c                                         : char;
  50.        b                                         : byte absolute c;
  51.        COMPORT,BAUDRATE,PARITY,DATABITS,STOPBITS : word;
  52.        BITMASK,EIGHTBIT,CR_CRLF                  : word;
  53.        PROTOKOL                                  : word;
  54.  
  55. { ------------------------ datei --------------------------- }
  56.  
  57. function datei(name : string) : boolean;
  58.  
  59. var filvar  : text;
  60.     ioerror : word;
  61.  
  62. begin
  63.   assign(filvar,name);
  64.   {$I-};
  65.   reset(filvar);
  66.   {$I+};
  67.   ioerror:=ioresult;
  68.   datei:=(ioerror=0);
  69.   if ioerror=0 then close(filvar);
  70. end;
  71.  
  72. { --------- clear_receive_buffer --------- }
  73.  
  74. procedure clear_receive_buffer;
  75.  
  76. begin
  77.   pzeiger:=0; pkopf:=0;
  78. end;
  79.  
  80. { ----------- send_com_buffer -------------- }
  81.  
  82. { Sende Puffer ab einer bestimmten Position }
  83.  
  84. procedure send_com_buffer(ab : word);
  85.  
  86. var  start_time : longint;
  87.  
  88. begin
  89.   repeat
  90.     { Teste ob gesendet werden kann }
  91.     start_time:=timer; time_out:=false;
  92.     repeat
  93.     until ((port[modem_line_status] and $20)=$20) or (timer-start_time>timeout_len);
  94.  
  95.     if timer-start_time<timeout_len then
  96.       port[dataport]:=ord(send_buffer[ab])
  97.     else
  98.       time_out:=true;
  99.  
  100.     inc(ab);
  101.   until (ab>max_buffer) or (send_buffer[ab]=#0) or time_out;
  102. end;
  103.  
  104. { --------- send_string ---------- }
  105.  
  106. procedure send_string(zk : string);
  107.  
  108. { Sende einen String }
  109.  
  110. var len : byte;
  111.  
  112. begin
  113.   len:=length(zk);
  114.   time_out:=false;
  115.   move(zk[1],send_buffer[1],len);
  116.   send_buffer[len+1]:=#0;
  117.   send_com_buffer(1);
  118. end;
  119.  
  120. { ------------ put_chr ----------- }
  121.  
  122. { Lege ein Zeichen im Puffer ab }
  123.  
  124. procedure put_chr;
  125.  
  126. var wert : char;
  127.  
  128. begin
  129.   wert:=chr(port[dataport]);  { Hole Zeichen }
  130.  
  131.   { Lege Zeichen in Puffer ab }
  132.  
  133.   receive_buffer[pkopf]:=wert;
  134.   inc(pkopf);
  135.   if pkopf>max_buffer then pkopf:=0;
  136. end;
  137.  
  138. { ---------- getchar --------- }
  139.  
  140. { Hole ein Zeichen aus dem Empfangspuffer }
  141.  
  142. function getchar : char;
  143.  
  144. begin
  145.   if pzeiger<>pkopf then
  146.     begin
  147.       getchar:=receive_buffer[pzeiger];
  148.       inc(pzeiger);
  149.       if pzeiger>max_buffer then pzeiger:=0;
  150.     end
  151.   else
  152.     getchar:=#0;
  153. end;
  154.  
  155. { -------- charready -------- }
  156.  
  157. function charready : boolean;
  158.  
  159. { Ist ein neues Zeichen im Puffer ? }
  160.  
  161. begin
  162.   charready:=pkopf<>pzeiger;
  163. end;
  164.  
  165. { ------- receive_string ------- }
  166.  
  167. function receive_string : string;
  168.  
  169. var ch         : char;
  170.     start_time : longint;
  171.     answer     : string;
  172.  
  173. begin
  174.   answer:='';
  175.   time_out:=false; start_time:=timer;
  176.   repeat
  177.     ch:=getchar;
  178.  
  179.     if ch>#31 then
  180.       begin
  181.         answer:=answer+ch;
  182.         start_time:=timer;
  183.       end;
  184.     if timer-start_time>timeout_len then time_out:=true;
  185.  
  186.   until (ch=CR) or time_out;
  187.   receive_string:=answer;
  188. end;
  189.  
  190. { ------- wait_string_until ------- }
  191.  
  192. function wait_string_until(c : char) : string;
  193.  
  194. var ch         : char;
  195.     start_time : longint;
  196.     answer     : string;
  197.  
  198. begin
  199.   answer:='';
  200.   time_out:=false; start_time:=timer;
  201.   repeat
  202.     ch:=getchar;
  203.  
  204.     if ch>#31 then
  205.       begin
  206.         answer:=answer+ch;
  207.         start_time:=timer;
  208.       end;
  209.     if timer-start_time>timeout_len then time_out:=true;
  210.  
  211.   until (ch=c) or (ch=CR) or time_out or keypressed;
  212.   wait_string_until:=answer;
  213. end;
  214.  
  215. { ------------------ Interrupt-Handler ----------------------- }
  216.  
  217. procedure inthandler; interrupt;
  218.  
  219. begin
  220.   inline($fb); { Gebe Interrupts frei }
  221.   put_chr;
  222.   port[rs8259]:=$20;
  223.   inline($fa); { Sperre Interupts }
  224. end;
  225.  
  226. { -------- close_com ------- }
  227.  
  228. procedure close_com;
  229.  
  230. begin
  231.   if com_open = false then exit;
  232.  
  233.   { Stelle alten Interruptvektor von com wieder her }
  234.  
  235.   inline($FA);
  236.  
  237.   port[modem_cntr_reg]:=0;
  238.   port[intctrl]:=port[intctrl] or $18;
  239.   port[int_enable_reg]:=0;
  240.   (* clear pending interupts *)
  241.   while (port[modem_read_i] and 1) = 0 do
  242.   begin
  243.     b:=port[modem_line_status];
  244.     b:=port[dataport+6];
  245.     b:=port[dataport];
  246.     port[rs8259]:=$20;
  247.   end;
  248.  
  249.   setintvec(int_com,intr_pointer);
  250.   inline($FB);
  251.   (*
  252.   { Interrupts an 8259 freigeben }
  253.   port[rs8259]:=$20;
  254.   *)
  255.   com_open := false;
  256. end;
  257.  
  258. { ------- Initialisiere Schnittstelle -------- }
  259.  
  260. procedure open_com(com_nr,baud,parity,databits,stopbits,bitmask : word);
  261.  
  262. var  wert,enable_IRQ   : byte;
  263.      zeiger            : pointer;
  264.      base              : word;
  265.  
  266. procedure com_init(baud,parity,databits,stopbits,bitmask : word);
  267.  
  268. { Einen COM Anschluss initialisieren }
  269.  
  270. begin
  271.   case baud of
  272.      600 : baud:=192;
  273.     1200 : baud:=96;
  274.     2400 : baud:=48;
  275.     4800 : baud:=24;
  276.     9600 : baud:=12;
  277.    19200 : baud:=6;
  278.   end;
  279.  
  280.   port[line_cntr_reg]:=$80; { DLAB = 1 }
  281.   port[dataport]:=baud;
  282.  
  283.   { DLAB = 0 }
  284.  
  285.   port[line_cntr_reg]:=databits-5+(stopbits-1)*4+byte((parity>0))*8+byte(parity=2)*16;
  286. end;
  287.  
  288. begin
  289.   if com_open = true then close_com;
  290.  
  291.   { Setze Pufferzeiger auf Anfang }
  292.  
  293.   pzeiger:=0; pkopf:=0;
  294.  
  295.   base:=com_BASE[com_nr];
  296.   int_com:=com_INTR[com_nr];
  297.  
  298.   if com_IRQ[com_nr]=4 then
  299.     enable_IRQ:=enable_IRQ4
  300.   else
  301.     enable_IRQ:=enable_IRQ3;
  302.  
  303.   { Setze Variablen auf }
  304.  
  305.   dataport         :=base;
  306.   int_enable_reg   :=base+1;
  307.   modem_read_i     :=base+2;
  308.   line_cntr_reg    :=base+3;
  309.   modem_cntr_reg   :=base+4;
  310.   modem_line_status:=base+5;
  311.  
  312.   com_init(baud,parity,databits,stopbits,bitmask);
  313.  
  314.   { Initialisiere 8250 }
  315.  
  316.   inline($FA);
  317.  
  318.   wert:=port[intctrl];
  319.   port[intctrl]:=wert and enable_IRQ;
  320.  
  321.   { 'Data ready' Interrupt auf 8250 einschalten }
  322.  
  323.   port[int_enable_reg]:=1;        { Data-Ready einschalten }
  324.   port[modem_cntr_reg]:=bitmask;  { RTS,DTR & OUT2 einschalten }
  325.   port[modem_line_status]:=$60;
  326.   port[base+7]:=15;
  327.  
  328.   { Interrupt einhängen für com }
  329.  
  330.   getintvec(int_com,intr_pointer);
  331.   zeiger:=@inthandler;
  332.   setintvec(int_com,zeiger);
  333.   inline($FB);
  334.  
  335.   com_open:=true;
  336. end;
  337.  
  338. { ---------- wait_for_ack --------- }
  339.  
  340. procedure wait_for_ack;
  341.  
  342. var start_time : longint;
  343.  
  344. begin
  345.   start_time:=timer;
  346.   repeat
  347.   until (getchar=ACK) or (timer-start_time>timeout_len);
  348.  
  349.   if timer-start_time>timeout_len then time_out:=true;
  350. end;
  351.  
  352. { -------- name_file ------- }
  353.  
  354. procedure name_file;
  355.  
  356. begin
  357.   writeln;
  358.   write('Input Name : ');
  359.   readln(filename);
  360.   writeln(^J);
  361. end;
  362.  
  363. { -------- send_file ------- }
  364.  
  365. procedure send_file;
  366.  
  367. var ch         : char;
  368.     sendfil    : text;
  369.     line       : string;
  370.     line_count : word;
  371.  
  372. begin
  373.   line_count:=0; time_out:=false;
  374.   if filename<>'' then
  375.     if not datei(filename) then
  376.       begin
  377.         writeln('File ',filename,' not found !!');
  378.         ch:=readkey;
  379.       end
  380.     else
  381.       begin
  382.         assign(sendfil,filename);
  383.         reset(sendfil);
  384.  
  385.         repeat
  386.           readln(sendfil,line);
  387.           send_string(line+CR);
  388.           inc(line_count);
  389.           write('Send Line : ',line_count:5,^M);
  390.           delay(100);
  391.           clear_receive_buffer;
  392.         until eof(sendfil) or time_out or keypressed;
  393.  
  394.         close(sendfil);
  395.         if keypressed then ch:=readkey;
  396.       end;
  397. end;
  398.  
  399.  
  400. { -------- terminal mode --------- }
  401.  
  402. procedure terminal_mode;
  403.  
  404. begin
  405.   time_out:=false;
  406.   clear_receive_buffer;
  407.   line:='';
  408.   repeat
  409.     while charready do
  410.     begin
  411.       c:=getchar;
  412.       if EIGHTBIT=0 then b:=b and $7F;
  413.       if not (c in [XON,XOFF]) then
  414.       begin
  415.         write(c);
  416.         if PROTOKOL<>0 then write(protfil,c);
  417.         if (CR_CRLF<>0) and (c=CR) then write(LF);
  418.       end;
  419.     end;
  420.     c:=CR;
  421.     if keypressed then
  422.     begin
  423.       c:=readkey;
  424.       if c=CR then
  425.       begin
  426.         send_string(line+CR);
  427.         line:='';
  428.       end
  429.       else
  430.       begin
  431.         line:=line+c;
  432.       end;
  433.       if c<>ESC then write(c);
  434.     end;
  435.   until c=ESC;
  436. end;
  437.  
  438.  
  439. { -------- metec mode --------- }
  440.  
  441. procedure metec_mode;
  442.  
  443. var delay_ms : word;
  444.  
  445. begin
  446.   writeln;
  447.   writeln('Connect the Metec M-3650 CR Digital Multimeter to your serial');
  448.   writeln('interface and select the range you want. To end press any key.');
  449.   writeln;
  450.   write  ('Enter the delay time between two data transmissions (ms): ');
  451.   readln(delay_ms);
  452.   writeln;
  453.  
  454.   time_out:=false;
  455.   clear_receive_buffer;
  456.  
  457.   repeat
  458.     while charready do
  459.     begin
  460.       c:=getchar;
  461.       if EIGHTBIT=0 then b:=b and $7F;
  462.       if not (c in [XON,XOFF]) then
  463.       begin
  464.         write(c);
  465.         if PROTOKOL<>0 then write(protfil,c);
  466.         if (CR_CRLF<>0) and (c=CR) then write(LF);
  467.       end;
  468.     end;
  469.     send_string('D'+CR);
  470.     delay(delay_ms);
  471.   until keypressed;
  472.   while keypressed do c:=readkey;
  473. end;
  474.  
  475.  
  476. { -------- download_tsm ------- }
  477.  
  478. procedure download_tsm;
  479.  
  480. var ch         : char;
  481.     downfil    : text;
  482.     line       : string;
  483.     line_count : word;
  484.     InWord     : boolean;
  485.  
  486. begin
  487.   line_count:=0; time_out:=false; InWord:=false;
  488.   if filename<>'' then
  489.     if not datei(filename) then
  490.       begin
  491.         writeln('File ',filename,' not found !!');
  492.         ch:=readkey;
  493.       end
  494.     else
  495.       begin
  496.         assign(downfil,filename);
  497.         reset(downfil);
  498.  
  499.         repeat
  500.           readln(downfil,line);
  501.           send_string(line+CR);
  502.           inc(line_count);
  503.           write('Send Line : ',line_count:5,^M);
  504.           answer:=receive_string;
  505.           if pos(':',line) > 0 then InWord:=true;
  506.           if pos(';',line) > 0 then InWord:=false;
  507.           if
  508.              (pos('END-DEFINE',line) > 0) or
  509.              (pos('CONSTANT',line) > 0) or
  510.              (pos('LABEL',line) > 0) or
  511.              ((pos(':=',line) > 0) and not InWord) or
  512.              (pos(';',line) > 0)
  513.           then
  514.           begin
  515.             if pos('OK',answer) = 0 then
  516.             begin
  517.               writeln(^J,line,answer);
  518.               exit;
  519.             end;
  520.           end;
  521.         until eof(downfil) or time_out;
  522.  
  523.         close(downfil);
  524.       end;
  525. end;
  526.  
  527.  
  528. { -------- edit_file ------- }
  529.  
  530. procedure edit_file;
  531.  
  532. var  filvar     : text;
  533.  
  534. begin
  535.   if filename<>'' then
  536.       begin
  537.         exec(EDITOR,filename);
  538.       end;
  539. end;
  540.  
  541.  
  542. { -------- exec_command ------- }
  543.  
  544. procedure exec_command;
  545.  
  546. var  filvar     : text;
  547.      name       : string;
  548.      line       : string;
  549.  
  550. begin
  551.   write('Input complete path and name: ');
  552.   readln(name);
  553.   if name<>'' then
  554.       begin
  555.         write('Input command line parameter: ');
  556.         readln(line);
  557.         exec(name,line);
  558.       end;
  559. end;
  560.  
  561. { -------- read_parameter ------- }
  562.  
  563. procedure read_parameter;
  564.  
  565. var ch         : char;
  566.     filvar     : text;
  567.     line       : string;
  568.     status     : integer;
  569.  
  570. begin
  571.     if not datei(PARFILE) then
  572.       begin
  573.         writeln('File ',PARFILE,' not found !!');
  574.         ch:=readkey;
  575.       end
  576.     else
  577.       begin
  578.         assign(filvar,PARFILE);
  579.         reset(filvar);
  580.  
  581.         readln(filvar,line);
  582.         if line <> PROGID then
  583.           begin
  584.             writeln('File ',PARFILE,' wrong program id!!');
  585.             ch:=readkey;
  586.             exit;
  587.           end;
  588.  
  589.         readln(filvar,line);
  590.         val(line,COMPORT,status);
  591.  
  592.         readln(filvar,line);
  593.         val(line,BAUDRATE,status);
  594.  
  595.         readln(filvar,line);
  596.         val(line,PARITY,status);
  597.  
  598.         readln(filvar,line);
  599.         val(line,DATABITS,status);
  600.  
  601.         readln(filvar,line);
  602.         val(line,STOPBITS,status);
  603.  
  604.         readln(filvar,line);
  605.         val(line,BITMASK,status);
  606.  
  607.         readln(filvar,line);
  608.         val(line,EIGHTBIT,status);
  609.  
  610.         readln(filvar,line);
  611.         val(line,CR_CRLF,status);
  612.  
  613.         readln(filvar,EDITOR);
  614.  
  615.         readln(filvar,filename);
  616.  
  617.         close(filvar);
  618.  
  619.         close_com;
  620.         open_com(COMPORT,BAUDRATE,PARITY,DATABITS,STOPBITS,BITMASK);
  621.  
  622.       end;
  623. end;
  624.  
  625.  
  626. { -------- save_parameter ------- }
  627.  
  628. procedure save_parameter;
  629.  
  630. var   filvar     : text;
  631.  
  632. begin
  633.         assign(filvar,PARFILE);
  634.         rewrite(filvar);
  635.  
  636.         writeln(filvar,PROGID);
  637.         writeln(filvar,COMPORT);
  638.         writeln(filvar,BAUDRATE);
  639.         writeln(filvar,PARITY);
  640.         writeln(filvar,DATABITS);
  641.         writeln(filvar,STOPBITS);
  642.         writeln(filvar,BITMASK);
  643.         writeln(filvar,EIGHTBIT);
  644.         writeln(filvar,CR_CRLF);
  645.         writeln(filvar,EDITOR);
  646.         writeln(filvar,filename);
  647.  
  648.         close(filvar);
  649. end;
  650.  
  651.  
  652. { -------- edit_parameter ------- }
  653.  
  654. procedure edit_parameter;
  655.  
  656. begin
  657.         writeln;
  658.         write('COMPORT : '); readln(COMPORT);
  659.         write('BAUDRATE: '); readln(BAUDRATE);
  660.         write('PARITY  :   (0=N/1=E)',CR,'PARITY  : '); readln(PARITY);
  661.         write('DATABITS: '); readln(DATABITS);
  662.         write('STOPBITS: '); readln(STOPBITS);
  663.         write('BITMASK :   (11/9)',CR,'BITMASK : '); readln(BITMASK);
  664.         write('EIGHTBIT: '); readln(EIGHTBIT);
  665.         write('CR->CRLF: '); readln(CR_CRLF);
  666.         write('EDITOR  : '); readln(EDITOR);
  667.         close_com;
  668.         open_com(COMPORT,BAUDRATE,PARITY,DATABITS,STOPBITS,BITMASK);
  669. end;
  670.  
  671. { -------- Parameter -------- }
  672.  
  673. procedure parameter;
  674.  
  675. var ch         : char;
  676.     line       : string;
  677.  
  678. begin
  679.   repeat
  680.     writeln;
  681.     write  ('s. Save Parameter (');
  682.     write  (PARFILE,',',COMPORT,',',BAUDRATE,',',PARITY,',',DATABITS,',',STOPBITS,',');
  683.     writeln(BITMASK,',',EIGHTBIT,',',CR_CRLF,',',EDITOR,')');
  684.     writeln('e. Edit Parameter');
  685.     writeln('r. Read Parameter');
  686.     writeln('n. Name Parm-File (',PARFILE,')');
  687.     writeln;
  688.     writeln('0. Exit Parameter');
  689.  
  690.     ch:=readkey;
  691.  
  692.     case ch of
  693.      's' : save_parameter;
  694.      'e' : edit_parameter;
  695.      'r' : read_parameter;
  696.      'n' : begin
  697.              line:=filename;
  698.              name_file;
  699.              if filename<>'' then PARFILE:=filename;
  700.              filename:=line;
  701.            end;
  702.     end;
  703.  
  704.   until ch in ['0',ESC];
  705. end;
  706.  
  707. { -------- protokoll ------- }
  708.  
  709. procedure protokoll;
  710.  
  711. var ch         : char;
  712.  
  713. begin
  714.   if PROTOKOL=0 then
  715.   begin
  716.     if filename<>'' then
  717.     begin
  718.       (*
  719.       if datei(filename) then
  720.         begin
  721.           writeln('File ',filename,' already exist !!');
  722.           writeln('Press <ESC> to cancel.');
  723.           ch:=readkey;
  724.           if ch=ESC then exit;
  725.         end;
  726.       *)
  727.       assign(protfil,filename);
  728.       rewrite(protfil);
  729.       PROTOKOL:=1;
  730.     end;
  731.   end
  732.   else
  733.   begin
  734.     close(protfil);
  735.     PROTOKOL:=0;
  736.   end;
  737. end;
  738.  
  739. { -------- exec_script -------- }
  740.  
  741. procedure exec_script;
  742.  
  743. var  thiscode   : char;
  744.      linkcode   : char;
  745.      thisline   : string;
  746.      answer     : string;
  747.      filvar     : text;
  748.      ch         : char;
  749.      waittime   : integer;
  750.      n          : integer;
  751.  
  752. begin
  753.   time_out:=false;
  754.   clear_receive_buffer;
  755.   if filename<>'' then
  756.     if not datei(filename) then
  757.       begin
  758.         writeln('File ',filename,' not found !!');
  759.         ch:=readkey;
  760.       end
  761.     else
  762.       begin
  763.         assign(filvar,filename);
  764.         reset(filvar);
  765.         repeat
  766.           readln(filvar,thisline);
  767.           thiscode:=thisline[1];
  768.           linkcode:=thisline[2];
  769.           delete(thisline,1,2);
  770.           writeln(thiscode,linkcode:1,thisline);
  771.  
  772.           case thiscode of
  773.             '#' : begin end; (* Only comment *)
  774.             'n' : if (thisline<>'') then filename:=thisline
  775.                   else name_file;
  776.             'P' : protokoll;
  777.             'p' : begin
  778.                     PARFILE:=thisline;
  779.                     read_parameter;
  780.                   end;
  781.             'd' : download_tsm;
  782.             's' : send_file;
  783.             '<' : begin
  784.                     if (linkcode=CARET)
  785.                       then thisline:=chr(ord(upcase(thisline[1]))-64);
  786.                     n:=pos('???',thisline);
  787.                     if n>0 then
  788.                     begin
  789.                       delete(thisline,n,3);
  790.                       insert(filename,thisline,n);
  791.                     end;
  792.                     send_string(thisline+CR)
  793.                   end;
  794.             'w' : begin
  795.                     val(thisline,waittime,n);
  796.                     delay(waittime);
  797.                     clear_receive_buffer;
  798.                   end;
  799.             '>' : repeat
  800.                     if linkcode=BLANK then linkcode:=CR;
  801.                     answer:=wait_string_until(linkcode);
  802.                     if PROTOKOL<>0 then writeln(protfil,answer);
  803.                     writeln('= ',answer);
  804.                   until (pos(thisline,answer)>0) or time_out or keypressed;
  805.           end;
  806.         until eof(filvar) or time_out or keypressed;
  807.         close(filvar);
  808.         if keypressed then ch:=readkey;
  809.       end;
  810. end;
  811.  
  812.  
  813. { -------- MasterMind -------- }
  814.  
  815. procedure mastermind;
  816.  
  817. var
  818. i,j,
  819. Zahl,
  820. Versuch                  :  integer;
  821. Taste                    :  char;
  822. MeineZahl,DeineZahl      :  string(.4.);
  823.  
  824.  
  825. function ZufallsZiffer   :  integer;
  826. (*
  827. Gibt eine zufaellige Ziffer zwischen 1 und 9 zurueck
  828. *)
  829.  
  830. var
  831. x                        :  integer;
  832.  
  833. begin
  834.   x                      := random(9);
  835.   x                      := succ(x);
  836.   ZufallsZiffer          := x;
  837. end;
  838.  
  839. function ZufallsZahl     :  integer;
  840. (*
  841. Gibt eine vierstellige Zufallszahl ohne Nullen und doppelte Ziffern zurueck
  842. *)
  843.  
  844. var
  845. Ziffer1,Ziffer2,
  846. Ziffer3,Ziffer4          :  integer;
  847.  
  848. begin
  849.   Ziffer1                := ZufallsZiffer;
  850.  
  851.   repeat
  852.     Ziffer2              := ZufallsZiffer;
  853.   until Ziffer2 <> Ziffer1;
  854.  
  855.   repeat
  856.     Ziffer3              := ZufallsZiffer;
  857.   until ((Ziffer3 <> Ziffer2) and (Ziffer3 <> Ziffer1));
  858.  
  859.   repeat
  860.     Ziffer4              := ZufallsZiffer;
  861.   until ((Ziffer4 <> Ziffer3) and (Ziffer4 <> Ziffer2)
  862.                               and (Ziffer4 <> Ziffer1));
  863.   ZufallsZahl            := Ziffer4*1000+Ziffer3*100+Ziffer2*10+Ziffer1;
  864.  
  865. end;
  866.  
  867.  
  868. begin
  869.   close_com;
  870.   repeat
  871.     writeln;
  872.     writeln('MasterMind: Guess my 4-digit #.');
  873.     writeln('(+ = right digit but wrong pos.; # = right digit and position)');
  874.     randomize;
  875.  
  876.     Zahl                 := ZufallsZahl;
  877.  
  878.     str(Zahl:4,MeineZahl);
  879.  
  880.     Versuch              := 1;
  881.     repeat
  882.       (*$i-*)
  883.       repeat
  884.         writeln; clreol;
  885.         write  (Versuch:3,' : ');
  886.         read   (Zahl);
  887.       until ((Zahl > 1222) and (Zahl < 9888) and (ioresult = 0));
  888.       (*$i+*)
  889.  
  890.       str(Zahl:4,DeineZahl);
  891.  
  892.       for i              := 1 to 4 do
  893.       begin
  894.         if DeineZahl[i] = MeineZahl[i] then write  ('# ')
  895.         else
  896.         begin
  897.           for j          := 1 to 4 do
  898.           begin
  899.             if DeineZahl[j] = MeineZahl[i] then write  ('+ ');
  900.           end;
  901.         end;
  902.       end;
  903.  
  904.       Versuch            := succ(Versuch);
  905.  
  906.     until DeineZahl = MeineZahl;
  907.  
  908.     writeln;
  909.     writeln(BEL);
  910.     writeln('You made it. Again ?');
  911.     repeat
  912.       repeat
  913.       until keypressed;
  914.       Taste              := readkey;
  915.       Taste              := upcase(Taste);
  916.     until (Taste in ['Y','N']);
  917.  
  918.   until Taste = 'N';
  919.   open_com(COMPORT,BAUDRATE,PARITY,DATABITS,STOPBITS,BITMASK);
  920. end;
  921.  
  922.  
  923. { -------- Hauptprogramm ------- }
  924.  
  925. begin
  926.   DirectVideo:=False;
  927.  
  928.   com_open:=false;
  929.   com_IRQ[1]:=4;     com_IRQ[2]:=3;
  930.   com_BASE[1]:=$3F8; com_BASE[2]:=$2F8;
  931.   com_INTR[1]:=$0C;  com_INTR[2]:=$0B;
  932.   COMPORT:=1;
  933.   BAUDRATE:=9600;
  934.   PARITY:=0;
  935.   DATABITS:=8;
  936.   STOPBITS:=1;
  937.   BITMASK :=$0B;
  938.   EIGHTBIT:=0;
  939.   CR_CRLF :=1;
  940.   PROTOKOL:=0;
  941.   PARFILE:='SND&REC.PAR';
  942.   EDITOR:='TED.COM';
  943.   filename:='';
  944.  
  945.   read_parameter;
  946.  
  947.   repeat
  948.     time_out:=false;
  949.  
  950.     writeln;
  951.     write  ('p. Parameter (');
  952.     write  (PARFILE,',',COMPORT,',',BAUDRATE,',',PARITY,',',DATABITS,',',STOPBITS,',');
  953.     writeln(BITMASK,',',EIGHTBIT,',',CR_CRLF,',',EDITOR,')');
  954.     writeln('P. Protokol  (',PROTOKOL,' --> ',filename,')');
  955.     writeln('t. Terminal Mode');
  956.     writeln('d. Download TSM');
  957.     writeln('m. Metec 3650CR Mode');
  958.     writeln('s. Send ASCII File');
  959.     writeln('x. Execute Script');
  960.     writeln('e. Edit File');
  961.     writeln('X. Execute Command');
  962.     writeln('n. Name File (',filename,')');
  963.     writeln('M. MasterMind');
  964.     writeln;
  965.     writeln('0. Exit Program');
  966.  
  967.     ch:=readkey;
  968.  
  969.     case ch of
  970.      'p' : parameter;
  971.      'P' : protokoll;
  972.      't' : terminal_mode;
  973.      'd' : download_tsm;
  974.      'm' : metec_mode;
  975.      's' : send_file;
  976.      'x' : exec_script;
  977.      'e' : edit_file;
  978.      'n' : name_file;
  979.      'X' : exec_command;
  980.      'M' : mastermind;
  981.     end;
  982.  
  983.     if time_out then
  984.       begin
  985.         writeln(^J^M,'Timeout occured !!');
  986.         ch1:=readkey;
  987.       end;
  988.  
  989.   until ch = '0';
  990.   close_com;
  991.   PARFILE:='SND&REC.PAR';
  992.   save_parameter;
  993. end.
  994.