Shop OBEX P1 Docs P2 Docs Learn Events
Syntax question by noob — Parallax Forums

Syntax question by noob

hr_seahr_sea Posts: 2
edited 2009-11-04 22:07 in BASIC Stamp
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

Comments

  • Larry~Larry~ Posts: 242
    edited 2009-11-04 16:57
    if datain(0) = "o" and datain(1) = "n" goto turnedon
  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-04 16:58
    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_seahr_sea Posts: 2
    edited 2009-11-04 21:09
    Ok, this all makes sense. (why would it be done any other way?). I'll try it out tonight.

    Thank you!
  • RiJoRiRiJoRi Posts: 157
    edited 2009-11-04 22:07
    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
Sign In or Register to comment.