home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / PASCAL / COMM_TP5.ZIP / COMM_TTY.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1989-11-10  |  30.4 KB  |  279 lines

  1. PROGRAM Comm_TTY;                                                               {-This is the comment column }
  2.                                                                                 { which will be used to tell }
  3. {         Comm_TTY.PAS Ver. 2.00 - Demo for Comm_TP4.PAS TPU Routines         } { what the program is doing  }
  4. {                             (c) Copyright, 1989                             } { where its not self-evident }
  5. {                              Kevin R. Bulgrien                              } {                            }
  6. {                                October, 1989                                } { Yes, it is wider than 80   }
  7. {                                                                             } { columns so you will need   }
  8. { See the accompanying file COMM_TP4.DOC for specific information regarding   } { to use compressed print to }
  9. { the distribution policies and usage information for this source code file.  } { print it out.              }
  10. {                                                                             } {                            }
  11. { Written by:  Kevin R. Bulgrien           Version 1.00 completed 10/24/89    } { I hope you don't complain  }
  12. {                                                                             } { too much, because I think  }
  13. { Contact at:  LeTourneau University       LeTourneau University BBS          } { the code documentation is  }
  14. {              Microcomputer Services      2400/1200/300 Baud                 } { much easier to read when   }
  15. {              P.O. Box 7001               (214) 237-2742                     } { it is not intermingled     }
  16. {              Longview, TX  75607                                            } { with the code.             }
  17. {                                                                             } {                            }
  18.                                                                                 { No code extends beyond the }
  19. USES DOS, CRT, Comm_TP4;                                                        { 80th column, so it can be  }
  20.                                                                                 { deleted easily enough.     }
  21. TYPE                                                                            {                            }
  22.   ComSettingsRecord = RECORD                                                    {-A structure to hold port   }
  23.                        Baud : BYTE;                                             { communications settings.   }
  24.                        Parity : BYTE;                                           {                            }
  25.                        Stop : BYTE;                                             {                            }
  26.                        Bits : BYTE;                                             {                            }
  27.                      END;                                                       {                            }
  28.                                                                                 {                            }
  29.   ComSettingsType = ARRAY [1..2] OF ComSettingsRecord;                          {                            }
  30.                                                                                 {                            }
  31. VAR                                                                             {                            }
  32.   ComSettings : ComSettingsType;                                                {-Current port settings      }
  33.   Select, PortNumber : BYTE;                                                    {-Port number currently set  }
  34.   InkeyVar : CHAR;                                                              {                            }
  35.                                                                                 {                            }
  36. { A crude but effective procedure to allow the user to change settings of a serial port in use. PortNumber & }
  37. { ComSettings determine how the port is currently set up. As the parameters are changed, ComSettings is also }
  38. { updated.  Once again, keep in mind that the object of this program is not to provide a glamorous terminal  }
  39. { program.  Rather it serves as an example for those wanting to use serial routines in their own programs.   }
  40.                                                                                 {                            }
  41. PROCEDURE SetupPort (Com : BYTE);                                               {                            }
  42. VAR                                                                             {                            }
  43.   ResetPort : BOOLEAN;                                                          {-TRUE when settings changed }
  44. BEGIN                                                                           {                            }
  45.   WRITELN;                                                                      {                            }
  46.   ResetPort := FALSE;                                                           {                            }
  47.   WRITELN ('COM', Com, ' Setup', #10);                                          {-Select a baud rate         }
  48.   WRITELN ('0)  110           5)  2400');                                       {                            }
  49.   WRITELN ('1)  150           6)  4800');                                       {-Note that defaults are     }
  50.   WRITELN ('2)  300           7)  9600');                                       { allowed if you press <CR>  }
  51.   WRITELN ('3)  600           8) 19200');                                       { at any of the prompts. The }
  52.   WRITELN ('4) 1200           9) 38400', #10);                                  { port is not reset unless   }
  53.   WRITE ('Select a baud rate [', ComSettings [Com] . Baud, ']: ');              { the defaults are changed.  }
  54.   REPEAT                                                                        {                            }
  55.     InkeyVar := READKEY;                                                        {                            }
  56.   UNTIL (InkeyVar IN ['0'..'9', #13]);                                          {                            }
  57.   WRITELN (InkeyVar, #10);                                                      {                            }
  58.   IF (InkeyVar <> #13)                                                          {                            }
  59.     THEN BEGIN                                                                  {                            }
  60.            ComSettings [Com] . Baud := ORD (InkeyVar) - 48;                     {                            }
  61.            ResetPort := TRUE;                                                   {                            }
  62.          END;                                                                   {                            }
  63.   WRITE ('Select number of data bits [', ComSettings [Com] . Bits, ']: ');      {-Select number of data bits }
  64.   REPEAT                                                                        {                            }
  65.     InkeyVar := READKEY;                                                        {                            }
  66.   UNTIL (InkeyVar IN ['5'..'8', #13]);                                          {                            }
  67.   WRITELN (InkeyVar, #10);                                                      {                            }
  68.   IF (InkeyVar <> #13)                                                          {                            }
  69.     THEN BEGIN                                                                  {                            }
  70.            ComSettings [Com] . Bits := ORD(InkeyVar) - 48;                      {                            }
  71.            ResetPort := TRUE;                                                   {                            }
  72.          END;                                                                   {                            }
  73.   WRITELN ('0) None           2) None');                                        {-Select a parity setting    }
  74.   WRITELN ('1) Odd            3) Even', #10);                                   {                            }
  75.   WRITE ('Select a parity type [', ComSettings [Com] . Parity, ']: ');          {                            }
  76.   REPEAT                                                                        {                            }
  77.     InkeyVar := READKEY;                                                        {                            }
  78.   UNTIL (InkeyVar IN ['0'..'3', #13]);                                          {                            }
  79.   WRITELN (InkeyVar, #10);                                                      {                            }
  80.   IF (InkeyVar <> #13)                                                          {                            }
  81.     THEN BEGIN                                                                  {                            }
  82.            ComSettings [Com] . Parity := ORD(InkeyVar) - 48;                    {                            }
  83.            ResetPort := TRUE;                                                   {                            }
  84.          END;                                                                   {                            }
  85.   WRITE ('Select number of stop bits [', ComSettings [Com] . Stop, ']: ');      {-Select number of stop bits }
  86.   REPEAT                                                                        {                            }
  87.     InkeyVar := READKEY;                                                        {                            }
  88.   UNTIL (InkeyVar IN ['1'..'2', #13]);                                          {                            }
  89.   WRITELN (InkeyVar, #10);                                                      {                            }
  90.   IF (InkeyVar <> #13)                                                          {                            }
  91.     THEN BEGIN                                                                  {                            }
  92.            ComSettings [Com] . Stop := ORD(InkeyVar) - 48;                      {                            }
  93.            ResetPort := TRUE;                                                   {                            }
  94.          END;                                                                   {                            }
  95.   IF ResetPort                                                                  {-If the settings changed,   }
  96.     THEN SetupCOMPort (Com, ComSettings [Com] . Baud,                           { reset the port             }
  97.                             ComSettings [Com] . Bits,                           {                            }
  98.                             ComSettings [Com] . Parity,                         {                            }
  99.                             ComSettings [Com] . Stop);                          {                            }
  100. END;                                                                            {                            }
  101.                                                                                 {                            }
  102. { This provides a simple terminal emulation that might be used to prove that Comm_TP4 really works, and that }
  103. { it is not hard to use.  I got to playing, and perhaps it got a bit more complex than necessary... but then }
  104. { again, who said it had to be quick and dirty.  The LocalEcho parameter determines if characters typed on   }
  105. { the keyboard should be echoed to the screen.                                                               }
  106.                                                                                 {                            }
  107. PROCEDURE TTY (LocalEcho : BOOLEAN);                                            {                            }
  108. VAR                                                                             {                            }
  109.   ExitTTY,                                                                      {-TRUE when ready to quit    }
  110.   DataReady,                                                                    {-TRUE if buffer not empty   }
  111.   ShowHelp : BOOLEAN;                                                           {-TRUE if need help screen   }
  112.   OldCarrier : ARRAY [1..2] OF BOOLEAN;                                         {-Helps detect carrier change}
  113.   Buffer : CHAR;                                                                {-A character buffer         }
  114.   Loop : BYTE;                                                                  {                            }
  115. BEGIN                                                                           {                            }
  116.   FOR Loop := 1 TO MaxPorts DO                                                  {-Force Carrier status to be }
  117.     OldCarrier [Loop] := NOT CD [Loop];                                         { displayed the first time   }
  118.   DataReady := FALSE;                                                           {                            }
  119.   ShowHelp := TRUE;                                                             {-Show help screen on start  }
  120.   ExitTTY := FALSE;                                                             {                            }
  121.   Buffer := #0;                                                                 {                            }
  122.   CLRSCR;                                                                       {                            }
  123.   REPEAT                                                                        {-Terminal emulation starts  }
  124.     IF ShowHelp                                                                 {-Print the help screen if   }
  125.       THEN                                                                      { it has been requested.     }
  126.         BEGIN                                                                   {                            }
  127.           WRITELN (#13, #10, 'Terminal emulator commands', #10);                {-Brief summary of command   }
  128.           WRITELN ('<Alt C>  Switch Port to be used');                          { keys that can be used      }
  129.           WRITELN ('<Alt E>  Toggle Local Echo On/Off');                        {                            }
  130.           WRITELN ('<Alt H>  Help With Commands');                              {                            }
  131.           WRITELN ('<Alt P>  Change Port Parameters');                          {                            }
  132.           WRITELN ('<Alt X>  Exit');                                            {                            }
  133.           ShowHelp := FALSE;                                                    {                            }
  134.         END;                                                                    {                            }
  135.     DisableInts;                                                                {                            }
  136.     DataReady := (InTail [PortNumber] <> InHead [PortNumber]);                  {-If data has been received, }
  137.     EnableInts;                                                                 { print one character        }
  138.     IF DataReady                                                                {                            }
  139.       THEN                                                                      {                            }
  140.         BEGIN                                                                   { CHR(12) is interpreted as  }
  141.           DisableInts;                                                          { a FormFeed, and so clears  }
  142.           Buffer := CHR(InBuffer [PortNumber, InHead [PortNumber]]);            { the screen                 }
  143.           InHead [PortNumber] := (InHead [PortNumber] + 1) MOD (MaxInSize + 1); {                            }
  144.           EnableInts;                                                           { Input buffer is updated    }
  145.           CASE Buffer OF                                                        {                            }
  146.             #12 : CLRSCR;                                                       {                            }
  147.             ELSE  WRITE (Buffer);                                               {                            }
  148.           END;                                                                  {                            }
  149.         END;                                                                    {                            }
  150.     IF (OldCarrier [PortNumber] <> CD [PortNumber])                             {-If a change in carrier     }
  151.       THEN                                                                      { detect occurs, notify the  }
  152.         BEGIN                                                                   { user of the new status     }
  153.           WRITELN;                                                              {                            }
  154.           IF CD [PortNumber]                                                    {                            }
  155.             THEN WRITELN ('CARRIER DETECTED (COM', PortNumber, ')')             {                            }
  156.             ELSE WRITELN ('NO CARRIER (COM', PortNumber, ')');                  {                            }
  157.           OldCarrier [PortNumber] := CD [PortNumber];                           {                            }
  158.         END;                                                                    {-If a key has been pressed, }
  159.     IF KEYPRESSED                                                               { process it                 }
  160.       THEN                                                                      {                            }
  161.         BEGIN                                                                   {                            }
  162.           Buffer := READKEY;                                                    {                            }
  163.           IF (Buffer = #00) AND KEYPRESSED                                      {-Extended key codes require }
  164.             THEN                                                                { another read               }
  165.               BEGIN                                                             {                            }
  166.                 Buffer := READKEY;                                              {                            }
  167.                 CASE Buffer OF                                                  {                            }
  168.                   #46 : REPEAT                                                  {-<ALT C> lets you select a  }
  169.                           PortNumber := (PortNumber + 1) MOD (MaxPorts + 1);    { different port if you have }
  170.                           PortNumber := ORD (PortNumber = 0) + PortNumber;      { installed more that one.   }
  171.                           WRITE (#13,#10, 'COM', COMNmbr [PortNumber]);         {                            }
  172.                           IF NOT IntInstalled [PortNumber]                      { It notifies you of the new }
  173.                             THEN                                                { COM number & any ports it  }
  174.                               BEGIN                                             { saw were uninstalled.      }
  175.                                 WRITE (' not installed');                       {                            }
  176.                               END;                                              {                            }
  177.                           WRITELN;                                              {                            }
  178.                         UNTIL (IntInstalled [PortNumber]);                      {                            }
  179.                   #18 : LocalEcho := NOT LocalEcho;                             {-<ALT E> toggles Local Echo }
  180.                   #35 : ShowHelp := TRUE;                                       {-<ALT H> shows help screen  }
  181.                   #25 : SetupPort (PortNumber);                                 {-<ALT P> allows port setup  }
  182.                   #45 : ExitTTY := TRUE;                                        {-<ALT X> exits the program  }
  183.                   ELSE  IWriteCOM (PortNumber, CHR(27) + Buffer);               {-Other extended key codes   }
  184.                 END;                                                            { are sent to the port       }
  185.               END                                                               {                            }
  186.             ELSE                                                                {                            }
  187.               BEGIN                                                             {-Normal key codes are sent  }
  188.                  CASE Buffer OF                                                 { or translated and sent     }
  189.                    #12 : BEGIN                                                  {                            }
  190.                            IWriteCOM (PortNumber, Buffer);                      {-FormFeed clears screen if  }
  191.                            IF LocalEcho THEN CLRSCR;                            { local echo is on           }
  192.                          END;                                                   {                            }
  193.                    #13 : BEGIN                                                  {-A carriage return also     }
  194.                            IWriteCOM (PortNumber, Buffer); { + CHR(10)); }      { sends a line feed          }
  195.                            IF LocalEcho THEN WRITELN;                           {                            }
  196.                          END;                                                   {                            }
  197.                    ELSE  BEGIN                                                  {-All other characters are   }
  198.                            IWriteCOM (PortNumber, Buffer);                      { sent as typed              }
  199.                            IF LocalEcho THEN WRITE (Buffer);                    {                            }
  200.                          END;                                                   {                            }
  201.                  END;                                                           {                            }
  202.               END;                                                              {                            }
  203.         END;                                                                    {                            }
  204.   UNTIL ExitTTY;                                                                {-Continue emulation until   }
  205. END;                                                                            { <ALT X> is pressed.        }
  206.                                                                                 {                            }
  207. BEGIN                                                                           {                            }
  208.   Select := 0;                                                                  {-Default no port selected.  }
  209.   REPEAT                                                                        {                            }
  210.     CLRSCR;                                                                     {                            }
  211.     WRITELN ('COMM_TTY Terminal Emulator', #10);                                {                            }
  212.     WRITELN ('The following ports are usable:', #10);                           {                            }
  213.     FOR PortNumber := 1 TO MaxPorts DO                                          {-List all of the ports that }
  214.       BEGIN                                                                     { the Comm_TP4 TPU is set up }
  215.         WRITE ('Port #', PortNumber, ':  COM', COMNmbr [PortNumber], ', IRQ');  { to handle.                 }
  216.         WRITE (IRQNmbr [PortNumber], ', 2400 baud, 8N1, ');                     {                            }
  217.         IF NOT IntInstalled [PortNumber]                                        {-For simplicity, we will    }
  218.           THEN WRITE ('Not ');                                                  { only allow the port to be  }
  219.         WRITE ('Installed');                                                    { set up to specific baud    }
  220.         IF Select = PortNumber                                                  { rates, bits, parity, and   }
  221.           THEN WRITE (', Selected');                                            { stop bits after the TTY    }
  222.         CLREOL;                                                                 { emulation is started.      }
  223.         WRITELN;                                                                {                            }
  224.         ComSettings [PortNumber] . Baud := ORD (B2400);                         { This is a very simple set- }
  225.         ComSettings [PortNumber] . Bits := 8;                                   { up routine, but it does    }
  226.         ComSettings [PortNumber] . Parity := ORD (None);                        { easily allow the user to   }
  227.         ComSettings [PortNumber] . Stop := 1;                                   { tell the program how his   }
  228.       END;                                                                      { machine is configured.  A  }
  229.     WRITELN (#10, 'You may change the baud, bits, parity, and stop bits ');     { program should not lock in }
  230.     WRITELN ('from within the terminal emulation.', #10);                       { what ports can be used.    }
  231.     WRITELN ('A port must be installed & selected in order to begin.', #10);    {                            }
  232.     WRITE ('<Esc>, B)egin, or (1-', MaxPorts, ') to ');                         {                            }
  233.     WRITE ('(un)install or select port: ');                                     {                            }
  234.     REPEAT                                                                      {                            }
  235.       InkeyVar := READKEY;                                                      {                            }
  236.     UNTIL (InkeyVar IN [#27, 'b', 'B', '1'..CHR(Maxports+48)]);                 {                            }
  237.     WRITE (InkeyVar, ' ');                                                      {                            }
  238.     IF InkeyVar IN ['1'..CHR(MaxPorts+48)]                                      {-Update port status         }
  239.       THEN                                                                      {                            }
  240.         BEGIN                                                                   {                            }
  241.           PortNumber := ORD (InkeyVar) -48;                                     {                            }
  242.           IF IntInstalled [PortNumber]                                          {-If the port has already    }
  243.             THEN                                                                { been installed, we want to }
  244.               BEGIN                                                             { select it, or uninstall it.}
  245.                 WRITE ('S)elect, U)ninstall? ');                                {                            }
  246.                 REPEAT                                                          {                            }
  247.                   InkeyVar := READKEY;                                          {                            }
  248.                 UNTIL InkeyVar IN ['s', 'S', 'u', 'U'];                         {                            }
  249.                 WRITE (InkeyVar);                                               {                            }
  250.                 CASE InkeyVar OF                                                {                            }
  251.                   's', 'S' : Select := PortNumber;                              {-Select the port            }
  252.                   'u', 'U' : BEGIN                                              {-Uninstall the port by      }
  253.                                Select := ORD (Select <> PortNumber) * Select;   { removing the interrupt     }
  254.                                RemoveInt (PortNumber);                          { handler and deselecting it }
  255.                              END;                                               { if it was selected.        }
  256.                 END;                                                            {                            }
  257.               END                                                               {                            }
  258.             ELSE                                                                {-If the port has not been   }
  259.               BEGIN                                                             { installed yet, install it  }
  260.                 SetupCOMPort (PortNumber,                                       { by setting up the port     }
  261.                               ComSettings [PortNumber] . Baud,                  { protocol and installing    }
  262.                               ComSettings [PortNumber] . Bits,                  { the interrupt handler.     }
  263.                               ComSettings [PortNumber] . Parity,                {                            }
  264.                               ComSettings [PortNumber] . Stop);                 {                            }
  265.                 InstallInt (PortNumber);                                        {                            }
  266.               END;                                                              {                            }
  267.         END;                                                                    {                            }
  268.   UNTIL (InkeyVar = #27) OR ((InkeyVar IN ['b', 'B']) AND (Select > 0));        {-Stay in setup loop until   }
  269.   IF (InkeyVar <> #27)                                                          { <Esc> was hit or B)egin    }
  270.     THEN                                                                        { was chosen and a port was  }
  271.       BEGIN                                                                     { selected.                  }
  272.         PortNumber := Select;                                                   {                            }
  273.         TTY (FALSE);                                                            {-TTY with local echo off on }
  274.       END;                                                                      { selected port now begins.  }
  275.                                                                                 {                            }
  276.   { IMPORTANT:  RemoveIntOnExit is always called when the program terminates! } {-RemoveIntOnExit invoked by }
  277.                                                                                 { Turbo.  Don't quit without }
  278. END.                                                                            { removing interrupts!       }
  279.