Shop OBEX P1 Docs P2 Docs Learn Events
HELP.spin - I Am Going In Circles And Getting Dizzy - Byte Array — Parallax Forums

HELP.spin - I Am Going In Circles And Getting Dizzy - Byte Array

idbruceidbruce Posts: 6,197
edited 2015-02-02 21:07 in Propeller 1
I thought I understood this stuff, but apparently not, because I am having quite a bit of trouble accessing the data in my arrays, and I have tried numerous methods. Anyhow, I need help.

The provided source code and archive represents the concept of what I want to achieve, but just in case it is unclear, I will provide a small description. Basically I am parsing a G-Code file one character at a time, and shoving that character into one of the elements of a predetermined array. When I need the data for further processing, I am attempting to parse the elements of the various arrays. Can someone please enlighten me and tell me what I am doing wrong?
CON

  _CLKMODE            = XTAL1 + PLL16X
  _XINFREQ            = 5_000_000

  TX                  = 30
  RX                  = 31
  COMM_MODE           = 0
  COMM_BAUD_RATE      = 115_200
               
VAR

  BYTE bFieldElement_4[7]
  BYTE bFieldElement_5[7]
             
OBJ

  Terminal:     "FullDuplexSerial"
 
PUB Main

  bFieldElement_4[0] := "-" 
  bFieldElement_4[1] := "9"
  bFieldElement_4[2] := "9"
  bFieldElement_4[3] := "."
  bFieldElement_4[4] := "9"
  bFieldElement_4[5] := "9"

  bFieldElement_5[0] := "1"
  bFieldElement_5[1] := "1"
  bFieldElement_5[2] := "."
  bFieldElement_5[3] := "1"
  bFieldElement_5[4] := "1"
  
  Terminal.Start(RX, TX, COMM_MODE, COMM_BAUD_RATE)
  
  WAITCNT(CNT + (1 * CLKFREQ))

  ProcessCoordinate(@bFieldElement_4)

  ProcessCoordinate(@bFieldElement_5)

  Terminal.Str(STRING("Finished"))

