PDA

View Full Version : identifying text commands



tbarania
- 24th October 2014, 21:53
I need to have my microcontroller act upon different commands sent. For example if "GC" is sent it turns on a motor. If "GQ" is sent it sends back data.

I would expect it to be something like this, as many other BASIC languages use:

hserin [command]
if command = "GC" then ....

if command = "GQ" then....

where the text inside the parentheses is what is compared. How is this done in PicBasic?

WAIT kind of does that but if I understand its operation correctly the program is suspend waiting for the text string. I need to check to see if anything was received and if so act on it but if not go do other things. I can't suspend everything and wait.

A STRING variable type would be handy.

HenrikOlsson
- 25th October 2014, 05:56
Hi,
PBP doesn't have a string type variable per se but it does have array type variable which is pretty much the same thing.

If all your commands are two character command then declare a 2 byte array, like

command VAR BYTE [2]


Then, once you have the command IN the array you can, for example, do something like

IF command[0] = "G" AND command[1] = "C" THEN
' Do Whatever
ENDIF

IF command[0] = "G" AND command[1] = "Q" THEN
' Do Whatever
ENDIF

This is a pretty straight forward way, depending on the number of different commands there might be "cleaner" and/or more efficient ways of doing it.

/Henrik.

AvionicsMaster1
- 28th October 2014, 13:07
I'm just curious if PBP could differentiate between caps and lower case? i.e.

Would this:

IF command[0] = "G" AND command[1] = "C" THEN
' Do Whatever
ENDIF

IF command[0] = "G" AND command[1] = "Q" THEN
' Do Whatever
ENDIF
Produce the same results as this:

IF command[0] = "g" AND command[1] = "c" THEN
' Do Whatever
ENDIF

IF command[0] = "g" AND command[1] = "q" THEN
' Do Whatever
ENDIF

If this is too elemental of a question I apologize for any inconvenience.

pedja089
- 28th October 2014, 13:25
It's not same...
Take look at ASSCI table
http://www.asciitable.com/index/asciifull.gif
"G" and "g" are just constant. You also could write 71 instead of "G", 103 instead "g".
IF command[0] = "G" AND command[1] = "C" THEN
is same as
IF command[0] = 71 AND command[1] = 67 THEN

For parsing string commands I use this:
http://www.picbasic.co.uk/forum/showthread.php?t=2728&p=117009#post117009
Scroll down code to CommandParser: label.
You can easily convert upper case to lower and lower to upper as in this sample
http://melabs.com/samples/PBP-mixed/serin.htm