<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="None">
  <name>Serial Port Communication</name>
  <metadata>
  <md:version>**new**</md:version>
  <md:created>2004/06/16 14:08:38.459 GMT-5</md:created>
  <md:revised>2004/06/16 14:09:48.607 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="NationalInstruments">
      <md:firstname>National</md:firstname>
      
      <md:surname>Instruments</md:surname>
      <md:email>certification@ni.com</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="harika">
      <md:firstname>Harika</md:firstname>
      
      <md:surname>Basana</md:surname>
      <md:email>ilsai@rice.edu</md:email>
    </md:maintainer>
    <md:maintainer id="mshiralkar05">
      <md:firstname>Malan</md:firstname>
      
      <md:surname>Shiralkar</md:surname>
      <md:email>mshiralkar05@hotmail.com</md:email>
    </md:maintainer>
    <md:maintainer id="lizzardg">
      <md:firstname>Elizabeth</md:firstname>
      
      <md:surname>Gregory</md:surname>
      <md:email>lizzardg@rice.edu</md:email>
    </md:maintainer>
    <md:maintainer id="NationalInstruments">
      <md:firstname>National</md:firstname>
      
      <md:surname>Instruments</md:surname>
      <md:email>certification@ni.com</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>National Instruments</md:keyword>
    <md:keyword>Labview</md:keyword>
    <md:keyword>Serial Port</md:keyword>
  </md:keywordlist>

  <md:abstract>In this lesson, you will learn about serial communication.</md:abstract>