PUB ProcessCoordinate(StrPtr) | Index, Char

  Index := 0

  REPEAT

    Char := BYTE[StrPtr]'[Index]

    IF(Char >= "0" AND Char =< "9")
      Terminal.Str(STRING("0-9"))
      Terminal.Tx(13)

    IF(Char == "-")
      Terminal.Str(STRING("Minus"))
      Terminal.Tx(13)

    IF(Char == ".")
      Terminal.Str(STRING("Period"))
      Terminal.Tx(13)

    Index++

  WHILE((Char => "0") AND (Char =< "9")) OR (Char == ".") OR (Char == "-")

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-02-02 16:13
    You have a ">=" where you probably want a "=>".

    If the equal sign is on the right, it's an assignment operator.

    You have them in the correct order in your "while" comparison.
  • BeanBean Posts: 8,129
    edited 2015-02-02 16:13
    You should be doing StrPtr++ instead of Index++

    Bean
  • idbruceidbruce Posts: 6,197
    edited 2015-02-02 16:24
    Duane - You are correct about the first conditional. My bad. Thank you very much Duane

    Bean- The ++ would be if you were parsing a variable, not the elements. The ++ made the terminal totally blank.

    The code now works with Duanes correct and the use of Index. Here is the final working source.
    CON
    
      _CLKMODE            = XTAL1 + PLL16X
      _XINFREQ            = 5_000_000
    
      TX                  = 30
      RX                  = 31
      COMM_MODE           = 0
      COMM_BAUD_RATE      = 115_200
                   
    VAR
    
      BYTE bFieldElement_4[7]
      BYTE bFieldElement_5[7]
                 
    OBJ
    
      Terminal:     "FullDuplexSerial"
     
    PUB Main
    
      bFieldElement_4[0] := "-" 
      bFieldElement_4[1] := "9"
      bFieldElement_4[2] := "9"
      bFieldElement_4[3] := "."
      bFieldElement_4[4] := "9"
      bFieldElement_4[5] := "9"
    
      bFieldElement_5[0] := "1"
      bFieldElement_5[1] := "1"
      bFieldElement_5[2] := "."
      bFieldElement_5[3] := "1"
      bFieldElement_5[4] := "1"
      
      Terminal.Start(RX, TX, COMM_MODE, COMM_BAUD_RATE)
      
      WAITCNT(CNT + (1 * CLKFREQ))
    
      ProcessCoordinate(@bFieldElement_4)
    
      ProcessCoordinate(@bFieldElement_5)
    
      Terminal.Str(STRING("Finished"))
    
    PUB ProcessCoordinate(StrPtr) | Index, Char
    
      Index := 0
    
      REPEAT
    
        Char := BYTE[StrPtr][Index]
    
        IF(Char => "0" AND Char =< "9")
          Terminal.Str(STRING("0-9"))
          Terminal.Tx(13)
    
        IF(Char == "-")
          Terminal.Str(STRING("Minus"))
          Terminal.Tx(13)
    
        IF(Char == ".")
          Terminal.Str(STRING("Period"))
          Terminal.Tx(13)
    
        Index++
    
      WHILE((Char => "0") AND (Char =< "9")) OR (Char == ".") OR (Char == "-")
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-02-02 16:34
    The code in post #1 had the index commented out.
    Char := BYTE[StrPtr]'[Index]
    

    Bean's suggestion is one of the ways the buffer could be incremented.

    It would work with Index++ or StrPtr++ but not both (with the latest version of the code).
  • idbruceidbruce Posts: 6,197
    edited 2015-02-02 16:43
    Duane

    The first source had Index commented out, because it was not doing me any good, until your correction about the operators. I also tried ++, but it did not work, however I just tried it again and it now works. Perhaps I did not wait long enough. My apologies Bean :)

    Here is better source code for those with similar problems.
    CON
    
      _CLKMODE            = XTAL1 + PLL16X
      _XINFREQ            = 5_000_000
    
      TX                  = 30
      RX                  = 31
      COMM_MODE           = 0
      COMM_BAUD_RATE      = 115_200
                   
    VAR
    
      BYTE bFieldElement_4[7]
      BYTE bFieldElement_5[7]
                 
    OBJ
    
      Terminal:     "FullDuplexSerial"
     
    PUB Main
    
      bFieldElement_4[0] := "-" 
      bFieldElement_4[1] := "9"
      bFieldElement_4[2] := "9"
      bFieldElement_4[3] := "."
      bFieldElement_4[4] := "9"
      bFieldElement_4[5] := "9"
    
      bFieldElement_5[0] := "1"
      bFieldElement_5[1] := "1"
      bFieldElement_5[2] := "."
      bFieldElement_5[3] := "1"
      bFieldElement_5[4] := "1"
      
      Terminal.Start(RX, TX, COMM_MODE, COMM_BAUD_RATE)
      
      WAITCNT(CNT + (1 * CLKFREQ))
    
      ProcessCoordinate(@bFieldElement_4)
    
      ProcessCoordinate(@bFieldElement_5)
    
      Terminal.Str(STRING("Finished"))
    
    PUB ProcessCoordinate(StrPtr) | Index, Char
    
      Index := 0
    
      REPEAT
    
        Char := BYTE[StrPtr][Index]
        'Char := BYTE[StrPtr++] 'This work also
    
        IF(Char => "0" AND Char =< "9")
          Terminal.Tx(Char)
    
        IF(Char == "-")
          Terminal.Tx(Char)
    
        IF(Char == ".")
          Terminal.Tx(Char)
    
        Index++
    
      WHILE((Char => "0") AND (Char =< "9")) OR (Char == ".") OR (Char == "-")
    
      Terminal.Tx(13)
      
    
  • JonnyMacJonnyMac Posts: 9,105
    edited 2015-02-02 18:25
    If you're going to have a byte array of ASCII characters, you might consider putting them into a DAT string. For example, I wrote a little program that accepts control input from an Android phone app, and allows the program to return values. I decided to synthesize a clock to send to the app. I have this in my app:
    dat
    
      TimeStr               byte    "00:00:00", 0
    


    You can get the address of your string with @ and modify it at will. For example
    p_time := @TimeStr
    
      repeat 8
        c := term.rx
        case c
          "0".."9", ":":
             byte[p_time++] := c
          other : 
            quit
    


    By terminating the ASCII string with 0 allows one to use .str() methods in serial objects.
  • idbruceidbruce Posts: 6,197
    edited 2015-02-02 18:55
    Jon

    That is a very good tip. Thanks.

    I have to go shovel some snow :( , but I will check it out as soon as possible and see how it fits into the grand scheme of things, but I think it sounds good and it is probably a much better solution than what I am using.

    I also suppose it would also depend how much grief it would cause me at this point. :)

    Thanks Jon
  • JonnyMacJonnyMac Posts: 9,105
    edited 2015-02-02 19:14
    One of the up-sides of using the DAT array versus simple variables is that you can pre-load the bytes with default values in the listing; no need for code to do this.

    If you decide to stick with your traditional byte arrays you can load them with one line of code:
    bytemove(@bFieldElement_4, string("-99.99"), 5)
    
  • idbruceidbruce Posts: 6,197
    edited 2015-02-02 21:07
    Jon

    After taking a look at the source, I could easily see that changing that aspect would be a major undertaking. However I sincerely appreciate the tips of good advice and the sharing of your knowledge.

    Bruce

    P.S. If you care to take a peek, I just post the source at this thread: http://forums.parallax.com/showthread.php/159950-The-Teacup-Port-A-Work-In-Progress-With-Added-Extras-3D-Printer-Firmware?p=1313959#post1313959
Sign In or Register to comment.