PDA

View Full Version : Is there a better way...



Terry
- 21st June 2007, 00:23
I have a device that is completely controlled via RS-232. The commands to make the PIC do something, like read ADC, change levels of output pins, read SPI devices, etc. is all controlled by 3 letter commands from a PC. I use the HW uart on the PIC chip to load the data into a circular buffer and then look for a cr as a terminator. Then in my code I used multiple IF/THEN lines to match a valid command and then a subroutine to perform the action. The part that bothers me is the multiple IF/THEN about 20 of them that look like this:

IF chrbuf(0)="R" AND chrbuf(1)="T" AND chrbuf(2)="F" THEN GOSUB xxxxx
.
.
Is there a better or cleaner way to decode the three letter commands than using long multiple IF/THEN statements?

Terry

mister_e
- 21st June 2007, 00:26
How about a checksum of the first 3 characters and a select case with the checksum?

Darrel Taylor
- 21st June 2007, 00:58
Not sure about the checksum.
Wouldn't RTF and FRT end up the with the same number?

It depends on the letter combinations you need, but if there are common letters between the commands, maybe ...
<font color="#000000"><b>IF </b>chrbuf(0)=<font color="#FF0000">&quot;R&quot; </font><b>THEN
IF </b>chrbuf(1)=<font color="#FF0000">&quot;T&quot; </font><b>THEN
IF </b>chrbuf(2)=<font color="#FF0000">&quot;F&quot; </font><b>THEN GOSUB </b>RTF
<b>IF </b>chrbuf(2)=<font color="#FF0000">&quot;M&quot; </font><b>THEN GOSUB </b>RTM
<b>IF </b>chrbuf(2)=<font color="#FF0000">&quot;R&quot; </font><b>THEN GOSUB </b>RTR
<b>ELSE
IF </b>chrbuf(1)=<font color="#FF0000">&quot;Q&quot; </font><b>THEN
IF </b>chrbuf(2)=<font color="#FF0000">&quot;P&quot; </font><b>THEN GOSUB </b>RQP
<b>IF </b>chrbuf(2)=<font color="#FF0000">&quot;C&quot; </font><b>THEN GOSUB </b>RQC
<b>IF </b>chrbuf(2)=<font color="#FF0000">&quot;A&quot; </font><b>THEN GOSUB </b>RQA
<b>ENDIF
ENDIF
ELSE
IF </b>chrbuf(0)=<font color="#FF0000">&quot;T&quot; </font><b>THEN
IF </b>chrbuf(1)=<font color="#FF0000">&quot;T&quot; </font><b>THEN
IF </b>chrbuf(2)=<font color="#FF0000">&quot;F&quot; </font><b>THEN GOSUB </b>TTF
<b>IF </b>chrbuf(2)=<font color="#FF0000">&quot;M&quot; </font><b>THEN GOSUB </b>TTM
<b>IF </b>chrbuf(2)=<font color="#FF0000">&quot;R&quot; </font><b>THEN GOSUB </b>TTR
<b>ENDIF
ELSE
IF </b>chrbuf(1)=<font color="#FF0000">&quot;Q&quot; </font><b>THEN
IF </b>chrbuf(2)=<font color="#FF0000">&quot;Z&quot; </font><b>THEN GOSUB </b>TQZ
<b>IF </b>chrbuf(2)=<font color="#FF0000">&quot;S&quot; </font><b>THEN GOSUB </b>TQS
<b>IF </b>chrbuf(2)=<font color="#FF0000">&quot;1&quot; </font><b>THEN GOSUB </b>TQ1
<b>ENDIF
ENDIF
ENDIF
</b>

Terry
- 22nd June 2007, 20:50
Thanks for the ideas, it seems like PBP really needs some string capability where then the "case" statement could be used which I think would then be a natural for this type of application.
Terry

Melanie
- 23rd June 2007, 05:43
There are lots of different ways... not nescessarilly better, just different...

Take a look at this example posted OMG!!! Three years ago!!!

http://www.picbasic.co.uk/forum/showthread.php?t=573

It does pretty much what you're doing, receiving a command string terminated by a CR, then parsing that string for embedded commands to act upon... just another angle on the same theme...

So, as another example, if all your commands are say 3 characters long each, then stick them all together into your program as a long string (who says you can't build strings with PICBasic!!!) with a separator character between them (you can have that "string" in EEPROM, or within your program code)... eg...


iRobotActions:
ASM

db 0x00,"HUG" ; Greeting
db 0x00,"EAT" ; Recharge Batteries
db 0x00,"FLY" ; Go Somewhere
db 0x00,"KIL" ; Prime Directive
db 0x00,"RTM" ; Learn
db 0x00,"SEX" ; Have Fun
db 0x00,"DIE" ; Wait for armageddon

ENDASM

...then simply scan that "string" to find the position of the command you seek (which in turn will give you a number based on the position of that command within the string). Then armed with that number you can then simply use your case statement (or even a bunch of simple IF's). This basically gives you unlimited scope for your embedded commands.

nb. You need a separator between the commands, otherwise a random jumble like 'EXD' as the last two characters of 'SEX' and the first of 'DIE' would return a result.

stma
- 6th July 2007, 08:09
There are lots of different ways... not nescessarilly better, just different...

Take a look at this example posted OMG!!! Three years ago!!!

http://www.picbasic.co.uk/forum/showthread.php?t=573

It does pretty much what you're doing, receiving a command string terminated by a CR, then parsing that string for embedded commands to act upon... just another angle on the same theme...

So, as another example, if all your commands are say 3 characters long each, then stick them all together into your program as a long string (who says you can't build strings with PICBasic!!!) with a separator character between them (you can have that "string" in EEPROM, or within your program code)... eg...


iRobotActions:
ASM

db 0x00,"HUG" ; Greeting
db 0x00,"EAT" ; Recharge Batteries
db 0x00,"FLY" ; Go Somewhere
db 0x00,"KIL" ; Prime Directive
db 0x00,"RTM" ; Learn
db 0x00,"SEX" ; Have Fun
db 0x00,"DIE" ; Wait for armageddon

ENDASM

...then simply scan that "string" to find the position of the command you seek (which in turn will give you a number based on the position of that command within the string). Then armed with that number you can then simply use your case statement (or even a bunch of simple IF's). This basically gives you unlimited scope for your embedded commands.

nb. You need a separator between the commands, otherwise a random jumble like 'EXD' as the last two characters of 'SEX' and the first of 'DIE' would return a result.

Could you please help with how to search this string to get the location number?
Many Thanks
Steve

mister_e
- 6th July 2007, 08:14
db?? nah... grandma tricks, there's a better way, use the following...

http://www.pbpgroup.com/modules/wfsection/article.php?articleid=10

Ioannis
- 6th July 2007, 10:26
db?? nah... grandma tricks...



Hmm, from the avatar I don't think Melanie is so much old grandma...!

Ioannis

mister_e
- 6th July 2007, 18:26
;) yeah i know... just kidding a little bit ;)

I figure the 'Embedded Strings in code space' stuff will be somehow easy to implement, and will eat less code space.