1059 lines
		
	
	
		
			58 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			1059 lines
		
	
	
		
			58 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<?xml version="1.0" encoding="utf-8"?>
 | 
						||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 | 
						||
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="6" MadCap:lastHeight="94" MadCap:lastWidth="853" MadCap:disableMasterStylesheet="true" MadCap:tocPath="" MadCap:InPreviewMode="false" MadCap:PreloadImages="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" lang="en-us" xml:lang="en-us" MadCap:PathToHelpSystem="../../../" MadCap:HelpSystemFileName="Default.xml" MadCap:SearchType="Stem">
 | 
						||
    <head><title>SEROUT</title>
 | 
						||
        <link href="../../SkinSupport/MadCap.css" rel="stylesheet" />
 | 
						||
        <link href="../../Resources/Stylesheets/BSE_Help.css" rel="stylesheet" />
 | 
						||
        <script src="../../SkinSupport/MadCapAll.js" type="text/javascript">
 | 
						||
        </script>
 | 
						||
    </head>
 | 
						||
    <body>
 | 
						||
        <h1 class="code">SEROUT</h1>
 | 
						||
        <div class="ImagePlusCaption">
 | 
						||
            <div class="Col2">
 | 
						||
                <p>
 | 
						||
                    <img src="../../graphics/pgm_icon1.gif" border="0" alt="BS1 icon" title="BS1 icon" />
 | 
						||
                    <img src="../../graphics/pgm_icon2.gif" border="0" alt="BS2 icon" title="BS2 icon" />
 | 
						||
                    <img src="../../graphics/pgm_icon2e.gif" border="0" alt="BS2e icon" title="BS2e icon" />
 | 
						||
                    <img src="../../graphics/pgm_icon2sx.gif" border="0" alt="BS2sx icon" title="BS2sx icon" />
 | 
						||
                    <img src="../../graphics/pgm_icon2p.gif" border="0" alt="BS2p icon" title="BS2p icon" />
 | 
						||
                    <img src="../../graphics/pgm_icon2pe.gif" border="0" alt="BS2pe icon" title="BS2pe icon" />
 | 
						||
                    <img src="../../graphics/pgm_icon2px.gif" border="0" alt="BS2px icon" title="BS2px icon" />
 | 
						||
                </p>
 | 
						||
            </div>
 | 
						||
            <p style="text-align: right;"><a href="../ExampleTopics/SeroutEx.htm" target="" title="" alt="" class="MCXref_0">SEROUT Examples</a>
 | 
						||
            </p>
 | 
						||
            <p> </p>
 | 
						||
        </div>
 | 
						||
        <p class="clear"> </p>
 | 
						||
        <p> </p>
 | 
						||
        <p class="PlainText">Syntax <img align="absmiddle" src="../../graphics/mini_1.gif" /> : <span class="keyword_in_text">SEROUT</span><i>Tpin</i>,<i> Baudmode</i>, ({<i>#</i>}<i> OutputData</i>)<br />Syntax <img align="absmiddle" src="../../graphics/mini_2.gif" /> : <span class="keyword_in_text">SEROUT</span><i>Tpin </i>{\<i>Fpin</i>},<i> Baudmode</i>, {<i>Pace</i>,} {<i>Timeout</i>,<i> Tlabel</i>,} [<i>OutputData</i>]</p>
 | 
						||
        <h2>Function</h2>
 | 
						||
        <p class="PlainText">Transmit asynchronous serial data (e.g., RS-232 data). 
 | 
						||
 | 
						||
</p>
 | 
						||
        <ul>
 | 
						||
            <li value="1"><b><i>Tpin</i></b> is a variable/constant/expression* (0 - 16) that specifies
 | 
						||
 the I/O pin through which the serial data will be transmitted. This pin will 
 | 
						||
 be set to output mode. On the BS2, BS2e, BS2sx, BS2p, BS2pe, and BS2px, if 
 | 
						||
    <i>Tpin</i> is set to 16, the BASIC Stamp uses the dedicated serial-output
 | 
						||
 pin (SOUT, physical pin 1), which is normally used by the Stamp Editor during
 | 
						||
 the download process.</li>
 | 
						||
            <li value="2"><b><i>Fpin</i></b> is an optional variable/constant/expression* (0 - 15) that
 | 
						||
 specifies the I/O pin to monitor for flow control status. This pin will be set
 | 
						||
 to input mode. NOTE: <i>Fpin</i> must be specified to use the optional 
 | 
						||
    <i>Timeout</i> and <i>Tlabel</i> arguments in the <span class="keyword_in_text">SEROUT</span> command.</li>
 | 
						||
            <li value="3"><b><i>Baudmode</i></b> is variable/constant/expression* (0 - 7 on the BS1, 
 | 
						||
 0 - 65535 on all other BASIC Stamp models) that specifies serial timing and 
 | 
						||
 configuration.</li>
 | 
						||
            <li value="4"><b><i>Pace</i></b> is an optional variable/constant/expression (0 - 65535)
 | 
						||
 that determines the length of the pause between transmitted bytes. NOTE: 
 | 
						||
    <i>Pace</i> cannot be used simultaneously with <i>Timeout</i>.</li>
 | 
						||
            <li value="5"><b><i>Timeout</i></b> is an optional variable/constant/expression* (0 - 65535)
 | 
						||
 that tells <span class="keyword_in_text">SEROUT</span> how long to wait for <i>Fpin</i> permission to send. 
 | 
						||
 If permission does not arrive in time, the program will jump to the address 
 | 
						||
 specified by <i>Tlabel</i>. NOTE: <i>Fpin</i> must be specified to use the
 | 
						||
 optional <i>Timeout</i> and <i>Tlabel</i> arguments in the <span class="keyword_in_text">SEROUT</span> 
 | 
						||
    command.</li>
 | 
						||
            <li value="6"><b><i>Tlabel</i></b> is an optional label that must be provided along with 
 | 
						||
    <i>Timeout</i>. <i>Tlabel</i> indicates where the program should go in the event
 | 
						||
 that permission to send data is not granted within the period specified by 
 | 
						||
    <i>Timeout</i>.</li>
 | 
						||
            <li value="7"><b><i>OutputData</i></b>‡ is a list of variables, constants, expressions
 | 
						||
 and formatters that tells <span class="keyword_in_text">SEROUT</span> how to format outgoing data. <span class="keyword_in_text">SEROUT</span>can transmit individual or repeating bytes, convert values into decimal, hex 
 | 
						||
 or binary text representations, or transmit strings of bytes from variable 
 | 
						||
 arrays. These actions can be combined in any order in the <i>OutputData</i> list.</li>
 | 
						||
        </ul>
 | 
						||
        <p class="PlainText">*<img src="../../graphics/bs1note.gif" style="vertical-align: super;" /> Note: expressions are not allowed as arguments on the BS1. The range
 | 
						||
 of the <i>Pin</i> argument on the BS1 is 0–7.</p>
 | 
						||
        <p class="PlainText">‡Note: The BS1's <i>OutputData</i> argument can only be a list of variables and the optional decimal modifier (#).</p>
 | 
						||
        <h2>Quick Facts</h2>
 | 
						||
        <table width="100%" cellpadding="4" cellspacing="0" border="1">
 | 
						||
            <tr bgcolor="#CFCFCF">
 | 
						||
                <td width="22%" align="center"> </td>
 | 
						||
                <td width="13%" align="center">BS1</td>
 | 
						||
                <td width="13%" align="center">BS2 and BS2e</td>
 | 
						||
                <td width="13%" align="center">BS2sx</td>
 | 
						||
                <td width="13%" align="center">BS2p</td>
 | 
						||
                <td width="13%" align="center">BS2pe</td>
 | 
						||
                <td width="13%" align="center">BS2px</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center" bgcolor="#CFCFCF">Units in <i>Pace</i></td>
 | 
						||
                <td align="center">N/A</td>
 | 
						||
                <td align="center">1 ms</td>
 | 
						||
                <td align="center">1 ms</td>
 | 
						||
                <td align="center">1 ms</td>
 | 
						||
                <td align="center">1 ms</td>
 | 
						||
                <td align="center">1 ms</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center" bgcolor="#CFCFCF">Units in <i>Timeout</i></td>
 | 
						||
                <td align="center">N/A</td>
 | 
						||
                <td align="center">1 ms</td>
 | 
						||
                <td align="center">0.4 ms</td>
 | 
						||
                <td align="center">0.4 ms</td>
 | 
						||
                <td align="center">1 ms</td>
 | 
						||
                <td align="center">0.4 ms</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center" bgcolor="#CFCFCF">Baud Range</td>
 | 
						||
                <td align="center">300, 600, 1200, and 2400 only</td>
 | 
						||
                <td align="center">243 to 50K</td>
 | 
						||
                <td align="center">608 to 115.2K</td>
 | 
						||
                <td align="center">608 to 115.2K</td>
 | 
						||
                <td align="center">243 to 50K</td>
 | 
						||
                <td align="center">972 to 115.2K</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center" bgcolor="#CFCFCF">Baud Limit with Flow Control</td>
 | 
						||
                <td align="center">N/A</td>
 | 
						||
                <td align="center">19.2K</td>
 | 
						||
                <td align="center">19.2K</td>
 | 
						||
                <td align="center">19.2K</td>
 | 
						||
                <td align="center">19.2K</td>
 | 
						||
                <td align="center">19.2K</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center" bgcolor="#CFCFCF">I/O Pins Available</td>
 | 
						||
                <td align="center">0 - 7</td>
 | 
						||
                <td align="center">0 - 15</td>
 | 
						||
                <td align="center">0 - 15</td>
 | 
						||
                <td align="center">0 - 15<br></br>(in current I/O block)</td>
 | 
						||
                <td align="center">0 - 15<br></br>(in current I/O block)</td>
 | 
						||
                <td align="center">0 - 15<br></br>(in current I/O block)</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center" bgcolor="#CFCFCF">Other Serial Port Pins</td>
 | 
						||
                <td align="center">N/A</td>
 | 
						||
                <td align="center" colspan="5">SOUT pin (physical pin 2) when <i>Tpin</i>= 16</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center" bgcolor="#CFCFCF">Related Commands</td>
 | 
						||
                <td align="center" colspan="1">
 | 
						||
                    <p colspan="" align="center"><a href="SERIN.htm" target="" title="" alt="" class="MCXref_0">SERIN</a>
 | 
						||
                    </p>
 | 
						||
                </td>
 | 
						||
                <td align="center" colspan="5">
 | 
						||
                    <p colspan="5" align="center"><a href="SERIN.htm" target="" title="" alt="" class="MCXref_0">SERIN</a>, <a href="DEBUG.htm" target="" title="" alt="" class="MCXref_0">DEBUG</a></p>
 | 
						||
                </td>
 | 
						||
            </tr>
 | 
						||
        </table>
 | 
						||
        <h2>Explanation</h2>
 | 
						||
        <p class="PlainText">One of the most popular forms of communication between electronic devices is serial
 | 
						||
communication. There are two major types of serial communication; asynchronous and
 | 
						||
synchronous. The <span class="keyword_in_text">SERIN</span> and <span class="keyword_in_text">SEROUT</span> commands are used to receive and 
 | 
						||
send asynchronous serial data. See the <a href="SHIFTIN.htm" target="" title="" alt="" class="MCXref_0">SHIFTIN</a> and 
 | 
						||
<a href="SHIFTOUT.htm" target="" title="" alt="" class="MCXref_0">SHIFTOUT</a> commands for information on the synchronous 
 | 
						||
method.</p>
 | 
						||
        <p class="Tip" MadCap:autonum="Tip:  "><span class="autonumber"><span class="TipSpan">Tip:  </span></span><b>Don't let your BASIC Stamp be mistaken for a mouse! </b>
 | 
						||
            <br />
 | 
						||
            <br />Connecting USB-based boards to your PC may cause the BASIC Stamp to reset. If your BASIC Stamp program immediately starts sending serial data to the computer via a <span class="keyword_in_text">DEBUG</span> or <span class="keyword_in_text">SEROUT</span> command, your computer's operating system may mistake it for a mouse or other serial plug-and-play device. To avoid this, place a 1-second pause at the beginning of the program.  </p>
 | 
						||
        <p class="PlainText">The following information is supplemental to what is discussed in the
 | 
						||
<span class="keyword_in_text">SERIN</span> command section. Please read through the <span class="keyword_in_text">SERIN</span> command
 | 
						||
section for additional information. </p>
 | 
						||
        <p>
 | 
						||
            <img src="../../graphics/bs2all_inline.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </p>
 | 
						||
        <p class="PlainText">All BASIC Stamp models (except the BS1) have a line driver on its SOUT pin (Tpin = 
 | 
						||
16). 
 | 
						||
The SOUT pin goes to a PC's serial data-in pin on the DB-9 connector built into 
 | 
						||
BASIC Stamp development boards. The connector is wired to allow both programming
 | 
						||
and run-time serial communication (unless you are using the Stamp 2 Carrier Board 
 | 
						||
which is only designed for programming). For the built-in serial port set the 
 | 
						||
<i>Tpin</i> argument to 16 in the <span class="keyword_in_text">SEROUT</span> command.</p>
 | 
						||
        <p class="PlainText">All BASIC Stamp models (including the BS1) can also transmit RS-232 data through any
 | 
						||
of their I/O pins (Tpin = 0 - 7 for BS1, Tpin = 0 - 15 on all other BASIC Stamp models).
 | 
						||
The I/O pins only provide a 0 to +5 volt swing (outside of RS-232 specs) and may 
 | 
						||
need to be connected through a line driver for proper operation with all serial 
 | 
						||
ports. Most serial ports are able to recognize a 0 to +5 volt swing, however. See
 | 
						||
the figure below sample wiring.</p>
 | 
						||
        <center>
 | 
						||
            <img src="../../graphics/serial_sch.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </center>
 | 
						||
        <p class="PlainText">Note: The connections shown in red are normally not necessary. They indicate optional connections to defeat hardware handshaking requirements.<br /></p>
 | 
						||
        <p class="PlainText">Note: A line driver may have to be used between the I/O pin and the receiving serial port to ensure proper communication.</p>
 | 
						||
        <p>
 | 
						||
            <center>
 | 
						||
                <table cellpadding="4" cellspacing="0" border="1">
 | 
						||
                    <tr bgcolor="#CFCFCF">
 | 
						||
                        <td width="175">Function</td>
 | 
						||
                        <td width="50" align="center">DB-9</td>
 | 
						||
                        <td width="50" align="center">DB-25</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td>Data Carrier Detect (DCD)</td>
 | 
						||
                        <td align="center">1</td>
 | 
						||
                        <td align="center">8</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td>Receive Data (RD)</td>
 | 
						||
                        <td align="center">2</td>
 | 
						||
                        <td align="center">3</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td>Transmit Data (TD)</td>
 | 
						||
                        <td align="center">3</td>
 | 
						||
                        <td align="center">2</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td>Data Terminal Ready (DTR)</td>
 | 
						||
                        <td align="center">4</td>
 | 
						||
                        <td align="center">20</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td>Signal Ground (SG)</td>
 | 
						||
                        <td align="center">5</td>
 | 
						||
                        <td align="center">7</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td>Data Set Ready (DSR)</td>
 | 
						||
                        <td align="center">6</td>
 | 
						||
                        <td align="center">6</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td>Request To Send (RTS)</td>
 | 
						||
                        <td align="center">7</td>
 | 
						||
                        <td align="center">4</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td>Clear To Send (CTS)</td>
 | 
						||
                        <td align="center">8</td>
 | 
						||
                        <td align="center">5</td>
 | 
						||
                    </tr>
 | 
						||
                </table>
 | 
						||
            </center>
 | 
						||
        </p>
 | 
						||
        <p> </p>
 | 
						||
        <p class="PlainText">The figure above shows the pinouts of the two styles of PC serial ports and how
 | 
						||
to connect them to the BASIC Stamp's I/O pin. Though not normally needed, the 
 | 
						||
figure also shows loop back connections that defeat hardware handshaking used by
 | 
						||
some PC software. Note that PC serial ports are always male connectors. The 25-pin
 | 
						||
style of serial port (called a DB-25) looks similar to a printer (parallel) port
 | 
						||
except that it is male, whereas a parallel port is female.</p>
 | 
						||
        <p class="PlainText">Asynchronous serial communication relies on precise timing. Both the sender and
 | 
						||
receiver must be set for identical timing, usually expressed in bits per second 
 | 
						||
(bps) called baud. </p>
 | 
						||
        <p class="PlainText">On all BASIC Stamp models, <span class="keyword_in_text">SEROUT</span> requires a value called Baudmode that tells
 | 
						||
it the important characteristics of the outgoing serial data; the bit period, number
 | 
						||
of data and parity bits, and polarity.</p>
 | 
						||
        <p>
 | 
						||
            <img src="../../graphics/bs1_inline.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </p>
 | 
						||
        <p class="PlainText">On the BS1, serial communication is limited to: no-parity, 8-data bits and 1-stop
 | 
						||
bit at one of four different speeds: 300, 600, 1200 or 2400 baud. The table below
 | 
						||
indicates the <i>Baudmode</i> value or symbols to use when selecting the desired 
 | 
						||
mode.</p>
 | 
						||
        <p>
 | 
						||
            <center>
 | 
						||
                <table cellpadding="4" cellspacing="0" border="1">
 | 
						||
                    <tr bgcolor="#CFCFCF">
 | 
						||
                        <td width="75" align="center" valign="top">Baudmode Value</td>
 | 
						||
                        <td width="75" align="center" valign="top">Symbol</td>
 | 
						||
                        <td width="75" align="center" valign="top">Baud Rate</td>
 | 
						||
                        <td width="225" align="center" valign="top">Polarity</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td align="center">0</td>
 | 
						||
                        <td align="center">T2400</td>
 | 
						||
                        <td align="center">2400</td>
 | 
						||
                        <td align="center">TRUE (always driven)</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td align="center">1</td>
 | 
						||
                        <td align="center">T1200</td>
 | 
						||
                        <td align="center">1200</td>
 | 
						||
                        <td align="center">TRUE (always driven)</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td align="center">2</td>
 | 
						||
                        <td align="center">T600</td>
 | 
						||
                        <td align="center">600</td>
 | 
						||
                        <td align="center">TRUE (always driven)</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td align="center">3</td>
 | 
						||
                        <td align="center">T300</td>
 | 
						||
                        <td align="center">300</td>
 | 
						||
                        <td align="center">TRUE (always driven)</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td align="center">4</td>
 | 
						||
                        <td align="center">N2400</td>
 | 
						||
                        <td align="center">2400</td>
 | 
						||
                        <td align="center">INVERTED (always driven)</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td align="center">5</td>
 | 
						||
                        <td align="center">N1200</td>
 | 
						||
                        <td align="center">1200</td>
 | 
						||
                        <td align="center">INVERTED (always driven)</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td align="center">6</td>
 | 
						||
                        <td align="center">N600</td>
 | 
						||
                        <td align="center">600</td>
 | 
						||
                        <td align="center">INVERTED (always driven)</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td align="center">7</td>
 | 
						||
                        <td align="center">N300</td>
 | 
						||
                        <td align="center">300</td>
 | 
						||
                        <td align="center">INVERTED (always driven)</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td align="center">8</td>
 | 
						||
                        <td align="center">OT2400</td>
 | 
						||
                        <td align="center">2400</td>
 | 
						||
                        <td align="center">TRUE (open drain, driven high)</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td align="center">9</td>
 | 
						||
                        <td align="center">OT1200</td>
 | 
						||
                        <td align="center">1200</td>
 | 
						||
                        <td align="center">TRUE (open drain, driven high)</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td align="center">10</td>
 | 
						||
                        <td align="center">OT600</td>
 | 
						||
                        <td align="center">600</td>
 | 
						||
                        <td align="center">TRUE (open drain, driven high)</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td align="center">11</td>
 | 
						||
                        <td align="center">OT300</td>
 | 
						||
                        <td align="center">300</td>
 | 
						||
                        <td align="center">TRUE (open drain, driven high)</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td align="center">12</td>
 | 
						||
                        <td align="center">ON2400</td>
 | 
						||
                        <td align="center">2400</td>
 | 
						||
                        <td align="center">INVERTED (open source, driven low)</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td align="center">13</td>
 | 
						||
                        <td align="center">ON1200</td>
 | 
						||
                        <td align="center">1200</td>
 | 
						||
                        <td align="center">INVERTED (open source, driven low)</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td align="center">14</td>
 | 
						||
                        <td align="center">ON600</td>
 | 
						||
                        <td align="center">600</td>
 | 
						||
                        <td align="center">INVERTED (open source, driven low)</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td align="center">15</td>
 | 
						||
                        <td align="center">N300</td>
 | 
						||
                        <td align="center">300</td>
 | 
						||
                        <td align="center">INVERTED (open source, driven low)</td>
 | 
						||
                    </tr>
 | 
						||
                </table>
 | 
						||
            </center>
 | 
						||
        </p>
 | 
						||
        <p> </p>
 | 
						||
        <p>
 | 
						||
            <img src="../../graphics/bs2all_inline.gif" border="0" />
 | 
						||
        </p>
 | 
						||
        <p class="PlainText">On the BS2, BS2e, BS2sx, BS2p, BS2pe, and BS2px, serial communication is very flexible. 
 | 
						||
The <i>Baudmode</i> argument for <span class="keyword_in_text">SEROUT</span> accepts a 16-bit value that determines
 | 
						||
its characteristics: 1-stop bit, 8-data bits/no-parity or 7-data bits/even-parity
 | 
						||
and virtually any speed from as low as 300 baud to greater than 100K baud (depending
 | 
						||
on the BASIC Stamp). The information below shows how <i>Baudmode</i> is calculated
 | 
						||
and show common baud modes for standard serial baud rates. </p>
 | 
						||
        <h3><i>Baudmode</i> Calculation:
 | 
						||
</h3>
 | 
						||
        <ol>
 | 
						||
            <li value="1">Determine the bit period (bits 0 - 11)
 | 
						||
    <ul><li value="1">BS2, BS2e, and BS2pe = INT(1,000,000 ÷ baud rate) - 20</li><li value="2">BS2sx and BS2p = INT(2,500,000 ÷ baud rate) - 20</li><li value="3">BS2px = INT(4,000,000 ÷ baud rate) - 20<br />        Note: INT means 'Convert to integer.' -- drop the numbers to the right
 | 
						||
 of the decimal point.</li></ul></li>
 | 
						||
            <li value="2">Set data bits and parity (bit 13)
 | 
						||
    <ul><li value="1">8-bit / No Parity = 0 ($0000)</li><li value="2">7-bit / Even Parity = 8192 ($2000)</li></ul></li>
 | 
						||
            <li value="3">Select polarity bit (bit 14)
 | 
						||
    <ul><li value="1">True = 0 ($0000)</li><li value="2">Inverted = 16384 ($4000)</li></ul></li>
 | 
						||
            <li value="4">Select driven or open output (bit 15)
 | 
						||
    <ul><li value="1">Driven = 0 ($0000)</li><li value="2">Open = 32768 ($8000)</li></ul></li>
 | 
						||
        </ol>
 | 
						||
        <p class="PlainText">Common Baud Rates and Corresponding <i>Baudmode</i> Values</p>
 | 
						||
        <p class="PlainText">
 | 
						||
            <img src="../../graphics/bs2epe_inline.gif" border="0" /> BS2, BS2e, BS2pe</p>
 | 
						||
        <table width="100%" cellpadding="4" cellspacing="0" border="1">
 | 
						||
            <tr bgcolor="#CFCFCF">
 | 
						||
                <td width="20%" align="center" valign="top">Baud Rate</td>
 | 
						||
                <td width="20%" align="center" valign="top">8-bit<br></br>No Parity<br></br>INVERTED</td>
 | 
						||
                <td width="20%" align="center" valign="top">8-bit<br></br>No Parity<br></br>TRUE</td>
 | 
						||
                <td width="20%" align="center" valign="top">7-bit<br></br>Even Parity<br></br>INVERTED</td>
 | 
						||
                <td width="20%" align="center" valign="top">7-bit<br></br>Even Parity<br></br>TRUE</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center">300</td>
 | 
						||
                <td align="center">19697 ($4CF1)</td>
 | 
						||
                <td align="center">3313 ($0CF1)</td>
 | 
						||
                <td align="center">27889 ($6CF1)</td>
 | 
						||
                <td align="center">11505 ($2CF1)</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center">600</td>
 | 
						||
                <td align="center">18030 ($466E)</td>
 | 
						||
                <td align="center">1646 ($066E)</td>
 | 
						||
                <td align="center">26222 ($666E)</td>
 | 
						||
                <td align="center">9838 ($266E)</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center">1200</td>
 | 
						||
                <td align="center">17197 ($432D)</td>
 | 
						||
                <td align="center">813 ($032D)</td>
 | 
						||
                <td align="center">25389 ($632D)</td>
 | 
						||
                <td align="center">9005 ($232D)</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center">2400</td>
 | 
						||
                <td align="center">16780 ($418C)</td>
 | 
						||
                <td align="center">396 ($018C)</td>
 | 
						||
                <td align="center">24972 ($618C)</td>
 | 
						||
                <td align="center">8588 ($218C)</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center">4800 </td>
 | 
						||
                <td align="center">16572 ($40BC)</td>
 | 
						||
                <td align="center">188 ($00BC)</td>
 | 
						||
                <td align="center">24764 ($60BC)</td>
 | 
						||
                <td align="center">8380 ($20BC)</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center">9600</td>
 | 
						||
                <td align="center">16468 ($4054)</td>
 | 
						||
                <td align="center">84 ($0054)</td>
 | 
						||
                <td align="center">24660 ($6054)</td>
 | 
						||
                <td align="center">8276 ($2054)</td>
 | 
						||
            </tr>
 | 
						||
        </table>
 | 
						||
        <p> </p>
 | 
						||
        <p> </p>
 | 
						||
        <p class="PlainText">
 | 
						||
            <img src="../../graphics/bsxp_inline.gif" border="0">
 | 
						||
            </img> BS2sx, BS2p</p>
 | 
						||
        <table width="100%" cellpadding="4" cellspacing="0" border="1">
 | 
						||
            <tr bgcolor="#CFCFCF">
 | 
						||
                <td width="20%" align="center" valign="top">Baud Rate</td>
 | 
						||
                <td width="20%" align="center" valign="top">8-bit<br></br>No Parity<br></br>INVERTED</td>
 | 
						||
                <td width="20%" align="center" valign="top">8-bit<br></br>No Parity<br></br>TRUE</td>
 | 
						||
                <td width="20%" align="center" valign="top">7-bit<br></br>Even Parity<br></br>INVERTED</td>
 | 
						||
                <td width="20%" align="center" valign="top">7-bit<br></br>Even Parity<br></br>TRUE</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center">1200</td>
 | 
						||
                <td align="center">18447 ($480F)</td>
 | 
						||
                <td align="center">2063 ($080F)</td>
 | 
						||
                <td align="center">26639 ($680F)</td>
 | 
						||
                <td align="center">10255 ($280F)</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center">2400</td>
 | 
						||
                <td align="center">17405 ($43FD)</td>
 | 
						||
                <td align="center">1021 ($03FD)</td>
 | 
						||
                <td align="center">25597 ($63FD)</td>
 | 
						||
                <td align="center">9213 ($23FD)</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center">4800 </td>
 | 
						||
                <td align="center">16884 ($41F4)</td>
 | 
						||
                <td align="center">500 ($01F4)</td>
 | 
						||
                <td align="center">25076 ($61F4)</td>
 | 
						||
                <td align="center">8692 ($21F4)</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center">9600</td>
 | 
						||
                <td align="center">16624 ($40F0)</td>
 | 
						||
                <td align="center">240 ($00F0)</td>
 | 
						||
                <td align="center">24816 ($60F0)</td>
 | 
						||
                <td align="center">8432 ($20F0)</td>
 | 
						||
            </tr>
 | 
						||
        </table>
 | 
						||
        <p> </p>
 | 
						||
        <p> </p>
 | 
						||
        <p class="PlainText">
 | 
						||
            <img src="../../graphics/bs2px_inline.gif" border="0">
 | 
						||
            </img> BS2px</p>
 | 
						||
        <table width="100%" cellpadding="4" cellspacing="0" border="1">
 | 
						||
            <tr bgcolor="#CFCFCF">
 | 
						||
                <td width="20%" align="center" valign="top">Baud Rate</td>
 | 
						||
                <td width="20%" align="center" valign="top">8-bit<br></br>No Parity<br></br>INVERTED</td>
 | 
						||
                <td width="20%" align="center" valign="top">8-bit<br></br>No Parity<br></br>TRUE</td>
 | 
						||
                <td width="20%" align="center" valign="top">7-bit<br></br>Even Parity<br></br>INVERTED</td>
 | 
						||
                <td width="20%" align="center" valign="top">7-bit<br></br>Even Parity<br></br>TRUE</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center">1200</td>
 | 
						||
                <td align="center">19697 ($4CF1)</td>
 | 
						||
                <td align="center">3313 ($0CF1)</td>
 | 
						||
                <td align="center">27889 ($6CF1)</td>
 | 
						||
                <td align="center">11505 ($2CF1)</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center">2400</td>
 | 
						||
                <td align="center">18030 ($466E)</td>
 | 
						||
                <td align="center">1646 ($066E)</td>
 | 
						||
                <td align="center">26222 ($666E)</td>
 | 
						||
                <td align="center">9838 ($266E)</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center">4800 </td>
 | 
						||
                <td align="center">17197 ($432D)</td>
 | 
						||
                <td align="center">813 ($032D)</td>
 | 
						||
                <td align="center">25389 ($632D)</td>
 | 
						||
                <td align="center">9005 ($232D)</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center">9600</td>
 | 
						||
                <td align="center">16780 ($418C)</td>
 | 
						||
                <td align="center">396 ($018C)</td>
 | 
						||
                <td align="center">24972 ($618C)</td>
 | 
						||
                <td align="center">8588 ($218C)</td>
 | 
						||
            </tr>
 | 
						||
        </table>
 | 
						||
        <p> </p>
 | 
						||
        <p class="Tip" MadCap:autonum="Tip:  "><span class="autonumber"><span class="TipSpan">Tip:  </span></span> For "open" baudmodes used in networking, add 32768 ($8000) to the values from the tables above. If the dedicated serial port (<i>Tpin</i> = 16) is used the data is inverted and driven regardless of the <i>Baudmode</i> setting.</p>
 | 
						||
        <p class="PlainText">If you're communicating with existing software or hardware, its speed(s) and 
 | 
						||
mode(s) will determine your choice of baud rate and mode. See the <span class="keyword_in_text">SERIN</span> 
 | 
						||
command description for more information.</p>
 | 
						||
        <p class="PlainText">The example below will transmit a single byte through I/O pin 1 at 2400 baud, 
 | 
						||
8N1, inverted: </p>
 | 
						||
        <p>
 | 
						||
            <img src="../../graphics/bs1_inline.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </p><pre class="BScode" xml:space="preserve">
 | 
						||
  SEROUT 1, N2400, (65)
 | 
						||
</pre>
 | 
						||
        <p>
 | 
						||
            <img src="../../graphics/bs2epe_inline.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </p><pre class="BScode" xml:space="preserve">
 | 
						||
  SEROUT 1, 16780, [65]                         ' baudmode set for BS2/e/pe
 | 
						||
</pre>
 | 
						||
        <p>
 | 
						||
            <img src="../../graphics/bsxp_inline.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </p><pre class="BScode" xml:space="preserve">
 | 
						||
  SEROUT 1, 17405, [65]                         ' baudmode set for BS2sx/p
 | 
						||
</pre>
 | 
						||
        <p class="PlainText">Here, <span class="keyword_in_text">SEROUT</span> will transmit a byte equal to 65 (the ASCII value of the 
 | 
						||
character "A") through pin 1. If the BASIC Stamp were connected to a PC running 
 | 
						||
a terminal program (set to the same baud rate) the character "A" would appear on
 | 
						||
the screen.</p>
 | 
						||
        <p class="PlainText">What if you really wanted the value 65 to appear on the screen? If you remember
 | 
						||
from the discussion in the <span class="keyword_in_text">SERIN</span> command, "It is up to the receiving side 
 | 
						||
(in serial communication) to interpret the values..." In this case, the PC is 
 | 
						||
interpreting the byte-sized value to be the ASCII code for the character "A". 
 | 
						||
Unless you're also writing the software for the PC, you can't change how the PC 
 | 
						||
interprets the incoming serial data, so to solve this problem, the data needs
 | 
						||
to be translated before it is sent.</p>
 | 
						||
        <p class="PlainText">The <span class="keyword_in_text">SEROUT</span> command provides a formatter, called the decimal formatter,
 | 
						||
which will translate the value 65 to two ASCII codes for the characters "6" and 
 | 
						||
"5" and then transmit them. Look at the following code:</p>
 | 
						||
        <p>
 | 
						||
            <img src="../../graphics/bs1_inline.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </p><pre class="BScode" xml:space="preserve">
 | 
						||
  SEROUT 1, N2400, (#65)
 | 
						||
</pre>
 | 
						||
        <p>
 | 
						||
            <img src="../../graphics/bs2epe_inline.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </p><pre class="BScode" xml:space="preserve">
 | 
						||
  SEROUT 1, 16780, [DEC 65]
 | 
						||
</pre>
 | 
						||
        <p>
 | 
						||
            <img src="../../graphics/bsxp_inline.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </p><pre class="BScode" xml:space="preserve">
 | 
						||
  SEROUT 1, 17405, [DEC 65]
 | 
						||
</pre>
 | 
						||
        <p class="PlainText">Notice the decimal formatter in the <span class="keyword_in_text">SEROUT</span> command. It is the "#" (for 
 | 
						||
the BS1) or "DEC" (for the other BASIC Stamp models) that appears just to the left of 
 | 
						||
the number 65. This tells <span class="keyword_in_text">SEROUT</span> to convert the number into separate ASCII 
 | 
						||
characters which represent the value in decimal form. If the value 65 in the code
 | 
						||
were changed to 123, the <span class="keyword_in_text">SEROUT</span> command would send three bytes (49, 50 and
 | 
						||
51) corresponding to the characters "1", "2" and "3".</p>
 | 
						||
        <p class="PlainText">The BS2, BS2e, BS2sx, BS2p, BS2pev and BS2px have many more conversion formatters
 | 
						||
available for the <span class="keyword_in_text">SEROUT</span> command. See below for more information.</p>
 | 
						||
        <p class="PlainText">The <span class="keyword_in_text">SEROUT</span> command sends quoted text exactly as it appears in the 
 | 
						||
<i>OutputData</i> list:</p>
 | 
						||
        <p>
 | 
						||
            <img src="../../graphics/bs1_inline.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </p><pre class="BScode" xml:space="preserve">
 | 
						||
  SEROUT 1, N2400, ("HELLO", CR)
 | 
						||
  SEROUT 1, N2400, ("Num = ", #100)
 | 
						||
</pre>
 | 
						||
        <p>
 | 
						||
            <img src="../../graphics/bs2epe_inline.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </p><pre class="BScode" xml:space="preserve">
 | 
						||
  SEROUT 1, 16780, ["HELLO", CR]
 | 
						||
  SEROUT 1, 16780, ["Num = ", DEC 100]
 | 
						||
</pre>
 | 
						||
        <p>
 | 
						||
            <img src="../../graphics/bsxp_inline.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </p><pre class="BScode" xml:space="preserve">
 | 
						||
  SEROUT 1, 17405, ["HELLO", CR]
 | 
						||
  SEROUT 1, 17405, ["Num = ", DEC 100]
 | 
						||
</pre>
 | 
						||
        <p class="PlainText">The above code will display "HELLO" on one line and "Num = 100" on the next 
 | 
						||
line. Notice that you can combine data to output in one <span class="keyword_in_text">SEROUT</span> command, 
 | 
						||
separated by commas. In the example above, we could have written it as one line 
 | 
						||
of code, with "HELLO", CR, "Num = ", DEC 100 in the <i>OutputData</i> list.</p>
 | 
						||
        <p>
 | 
						||
            <img src="../../graphics/bs1_inline.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </p>
 | 
						||
        <p class="PlainText">The BS1's <span class="keyword_in_text">SEROUT</span> command is limited to above-mentioned features. If you
 | 
						||
are not using a BS1, please continue reading about the additional features below.</p>
 | 
						||
        <p>
 | 
						||
            <img src="../../graphics/bsall_inline.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </p>
 | 
						||
        <p class="PlainText">The <span class="keyword_in_text">SEROUT</span> command can also be configured to pause between transmitted
 | 
						||
bytes. This is the purpose of the optional <i>Pace</i> argument. For example 
 | 
						||
(9600 baud N8, inverted): </p><pre class="BScode" xml:space="preserve">
 | 
						||
  SEROUT 1, 16468, 1000, ["Slowly"]             ' baudmode set for BS2
 | 
						||
</pre>
 | 
						||
        <p>Here, the BASIC Stamp transmits the word "Slowly" with a 1 second delay between
 | 
						||
each character. See the table above for units of the <i>Pace</i> argument. One 
 | 
						||
good reason to use the Pace feature is to support devices that require more than 
 | 
						||
one stop bit. Normally, the BASIC Stamp sends data as fast as it can (with a 
 | 
						||
minimum of 1 stop bit between bytes). Since a stop bit is really just a resting 
 | 
						||
state in the line (no data transmitted), using the <i>Pace</i> option will 
 | 
						||
effectively add multiple stop bits. Since the requirement for 2 or more stop bits
 | 
						||
(on some devices) is really just a "minimum" requirement, the receiving side should
 | 
						||
receive this data correctly. </p>
 | 
						||
        <p class="PlainText">Keep in mind that when we type something like "XYZ" into the <span class="keyword_in_text">SEROUT</span> 
 | 
						||
command, the BASIC Stamp actually uses the ASCII codes for each of those characters
 | 
						||
for its tasks. We could also typed: 88, 89, 90 in place of "XYZ" and the program 
 | 
						||
would run the same way since 88 is the ASCII code for the "X" character, 89 is the
 | 
						||
ASCII code for the "Y" character, and so on. </p>
 | 
						||
        <p class="PlainText">The decimal formatter is only one of a whole family of conversion formatters 
 | 
						||
available with <span class="keyword_in_text">SEROUT</span> on the BS2, BS2e, BS2sx, BS2p, BS2pe, and BS2px. See
 | 
						||
the table below for a list of available conversion formatters. All of the conversion
 | 
						||
formatters work similar to the decimal formatter. The formatters translate the
 | 
						||
value into separate bytes of data until the entire number is translated or until
 | 
						||
the indicated number of digits (in the case of the fixed length formatters) is
 | 
						||
translated. </p>
 | 
						||
        <p class="PlainText">
 | 
						||
            <img src="../../graphics/bs2all_inline.gif" border="0" />
 | 
						||
            <br />
 | 
						||
        </p>
 | 
						||
        <table width="100%" cellpadding="4" cellspacing="0" border="1">
 | 
						||
            <tr bgcolor="#CFCFCF" align="center" valign="top">
 | 
						||
                <td width="20%">Conversion Formatter</td>
 | 
						||
                <td width="70%">Type of Number</td>
 | 
						||
                <td width="10%">Notes</td>
 | 
						||
            </tr>
 | 
						||
            <tr align="center">
 | 
						||
                <td>DEC{1..5}</td>
 | 
						||
                <td align="left">Decimal, optionally fixed to 1 - 5 digits</td>
 | 
						||
                <td>1</td>
 | 
						||
            </tr>
 | 
						||
            <tr align="center">
 | 
						||
                <td>SDEC{1..5}</td>
 | 
						||
                <td align="left">Signed decimal, optionally fixed to 1 - 5 digits</td>
 | 
						||
                <td>1,2</td>
 | 
						||
            </tr>
 | 
						||
            <tr align="center">
 | 
						||
                <td>HEX{1..4}</td>
 | 
						||
                <td align="left">Hexadecimal, optionally fixed to 1 - 4 digits</td>
 | 
						||
                <td>1</td>
 | 
						||
            </tr>
 | 
						||
            <tr align="center">
 | 
						||
                <td>SHEX{1..4}</td>
 | 
						||
                <td align="left">Signed hexadecimal, optionally fixed to 1 - 4 digits </td>
 | 
						||
                <td>1,2</td>
 | 
						||
            </tr>
 | 
						||
            <tr align="center">
 | 
						||
                <td>IHEX{1..4}</td>
 | 
						||
                <td align="left">Indicated hexadecimal, optionally fixed to 1 - 4 digits</td>
 | 
						||
                <td>1</td>
 | 
						||
            </tr>
 | 
						||
            <tr align="center">
 | 
						||
                <td>ISHEX{1..4}</td>
 | 
						||
                <td align="left">Signed, indicated hexadecimal, optionally fixed to 1 - 4 digits ($ prefix)</td>
 | 
						||
                <td>1,2</td>
 | 
						||
            </tr>
 | 
						||
            <tr align="center">
 | 
						||
                <td>BIN{1..16}</td>
 | 
						||
                <td align="left">Binary, optionally fixed to1 - 16 digits</td>
 | 
						||
                <td>1</td>
 | 
						||
            </tr>
 | 
						||
            <tr align="center">
 | 
						||
                <td>SBIN{1..16}</td>
 | 
						||
                <td align="left">Signed binary, optionally fixed to 1 - 16 digits</td>
 | 
						||
                <td>1,2</td>
 | 
						||
            </tr>
 | 
						||
            <tr align="center">
 | 
						||
                <td>IBIN{1..16}</td>
 | 
						||
                <td align="left">Indicated binary, optionally fixed to 1 - 16 digits</td>
 | 
						||
                <td>1</td>
 | 
						||
            </tr>
 | 
						||
            <tr align="center">
 | 
						||
                <td>ISBIN{1..16}</td>
 | 
						||
                <td align="left">Signed, indicated binary, optionally fixed to 1 - 16 digits</td>
 | 
						||
                <td>1,2</td>
 | 
						||
            </tr>
 | 
						||
        </table>
 | 
						||
        <ol>
 | 
						||
            <li value="1">Fixed-digit formatters like DEC4 will pad the number with leading 0s if 
 | 
						||
 necessary; ex: DEC4 65 sends 0065. If a number is larger than the specified 
 | 
						||
 number of digits, the leading digits will be dropped; ex: DEC4 56422 sends 
 | 
						||
 6422.</li>
 | 
						||
            <li value="2">Signed modifiers work under two's complement rules.</li>
 | 
						||
        </ol>
 | 
						||
        <p class="PlainText">The BS2, BS2e, BS2sx, BS2p, BS2pe, and BS2px also have special formatters for 
 | 
						||
outputting a string of characters, repeated characters and undesirable
 | 
						||
characters.</p>
 | 
						||
        <p class="PlainText">
 | 
						||
            <img src="../../graphics/bs2all_inline.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </p>
 | 
						||
        <table width="100%" cellpadding="4" cellspacing="0" border="1">
 | 
						||
            <tr bgcolor="#CFCFCF" align="center" valign="top">
 | 
						||
                <td width="20%">Special Formatter</td>
 | 
						||
                <td width="80%">Action</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center">?</td>
 | 
						||
                <td>Displays "symbol = x' + carriage return; where x is a number. Default 
 | 
						||
 format is decimal, but may be combined with conversion formatters (ex: BIN
 | 
						||
 ? x to display "x = binary_number").</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center">ASC ?</td>
 | 
						||
                <td>Displays "symbol = 'x'" + carriage return; where x is an ASCII character.</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center">STR ByteArray {\L}</td>
 | 
						||
                <td>Send character string from an array. The optional \L argument can be used
 | 
						||
 to limit the output to L characters, otherwise, characters will be sent up 
 | 
						||
 to the first byte equal to 0 or the end of RAM space is reached.</td>
 | 
						||
            </tr>
 | 
						||
            <tr>
 | 
						||
                <td align="center">REP Byte\L</td>
 | 
						||
                <td>Send a string consisting of Byte repeated L times (ex: REP "X"\10 sends
 | 
						||
 "XXXXXXXXXX").</td>
 | 
						||
            </tr>
 | 
						||
        </table>
 | 
						||
        <p> </p>
 | 
						||
        <p class="PlainText">The string formatter is useful for transmitting a string of characters from a 
 | 
						||
byte array variable. A string of characters is a set of characters that are 
 | 
						||
arranged or accessed in a certain order. The characters "ABC" could be stored in
 | 
						||
a string with the "A" first, followed by the "B" and then followed by the "C." 
 | 
						||
A byte array is a similar concept to a string; it contains data that is arranged 
 | 
						||
in a certain order. Each of the elements in an array is the same size. The string
 | 
						||
"ABC" could be stored in a byte array containing three bytes (elements). </p>
 | 
						||
        <p class="PlainText">Here is an example that transmits five bytes (from a byte array) through I/O 
 | 
						||
pin 1 at 9600 bps, N81/inverted: </p>
 | 
						||
        <p>
 | 
						||
            <img src="../../graphics/bs2epe_inline.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </p><pre class="BScode" xml:space="preserve">
 | 
						||
serStr  VAR     Byte(5)                         ' Make a 5-byte array. 
 | 
						||
 | 
						||
Main:
 | 
						||
  serStr(0) = "H"                    
 | 
						||
  serStr(1) = "E"
 | 
						||
  serStr(2) = "L"
 | 
						||
  serStr(3) = "L"
 | 
						||
  serStr(4) = "O"
 | 
						||
 | 
						||
  SEROUT 1, 16468, [STR serStr\5]               ' Send 5-byte string
 | 
						||
</pre>
 | 
						||
        <p class="PlainText">Note that we use the optional \L argument of STR. If we didn't specify this, 
 | 
						||
the BASIC Stamp would try to keep sending characters until it found a byte equal
 | 
						||
to 0. Since we didn't specify a last byte of 0 in the array, we chose to tell it 
 | 
						||
explicitly to only send 5 characters. </p>
 | 
						||
        <p class="PlainText">Parity is a simple error-checking feature. When the <span class="keyword_in_text">SEROUT</span> command's 
 | 
						||
<i>Baudmode</i> is set for even parity it counts the number of 1s in the outgoing
 | 
						||
byte and uses the parity bit to make that number even. For instance, if it is 
 | 
						||
sending the 7-bit value: %0011010, it sets the parity bit to 1 in order to make
 | 
						||
an even number of 1s (four). </p>
 | 
						||
        <p class="PlainText">The receiver also counts the data bits to calculate what the parity bit should
 | 
						||
be. If it matches the parity bit received, the serial receiver assumes that the 
 | 
						||
data was received correctly. Of course, this is not necessarily true, since two 
 | 
						||
incorrectly received bits could make parity seem correct when the data was wrong,
 | 
						||
or the parity bit itself could be bad when the rest of the data was OK. Parity
 | 
						||
errors are only detected on the receiver side. Generally, the receiver determines
 | 
						||
how to handle the error. In a more robust application, the receiver and transmitter
 | 
						||
might be set up such that the receiver can request a re-send of data that was 
 | 
						||
received with a parity error.</p>
 | 
						||
        <p class="PlainText">When you design an application that requires serial communication between BASIC
 | 
						||
Stamp modules, you have to work within these limitations: </p>
 | 
						||
        <ul>
 | 
						||
            <li value="1">When the BASIC Stamp is sending or receiving data, it can't execute other 
 | 
						||
 instructions. </li>
 | 
						||
            <li value="2">When the BASIC Stamp is executing other instructions, it can't send or 
 | 
						||
 receive data. The BASIC Stamp does not have a serial buffer as there is in 
 | 
						||
 PCs. At most serial rates, the BASIC Stamp cannot receive data via <span class="keyword_in_text">SERIN</span>,
 | 
						||
 process it, and execute another <span class="keyword_in_text">SERIN</span> in time to catch the next chunk
 | 
						||
 of data, unless there are significant pauses between data transmissions.</li>
 | 
						||
        </ul>
 | 
						||
        <p class="PlainText">These limitations can sometimes be addressed by using flow control; the <i>Fpin</i>option for <span class="keyword_in_text">SERIN</span> and <span class="keyword_in_text">SEROUT</span> (at baud rates of up to the limitation 
 | 
						||
shown above). Through <i>Fpin</i>, <span class="keyword_in_text">SERIN</span> can tell a BASIC Stamp sender when
 | 
						||
it is ready to receive data and <span class="keyword_in_text">SEROUT</span> (on the sender) will wait for 
 | 
						||
permission to send. (For that matter, <i>Fpin</i> flow control follows the rules 
 | 
						||
of other serial handshaking schemes, but most computers other than the BASIC Stamp
 | 
						||
cannot start and stop serial transmission on a byte-by-byte basis. That's why this
 | 
						||
discussion is limited to communication between BASIC Stamp modules.)</p>
 | 
						||
        <p class="PlainText">Here's an example using flow control on the BS2 (data through I/O pin 1, flow
 | 
						||
control through I/O pin 0, 9600 baud, N8, non-inverted):</p>
 | 
						||
        <p>
 | 
						||
            <img src="../../graphics/bs2epe_inline.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </p><pre class="BScode" xml:space="preserve">
 | 
						||
sData   VAR     Byte
 | 
						||
 | 
						||
Main:
 | 
						||
  SEROUT 1\0, 84, [sData]                       ' baudmode set for BS2
 | 
						||
</pre>
 | 
						||
        <p class="PlainText">When <span class="keyword_in_text">SEROUT</span> executes, I/O pin 1 (<i>Tpin</i>) is made an output, and
 | 
						||
I/O pin 0 (<i>Fpin</i>) is made an input, to wait for the "go" signal from the 
 | 
						||
receiver. Here's the relationship of serial polarity to <i>Fpin</i> states. </p>
 | 
						||
        <p>
 | 
						||
            <center>
 | 
						||
                <table cellpadding="4" cellspacing="0" border="1">
 | 
						||
                    <tr bgcolor="#CFCFCF" align="center" valign="top">
 | 
						||
                        <td width="150"> </td>
 | 
						||
                        <td width="150">Ready to Receive<br></br>("Go")</td>
 | 
						||
                        <td width="150">Not Ready to Receive<br></br>("Stop")</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr align="center">
 | 
						||
                        <td>Inverted</td>
 | 
						||
                        <td><i>Fpin</i> is High (1)</td>
 | 
						||
                        <td><i>Fpin</i> is Low (0)</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr align="center">
 | 
						||
                        <td>Non-Inverted</td>
 | 
						||
                        <td><i>Fpin</i> is Low (0)</td>
 | 
						||
                        <td><i>Fpin</i> is High (1)</td>
 | 
						||
                    </tr>
 | 
						||
                </table>
 | 
						||
            </center>
 | 
						||
        </p>
 | 
						||
        <p> </p>
 | 
						||
        <p class="PlainText">See the <a href="../ExampleTopics/SeroutEx.htm" target="" title="" alt="" class="MCXref_0">SEROUT Examples</a> for a flow control example
 | 
						||
using two BS2s. In the demo program example, without flow control, the sender would
 | 
						||
transmit the whole word "HELLO!" in about 6 ms. The receiver would catch the first
 | 
						||
byte at most; by the time it got back from the first 1-second <span class="keyword_in_text">PAUSE</span>, the 
 | 
						||
rest of the data would be long gone. With flow control, communication is flawless
 | 
						||
since the sender waits for the receiver to catch up. </p>
 | 
						||
        <p class="PlainText">In the figure below, I/O pin 0, <i>Fpin</i>, is pulled to ground through a 10
 | 
						||
kΩ resistor. This is to ensure that the sender sees a stop signal (0 for 
 | 
						||
inverted communications) when the receiver is being programmed.</p>
 | 
						||
        <center>
 | 
						||
            <img src="../../graphics/flow_ctrl_sch.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </center>
 | 
						||
        <p> </p>
 | 
						||
        <p class="PlainText">In the flow control examples above, the only way the <span class="keyword_in_text">SEROUT</span> instruction 
 | 
						||
will end (other than RESET or power-off) is if the receiver allows it to send the
 | 
						||
entire <i>OutputData</i> list. If <i>Fpin</i> permission never occurs, the program
 | 
						||
is stuck. However, you can tell the BASIC Stamp to abort <span class="keyword_in_text">SEROUT</span> if it doesn't
 | 
						||
receive <i>Fpin</i> permission within a specified time period. For instance, to 
 | 
						||
transmit a decimal number through pin 1 at 9600 baud, 8N, inverted and abort
 | 
						||
<span class="keyword_in_text">SEROUT</span> after 2 seconds (2000 ms) if no <i>Fpin</i> permission arrives on
 | 
						||
I/O pin 0: </p>
 | 
						||
        <p>
 | 
						||
            <img src="../../graphics/bs2epe_inline.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </p><pre class="BScode" xml:space="preserve">
 | 
						||
Main:
 | 
						||
  SEROUT 1\0, 16468, 2000, No_Permission, [DEC 150]
 | 
						||
  STOP
 | 
						||
 | 
						||
No_Permission: 
 | 
						||
  DEBUG CLS, "Timed out"
 | 
						||
  STOP
 | 
						||
</pre>
 | 
						||
        <p>If no <i>Fpin</i> permission arrives within 2 seconds, the program aborts 
 | 
						||
<span class="keyword_in_text">SEROUT</span> and continues at the label NoPermission.</p>
 | 
						||
        <p class="PlainText">The <span class="keyword_in_text">SEROUT</span> command supports open-drain and open-source output, which 
 | 
						||
makes it possible to network multiple BASIC Stamp modules on a single pair of wires. These
 | 
						||
"open baudmodes" only actively drive the <i>Tpin</i> in one state (for the other 
 | 
						||
state, they simply disconnect the pin; setting it to an input mode). If two BASIC
 | 
						||
Stamp modules in a network had their <span class="keyword_in_text">SEROUT</span> lines connected together (while a third
 | 
						||
device listened on that line) and the BASIC Stamp modules were using always-driven 
 | 
						||
baudmodes, they could simultaneously output two opposite states (i.e., +5 volts and
 | 
						||
ground). This would create a short circuit. The heavy current flow would likely 
 | 
						||
damage the I/O pins or the BASIC Stamp modules themselves. Since the open baudmodes only
 | 
						||
drive in one state and float in the other, there's no chance of this kind of short.</p>
 | 
						||
        <p class="PlainText">The polarity selected for <span class="keyword_in_text">SEROUT</span> determines which state is driven and 
 | 
						||
which is open as shown below:</p>
 | 
						||
        <p>
 | 
						||
            <center>
 | 
						||
                <table cellpadding="4" cellspacing="0" border="1">
 | 
						||
                    <tr bgcolor="#CFCFCF" align="center" valign="top">
 | 
						||
                        <td width="125"> </td>
 | 
						||
                        <td width="100">State (0)</td>
 | 
						||
                        <td width="100">State (1)</td>
 | 
						||
                        <td width="100">Resistor pulled to...</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr align="center">
 | 
						||
                        <td>Inverted</td>
 | 
						||
                        <td>Open</td>
 | 
						||
                        <td>Driven</td>
 | 
						||
                        <td>Gnd (Vss)</td>
 | 
						||
                    </tr>
 | 
						||
                    <tr align="center">
 | 
						||
                        <td>Non-Inverted</td>
 | 
						||
                        <td>Driven</td>
 | 
						||
                        <td>Open</td>
 | 
						||
                        <td>+5V (Vdd)</td>
 | 
						||
                    </tr>
 | 
						||
                </table>
 | 
						||
            </center>
 | 
						||
        </p>
 | 
						||
        <p> </p>
 | 
						||
        <p class="PlainText">Since open baudmodes only drive to one state, they need a resistor to pull the
 | 
						||
networked line into the other state, as shown in the table above and the figures 
 | 
						||
below.</p>
 | 
						||
        <p class="PlainText">Open baudmodes allow the BASIC Stamp to share a line, but it is up to your 
 | 
						||
program to resolve other networking issues such as who talks when and how to 
 | 
						||
detect, prevent and fix data errors.</p>
 | 
						||
        <p>
 | 
						||
            <center>
 | 
						||
                <table cellpadding="4" cellspacing="0" border="0">
 | 
						||
                    <tr>
 | 
						||
                        <td>SEROUT Open-Drain circuit:<br /></td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td>
 | 
						||
                            <img src="../../graphics/serout_od.gif" border="0">
 | 
						||
                            </img>
 | 
						||
                        </td>
 | 
						||
                    </tr>
 | 
						||
                </table>
 | 
						||
            </center>
 | 
						||
        </p>
 | 
						||
        <p>
 | 
						||
            <center>
 | 
						||
                <table cellpadding="4" cellspacing="0" border="0">
 | 
						||
                    <tr>
 | 
						||
                        <td><span class="keyword_in_text">SEROUT</span> Open-Source circuit:<br></br></td>
 | 
						||
                    </tr>
 | 
						||
                    <tr>
 | 
						||
                        <td>
 | 
						||
                            <img src="../../graphics/serout_os.gif" border="0">
 | 
						||
                            </img>
 | 
						||
                        </td>
 | 
						||
                    </tr>
 | 
						||
                </table>
 | 
						||
            </center>
 | 
						||
        </p>
 | 
						||
        <h3>SEROUT Troubleshooting</h3>
 | 
						||
        <p class="PlainText">Serial communication, because of its complexity, can be very difficult to work
 | 
						||
with at times. Please follow these guidelines when developing a project using the
 | 
						||
<span class="keyword_in_text">SERIN</span> and <span class="keyword_in_text">SEROUT</span> commands:</p>
 | 
						||
        <ol>
 | 
						||
            <li value="1">Always build your project in steps.
 | 
						||
    <ol type="a"><li value="1">Start with small, manageable pieces of code, that deals with serial 
 | 
						||
 communication) and test them, one at a time.</li><li value="2">Add more and more small pieces, testing them each time, as you go.</li><li value="3">Never write a large portion of code that works with serial communication
 | 
						||
 without testing its smallest workable pieces first.</li></ol></li>
 | 
						||
            <li value="2">Pay attention to timing.
 | 
						||
    <ol type="a"><li value="1">Be very careful to calculate and overestimate the amount of time 
 | 
						||
 operations should take within the BASIC Stamp. Misunderstanding the timing
 | 
						||
 constraints is the source of most problems with code that communicate 
 | 
						||
 serially.</li><li value="2">If the serial communication in your project is bidirectional, the above
 | 
						||
 statement is even more critical.</li></ol></li>
 | 
						||
            <li value="3">Pay attention to wiring.
 | 
						||
    <ol type="a"><li value="1">Take extra time to study and verify serial communication wiring diagrams.
 | 
						||
 A mistake in wiring can cause strange problems in communication, or no 
 | 
						||
 communication at all. Make sure to connect the ground pins (Vss) between 
 | 
						||
 the devices that are communicating serially.</li></ol></li>
 | 
						||
            <li value="4">Verify port setting on the PC and in the <span class="keyword_in_text">SERIN</span>/<span class="keyword_in_text">SEROUT</span> commands.
 | 
						||
    <ol type="a"><li value="1">Unmatched settings on the sender and receiver side will cause garbled 
 | 
						||
 data transfers or no data transfers. If the data you receive is unreadable,
 | 
						||
 it is most likely a baud rate setting error.</li></ol></li>
 | 
						||
            <li value="5">If data transmitted to the Stamp Editor's Debug Terminal is garbled, verify
 | 
						||
 the output format.
 | 
						||
    <ol type="a"><li value="1">A common mistake is to send data with <span class="keyword_in_text">SEROUT</span> in ASCII format. For
 | 
						||
 example, <span class="keyword_in_text">SEROUT</span> 16, 84, [ 0 ] instead of <span class="keyword_in_text">SEROUT</span> 16, 84, 
 | 
						||
 [ DEC 0 ]. The first example will send a byte equal to 0 to the PC, 
 | 
						||
 resulting in the <span class="keyword_in_text">DEBUG</span> Terminal clearing the screen (since 0 is 
 | 
						||
 the control character for a clear-screen action).</li></ol></li>
 | 
						||
            <li value="6">When using a BS2-family module, you can simplify <i>Baudmode</i> parameter
 | 
						||
 selection by using conditional compilation. The example below can be 
 | 
						||
 included in the standard programming template and will handle most serial
 | 
						||
 IO requirements. Remember that defining constants does not affect compiled
 | 
						||
 program space, and yet does make program debugging easier.
 | 
						||
  </li>
 | 
						||
        </ol>
 | 
						||
        <p>
 | 
						||
            <img src="../../graphics/bs2all_inline.gif" border="0">
 | 
						||
            </img>
 | 
						||
        </p><pre class="BScode" xml:space="preserve">
 | 
						||
' {$PBASIC 2.5}  
 | 
						||
 | 
						||
#SELECT $STAMP
 | 
						||
  #CASE BS2, BS2E, BS2PE
 | 
						||
    T1200       CON     813
 | 
						||
    T2400       CON     396
 | 
						||
    T4800       CON     188
 | 
						||
    T9600       CON     84
 | 
						||
    T19K2       CON     32
 | 
						||
    T38K4       CON     6
 | 
						||
  #CASE BS2SX, BS2P
 | 
						||
    T1200       CON     2063
 | 
						||
    T2400       CON     1021
 | 
						||
    T4800       CON     500
 | 
						||
    T9600       CON     240
 | 
						||
    T19K2       CON     110
 | 
						||
    T38K4       CON     45
 | 
						||
  #CASE BS2PX
 | 
						||
    T1200       CON     3313
 | 
						||
    T2400       CON     1646
 | 
						||
    T4800       CON     813
 | 
						||
    T9600       CON     396
 | 
						||
    T19K2       CON     188
 | 
						||
    T38K4       CON     84
 | 
						||
#ENDSELECT
 | 
						||
 | 
						||
SevenBit        CON     $2000
 | 
						||
Inverted        CON     $4000
 | 
						||
Open            CON     $8000
 | 
						||
 | 
						||
Baud            CON     T9600 + Inverted        ' match DEBUG
 | 
						||
</pre>
 | 
						||
        <div class="Col2">
 | 
						||
            <div class="MasterFoot">
 | 
						||
                <p MadCap:conditions="BSEconditions.BSEWebHelp (Primary)-INCLUDE"><a href="../../HomeTopics/HomePage.htm">Go to Welcome page</a>
 | 
						||
                </p>
 | 
						||
            </div>
 | 
						||
        </div>
 | 
						||
        <div class="Col2">
 | 
						||
            <div class="MasterFoot">
 | 
						||
                <p style="text-align: right;"><span class="ContactInfoProjectName">BASIC Stamp Help</span> <![CDATA[ ]]><span class="ContactInfoVersion#">Version 2.5.4</span> <![CDATA[ ]]></p>
 | 
						||
                <p style="text-align: right;">Copyright © <span class="ContactInfoCompanyName">Parallax Inc.</span></p>
 | 
						||
                <p style="text-align: right;"><span class="SystemShortDate">8/8/2012</span>
 | 
						||
                </p>
 | 
						||
            </div>
 | 
						||
        </div>
 | 
						||
        <script type="text/javascript">/* <![CDATA[ */
 | 
						||
 | 
						||
			var _gaq = _gaq || [];
 | 
						||
			_gaq.push(['_setAccount', 'UA-285614-1']);
 | 
						||
			_gaq.push(['_trackPageview']);
 | 
						||
 | 
						||
			(function() {
 | 
						||
			var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
 | 
						||
			ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
 | 
						||
			var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
 | 
						||
			})();
 | 
						||
 | 
						||
		/* ]]> */</script>
 | 
						||
        <script type="text/javascript" src="../../SkinSupport/MadCapBodyEnd.js">
 | 
						||
        </script>
 | 
						||
    </body>
 | 
						||
</html> |