Syntax question by noob
hr_sea
Posts: 2
When using an If - then statement, can I compare against a string?
I'm working on interfacing a Basic Stamp 2 to a laptop by wireless RS-232. I'd like to pass it commands in plain text via Windows Hyperterminal and have the stamp turn pins on and off. I can't seem to only be able to get if-then statements to work with numerical values.
I'm collecting 5 characters or less into "DataIn" array. Appropriate code snippets
SERIN SerialInput,BaudMode,TimeOut,begin,[noparse][[/noparse]SKIP 1, STR DataIn\5\"*"]
IF DataIn ="on" THEN
HIGH 1
ENDIF
I'm working on interfacing a Basic Stamp 2 to a laptop by wireless RS-232. I'd like to pass it commands in plain text via Windows Hyperterminal and have the stamp turn pins on and off. I can't seem to only be able to get if-then statements to work with numerical values.
I'm collecting 5 characters or less into "DataIn" array. Appropriate code snippets
SERIN SerialInput,BaudMode,TimeOut,begin,[noparse][[/noparse]SKIP 1, STR DataIn\5\"*"]
IF DataIn ="on" THEN
HIGH 1
ENDIF
Comments
There really are no string values in PBasic. They're treated as special cases in the various I/O statements like SERIN and SEROUT, but they're really considered to be arrays of bytes. None of the expression operators work on arrays.
You can do character comparisons like: IF DataIn(0) = "o" and DataIn(1) = "n" and DataIn(2) = 0 THEN
In this case, you're comparing individual characters and checking that the 'string' ends after two characters. That might be enough for what you want.
Thank you!
--Rich