1235 lines
69 KiB
HTML
1235 lines
69 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>SERIN</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">SERIN</h1>
|
||
<div class="ImagePlusCaption">
|
||
<div class="Col2">
|
||
<p class="PlainText">
|
||
<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" /><span class="code_in_text"> {PBASIC 2.5}</span>
|
||
</p>
|
||
</div>
|
||
<p style="text-align: right;"><a href="../ExampleTopics/SerinEx.htm" target="" title="" alt="" class="MCXref_0">SERIN Examples</a>
|
||
</p>
|
||
<p> </p>
|
||
</div>
|
||
<p class="clear"> </p>
|
||
<p> </p>
|
||
<p>Syntax <img align="absmiddle" src="../../graphics/mini_1.gif" /> : <span class="keyword_in_text">SERIN</span> <![CDATA[ ]]><i>Rpin</i>,<i> Baudmode</i>, {(<i>Qualifier</i>),} {<i>#</i>}<i> InputData</i><br />Syntax <img align="absmiddle" src="../../graphics/mini_2.gif" /> :
|
||
<span class="keyword_in_text">SERIN</span> <![CDATA[ ]]><i>Rpin </i>{\<i>Fpin</i>},<i> Baudmode</i>, {<i>Plabel</i>,} {<i>Timeout</i>,<i> Tlabel</i>,} [<i>InputData</i>]</p>
|
||
<h2>Function</h2>
|
||
<p>Receive asynchronous serial data (e.g., RS-232 data).
|
||
|
||
</p>
|
||
<ul>
|
||
<li value="1"><b><i>Rpin</i></b> is a variable/constant/expression* (0 - 16) that specifies
|
||
the I/O pin through which the serial data will be received. This pin will be
|
||
set to input mode. On the BS2, BS2e, BS2sx, BS2p, BS2pe, and BS2px, if Rpin is
|
||
set to 16, the BASIC Stamp uses the dedicated serial-input pin (SIN, physical
|
||
pin 2), 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 indicate flow control status on. This pin will
|
||
be set to output mode.</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>Qualifier</i></b> is an optional variable/constant (0 - 255) indicating
|
||
data that must be received before execution can continue. Multiple qualifiers
|
||
can be indicated with commas separating them.</li>
|
||
<li value="5"><b><i>Plabel</i></b> is an optional label indicating where the program should
|
||
go in the event of a parity error. This argument should only be provided if
|
||
<i>Baudmode</i> indicates 7 bits, and even parity.</li>
|
||
<li value="6"><b><i>Timeout</i></b> is an optional variable/constant/expression* (0 - 65535)
|
||
that tells <span class="keyword_in_text">SERIN</span> how long to wait for incoming data. If data does not
|
||
arrive in time, the program will jump to the address specified by <i>Tlabel</i>.</li>
|
||
<li value="7"><b><i>Tlabel</i></b> is an optional label that must be provided along with
|
||
<i>Timeout</i>, indicating where the program should go in the event that data
|
||
does not arrive within the period specified by <i>Timeout</i>.</li>
|
||
<li value="8"><b><i>InputData</i></b>‡ is a list of variables and formatters that
|
||
tells <span class="keyword_in_text">SERIN</span> what to do with incoming data. <span class="keyword_in_text">SERIN</span> can store data
|
||
in a variable or array, interpret numeric text (decimal, binary, or hex) and
|
||
store the corresponding value in a variable, wait for a fixed or variable
|
||
sequence of bytes, or ignore a specified number of bytes. These actions can
|
||
be combined in any order in the <i>InputData</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>‡<img src="../../graphics/bs1note.gif" style="vertical-align: super;" /> Note: The BS1's <i>InputData</i> argument can only be a list of variables and the optional decimal modifier (#). <![CDATA[ ]]></p>
|
||
<p> </p>
|
||
<p>(Jump down to <a href="#Baudmode" target="" title="" alt="" class="MCXref_0">Baudmode Calculation</a>; <a href="#Baud" target="" title="" alt="" class="MCXref_0">Baud Rate Tables</a>; <a href="#Decimal" target="" title="" alt="" class="MCXref_0">Decimal Formatter Specifics</a>; <a href="#Convers" target="" title="" alt="" class="MCXref_0"> Conversion Formatter Table</a>; <a href="#Special" target="" title="" alt="" class="MCXref_0">Special Formatter Tables</a>; <a href="#SERIN" target="" title="" alt="" class="MCXref_0">SERIN Troubleshooting</a>)</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>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">Limit to Qualifiers</td>
|
||
<td align="center">Unlimited</td>
|
||
<td align="center" colspan="5">6 (in WAIT formatter)</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">SIN pin (physical pin 2) when <i>Rpin</i>= 16</td>
|
||
</tr>
|
||
<tr>
|
||
<td align="center" bgcolor="#CFCFCF">Related Commands</td>
|
||
<td align="center" colspan="1">
|
||
<p colspan="" align="center"><a href="SEROUT.htm" target="" title="" alt="" class="MCXref_0">SEROUT</a>
|
||
</p>
|
||
</td>
|
||
<td align="center" colspan="5">
|
||
<p colspan="5" align="center"><a href="SEROUT.htm" target="" title="" alt="" class="MCXref_0">SEROUT</a>, <a href="DEBUGIN.htm" target="" title="" alt="" class="MCXref_0">DEBUGIN</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="PlainText"><span class="keyword_in_text">SERIN</span> can wait for, filter and convert incoming data in powerful ways.
|
||
<span class="keyword_in_text">SERIN</span> deserves some lengthy discussion, below, since all this power brings
|
||
some complexity. </p>
|
||
<p class="PlainText">The term asynchronous means "no clock." More specifically, "asynchronous serial
|
||
communication" means data is transmitted and received without the use of a separate
|
||
"clock" wire. Data can be sent using as little as two wires; one for data and one
|
||
for ground. The PC's serial ports (also called COM ports or RS-232 ports) use
|
||
asynchronous serial communication. Note: the other kind of serial communication,
|
||
synchronous, uses at least three wires; one for clock, one for data and one for
|
||
ground.</p>
|
||
<p class="PlainText">RS-232 is the electrical specification for the signals that PC serial ports use.
|
||
Unlike normal logic, where a 5 volts is a logic 1 and 0 volts is logic 0, RS-232
|
||
uses -12 volts for logic 1 and +12 volts for logic 0. This specification allows
|
||
communication over longer wire lengths without amplification.</p>
|
||
<p class="PlainText">Most circuits that work with RS-232 use a line driver/receiver. This component
|
||
does two things: (1) it converts the ±12 volts of RS-232 to TTL-compatible 0 to
|
||
5-volt levels and (2) it inverts the relationship of the voltage levels, so that
|
||
5 volts = logic 1 and 0 volts = logic 0. Note: When connecting a line driver/receiver to the BASIC Stamp, make sure to power the line driver/receiver with 5 volts DC from a regulated source other than BASIC Stamp's 5-volt VDD pin; the BASIC Stamp's on-board regulator may be unable to provide the required amount of current to the line driver/receiver chip.</p>
|
||
<p>
|
||
<img src="../../graphics/bs2all_inline.gif" border="0">
|
||
</img>
|
||
</p>
|
||
<p>
|
||
<br>
|
||
</br>
|
||
</p>
|
||
<p class="PlainText">All BASIC Stamp models (except the BS1) have a line receiver on its SIN pin (Rpin = 16). The SIN pin goes to a PC's serial data-out 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 Rpin argument to 16 in the SERIN command.<br /></p>
|
||
<p class="PlainText">All BASIC Stamp models (including the BS1) can also receive RS-232 data through any
|
||
of their I/O pins (<i>Rpin</i> = 0 - 7 for BS1, <i>Rpin</i> = 0 - 15 on all other
|
||
BASIC Stamp models). The I/O pins don't need a line receiver, just a 22 kΩ resistor.
|
||
The resistor limits current into the I/O pin's built-in clamping diodes, which keep
|
||
input voltages within a safe range.</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.</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 (the 22 kΩ resister is not
|
||
needed if connecting to the SIN 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>On all BASIC Stamp models, <span class="keyword_in_text">SERIN</span> requires a value called Baudmode that tells
|
||
it the important characteristics of the incoming serial data; the bit period,
|
||
number of data and parity bits, and polarity.</p>
|
||
<p> </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="75" 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</td>
|
||
</tr>
|
||
<tr>
|
||
<td align="center">1</td>
|
||
<td align="center">T1200</td>
|
||
<td align="center">1200</td>
|
||
<td align="center">TRUE</td>
|
||
</tr>
|
||
<tr>
|
||
<td align="center">2</td>
|
||
<td align="center">T600</td>
|
||
<td align="center">600</td>
|
||
<td align="center">TRUE</td>
|
||
</tr>
|
||
<tr>
|
||
<td align="center">3</td>
|
||
<td align="center">T300</td>
|
||
<td align="center">300</td>
|
||
<td align="center">TRUE</td>
|
||
</tr>
|
||
<tr>
|
||
<td align="center">4</td>
|
||
<td align="center">N2400</td>
|
||
<td align="center">2400</td>
|
||
<td align="center">INVERTED</td>
|
||
</tr>
|
||
<tr>
|
||
<td align="center">5</td>
|
||
<td align="center">N1200</td>
|
||
<td align="center">1200</td>
|
||
<td align="center">INVERTED</td>
|
||
</tr>
|
||
<tr>
|
||
<td align="center">6</td>
|
||
<td align="center">N600</td>
|
||
<td align="center">600</td>
|
||
<td align="center">INVERTED</td>
|
||
</tr>
|
||
<tr>
|
||
<td align="center">7</td>
|
||
<td align="center">N300</td>
|
||
<td align="center">300</td>
|
||
<td align="center">INVERTED</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">SERIN</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><a name="Baudmode"></a><b><i>Baudmode</i></b> 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>
|
||
</ol>
|
||
<h3><a name="Baud"></a>Baud Rate Tables</h3>
|
||
<p class="PlainText">These tables give common baud rates and their corresponding <i>Baudmode</i> <![CDATA[ ]]>values.<br /></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 class="Tip" MadCap:autonum="Tip:  "><span class="autonumber"><span class="TipSpan">Tip:  </span></span>The BS2, BS2e, and BS2pe may have trouble synchronizing with the incoming serial stream at this 4800 baud and higher while using certain complex expressions, arrays, and/or formatters.  Try using simple variables and WAIT or WAITSTR formatters to achieve the necessary results.</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="Tip" MadCap:autonum="Tip:  "><span class="autonumber"><span class="TipSpan">Tip:  </span></span>The BS2sx and BS2p may have trouble synchronizing with the incoming serial stream at this 4800 baud and higher while using certain complex expressions, arrays, and/or formatters.  Try using simple variables and WAIT or WAITSTR formatters to achieve the necessary results.</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 class="PlainText"> </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. In general,
|
||
7-bit/even-parity (7E) mode is used for text, and 8-bit/no-parity (8N) for
|
||
byte-oriented data. Note: the most common mode is 8-bit/no-parity, even when
|
||
the data transmitted is just text. Most devices that use a 7-bit data mode do
|
||
so in order to take advantage of the parity feature. Parity can detect some
|
||
communication errors, but to use it you lose one data bit. This means that
|
||
incoming data bytes transferred in 7E (even-parity) mode can only represent
|
||
values from 0 to 127, rather than the 0 to 255 of 8N (no-parity) mode.</p>
|
||
<p class="PlainText">Usually a device requires only one stop bit per byte. Occasionally, however,
|
||
you may find a device that requires 2 or more stop bits. Since a stop bit is
|
||
really just a delay between transmitted bytes (leaving the line in a resting state)
|
||
the BASIC Stamp can receive transmissions with multiple stop bits per byte without
|
||
any trouble. In fact, sometimes it is desirable to have multiple stop bits (see
|
||
the "SERIN Troubleshooting" section, below, for more information).</p>
|
||
<p class="PlainText">The example below will receive 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">
|
||
SYMBOL sData = B0
|
||
|
||
Main:
|
||
SERIN 1, N2400, sData
|
||
END
|
||
</pre>
|
||
<p>
|
||
<img src="../../graphics/bs2all_inline.gif" border="0">
|
||
</img>
|
||
</p><pre class="BScode" xml:space="preserve">
|
||
sData VAR Byte
|
||
|
||
Main:
|
||
SERIN 1, 16780, [sData] ' baudmode set for BS2
|
||
END
|
||
</pre>
|
||
<p class="PlainText">Here, <span class="keyword_in_text">SERIN</span> will wait for and receive a single byte of data through pin
|
||
1 and store it in the variable sData. If the BASIC Stamp were connected to a PC
|
||
running a terminal program (set to the same baud rate) and the user pressed the
|
||
"A" key on the keyboard, after the <span class="keyword_in_text">SERIN</span> command executed, the variable
|
||
sData would contain 65, the ASCII code for the letter "A".</p>
|
||
<p class="PlainText">What would happen if, using the example above, the user pressed the "1" key?
|
||
The result would be that sData would contain the value 49 (the ASCII code for
|
||
the character "1"). This is a critical point to remember: every time you press
|
||
a character on the keyboard, the computer receives the ASCII value of that
|
||
character. It is up to the receiving side (in serial communication) to interpret
|
||
the values as necessary. In this case, perhaps we actually wanted sData to end
|
||
up with the value 1, rather than the ASCII code 49.</p>
|
||
<p class="PlainText">The <span class="keyword_in_text">SERIN</span> command provides a formatter, called the decimal formatter,
|
||
which will interpret this for us. Look at the following code: </p>
|
||
<p>
|
||
<img src="../../graphics/bs1_inline.gif" border="0">
|
||
</img>
|
||
</p><pre class="BScode" xml:space="preserve">
|
||
SYMBOL sData = B2
|
||
|
||
Main:
|
||
SERIN 1, N2400, #sData
|
||
END
|
||
</pre>
|
||
<p>
|
||
<img src="../../graphics/bs2all_inline.gif" border="0">
|
||
</img>
|
||
</p><pre class="BScode" xml:space="preserve">
|
||
sData VAR Byte
|
||
|
||
Main:
|
||
SERIN 1, 16780, [DEC sData]
|
||
END
|
||
</pre>
|
||
<p class="PlainText">Notice the decimal formatter in the <span class="keyword_in_text">SERIN</span> command. It is the "#" (for
|
||
the BS1) or "DEC" (for the other BASIC Stamp models) that appears just to the left of
|
||
the sData variable. This tells <span class="keyword_in_text">SERIN</span> to convert incoming text representing
|
||
decimal numbers into true-decimal form and store the result in sData. If the
|
||
user running the terminal software pressed the "1", "2" and then "3" keys followed
|
||
by a space or other non-numeric text, the value 123 will be stored in sData.
|
||
Afterwards, the program can perform any numeric operation on the number just like
|
||
with any other number. Without the decimal formatter, however, you would have been
|
||
forced to receive each character ("1", "2" and "3") separately, and then would
|
||
still have to do some manual conversion to arrive at the number 123 (one hundred
|
||
twenty three) before you can do the desired calculations on it.</p>
|
||
<h3><a name="Decimal"></a>Decimal Formatter Specifics</h3>
|
||
<p class="PlainText">The decimal formatter is designed to seek out text that represents decimal numbers.
|
||
The characters that represent decimal numbers are the characters "0" through "9".
|
||
Once the <span class="keyword_in_text">SERIN</span> command is asked to use the decimal formatter for a particular
|
||
variable, it monitors the incoming serial data, looking for the first decimal
|
||
character. Once it finds the first decimal character, it will continue looking
|
||
for more (accumulating the entire multi-digit number) until is finds a non-decimal
|
||
numeric character. Keep in mind that it will not finish until it finds at least
|
||
one decimal character followed by at least one non-decimal character.</p>
|
||
<p class="PlainText">To further illustrate this, consider the following examples (assuming we're
|
||
using the same code example as above):</p>
|
||
<ol>
|
||
<li value="1"><b>Serial input</b>: ABC<br></br><b>Result</b>: The BASIC Stamp halts at the SERIN command, continuously
|
||
waiting for decimal text.</li>
|
||
<li value="2"><b>Serial input</b>: 123 (with no characters following it)<br></br><b>Result</b>: The BASIC Stamp halts at the SERIN command. It recognizes
|
||
the characters "1", "2" and "3" as the number one hundred twenty three, but
|
||
since no characters follow the "3", it waits continuously, since there's no
|
||
way to tell whether 123 is the entire number or not.</li>
|
||
<li value="3"><b>Serial input</b>: 123 (followed by a space character)<br></br><b>Result</b>: Similar to example 2, above, except once the space character
|
||
is received, the BASIC Stamp knows the entire number is 123, and stores this
|
||
value in sData. The SERIN command then ends, allowing the next line of code,
|
||
if any, to run.</li>
|
||
<li value="4"><b>Serial input</b>: 123A<br></br><b>Result</b>: Same as example 3, above. The "A" character, just like the
|
||
space character, is the first non-decimal text after the number 123, indicating
|
||
to the BASIC Stamp that it has received the entire number.</li>
|
||
<li value="5"><b>Serial input</b>: ABCD123EFGH<br></br> Result: Similar to examples 3 and 4 above. The characters "ABCD" are ignored
|
||
(since they're not decimal text), the characters "123" are evaluated to be the
|
||
number 123 and the following character, "E", indicates to the BASIC Stamp that
|
||
it has received the entire number.</li>
|
||
</ol>
|
||
<p class="PlainText">Of course, as with all numbers in the BASIC Stamp, the final result is limited
|
||
to 16 bits (up to the number 65535). If a number larger than this is received by
|
||
the decimal formatter, the end result will look strange because the result
|
||
rolled-over the maximum 16-bit value. </p>
|
||
<p class="PlainText">While BS1 is limited to the decimal formatter (#) shown above, the BS2, BS2e,
|
||
BS2sx, BS2p, BS2pe, and BS2px have many more conversion formatters available for the
|
||
<span class="keyword_in_text">SERIN</span> command. If not using a BS1, see the "Additional Conversion Formatters"
|
||
section below for more information.</p>
|
||
<p class="PlainText">The <span class="keyword_in_text">SERIN</span> command can also be configured to wait for specified data
|
||
before it retrieves any additional input. For example, suppose a device that is
|
||
attached to the BASIC Stamp is known to send many different sequences of data,
|
||
but the only data you desire happens to appear right after the unique characters,
|
||
"XYZ". The BS1 has optional <i>Qualifier</i> arguments for this purpose. On the
|
||
BS2, BS2e, BS2sx, BS2p, BS2pe, and BS2px a special formatter called WAIT can be
|
||
used for this. </p>
|
||
<p>
|
||
<img src="../../graphics/bs1_inline.gif" border="0">
|
||
</img>
|
||
</p><pre class="BScode" xml:space="preserve">
|
||
SYMBOL sData = B2
|
||
|
||
Main:
|
||
SERIN 1, N2400, ("XYZ"), #sData
|
||
END
|
||
</pre>
|
||
<p>
|
||
<img src="../../graphics/bs2all_inline.gif" border="0">
|
||
</img>
|
||
</p><pre class="BScode" xml:space="preserve">
|
||
sData VAR Byte
|
||
|
||
Main:
|
||
SERIN 1, 16780, [WAIT("XYZ"), DEC sData]
|
||
END
|
||
</pre>
|
||
<p class="PlainText">The above code waits for the characters "X", "Y" and "Z" to be received, in
|
||
that order, and then it looks for a decimal number to follow. If the device in
|
||
this example were to send the characters "XYZ100" followed by a carriage return
|
||
or some other non-decimal numeric character, the sData variable would end up with
|
||
the number 100 after the <span class="keyword_in_text">SERIN</span> line finishes. If the device sent some data
|
||
other than "XYZ" followed by a number, the BASIC Stamp would continue to wait at
|
||
the <span class="keyword_in_text">SERIN</span> command.</p>
|
||
<p class="PlainText">The BS1 will accept an unlimited number of Qualifiers. The BS2, BS2e, BS2sx,
|
||
BS2p, BS2pe, and BS2px will only accept up to six bytes (characters) in the <span class="keyword_in_text">WAIT</span>formatter.</p>
|
||
<p class="PlainText">Keep in mind that when we type "XYZ" into the <span class="keyword_in_text">SERIN</span> command, the BASIC
|
||
Stamp actually uses the ASCII codes for each of those characters for its tasks.
|
||
We could also have typed: 88, 89, 90 in place of "XYZ" and the code 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. Also note, serial communication with the BASIC
|
||
Stamp is case sensitive. If the device mentioned above sent, "xYZ" or "xyZ", or
|
||
some other combination of lower and upper-case characters, the BASIC Stamp would
|
||
have ignored it because we told it to look for "XYZ" (all capital letters).</p>
|
||
<p class="PlainText">The BS1's <span class="keyword_in_text">SERIN</span> command is limited to previously-mentioned features. If
|
||
you are not using a BS1, please continue reading about the additional features
|
||
below.</p>
|
||
<p class="PlainText">The decimal formatter is only one of a whole family of conversion formatters
|
||
available with <span class="keyword_in_text">SERIN</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 (as described in the "Decimal
|
||
Formatter Specifics" section, above). The formatters receive bytes of data,
|
||
waiting for the first byte that falls within the range of characters they accept
|
||
(e.g., "0" or "1" for binary, "0" to "9" for decimal, "0" to "9" and "A" to "F"
|
||
for hex, and "+" or "-" for signed variations of any type). Once they receive a
|
||
numeric character, they keep accepting input until a non-numeric character arrives
|
||
or (in the case of the fixed length formatters) the maximum specified number of
|
||
digits arrives. </p>
|
||
<p class="PlainText">While very effective at filtering and converting input text, the formatters
|
||
aren't completely foolproof. As mentioned before, many conversion formatters will
|
||
keep accepting text until the first non-numeric text arrives, even if the resulting
|
||
value exceeds the size of the variable. After <span class="keyword_in_text">SERIN</span>, a byte variable will
|
||
contain the lowest 8 bits of the value entered and a word would contain the lowest
|
||
16 bits. You can control this to some degree by using a formatter that specifies
|
||
the number of digits, such as DEC2, which would accept values only in the range of
|
||
0 to 99. </p>
|
||
<h3><a name="Convers"></a>
|
||
<img src="../../graphics/bs2all_inline.gif" border="0" /> Conversion Formatter Table<br /></h3>
|
||
<table width="100%" cellpadding="4" cellspacing="0" border="1">
|
||
<tr bgcolor="#CFCFCF" align="center" valign="top">
|
||
<td width="15%">Conversion Formatter</td>
|
||
<td width="45%">Type of Number</td>
|
||
<td width="30%">Numeric Characters Accepted</td>
|
||
<td width="10%">Notes</td>
|
||
</tr>
|
||
<tr align="center">
|
||
<td>DEC{1..5}</td>
|
||
<td>Decimal, optionally limited to 1 - 5 digits</td>
|
||
<td>0 through 9</td>
|
||
<td>1</td>
|
||
</tr>
|
||
<tr align="center">
|
||
<td>SDEC{1..5}</td>
|
||
<td>Signed decimal, optionally limited to 1 - 5 digits</td>
|
||
<td>-, 0 through 9</td>
|
||
<td>1,2</td>
|
||
</tr>
|
||
<tr align="center">
|
||
<td>HEX{1..4}</td>
|
||
<td>Hexadecimal, optionally limited to 1 - 4 digits</td>
|
||
<td>0 through 9, A through F</td>
|
||
<td>1,3</td>
|
||
</tr>
|
||
<tr align="center">
|
||
<td>SHEX{1..4}</td>
|
||
<td>Signed hexadecimal, optionally limited to 1 - 4 digits </td>
|
||
<td>-, 0 through 9, A through F</td>
|
||
<td>1,2,3</td>
|
||
</tr>
|
||
<tr align="center">
|
||
<td>IHEX{1..4}</td>
|
||
<td>Indicated hexadecimal, optionally limited to 1 - 4 digits</td>
|
||
<td>$, 0 through 9, A through F</td>
|
||
<td>1,3,4</td>
|
||
</tr>
|
||
<tr align="center">
|
||
<td>ISHEX{1..4}</td>
|
||
<td>Signed, indicated hexadecimal, optionally limited to 1 - 4 digits</td>
|
||
<td>-, $, 0 through 9, A through F</td>
|
||
<td>1,2,3,4</td>
|
||
</tr>
|
||
<tr align="center">
|
||
<td>BIN{1..16}</td>
|
||
<td>Binary, optionally limited to1 - 16 digits</td>
|
||
<td>0, 1</td>
|
||
<td>1</td>
|
||
</tr>
|
||
<tr align="center">
|
||
<td>SBIN{1..16}</td>
|
||
<td>Signed binary, optionally limited to 1 - 16 digits</td>
|
||
<td>-, 0, 1</td>
|
||
<td>1,2</td>
|
||
</tr>
|
||
<tr align="center">
|
||
<td>IBIN{1..16}</td>
|
||
<td>Indicated binary, optionally limited to 1 - 16 digits</td>
|
||
<td>%, 0, 1</td>
|
||
<td>1,4</td>
|
||
</tr>
|
||
<tr align="center">
|
||
<td>ISBIN{1..16}</td>
|
||
<td>Signed, indicated binary, optionally limited to 1 - 16 digits</td>
|
||
<td>-, %, 0, 1</td>
|
||
<td>1,2,4</td>
|
||
</tr>
|
||
<tr align="center">
|
||
<td>NUM</td>
|
||
<td>Generic numeric input; hex or binary number must be indicated</td>
|
||
<td>$, %, 0 through 9, A through F</td>
|
||
<td>1,3,4</td>
|
||
</tr>
|
||
<tr align="center">
|
||
<td>SNUM</td>
|
||
<td>Similar to NUM with value treated as signed with range -32768 to +32767</td>
|
||
<td>-, $, %, 0 through 9, A through F</td>
|
||
<td>1,2,3,4</td>
|
||
</tr>
|
||
</table>
|
||
<ol>
|
||
<li value="1">All numeric conversions will continue to accept new data until receiving
|
||
either the specified number of digits (ex: three digits for DEC3) or a non-numeric character.</li>
|
||
<li value="2">To be recognized as part of a number, the minus sign (-) must immediately
|
||
precede a numeric character. The minus sign character occurring in non-numeric
|
||
text is ignored and any character (including a space) between a minus and a
|
||
number causes the minus to be ignored.</li>
|
||
<li value="3">The hexadecimal formatters are not case-sensitive; "a" through "f" means
|
||
the same as "A" through "F".</li>
|
||
<li value="4">Indicated hexadecimal and binary formatters ignore all characters, even
|
||
valid numerics, until they receive the appropriate prefix ($ for hexadecimal,
|
||
% for binary). The indicated formatters can differentiate between text and
|
||
hexadecimal (ex: ABC would be interpreted by HEX as a number but IHEX would
|
||
ignore it unless expressed as $ABC). Likewise, the binary version can
|
||
distinguish the decimal number 10 from the binary number %10. A prefix
|
||
occurring in non-numeric text is ignored, and any character (including a
|
||
space) between a prefix and a number causes the prefix to be ignored. Indicated,
|
||
signed formatters require that the minus sign come before the prefix, as in
|
||
-$1B45.</li>
|
||
</ol>
|
||
<h3><a name="Special"></a>Special Formatter Tables</h3>
|
||
<p class="PlainText">The BS2, BS2e, BS2sx, BS2p, BS2pe, and BS2px also have special formatters
|
||
for handling a string of characters, a sequence of characters and undesirable
|
||
characters. See the table below for a list of these special formatters.</p>
|
||
<p>
|
||
<img src="../../graphics/bs2all_inline.gif" border="0">
|
||
</img>
|
||
<br>
|
||
</br>
|
||
<br>
|
||
</br>
|
||
</p>
|
||
<table width="100%" cellpadding="4" cellspacing="0" border="1">
|
||
<tr bgcolor="#CFCFCF" align="center" valign="top">
|
||
<td width="150">Special Formatter</td>
|
||
<td>Action</td>
|
||
</tr>
|
||
<tr>
|
||
<td align="center">STR ByteArray \L {\E}</td>
|
||
<td>Input a character string of length L into an array. If specified, an end
|
||
character E causes the string input to end before reaching length L.
|
||
Remaining bytes are filled with 0s (zeros).</td>
|
||
</tr>
|
||
<tr>
|
||
<td align="center">WAIT (Value)</td>
|
||
<td>Wait for a sequence of bytes specified by value. Value can be numbers
|
||
separated by commas or quoted text (ex: 65, 66, 67 or "ABC"). The WAIT
|
||
formatter is limited to a maximum of six characters.</td>
|
||
</tr>
|
||
<tr>
|
||
<td align="center">WAITSTR ByteArray {\L}</td>
|
||
<td>Wait for a sequence of bytes matching a string stored in an array variable,
|
||
optionally limited to L characters. If the optional L argument is left off,
|
||
the end of the array-string must be marked by a byte containing a zero (0).</td>
|
||
</tr>
|
||
<tr>
|
||
<td align="center">SKIP Length</td>
|
||
<td>Ignore Length bytes of characters.</td>
|
||
</tr>
|
||
</table>
|
||
<p> </p>
|
||
<p>
|
||
<img src="../../graphics/bs2ppepx_inline.gif" border="0" />
|
||
<br />
|
||
<br />
|
||
</p>
|
||
<table width="100%" cellpadding="4" cellspacing="0" border="1">
|
||
<tr bgcolor="#CFCFCF" align="center" valign="top">
|
||
<td width="150">Special Formatter</td>
|
||
<td>Action</td>
|
||
</tr>
|
||
<tr>
|
||
<td align="center">SPSTR L</td>
|
||
<td>Buffer length L bytes (up to 126) of serial characters to Scratchpad RAM,
|
||
starting at location 0. Use <a href="GET.htm" target="" title="" alt="" class="MCXref_0">GET</a> to retrieve the
|
||
characters.</td>
|
||
</tr>
|
||
</table>
|
||
<p> </p>
|
||
<p class="PlainText">The string formatter is useful for receiving a string of characters into 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 receives nine bytes through I/O pin 1 at 9600 bps,
|
||
N81/inverted and stores them in a 10-byte array: </p>
|
||
<p>
|
||
<img src="../../graphics/bs2all_inline.gif" border="0">
|
||
</img>
|
||
</p><pre class="BScode" xml:space="preserve">
|
||
serStr VAR Byte(10) ' Make a 10-byte array
|
||
|
||
Main:
|
||
serStr(9) = 0 ' Put 0 in last byte
|
||
SERIN 1, 16468, [STR serStr\9] ' Get 9-byte string
|
||
DEBUG STR serString ' Display the string
|
||
END
|
||
</pre>
|
||
<p class="PlainText">If the serial input were "hello*" <span class="keyword_in_text">DEBUG</span> would display "hello" since it
|
||
collects bytes up to (but not including) the end character. It fills the unused
|
||
bytes up to the specified length with 0s. <span class="keyword_in_text">DEBUG</span>'s normal STR formatter
|
||
understands a 0 to mean end-of-string. However, if you use <span class="keyword_in_text">DEBUG</span>'s
|
||
fixed-length string modifier, STR ByteArray\L, you will inadvertently clear the
|
||
<span class="keyword_in_text">DEBUG</span> screen. The fixed-length specification forces <span class="keyword_in_text">DEBUG</span> to read
|
||
and process the 0s at the end of the string, and 0 is equivalent to <span class="keyword_in_text">DEBUG</span>'s
|
||
CLS (clear-screen) instruction! Be alert for the consequences of mixing fixed-
|
||
and variable-length string operations. </p>
|
||
<p class="PlainText">As shown before, <span class="keyword_in_text">SERIN</span> can compare incoming data with a predefined
|
||
sequence of bytes using the WAIT formatter. The simplest form waits for a sequence
|
||
of up to six bytes specified as part of the <i>InputData</i> list, like so: </p>
|
||
<p>
|
||
<img src="../../graphics/bs2all_inline.gif" border="0">
|
||
</img>
|
||
</p><pre class="BScode" xml:space="preserve">
|
||
SERIN 1, 16468, [WAIT ("SESAME")] ' Wait for word SESAME
|
||
DEBUG "Password accepted"
|
||
</pre>
|
||
<p class="PlainText"><span class="keyword_in_text">SERIN</span> will wait for that word, and the program will not continue until
|
||
it is received. Since WAIT is looking for an exact match for a sequence of bytes,
|
||
it is case-sensitive-"sesame" or "SESAmE" or any other variation from "SESAME"
|
||
would be ignored. </p>
|
||
<p class="PlainText"><span class="keyword_in_text">SERIN</span> can also wait for a sequence that matches a string stored in an
|
||
array variable with the WAITSTR formatter. In the example below, we'll capture a
|
||
string with STR then have WAITSTR look for an exact match: </p>
|
||
<p>
|
||
<img src="../../graphics/bs2all_inline.gif" border="0">
|
||
</img>
|
||
</p><pre class="BScode" xml:space="preserve">
|
||
serStr VAR Byte(10) ' Make a 10-byte array
|
||
|
||
Main:
|
||
serString(9) = 0 ' Put 0 in last byte
|
||
SERIN 1, 16468, [STR serStr\9\"!"] ' Input password string
|
||
DEBUG "Waiting for: ", STR serString, CR
|
||
SERIN 1, 16468, [WAITSTR serStr] ' Wait for the password
|
||
DEBUG "Password accepted!", CR
|
||
END
|
||
</pre>
|
||
<p class="PlainText">You can also use WAITSTR with fixed-length strings as in the following
|
||
example: </p>
|
||
<p>
|
||
<img src="../../graphics/bs2all_inline.gif" border="0">
|
||
</img>
|
||
</p>
|
||
<p>
|
||
<br>
|
||
</br>
|
||
</p><pre class="BScode" xml:space="preserve">
|
||
serStr VAR Byte(4) ' Make a 4-byte array
|
||
|
||
Main:
|
||
DEBUG "Enter a 4-character password", CR
|
||
SERIN 1, 16468, [STR serStr\4] ' Get the string
|
||
DEBUG "Waiting for: ", STR serStr\4, CR
|
||
SERIN 1, 16468, [WAITSTR serStr\4] ' Wait for a match
|
||
DEBUG "Password accepted!", CR
|
||
END
|
||
</pre>
|
||
<p class="PlainText"><span class="keyword_in_text">SERIN</span>'s <i>InputData</i> can be structured as a sophisticated list of
|
||
actions to perform on the incoming data. This allows you to process incoming data
|
||
in powerful ways. For example, suppose you have a serial stream that contains
|
||
"pos: xxxx yyyy" (where xxxx and yyyy are 4-digit numbers) and you want to capture
|
||
just the decimal y value. The following code would do the trick: </p>
|
||
<p>
|
||
<img src="../../graphics/bs2all_inline.gif" border="0">
|
||
</img>
|
||
</p><pre class="BScode" xml:space="preserve">
|
||
yCoord VAR Word ' y coordinate
|
||
|
||
Main:
|
||
SERIN 1, 16468, [WAIT ("pos: "), SKIP 4, DEC yCoord]
|
||
DEBUG ? yCoord
|
||
END
|
||
</pre>
|
||
<p class="PlainText">The items of the <i>InputData</i> list work together to locate the label "pos: ",
|
||
skip over the four-byte x data, then convert and capture the decimal y data. This
|
||
sequence assumes that the x data is always four digits long; if its length varies,
|
||
the following code would be more appropriate: </p>
|
||
<p>
|
||
<img src="../../graphics/bs2all_inline.gif" border="0">
|
||
</img>
|
||
</p><pre class="BScode" xml:space="preserve">
|
||
yCoord VAR Word
|
||
|
||
Main:
|
||
SERIN 1, 16468, [WAIT ("pos: "), DEC yCoord, DEC yCoord]
|
||
DEBUG ? yOffset
|
||
END
|
||
</pre>
|
||
<p class="PlainText">The unwanted x data is stored in yCoord then replaced by the desired y data.
|
||
This is a sneaky way to filter out a number of any size without using an extra
|
||
variable. With a little creativity, you can combine the <i>InputData</i> modifiers
|
||
to filter and extract almost any data. </p>
|
||
<p class="PlainText">Parity is a simple error-checking feature. When a serial sender is set for even
|
||
parity (the mode the BASIC Stamp modules support) it counts the number of 1s in an 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>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 okay. </p>
|
||
<p class="PlainText">Many systems that work exclusively with text use (or can be set for)
|
||
7-bit/even-parity mode. The tables above show appropriate <i>BaudMode</i> settings
|
||
for different BASIC Stamp models. For example, with the BS2, to receive one data byte
|
||
through pin 1 at 9600 baud, 7E, inverted: </p>
|
||
<p>
|
||
<img src="../../graphics/bs2all_inline.gif" border="0">
|
||
</img>
|
||
</p><pre class="BScode" xml:space="preserve">
|
||
sData VAR Byte
|
||
|
||
Main:
|
||
SERIN 1, 24660, [sData]
|
||
END
|
||
</pre>
|
||
<p class="PlainText">That instruction will work, but it doesn't tell the BS2 what to do in the event
|
||
of a parity error. Here's an improved version that uses the optional <i>Plabel</i>argument: </p>
|
||
<p>
|
||
<img src="../../graphics/bs2all_inline.gif" border="0">
|
||
</img>
|
||
</p><pre class="BScode" xml:space="preserve">
|
||
sData VAR Byte
|
||
|
||
Main:
|
||
SERIN 1, 24660, Bad_Data, [sData]
|
||
DEBUG ? sData
|
||
STOP
|
||
|
||
Bad_Data:
|
||
DEBUG "Parity error"
|
||
STOP
|
||
</pre>
|
||
<p class="PlainText">If the parity matches, the program continues at the <span class="keyword_in_text">DEBUG</span> instruction
|
||
after <span class="keyword_in_text">SERIN</span>. If the parity doesn't match, the program goes to the label
|
||
Bad_Data. Note that a parity error takes precedence over other <i>InputData</i>
|
||
specifications (as soon as an error is detected, <span class="keyword_in_text">SERIN</span> aborts and goes to
|
||
the <i>Plabel</i> routine). </p>
|
||
<p class="PlainText">In all the examples above, the only way to end the <span class="keyword_in_text">SERIN</span> instruction
|
||
(other than RESET or power-off) is to give <span class="keyword_in_text">SERIN</span> the serial data it wants.
|
||
If no serial data arrives, the program is stuck. However, you can tell the BASIC
|
||
Stamp to abort <span class="keyword_in_text">SERIN</span> if it doesn't receive data within a specified number
|
||
of milliseconds. For instance, to receive a decimal number through pin 1 at 9600
|
||
baud, 8N, inverted and abort <span class="keyword_in_text">SERIN</span> after two seconds (2000 milliseconds)
|
||
of inactivity on the serial input.</p>
|
||
<p>
|
||
<img src="../../graphics/bs2all_inline.gif" border="0">
|
||
</img>
|
||
</p><pre class="BScode" xml:space="preserve">
|
||
result VAR Byte
|
||
|
||
Main:
|
||
SERIN 1, 16468, 2000, No_Data, [DEC result]
|
||
DEBUG CLS, ? result
|
||
STOP
|
||
|
||
No_Data:
|
||
DEBUG CLS, "Timed out"
|
||
STOP
|
||
</pre>
|
||
<p class="PlainText">If no data arrives within two seconds, the program aborts <span class="keyword_in_text">SERIN</span> and
|
||
continues at the label No_Data. Note that on multi-byte input, the timeout timer
|
||
is reset after the receipt of any valid data byte; with long timeout values this
|
||
factor could have an adverse affect on program operation if data packets are
|
||
transmitted with gaps between individual data bytes. Finally, be cautious when
|
||
using very short timeout values. Without external flow control, very short
|
||
timeout values may cause the program to branch to the <i>Tlabel</i> address
|
||
unnecessarily.</p>
|
||
<p class="PlainText">Here's a very important concept: this timeout feature is not picky about the
|
||
kind of data <span class="keyword_in_text">SERIN</span> receives; if any serial data is received, it prevents
|
||
the timeout. In the example above, <span class="keyword_in_text">SERIN</span> wants a decimal number. But even
|
||
if <span class="keyword_in_text">SERIN</span> received letters "ABCD..." at intervals of less than two seconds,
|
||
it would never abort. </p>
|
||
<p class="PlainText">You can combine parity and serial timeout. Here is an example for the BS2
|
||
designed to receive a decimal number through pin 1 at 2400 baud, 7E, inverted
|
||
with a 10-second timeout: </p>
|
||
<p>
|
||
<img src="../../graphics/bs2all_inline.gif" border="0">
|
||
</img>
|
||
</p><pre class="BScode" xml:space="preserve">
|
||
result VAR Byte
|
||
|
||
Again:
|
||
SERIN 1, 24660, Bad_Data, 10000, No_Data, [DEC result]
|
||
DEBUG CLS, ? result
|
||
GOTO Again
|
||
|
||
No_Data:
|
||
DEBUG CLS, "Timed out"
|
||
GOTO Again
|
||
|
||
Bad_Data:
|
||
DEBUG CLS, "Parity error"
|
||
GOTO Again
|
||
</pre>
|
||
<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. (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/bs2all_inline.gif" border="0">
|
||
</img>
|
||
</p><pre class="BScode" xml:space="preserve">
|
||
sData VAR Byte
|
||
|
||
Main:
|
||
SERIN 1\0, 84, [sData]
|
||
STOP
|
||
</pre>
|
||
<p class="PlainText">When <span class="keyword_in_text">SERIN</span> executes, I/O pin 1 (<i>Rpin</i>) is made an input in
|
||
preparation for incoming data, and I/O pin 0 (<i>FPin</i>) is made output low,
|
||
to signal "go" to the sender. After <span class="keyword_in_text">SERIN</span> finishes receiving, I/O pin 0
|
||
goes high to tell the sender to stop. If an inverted <i>BaudMode</i> had been
|
||
specified, the <i>FPin</i>'s responses would have been reversed. 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 example program 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 1.5 ms. The receiver would catch the first
|
||
byte at most; by the time it got back from the first 1-second <b>PAUSE</b>, 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>
|
||
<h3><a name="SERIN"></a>
|
||
<br />SERIN 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 receiving data from another device that is not a BASIC Stamp, try to use
|
||
baud rates of 4800 and below.
|
||
<ol type="a"><li value="1">Because of additional overhead in the BASIC Stamp, and the fact that
|
||
the BASIC Stamp has no hardware receive buffer for serial communication,
|
||
received data may sometimes be missed or garbled. If this occurs, try
|
||
lowering the baud rate (if possible), adding extra stop bits, and not
|
||
using formatters in the <span class="keyword_in_text">SERIN</span> command. Using simple variables
|
||
(not arrays) and no formatters will increase the chance that the BASIC
|
||
Stamp can receive the data properly.</li></ol></li>
|
||
<li value="6">Be sure to study the effects of <span class="keyword_in_text">SERIN</span> formatters
|
||
<ol type="a"><li value="1">Because of additional overhead in the BASIC Stamp, and the fact that
|
||
the BASIC Stamp has no hardware receive buffer for serial communication,
|
||
received data may sometimes be missed or garbled. If this occurs, try
|
||
lowering the baud rate (if possible), adding extra stop bits, and not
|
||
using formatters in the <span class="keyword_in_text">SERIN</span> command. Using simple variables
|
||
(not arrays) and no formatters will increase the chance that the BASIC
|
||
Stamp can receive the data properly.</li></ol></li>
|
||
<li value="7">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> |