Specifications

BASIC Stamp II
Page 312 • BASIC Stamp Programming Manual 1.8 • Parallax, Inc.
receives nine bytes through pin 1 at 2400 bps, N81/inverted and stores
them in a 10-byte array:
serString var byte(10) ' Make a 10-byte array.
serString(9) = 0 ' Put 0 in last byte.
SERIN 1,16780,[STR serString\9] ' Get 9-byte string.
debug str serString ' Display the string.
Why store only 9 bytes in a 10-byte array? We want to reserve space for
the 0 byte that many BS2 string-handling routines regard as an end-of-
string marker. This becomes important when dealing with variable-
length arrays. For example, the STR modifier can accept a second pa-
rameter telling it to end the string when a particular byte is received, or
when the specified length is reached, whichever comes first. An example:
serString var byte(10) ' Make a 10-byte array.
serString(9) = 0 ' Put 0 in last byte.
SERIN 1,16780,[STR serString\9\"*"] ' Stop at "*" or 9 bytes.
debug str serString ' Display the string.
If the serial input were “hello*” Debug 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. Debug’s normal STR
modifier understands a 0 to mean end-of-string. However, if you use
Debug’s fixed-length string modifier STR bytearray\n you will inad-
vertently clear the Debug screen. The fixed-length specification forces
Debug to read and process the 0s at the end of the string, and 0 is
equivalent to Debug’s CLS (clear-screen) instruction! Be alert for the
consequences of mixing fixed- and variable-length string operations.
Matching a Sequence
Serin can compare incoming data with a predefined sequence of bytes
using the Wait modifiers. The simplest form waits for a sequence of up
to six bytes specified as part of the inputData list, like so:
SERIN 1,16780,[WAIT ("SESAME")] 'Wait for word SESAME.
debug "Password accepted"
Serin 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 varia-
tion from “SESAME” would be ignored.