Shop OBEX P1 Docs P2 Docs Learn Events
Testing whether strings are equivilant — Parallax Forums

Testing whether strings are equivilant

robo_noobrobo_noob Posts: 1
edited 2007-11-28 09:44 in Propeller 1
Hello all,

I am new to SPIN and micro controllers in general. Surprisingly I have been able poke and prod enough to get the a number of things working. One issue that I have not been able to figure out is testing equivilancy of strings. I am trying to send commands over a serial connection from a PC to verify whether my application is working correctly. I can receive a string and print it back out through the terminal.

Here is a snippet of my code. In it, I am storing the string in a long variable and am trying to evaluate it in a case statement. When I use numbers rather than character arrays this case statement works. Can someone explain to me what I am doing wrong? I am pretty sure I am missing some concept very important involving working with variables.

Thanks in advance!

VAR
  long command

OBJ
  comm         : "SeriialDriver"
  ...

PUB main

  comm.start( PIN_RX, PIN_TX, 0 & INVERT, BAUDRATE ) 
  ...
  
  ...
  repeat
    comm.getstr(command)
    case command
      "showVersion" :
        comm.str(string("command recognized:"))
        ...
      other :
        comm.str(string("unknown command: "))
        comm.str(command)      

Comments

  • AleAle Posts: 2,363
    edited 2007-11-28 08:51
    variables of type LONG can contain only 4 bytes.... how are comparing a string with long where the string does not fit in 4 bytes... is well erroneouos.

    Use for instance 4 letter commands like "shwv" and be done.
  • deSilvadeSilva Posts: 2,967
    edited 2007-11-28 08:58
    You have to manage "strings" all by yourself, there is little help from the language...
    A "string" is a pointer to a piece of memory you have to allocate in advance, terminated by a zero.byte (=C-convention)
    Comparing such pieces of memory needs hand made loops, CASE works with 32 bit values only.

    BTW: I do not know your "SeriialDriver"; and 0&INVERT also looks funny (or is that a "disabled INVERT" smile.gif )...
  • Harrison.Harrison. Posts: 484
    edited 2007-11-28 09:44
    Actually, spin does include 2 string functions: strsize and strcomp. For your purposes, you can use strcomp to compare the received string with the string you are expecting.

    You'll need to post your entire code (including this illusive "SeriialDriver"). From what I can see, you are calling the getstr() method incorrectly (generally functions that return a string will require a pointer to an array of bytes to be provided as an argument).

    In your case, you would likely need to do:

    VAR
       byte myString[noparse][[/noparse]32]
    
    ....
    
    PUB main
    
       comm.getstr(@myString)
    
       if strcomp(@myString, string("showVersion"))
          ' do stuff here....
       elseif ....
    
    
Sign In or Register to comment.