</metadata>

  <content>
    <para id="p1">
      <cnxn target="f1">Serial communication</cnxn> is a popular means
      of transmitting data between a computer and a peripheral device
      such as a programmable instrument or even another
      computer. Serial communication uses a transmitter to send data,
      one bit at a time, over a single communication line to a
      receiver. You can use this method when data transfer rates are
      low or you must transfer data over long distances. Serial
      communication is popular because most computers have one or more
      serial ports, so no extra hardware is needed other than a cable
      to connect the instrument to the computer or two computers
      together.
    </para>
    <figure id="f1">
      <media type="image/jpg" src="sercomm.png"/>
      <caption>
	1: RS-232 Instrument,2:	RS-232 Cable, 3: Serial Port
      </caption>
    </figure>
    <para id="p2">
      Serial communication requires that you specify the following
      four parameters:
      <list id="p2l1">
	<item>
	  The baud rate of the transmission
	</item>
	<item>
	  The number of data bits encoding a character
	</item>
	<item>
	  The sense of the optional parity bit
	</item>
	<item>
	  The number of stop bits
	</item>
      </list>
    </para>
    <para id="p3">
      Each transmitted character is packaged in a character frame that
      consists of a single start bit followed by the data bits, the
      optional parity bit, and the stop bit or bits. <cnxn target="f2"/> shows a typical character frame encoding the
      letter <emphasis>m</emphasis>.
    </para>
    <figure id="f2">
      <media type="image/jpg" src="charframe.png"/>
    </figure>
    <para id="p4">
      Baud rate is a measure of how fast data are moving between
      instruments that use serial communication. RS-232 uses only two
      voltage states, called <code>MARK</code> and <code>SPACE</code>. In such a two-state
      coding scheme, the baud rate is identical to the maximum number
      of bits of information, including control bits, that are
      transmitted per second.
    </para>
    <para id="p5">
      <code>MARK</code> is a negative voltage, and <code>SPACE</code> is positive. <cnxn target="f2"/> shows how the idealized signal looks on an
      oscilloscope. The following is the truth table for RS-232:
      <m:math display="block">
	<m:apply>
	  <m:eq/>
	  <m:apply>
	    <m:gt/>
	    <m:ci>Signal</m:ci>
	    <m:apply>
	      <m:times/>
	      <m:cn>3</m:cn>
	      <m:ci>V</m:ci>
	    </m:apply>
	  </m:apply>
	  <m:cn>0</m:cn>
	</m:apply>
      </m:math>
      <m:math display="block">
	<m:apply>
	  <m:eq/>
	  <m:apply>
	    <m:gt/>
	    <m:ci>Signal</m:ci>
	    <m:apply>
	      <m:times/>
	      <m:cn>-3</m:cn>
	      <m:ci>V</m:ci>
	    </m:apply>
	  </m:apply>
	  <m:cn>1</m:cn>
	</m:apply>
      </m:math>
    </para>
    <para id="p6">
      The output signal level usually swings between +12 V and -12
      V. The dead area between +3 V and -3 V is designed to absorb
      line noise.
    </para>
    <para id="p7">
      A start bit signals the beginning of each character frame. It is
      a transition from negative (<code>MARK</code>) to positive (<code>SPACE</code>)
      voltage. Its duration in seconds is the reciprocal of the baud
      rate. If the instrument is transmitting at 9,600 baud, the
      duration of the start bit and each subsequent bit is about 0.104
      ms. The entire character frame of eleven bits would be
      transmitted in about 1.146 ms.
    </para>
    <para id="p8">
      Data bits are transmitted upside down and backwards. That is,
      inverted logic is used, and the order of transmission is from
      least significant bit (LSB) to most significant bit (MSB). To
      interpret the data bits in a character frame, you must read from
      right to left and read 1 for negative voltage and 0 for positive
      voltage. This yields 1101101 (binary) or 6D (hex). An ASCII
      conversion table shows that this is the letter m.
    </para>
    <para id="p9">
      An optional parity bit follows the data bits in the character
      frame. The parity bit, if present, also follows inverted logic,
      1 for negative voltage and 0 for positive voltage. This bit is
      included as a simple means of error handling. You specify ahead
      of time whether the parity of the transmission is to be even or
      odd. If the parity is chosen to be odd, the transmitter then
      sets the parity bit in such a way as to make an odd number of
      ones among the data bits and the parity bit. This transmission
      uses odd parity. There are five ones among the data bits,
      already an odd number, so the parity bit is set to 0.
    </para>
    <para id="p10">
      The last part of a character frame consists of 1, 1.5, or 2 stop
      bits. These bits are always represented by a negative
      voltage. If no further characters are transmitted, the line
      stays in the negative (<code>MARK</code>) condition. The transmission of the
      next character frame, if any, is heralded by a start bit of
      positive (<code>SPACE</code>) voltage.
    </para>
    <section id="s1">
      <name>How Fast Can I Transmit?</name>
      <para id="s1p1">
	Knowing the structure of a character frame and the meaning of
	baud rate as it applies to serial communication, you can
	calculate the maximum transmission rate, in characters per
	second, for a given communication setting. This rate is just
	the baud rate divided by the bits per frame. In the previous
	example, there are a total of eleven bits per character
	frame. If the transmission rate is set at 9,600 baud, you get
	<m:math>
	  <m:apply>
	    <m:eq/>
	    <m:apply>
	      <m:divide/>
	      <m:cn>9,600</m:cn>
	      <m:cn>11</m:cn>
	    </m:apply>
	    <m:cn>872</m:cn>
	  </m:apply>
	</m:math> characters per second. Notice that this is the
	maximum character transmission rate. The hardware on one end
	or the other of the serial link might not be able to reach
	these rates, for various reasons.
      </para>
    </section>
    <section id="s2">
      <name>Hardware Overview</name>
      <para id="s2p1">
	There are many different recommended standards of serial port
	communication, including the following most common types.
      </para>
      <section id="s2s1">
	<name>RS-232</name>
	<para id="s2s1p1">
	  The <term>RS-232</term> is a standard developed by the
	  <term>Electronic Industries Association</term>
	  (<term>EIA</term>) and other interested parties, specifying
	  the serial interface between <term>Data Terminal
	  Equipment</term> (<term>DTE</term>) and <term>Data
	  Communications Equipment</term> (<term>DCE</term>). The
	  RS-232 standard includes electrical signal characteristics
	  (voltage levels), interface mechanical characteristics
	  (connectors), functional description of interchange circuits
	  (the function of each electrical signal), and some recipes
	  for common kinds of terminal-to-modem connections. The most
	  frequently encountered revision of this standard is called
	  RS-232C. Parts of this standard have been adopted (with
	  various degrees of fidelity) for use in serial
	  communications between computers and printers, modems, and
	  other equipment. The serial ports on standard IBM-compatible
	  personal computers follow RS-232.
	</para>
      </section>
      <section id="s2s2">
	<name>RS-449, RS-422, RS-423</name>
	<para id="s2s2p1">
	  The RS-449, RS-422, and RS-423 are additional EIA serial
	  communication standards related to RS-232. RS-449 was issued
	  in 1975 and was supposed to supersede RS-232, but few
	  manufacturers have embraced the newer standard. RS-449
	  contains two subspecifications called RS-422 and
	  RS-423. While RS-232 modulates a signal with respect to a
	  common ground, or single-ended transmission, RS-422
	  modulates two signals against each other, or differential
	  transmission. The RS-232C receiver senses whether the
	  received signal is sufficiently negative with respect to
	  ground to be a logical 1, whereas the RS-422 receiver senses
	  which line is more negative than the other. This makes
	  RS-422 more immune to noise and interference and more
	  versatile over longer distances. The Macintosh serial ports
	  follow RS-422, which can be converted to RS-423 by proper
	  wiring of an external cable. RS-423 can then communicate
	  with most RS-232 devices over distances of 15 m or so.
	</para>
      </section>
      <section id="s2s3">
	<name>RS-232 Cabling</name>
	<para id="s2s3p1">
	  Devices that use serial cables for their communication are
	  split into two categories. These are DCE and DTE. DCE are
	  devices such as a modem, TA adapter, plotter, and so on,
	  while DTE is a computer or terminal. RS-232 serial ports
	  come in two sizes, the D-Type 25-pin connector and the
	  D-Type 9-pin connector. Both of these connectors are male on
	  the back of the PC. Thus, you require a <cnxn target="s2s3f1">female connector</cnxn> on the device. <cnxn target="s2s3t1"/> shows the pin connections for the 9-pin and
	  25-pin D-Type connectors.
	</para>
	<figure id="s2s3f1">
	  <media type="image/jpg" src="dtypepin.png"/>
	</figure>
	<table frame="all" id="s2s3t1">
	  <tgroup cols="5" align="left" colsep="1" rowsep="1">
	    <thead valign="top">
	      <row>
	        <entry align="center">Function</entry>
	        <entry align="center">Signal</entry>
	        <entry align="center">PIN</entry>
	        <entry align="center">DTE</entry>
	        <entry align="center">DCE</entry>
	      </row>
	    </thead>
	    <tbody valign="top">
	      <row>
	        <entry morerows="1">Data</entry>
	        <entry align="center">TxD</entry>
	        <entry align="center">3</entry>
	        <entry align="center">Output</entry>
	        <entry align="center">Input</entry>
	      </row>
	      <row>
	        <entry align="center">RxD</entry>
	        <entry align="center">2</entry>
	        <entry align="center">Input</entry>
	        <entry align="center">Output</entry>
	      </row>
	      <row>
	        <entry morerows="4">Handshake</entry>
	        <entry align="center">RTS</entry>
	        <entry align="center">7</entry>
	        <entry align="center">Output</entry>
	        <entry align="center">Input</entry>
	      </row>
	      <row>
	        <entry align="center">CTS</entry>
	        <entry align="center">8</entry>
	        <entry align="center">Input</entry>
	        <entry align="center">Output</entry>
	      </row>
	      <row>
	        <entry align="center">DSR</entry>
	        <entry align="center">6</entry>
	        <entry align="center">Input</entry>
	        <entry align="center">Output</entry>
	      </row>
	      <row>
	        <entry align="center">DCD</entry>
	        <entry align="center">1</entry>
	        <entry align="center">Input</entry>
	        <entry align="center">Output</entry>
	      </row>
	      <row>
	        <entry align="center">STR</entry>
	        <entry align="center">4</entry>
	        <entry align="center">Output</entry>
	        <entry align="center">Input</entry>
	      </row>
	      <row>
	        <entry>Common</entry>
	        <entry align="center">Com</entry>
	        <entry align="center">5</entry>
	        <entry align="center">--</entry>
	        <entry align="center">--</entry>
	      </row>
	      <row>
	        <entry>Other</entry>
	        <entry align="center">RI</entry>
	        <entry align="center">9</entry>
	        <entry align="center">Output</entry>
	        <entry align="center">Input</entry>
	      </row>
	    </tbody>
	  </tgroup>
	</table>
	<para id="s2s3p2">
	  The DB-9 connector is occasionally found on smaller RS-232
	  lab equipment. It is compact, yet has enough pins for the
	  core set of serial pins (with one pin extra).
	</para>
	<note>
	  The DB-9 pin numbers for transmit and receive (3 and 2) are
	  opposite of those on the DB-25 connector (2 and 3). Be
	  careful of this difference when you are determining if a
	  device is DTE or DCE.
	</note>
	<para id="s2s3p3">
	  The <cnxn target="s2s3f2">DB-25 connector</cnxn> is the
	  standard RS-232 connector, with enough pins to cover all the
	  signals specified in the standard. <cnxn target="s2s3t2"/>
	  shows only the core set of pins that are used for most
	  RS-232 interfaces.
	</para>
	<figure id="s2s3f2">
	  <media type="image/jpg" src="db25pin.png"/>
	</figure>
	<table frame="all" id="s2s3t2">
	  <tgroup cols="5" align="left" colsep="1" rowsep="1">
	    <thead valign="top">
	      <row>
	        <entry align="center">Function</entry>
	        <entry align="center">Signal</entry>
	        <entry align="center">PIN</entry>
	        <entry align="center">DTE</entry>
	        <entry align="center">DCE</entry>
	      </row>
	    </thead>
	    <tbody valign="top">
	      <row>
	        <entry morerows="1">Data</entry>
	        <entry align="center">TxD</entry>
	        <entry align="center">2</entry>
	        <entry align="center">Output</entry>
	        <entry align="center">Input</entry>
	      </row>
	      <row>
	        <entry align="center">RxD</entry>
	        <entry align="center">3</entry>
	        <entry align="center">Input</entry>
	        <entry align="center">Output</entry>
	      </row>
	      <row>
	        <entry morerows="4">Handshake</entry>
	        <entry align="center">RTS</entry>
	        <entry align="center">4</entry>
	        <entry align="center">Output</entry>
	        <entry align="center">Input</entry>
	      </row>
	      <row>
	        <entry align="center">CTS</entry>
	        <entry align="center">5</entry>
	        <entry align="center">Input</entry>
	        <entry align="center">Output</entry>
	      </row>
	      <row>
	        <entry align="center">DSR</entry>
	        <entry align="center">6</entry>
	        <entry align="center">Input</entry>
	        <entry align="center">Output</entry>
	      </row>
	      <row>
	        <entry align="center">DCD</entry>
	        <entry align="center">8</entry>
	        <entry align="center">Input</entry>
	        <entry align="center">Output</entry>
	      </row>
	      <row>
	        <entry align="center">STR</entry>
	        <entry align="center">20</entry>
	        <entry align="center">Output</entry>
	        <entry align="center">Input</entry>
	      </row>
	      <row>
	        <entry>Common</entry>
	        <entry align="center">Com</entry>
	        <entry align="center">7</entry>
	        <entry align="center">--</entry>
	        <entry align="center">--</entry>
	      </row>
	    </tbody>
	  </tgroup>
	</table>
      </section>
    </section>
    <section id="s3">
      <name>Software Overview</name>
      <para id="s3p1">
	Use the VIs and functions located on the <code>Functions&gt;&gt;All
	Functions&gt;&gt;Instrument I/O&gt;&gt;Serial</code> palette for serial
	port communication.
      </para>
      <para id="s3p2">
	You used some of the VISA functions on this palette for GPIB
	communication. The <code>VISA Write</code> and <code>VISA
	Read</code> functions work with any type of instrument
	communication and are the same whether you are doing GPIB or
	serial communication. However, because serial communication
	requires you to configure extra parameters, you must start the
	serial port communication with the <code>VISA Configure Serial
	Port</code> VI.
      </para>
      <para id="s3p3">
	The <code>VISA Configure Serial Port</code> VI initializes the
	port identified by <code>VISA resource name</code> to the
	specified settings. <code>timeout</code> sets the timeout
	value for the serial communication. <code>baud rate</code>,
	<code>data bits</code>, <code>parity</code>, and <code>flow
	control</code> specify those specific serial port
	parameters. The <code>error in</code> and <code>error
	out</code> clusters maintain the error conditions for this VI.
      </para>
      <example id="s3ex1">
	<para id="s3ex1p1">
	  <cnxn target="s3ex1f1"/> shows how to send the
	  identification query command <code>*IDN?</code> to the
	  instrument connected to the COM2 serial port. The <code>VISA
	  Configure Serial Port</code> VI opens communication with
	  COM2 and sets it to 9,600 baud, eight data bits, odd parity,
	  one stop bit, and XON/XOFF software handshaking. Then the
	  <code>VISA Write</code> function sends the command. The
	  <code>VISA Read</code> function reads back up to 200 bytes
	  into the read buffer, and the <code>Simple Error
	  Handler</code> VI checks the error condition.
	</para>
	<figure id="s3ex1f1">
	  <media type="image/jpg" src="serialVISAcnfg.png"/>
	</figure>
      </example>
      <note>
	The VIs and functions located on the <code>Functions&gt;&gt;All
	Functions&gt;&gt;Instrument I/O&gt;&gt;Serial</code> palette are also used
	for parallel port communication. You specify the VISA resource
	name as being one of the LPT ports. For example, you can use
	MAX to determine that LPT1 has a VISA resource name of
	<code>ASRL10::INSTR</code>.
      </note>
    </section>

  </content>
  
</document>
