Shop OBEX P1 Docs P2 Docs Learn Events
Proper string handling syntax — Parallax Forums

Proper string handling syntax

I am trying to wait for a serial string that starts with "$ULTW" to arrive before I pay attention to any data that follows that header.

I've tried:
repeat until (inbuf[0]) := $24

This returns a "Expected end of line" error, which isn't leading me to much of a clue for a solution. The buffer is 60 bytes and the data comes in at roughly 5 minute intervals.

I appreciate any help!

Comments

  • JonnyMacJonnyMac Posts: 9,105
    edited 2016-02-26 01:09
    I use an approach something like this:
    dat
    
      Header        byte    "$ULTW"
                                                              
                                  
    pub main | c, state                                                       
                                                                     
      setup
    
      state := 0                                                        
                                                                     
      repeat
        c := term.rx
        if (c == Header[state])
          if (++state == 5)
            quit
        else
          state := 0
    
      ' get rest of string
    
  • You're getting the error you report because

    repeat until (inbuf[0]) := $24

    is an assignment, rather than a test for equals. Normally that would not flag an error. But in your case you're trying to make the assignment to an expression, rather than to a variable, due to the parentheses.

    The parentheses are not necessary, and the := should be changed to == to perform the test.

    -Phil
  • Jon,

    Before setting state back to zero in the else, you should check if the character that kicked you out is the first one in the header, otherwise you could miss a frame.

    ...$UL$ULTW...

    would be missed.
    repeat
        c := term.rx
        if (c == Header[state])
          if (++state == 5)
            quit
        else
          if (c == Header[0])
            state := 1
          else
            state := 0
    
  • Yep, you're absolutely right.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2016-02-26 18:33
    I usually handle this sort of task by passing the incoming data through a window that consists of say two longs, 8 bytes. Serial bytes enter at the msbyte of the high word and shift left with each new character. For example, sequence for a GPS sentence:
    ' 00000000
    ' 0000000_     ' for illustration, "_" stands in for $A, line feed.
    ' 000000_$
    ' 00000_$G
    ' 0000_$GP
    ' 000_$GPR
    ' 00_$GPRM
    ' 0_$GPRMC
    ' _$GPRMC,
    ' into two consecutive words, char0,char1
    ' compare with:
    
    DAT
      long
      gps byte  $A,"$GP"                            ' line feed + string $GP
      gga byte "GGA,"                               ' string GGA + comma
      rmc byte "RMC,"                                ' etc.
      scv byte "GSV,"
    

    At each step the two longs are compared for alignment with the long aligned DATa.
    if char0 ==  long[@gps]  ' key string for $GP at start of a line.
             case char1
               long[@gga] : ParseGGA
               long[@rmc] : ParseRMC
               long[@scv] : ParseSCV
    

    It's straightforward to branch on a set of possibilities within a fixed length command set.
Sign In or Register to comment.