Shop OBEX P1 Docs P2 Docs Learn Events
Problem with string input from C# and Basic Stamp Debug Window — Parallax Forums

Problem with string input from C# and Basic Stamp Debug Window

BibbiBibbi Posts: 4
edited 2011-05-12 19:26 in General Discussion
Okay, here's what's going on, I'd like to know how to send various strings of text to the Basic Stamp, and depending on what the Basic Stamp has received, will determine what it'll do next.

Here's all the code I have for testing right now since I can't get it to work the way I want it to

command VAR Byte(10)

Main:
command(9) = 0
SERIN 16\15, 84, [STR command\9\"|"]
SEROUT 16, 84, [CR, STR command, CR]
GOTO Main

This code receives the string input and sends it back like it should, but what I can't figure out is how to make it so that if the Basic Stamp receives EX. "PIN1HIGH", to do

IF command = "PIN1HIGH" THEN HIGH 1

That gives me an error at the I in PIN saying it Expected 'THEN'
And if I try a Select Case like so

SELECT command
CASE "PIN1HIGH"
HIGH 1
CASE "PIN1LOW"
LOW 1
ENDSELECT

It doesn't give me an error, but it will always activate the CASE "PIN1HIGH" since it is actually only comparing the first letter, which in either CASE is P, and activating the first CASE.

So if you could help me out, that would be great.

Also, just because I thought it might work, I also tried

IF command(0) = "P" & command(1) = "I" & command(2) = "N" & command(3) = "1" &
command(4) = "H" & command(5) = "I" & command(6) = "G" & command(7) = "H"
THEN HIGH 1

Comments

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2011-05-12 08:29
    Hi, here's a link, although it's VB Express edition the serial routines are handled just the same as they are in C# .

    http://forums.parallax.com/showthread.php?96973-VB-Express-to-Stamp-Template/page2

    The post in the above thread I think would probably be of most interest for you is Post #31

    Jeff T.
  • Mike GMike G Posts: 2,702
    edited 2011-05-12 08:34
    You have the right idea, in PBasic you have to compare each of the individual bytes received in the string array "PIN1HIGH". PBASIC does not handle strings natively. To make it easy on yourself, why not send a byte that represents a command . Something like "PIN1HIGH" = $80.

    Where does C# come in? Do you need to send/receive an ASCII encoded string to the STAMP using C#?

    Read the SERIN command in the PBasic help file for more information on receiving strings (arrays).
  • BibbiBibbi Posts: 4
    edited 2011-05-12 15:28
    Okay, I posted this question just before I went to bed, but as soon as I woke up, I had a new idea of how to do it, and it worked, but if some one could make it work better in a smaller amount of space, that would be great, but on the other hand, here is the working code.

    command VAR Byte(10)

    Main:
    command(9) = 0
    SERIN 16\15, 84, [STR command\9\"|"]

    IF command(0) = "P" THEN IF command(1) = "I" THEN
    IF command(2) = "N" THEN IF command(3) = "1" THEN
    IF command(4) = "H" THEN IF command(5) = "I" THEN
    IF command(6) = "G" THEN IF command(7) = "H" THEN
    IF command (8) = 0 THEN HIGH 1
    ENDIF
    ENDIF
    ENDIF
    ENDIF

    IF command(0) = "P" THEN IF command(1) = "I" THEN
    IF command(2) = "N" THEN IF command(3) = "1" THEN
    IF command(4) = "L" THEN IF command(5) = "O" THEN
    IF command(6) = "W" THEN IF command(7) = 0 THEN LOW 1
    ENDIF
    ENDIF
    ENDIF
    ENDIF

    SEROUT 16, 84, [CR, STR command,"|", command(8),"|", command(5), CR]
    GOTO Main

    Always need to remember the command(last number used) = 0, not "0", otherwise there is an undesired effect.
  • Mike GMike G Posts: 2,702
    edited 2011-05-12 16:54
    Yeah, I would send an value that represents the command to run, Like "!P[$81]" Where 0x81 [1000_0001] means Pin 1 - High. The most significant bit determines High/Low the other 7 bit can be used for the Pin ID.

    Pin 1 - Low could be $01 [0000_0001] or make up your own encoding.

    That way you'd have 3 characters to parse. First the "!" - attention. The the instruction identifier "P" for Pin and finally the command action. It's a lot easier to parse a byte then a string of bytes.

    If you need to send a string like "PIN1HIGH" then you could create a table of strings in EEPROM and compare the received array with the EEPROM values until you find a match. Return the memory address of the match to identify the string found.
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2011-05-12 19:26
    I'm attaching a program outline that you can use to do what you want. I don't have a BS2 readily available to test it with, but it should work.

    The basic flow of the program is that you read a value from the PC, do something based on that value, send output to the PC, and wait for another value from the PC.
Sign In or Register to comment.