how to check for a string in SELECT command

Sean87

Member
Hi all,

I am trying to simulate a program, but it always pop ups for a syntax error in the following line :
Code:
        case ("restart")
	        sertxd("Reseting the program!")  
	        b1 = 0
        	goto main
How to write case ("restart") ?

this is the full program if it helps answering:

Code:
main: 'main program entry
    low 0,1,2,3,4,5,6,7 'set all outputs to low
    serrxd ("go") 'wait to receive string "go" from serial port
    reconnect 'reconnects after receiving "go"from serial port           
    goto _read 'Goes to _read sub
    
_read:    
        serrxd b0 'waits to receive a string from serial port

    select b0   
    
        case "0"
      	  sertxd("LED 0 Toggled!")
        	toggle 0


        case "1"
      	  sertxd("LED 1 Toggled!") 
        	toggle 1
        	       	
        	
        case "2"
     	   sertxd("LED 2 Toggled!")   
        	toggle 2
        	     	
        	
        case "3"
     	   sertxd("LED 3 Toggled!")
        	toggle 3
        	  
        	
        case "4"
       	 sertxd("LED 4 Toggled!") 
        	toggle 4
        	goto main
        	 
        	
        case "5"
        	sertxd("LED 5 Toggled!")
        	toggle 5        	     
        	     	        	
        	
        case "6"
	     	sertxd("Dancing!") 
        	goto dance

        case ("restart")
	        sertxd("Reseting the program!")  
	        b1 = 0
        	goto main 
        	
        else
        	goto _read
        
    endselect
    
    goto _read

dance:
high 0,2,4
pause 50
low 0,2,4
pause 50
high 1,3,5
pause 50
low 1,3,5
high 0,2,4
pause 100
low 0,2,4
pause 100
high 1,3,5
pause 100
low 1,3,5
high 0,2,4
pause 200
low 0,2,4
pause 200
high 1,3,5
pause 200
low 1,3,5
goto _read
 

MartinM57

Moderator
No correction, but apart from fixed strings in sertxd-type statements only single characters can be held in variables and tested against

e.g.
b1 = "A"
if B1 = "A" then ...
 

papaof2

Senior Member
So is there a workaround to do some stuff based on strings received?
As Martin said, you can test SINGLE characters. There are NO string variables. It's part of of the design.

There is NO capability for
IF StringVariable = "SomeValue" then
do_whatever
END IF

The only workaround is to receive a string into multiple byte variables and test each byte in turn to determine what the sequence of characters is. That's inherently slow and complex.

John
 

ckoehn

Member
Indeed it is like John said. You would need something like this..

Code:
#picaxe 20x2

eeprom 0,("restart","start")

main:
	ptr=0	'set scratchpad location
nextbyte:
	'read till 13 is received
	serrxd b0	'wait for char, then inc pointer
	reconnect
	
	@ptrinc=b0
	if b0=13 then
		'================ check if strings match =======================
		b1=0	'"restart" eeprom position
		b2=7	'7 char to match
		gosub checkmatch
		if b1=255 then
			'matches "restart"
			'do something

                        goto main
		endif
		
		b1=7	'"start" eeprom position
		b2=5	'5 char to match
		gosub checkmatch
		if b1=255 then
			'matches "start"			
			'do something

                        goto main
		endif		

		goto main	'do again
	endif
	goto nextbyte	
	
checkmatch:
	ptr=4
	b3=b1
	for b0=1 to b2
		read b3,b1
		if b1<>@ptr then
			'not a match
			b1=0
			return
		endif
		
		inc b3	'get next location to compare
		inc ptr
		
	next
	b1=255	'made it all the way through
	return
Later,
Clint
 
Last edited:
Top