home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / programming / misc_programming / comm < prev    next >
Text File  |  1990-01-05  |  18KB  |  338 lines

  1.                           Chapter  10
  2.  
  3.                 Asynchronous Serial Communication Services
  4.                 ------------------------------------------
  5.  
  6.  1. The Asynchronous Serial Protocol
  7.  
  8.     The main concept behind the serial protocol is that all the data and
  9.     control information necessary to transmit and receive a character of
  10.     information must move over a single data line, one bit at a time.
  11.     The way this is done is shown in Figure 1-1.
  12.  
  13.     MARKING
  14.    (LOGICAL 1) ───┐    ┌────┬────┬────┬────┬────┬────┬────┬────┬────────
  15.                   │    │    |    |    |    |    |    |    │    │
  16.     SPACING       └────┴────┴────┴────┴────┴────┴────┴────┴────┘
  17.    (LOGICAL 0)    START          DATA BITS                PARITY   STOP
  18.                    BIT                                     BIT     BIT(S)
  19.                                                         (OPTIONAL)
  20.                            <─────── DATA FLOW
  21.  
  22.           Figure 1-1. Asynchronous serial data format
  23.           -------------------------------------------
  24.  
  25.     The width of each bit is determined by the data transmission speed,   
  26.     which is measured in bits per second.  This speed is called the 
  27.     "baud rate".
  28.  
  29.     When no data is being transmitted on the line, the line is left in a
  30.     logical 1 state, or a "marking state".  When we wish to transmit a
  31.     character of data, the first bit to be transmitted is the "start bit".
  32.     This start bit is represented by a logical 0 state, or a "spacing 
  33.     state", on the line.  The receiver knows to expect a character of data
  34.     when it notices the line change from a marking state to a spacing state.
  35.  
  36.     The data bits, which make up the character of information that is being
  37.     transmitted, immediately follow the start bit.  The number of data bits
  38.     can be 5, 6, 7, or 8, but each character must contain the same number 
  39.     of data bits in the same transmission.
  40.  
  41.     An optional "parity bit" immediately follows the data bits.  There are
  42.     three types of parity: even, odd, and none parity.  The parity bit 
  43.     allows the receiver to detect certain types of transmission errors.
  44.  
  45.     There ara 1, 1.5, or 2 bits of marking (logical 1) following the parity
  46.     bit (or data bits if no parity bit is present).  These bits are called
  47.     the "stop bits".  The stop bits represent the minimum amount of time 
  48.     that the line must be in a marking condition before the next start bit
  49.     appears.
  50.  
  51.     Because data characters can start and stop arriving at any time, the
  52.     serial communications protocol discussed above is an "asynchronous"
  53.     protocol.  The receiver has to synchronize with the sender when the 
  54.     start bit of a new character is received.  Since the baud rates of the
  55.     sender and the receiver are set up to be the same, the receiver is able
  56.     to pick off the start, data, parity, and stop bits from the line as a
  57.     function of time; the beginning of the start bit is used as the 
  58.     synchronizing event.
  59.  
  60.  
  61.  2. The Universal Asynchronous Receiver Transmitter (UART)
  62.  
  63.     It would seem quit complex to write a program to send and receive data
  64.     characters in an asynchronous serial communications environment.
  65.     Fortunately, a dedicated microprocessor called a "Universal 
  66.     Asynchronous Receiver Transmitter (UART)", is used to perform most of 
  67.     the serial protocol conversion.  There are several UART's: Intel 8250,
  68.     8251(USART), NS 16450, 16550(with FIFOs).
  69.  
  70.     A functional diagram of a UART is shown in Figure 2-1.  Modem and
  71.     interrupt control signals have been omitted for clarity.
  72.  
  73.               ┌--------------------------------------------┐
  74.               |    ┌───────────┐           ┌───────────┐   |
  75.    RECEIVED   |    │  RECEIVER │           │  RECEIVER │   | COMMUNICATIONS
  76.      DATA  <───────┤    DATA   │<──────────┤   SHIFT   │<────   INPUT
  77.               |    │  REGISTER │           │  REGISTER │   |
  78.               |    └───────────┘           └───────────┘   |
  79.               |                                            |
  80. COMMUNICATIONS|   STATUS                                   |
  81.    STATUS  <─────    ERROR     *PARITY                     |
  82.               |   CONDITIONS   *OVERRUN                    |
  83.               |                *FRAMING                    |
  84.               |                *BREAK                      |
  85.               |   *RECEIVED DATA READY                     |
  86.               |   *TRANSMITTER HOLDING REGISTER EMPTY      |
  87.               |                                            |
  88. COMMUNICATIONS|                                            |
  89.    CONTROL ─────> CONTROL                                  |
  90.               |   *DESIRED BAUD RATE                       |
  91.               |   *NUMBER OF DATA BITS                     |
  92.               |   *SELECTION AND TYPE OF PARITY            |
  93.               |   *NUMBER OF STOP BITS                     |
  94.               |                                            |
  95.               |    ┌───────────┐           ┌───────────┐   |
  96.  TRANSMITTED  |    │TRANSMITTER│           │TRANSMITTER│   | COMMUNICATIONS
  97.     DATA   ───────>│  HOLDING  ├──────────>│   SHIFT   ├─────>  OUTPUT
  98.               |    │  REGISTER │           │  REGISTER │   |
  99.               |    └───────────┘           └───────────┘   |
  100.               |                   ┌───────────┐            |
  101.               |                   │   BAUD    │            |
  102.               |                 ┌>│   RATE    │            |
  103.               |                 │ │ GENERATOR │            |
  104.               |                 │ └───────────┘            |
  105.               └-----------------│--------------------------┘
  106.        <── TO COMPUTER          │                TO COMMUNICATIONS LINE ──>
  107.                                CLOCK
  108.  
  109.           Figure 2-1. Functional diagram of UART
  110.           ----------------------------------------
  111.  
  112.     When we want to transmit a character of data, we check the UART status
  113.     to see if the "transmitter holding register" is empty.  If it is, we
  114.     can output a byte of data to the UART, and this byte is placed in the
  115.     transmitter holding register.  When the UART has completed transmitting
  116.     the previous character or is currently not transmitting any data, the
  117.     contents of the transmitter holding register are placed in the
  118.     "transmitter shift register".  The transmitter holding register is then
  119.     available for another output character from the computer.
  120.  
  121.     The UART places any serial input it receives into the "receiver shift
  122.     register".  After the appropriate number of stop bits are received and
  123.     error checking is performed, the character is placed in the "receiver
  124.     data register".  The UART then updates its status to show that receiver
  125.     data is ready.  The computer knows that it can read another byte of
  126.     data from the UART when the receiver-data-ready status is correct. 
  127.     After the data is read by the computer, the UART does not show receiver
  128.     data ready again until it has placed another character into the 
  129.     receiver data register.
  130.  
  131.     If the UART must place a second character into the receiver data
  132.     register before the first character is read by the computer, then an
  133.     "overrun" error results.
  134.  
  135.     If the UART does not receive a stop bit when it is expecting one, then
  136.     a "framing" error results.
  137.  
  138.     To send a break condition, the line must be held in a spacing condition
  139.     for as long as it takes to transmit one character with all its
  140.     associated control bits.  The UART will reflect the reception of a
  141.     break condition in its status output.
  142.  
  143.  
  144.  3. The Modem
  145.  
  146.     The RS232 interface electronically connects the UART to the outside
  147.     world. It can be directly connected to another RS232 interface only
  148.     over short distances.  If we want two computers to be able to talk to
  149.     each other over long distances or over the phonelines, then the 
  150.     electronic output of the RS232 interface must be used with an
  151.     additional interfacing equipment called "MODEM". (see Figure 3-1)
  152.  
  153.     ┌──────┐     ┌─────────┐         SHORT         ┌─────────┐     ┌──────┐
  154.     │ UART │     │  RS232  │        DISTANCE       │  RS232  │     │ UART │
  155.     │  1   │<───>│INTERFACE│<─────────────────────>│INTERFACE│<───>│  2   │
  156.     └──────┘     └─────────┘                       └─────────┘     └──────┘
  157.  
  158.     ┌──────┐     ┌─────────┐                       ┌─────────┐     ┌──────┐
  159.     │ UART │     │  RS232  │                       │  RS232  │     │ UART │
  160.     │  1   │<───>│INTERFACE│<┐                   ┌>│INTERFACE│<───>│  2   │
  161.     └──────┘     └─────────┘ │                   │ └─────────┘     └──────┘
  162.                              │      PHONELINE    │
  163.                  ┌─────────┐ │         OR        │ ┌─────────┐
  164.                  │  MODEM  │<┘    LONG DISTANCE  └>│  MODEM  │
  165.                  │         │<─────────────────────>│         │
  166.                  └─────────┘                       └─────────┘
  167.  
  168.                          Figure 3-1. Use of MODEM
  169.                          ------------------------
  170.  
  171.     The modem control signals are described in Table 3-1.
  172.  
  173.                     Table 3-1. Modem Control Signals
  174.  ┌────────────────────────────────────────────────────────────────────────┐
  175.  │Signals to Modem From Computer:                                         │
  176.  │DTR - DATA TERMINAL READY                                               │
  177.  │      Under program control. Used to tell the modem that the computer   │
  178.  │      is powered up and ready.                                          │
  179.  │RTS - REQUEST TO SEND                                                   │
  180.  │      Under program control. Used to tell the modem that the computer   │
  181.  │      wants to send data now.                                           │
  182.  │                                                                        │
  183.  │Signals From Modem to Computer:                                         │
  184.  │DSR - DATA SET READY                                                    │
  185.  │      Used to tell the computer that the modem is powered up and ready. │
  186.  │CTS - CLEAR TO SEND                                                     │
  187.  │      Used to tell the computer that the modem is ready to accept data  │
  188.  │      transmission.                                                     │
  189.  │DCD - DATA CARRIER DETECT                                               │
  190.  │      Used to tell the computer that the modem has established a        │
  191.  │      connection with the modem on the other end of the phone line.     │
  192.  │RI  - RING INDICATOR                                                    │
  193.  │      Used to tell the computer that the phone connected to the modem   │
  194.  │      is ringing.                                                       │
  195.  └────────────────────────────────────────────────────────────────────────┘
  196.  
  197.  
  198.  4. The Physical Interface
  199.  
  200.     The communications adapter for the computer provides a standard RS232
  201.     interface to the outside world.  The physical pinout for the RS232
  202.     connector is shown in Table 4-1.
  203.  
  204.                   Table 4-1.  Standard RS232 Pinout
  205.     ┌───────────────┬───────────────┬────────────────────────────┐
  206.     │ Pin Number    │ Direction     │  Function                  │
  207.     ├───────────────┼───────────────┼────────────────────────────┤
  208.     │ 2             │ Output        │ Transmitted Data           │
  209.     │ 3             │ Input         │ Received Data              │
  210.     │ 4             │ Output        │ Request To Send            │
  211.     │ 5             │ Input         │ Clear To Send              │
  212.     │ 6             │ Input         │ Data Set Ready             │
  213.     │ 7             │               │ Signal Ground              │
  214.     │ 8             │ Input         │ Data Carrier Detect        │
  215.     │ 20            │ Output        │ Data Terminal Ready        │
  216.     │ 22            │ Input         │ Ring Indicator             │
  217.     └───────────────┴───────────────┴────────────────────────────┘
  218.  
  219.     However, if we wish to connect a PC to another PC, we cannot simply
  220.     connect pin 2 to pin 2, and so on.  If we did this, then the two 
  221.     transmit-data pins would be connected to each other, which wouldn't
  222.     work very well. The correct way to connect two machines with the
  223.     interface shown in Table 4-1 is shown in Figure 4-1.  This connection
  224.     scheme is sometimes called a "null modem".
  225.  
  226.              COMPUTER 1                               COMPUTER 2
  227.         ┌──────────────────┐                     ┌──────────────────┐
  228.         │ TRANSMIT DATA   2├────────────────────>│3   RECEIVED DATA │
  229.         │ RECEIVED DATA   3│<────────────────────┤2   TRANSMIT DATA │
  230.         │           RTS   4├────────────────────>│5   CTS           │
  231.         │           CTS   5│<────────────────────┤4   RTS           │
  232.         │           DSR   6│<────────────────────┤20  DTR           │
  233.         │           DTR  20├────────────────────>│6   DSR           │
  234.         │        GROUND   7│<───────────────────>│7   GROUND        │
  235.         └──────────────────┘                     └──────────────────┘
  236.  
  237.                       Figure 4-1. Null Modem Connection
  238.                       ---------------------------------
  239.  
  240.  
  241.  5. Programming The UART
  242.  
  243.     Table 5-1 shows all the UART registers which is addressed for COM1.
  244.     Please reference the PORT SPECIFICATION for more informations.
  245.  
  246.                   Table 5-1.  Accessing the UART Registers
  247.  ┌───────────────────┬───────────────┬────────────────────────────────────┐
  248.  │ I/O Port Address  │ Read/Write    │  Descriptions                      │
  249.  ├───────────────────┼───────────────┼────────────────────────────────────┤
  250.  │ 3F8h*             │ Write         │ Transmitter Holding Register       │
  251.  │ 3F8h*             │ Read          │ Receiver Data Register             │
  252.  │ 3F8h%             │ Write         │ Baud-Rate Divisor (LSB)            │
  253.  │ 3F9h%             │ Write         │ Baud-Rate Divisor (MSB)            │
  254.  │ 3F9h*             │ Write         │ Interrupt-Enable Register          │
  255.  │ 3FAh              │ Read          │ Interrupt-Identification Register  │
  256.  │ 3FBh              │ Write         │ Line-Control Register              │
  257.  │ 3FCh              │ Write         │ Modem-Control Register             │
  258.  │ 3FDh              │ Read          │ Line-Status Register               │
  259.  │ 3FEh              │ Read          │ Modem-Status Register              │
  260.  └───────────────────┴───────────────┴────────────────────────────────────┘
  261.   * Bit 7 of the Line-Control Register = 0
  262.      % Bit 7 of the Line-Control Register = 1
  263.  
  264.  
  265.     The UART's clock input runs at 1.8432 MHz.  The value for 
  266.     Baud-Rate-Divisor Registers are calculated as follows:
  267.  
  268.                 1,843,200      115,200
  269.                ----------- = ----------- = Divisor
  270.                   2 * 8       Baud Rate
  271.  
  272.     Table 5-2 shows the Baud-Rate-Divisor values that are supported by the 
  273.     BIOS Asynchronous Serial Communication Services.
  274.  
  275.                       Table 5-2.  Baud-Rate-Divisor Values
  276.         ┌───────────────────┬───────────────────────────────────────┐
  277.         │ Desired Baud Rate │ Value for Baud-Rate-Divisor Registers │
  278.         ├───────────────────┼───────────────────────────────────────┤
  279.         │        110        │           0417h                       │
  280.         │        150        │           0300h                       │
  281.         │        300        │           0180h                       │
  282.         │        600        │           00C0h                       │
  283.         │       1200        │           0060h                       │
  284.         │       2400        │           0030h                       │
  285.         │       4800        │           0018h                       │
  286.         │       9600        │           000Ch                       │
  287.         └───────────────────┴───────────────────────────────────────┘
  288.  
  289.  
  290.  6. BIOS Asynchronous Serial Communication Services
  291.  
  292.     The BIOS Asynchronous Serial Communication Services initialize serial
  293.     communications port, send character, receive character, and return the
  294.     status of the serial ports.
  295.  
  296.     All functions require a serial port number in DX (0 through 1).  This
  297.     serial port number indexes a table in BIOS data area which contains the
  298.     port base addresses for each of the two corresponding serial ports.
  299.  
  300.     The power-on self test routine determines whether a serial port adapter
  301.     is active at ports 3F8h, and 2F8h, and, if it detects an adapter
  302.     installed, it stores its port address in the table.
  303.  
  304.     All the Asynchronous Serial Communication functions return a status
  305.     byte in AH.  The most significant bit is supplied by the Send Character
  306.     and Receive Character functions to signal a timeout error: the serial
  307.     port will not be ready after the character was sent or received, and
  308.     the timeout count expired.  All three functions are listed as follows:
  309.  
  310.     function:
  311.  
  312.     ah = 000h Initialize the communications port
  313.     ah = 001h Send character
  314.     ah = 002h Receive a character
  315.     ah = 003h Return the communications port status
  316.     ah = 004h Extended initialize
  317.     ah = 005h Extended communications port control
  318.     ah = 006h - 0ffh - reserved - null return
  319.  
  320.       Note: Function 4 and 5 are only for PS/2 products.
  321.  
  322.     input:
  323.     ah - function number
  324.     dx - communications port number (0,1,2,3)
  325.  
  326.     output:
  327.     ah = status
  328.          bit 7 = time out
  329.          bit 6 = trans shift register empty
  330.          bit 5 = tran holding register empty
  331.          bit 4 = break detect
  332.          bit 3 = framing error
  333.          bit 2 = parity error
  334.          bit 1 = overrun error
  335.          bit 0 = data ready
  336.  
  337.  
  338.