Problem with string input from C# and Basic Stamp Debug Window
Bibbi
Posts: 4
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
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
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.
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).
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.
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.
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.