Shop OBEX P1 Docs P2 Docs Learn Events
How to receive and parse a string. — Parallax Forums

How to receive and parse a string.


I am very confused about how to receive a string from a PLC ASCII module into the Propeller, and then separate out the pertinent embedded variables. In this case the ASCII string from the PLC is something like this:
"1A3000B200"

The first character, the number 1, is to be stored as variable C. The number 3000 is to be stored as variable A, and the number 200 is to be stored as variable B.

I have looked through the forum and obex and studied what I can that seemed relevant, but nothing has clicked yet. A number of promising leads point to missing pages, links not working.

I have seen references to '[noparse][[/noparse]', and I can't seem to discover where that originated.

I would appreciate links to active tutorial pages if any exist, and active objects that I could learn from.

Any code snippets would be appreciated as well!

Dave

Comments

  • Are you looking for C/C++ or Spin?
  • Spin, thanks for the question. I like Spin and I'm attempting to do all of my embedded controller projects to use the propeller and programmed in Spin.

    Dave
  • What is the model/part # of the PLC ASCII module?
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2015-11-02 19:30
    A snippet (untested!), addressed to the xAyyyyBzzz format.
    VAR
      long C,B,A    ' variables together in this order.
      byte S[12]     'xAyyyyBzzzz  null terminated string from somewhere
    
    PUB Parse : accumulator  | char,idx, jdx
      idx := 0
      jdx := 0
      repeat
        if (char := S[idx++]) => "0" and char =< "9"
          accumulator := accumulator * 10 + (char - "0")   ' build the digits
        else
          C[jdx++] := accumulator~      ' capture the value and post-zero accumulator
      until char==0                 ' end of string
    
  • SeairthSeairth Posts: 2,474
    edited 2015-11-02 19:46
    Ahh. Maybe my prior question is the wrong focus on your problem. Is it that you need to be able to parse a string you've already received, or are you still trying to get the string from the module?

    Edit: if string parsing and conversion is the primary concern, then use http://obex.parallax.com/object/582 (for the parsing) and http://obex.parallax.com/object/435 (for the conversion).
  • Here's an idea to toss into your basket: I have a couple helper routines; one looks for the field letter, the other can be used to extract a value knowing its position in a string.
    pub find(ch, p_str) | idx
    
    '' Find position of char ch in string at p_str
    '' -- returns -1 if not found
    
      repeat idx from 0 to strsize(p_str)-1
        if (byte[p_str][idx] == ch)
          return idx
    
      return -1
    
    
    pub str2dec(p_str, len) | ch, value 
    
    '' Convert string at p_str to decimal value
    '' -- max length is len characters
    
      value := 0                                                     ' initialize result
      
      repeat len
        ch := byte[p_str++]                                          ' get a character
        if ((ch => "0") and (ch =< "9"))                             ' valid digit?
          value := (value * 10) + (ch - "0")                         ' ASCII --> decimal
        else
          quit
      
      return value
    
  • A snippet (untested!), addressed to the xAyyyyBzzz format.

    Thanks Tracy, I see how that works.

    Dave
  • Edit: if string parsing and conversion is the primary concern, then use http://obex.parallax.com/object/582 (for the parsing) and http://obex.parallax.com/object/435 (for the conversion).[/quote]

    Thanks for those links. And indeed the PLC module is not the issue, just trying to get the string into the propeller and operate on it.

    Dave
  • JonnyMac wrote: »
    Here's an idea to toss into your basket: I have a couple helper routines; one looks for the field letter, the other can be used to extract a value knowing its position in a string.

    Thanks JonnyMac! I understand the code you have written and will work with it. That was very kind of you to send a complete demo, that has certainly helped me along. (All of your demos have been of great value to me.)

    And I enjoyed 'these are crappy var names' ! That was fun....

    Dave
Sign In or Register to comment.