PDA

View Full Version : Syntax question by noob



hr_sea
11-05-2009, 12:51 AM
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,[SKIP 1, STR DataIn\5\"*"]
IF DataIn ="on" THEN
HIGH 1
ENDIF

Larry~
11-05-2009, 12:57 AM
if datain(0) = "o" and datain(1) = "n" goto turnedon

Mike Green
11-05-2009, 12:58 AM
No

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.

hr_sea
11-05-2009, 05:09 AM
Ok, this all makes sense. (why would it be done any other way?). I'll try it out tonight.

Thank you!

RiJoRi
11-05-2009, 06:07 AM
Of course, you may want to imitate C, which also has no strings (at least K&R doesn't). They use library functions to manipulate character arrays -- strcmp, strlen, strcpy, etc. Because C has pointers, it's a little more general than BASIC, but it can be done.

--Rich