Is there a better way...


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2005
    Posts
    24

    Default Is there a better way...

    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

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    How about a checksum of the first 3 characters and a select case with the checksum?
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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 ...
    Code:
    <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>
    DT

  4. #4
    Join Date
    Dec 2005
    Posts
    24


    Did you find this post helpful? Yes | No

    Default

    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

  5. #5
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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...
    Code:
    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.

  6. #6
    Join Date
    Nov 2005
    Posts
    2


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie View Post
    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...
    Code:
    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

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    db?? nah... grandma tricks, there's a better way, use the following...

    http://www.pbpgroup.com/modules/wfse...p?articleid=10
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  8. #8
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,115


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e View Post
    db?? nah... grandma tricks...
    Hmm, from the avatar I don't think Melanie is so much old grandma...!

    Ioannis

  9. #9
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts