Announcement

Collapse
No announcement yet.

strtok usercmd

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • strtok usercmd

    I made this for parsing commands received from a serial connection .

    its pretty simple to use ,quick and efficient

    usage
    needs a pic18 or pic16f1xxx enhanced core
    there is an ASSEMBLER DEFINE TO REPLACE TOKEN WITH NULL "C" STYLE IN INPUT if required


    example
    some data to play with
    arraywrite head,["i 123,p4,d 5678,t43,s-4",13,10,"j 777",13,10]


    first call zero index and set token

    strtok head,0,","
    result
    STRTOK_LEN=6
    STRTOK_INX=6

    next call
    strtok head {same token}
    STRTOK_LEN=3
    STRTOK_INX=9

    next call
    strtok head {same token}
    STRTOK_LEN=7
    STRTOK_INX=16

    seek a different token
    strtok head, "s"
    STRTOK_LEN=5
    STRTOK_INX=21



    when chr(10) or chr(0) is encountered the sequence will terminate
    Attached Files

  • #2
    Thanks for posting Richard. I am sure it will be very handy.

    Ioannis

    Comment


    • #3
      updated version with right justify routine
      Attached Files

      Comment


      • #4
        Hi Richard.

        I am on a remote control design that has a matrix 3x4 keyboard and besides the general use of the keys (to control things remotely), the keys will be used to make some settings also.

        Do you think your macro fits in the idea or better to use if-then's?

        Something like this is what I consider:

        *01#xxx or *02#xxx to set parameters on the remote. xxx are 3 digits data.

        Ioannis

        Comment


        • #5
          not quite sure what you mean.
          the whole idea behind strtok is to locate a single char "token" in a string and set an index to that position in the string [optionally replacing that token with a null] so as to break the buffer up into data fields.
          so if you have a string (buffer[]) "???*01#xxx*02#xxx????" strtok can easily find the * or the # for instance

          strtoking for * would yield fields "???", "01#xxx", "02#xxx????" [this seems the most appropriate assuming the 01,02 identify the data xxx]
          strtoking for # would yield fields "???*01", "xxx*02", "xxx????"

          once a field has been suitably extracted from the buffer it can be parsed in the usual way with arrayread or other logical methods.
          the strtok index remains setup ready for the next field to be extracted




          Comment


          • #6
            Thank Richard.

            The *01# etc was an arbitrary sequence I selected that seems unlikely a user will accidentally hit on the keyboard. Then follows the real data to be configured in the device memory as new setting.

            What I am after is an easy method to:
            1. confirm correct sequence of command
            2. store new data setting
            3. exit configuration, say with a beep

            I have not yet used your macro as my hardware is in the process of building right now. Will soon hopefuly.

            Ioannis

            Comment

            Working...
            X