Shop OBEX P1 Docs P2 Docs Learn Events
A very strange problem — Parallax Forums

A very strange problem

g3cwig3cwi Posts: 262
edited 2012-03-10 10:54 in Propeller 1
I have a simple piece of code that acts on some variables. They are different and have different names. However, a few lines down they all become the same value - without any code changing them. I have stopped all the parallel processes and searched for the variable being reassigned somewhere but nothing.
  heading_avg_last := 180 'debug
      heading_avg_str  := num.ToStr(heading_avg_last, num#DEC)
      waitcnt(clkfreq/20 + cnt)
      
      speed_avg_last := 123 'debug
      speed_avg_str  := num.ToStr(speed_avg_last, num#DEC)
      waitcnt(clkfreq/20 + cnt)
      
      alt_avg_last := 1034 'debug
      alt_avg_str  := num.ToStr(alt_avg_last, num#DEC)
      waitcnt(clkfreq/20 + cnt)
      


      
      {Debug.cls
      Debug.str(string("Packet filler "))
      debug.str(gps.satellites)
      Debug.nl
      debug.dec(speed_avg_last)
      debug.nl
      debug.dec(alt_avg_last)}
     
        
                       
      '****Your data payload goes here****
      'Max 255 characters
      
      addToPacket(string("APRS implemented by G3CWI."), 26)
      addToPacket(string(" Test data: "), 12) 
      
          {'Add speed to packet
          if speed_dec > 1
            addToPacket(string("Speed "), 6)
            addToPacket(speed_str,3) 
            addToPacket(string(" MPH. "), 6)
          else
            addToPacket(String("Not moving. "), 12)
           'debug.str (speed_dec)
        
          'Add altitude to packet
          if gps_alt_dec > 1
              addToPacket(string("Alt. ") ,5)
              addToPacket(gps_alt_str ,4) 
              addToPacket(string(" m. ") ,4)
          else
              addToPacket(String("Alt. not available. ") ,20)
              'debug.str (speed_dec)

          'Add heading to packet
          if speed_dec > 1 'Unreliable if speed too low/stopped
               addToPacket(string("Heading ") ,8)
               addToPacket(gps_heading_str ,3) 
               addToPacket(string(" degrees.") ,9)
          Else
               addToPacket(string("Heading not available. ") ,23) }

        'repeat 70 'debug delay
          'waitcnt(clkfreq + cnt)

           'Add average heading to packet
        
        if speed_avg_last > 1
        
          addToPacket(string(" Speed ") ,7)
          
          debug.cls
          debug.str (speed_avg_str)
          debug.nl
          
          addToPacket(speed_avg_str , strsize(speed_avg_str)) 
          addToPacket(string(" mph.") ,5)
          
        else
          addToPacket(string(" Not moving.") ,12)
            
        
        if speed_avg_last > 1

          addToPacket(string(" Hdg. ") ,6)

          debug.str (heading_avg_str)
          debug.nl
          addToPacket(heading_avg_str , strsize(heading_avg_str))
           
          addToPacket(string(" deg.") ,5)


At the top: heading_avg_str is 180
but by the bottom it (and speed_avg_str) = 1034.

Most of the code is commented out so there is nothing there that I can see to cause this...

Puzzled!

Richard

Comments

  • g3cwig3cwi Posts: 262
    edited 2012-03-10 09:23
    Further tracing shows that the problem is in the first part of the program - but not what the problem is:
    heading_avg_last := 180 'debug
          heading_avg_str  := num.ToStr(heading_avg_last, num#DEC)
       
          speed_avg_last := 123 'debug
          speed_avg_str  := num.ToStr(speed_avg_last, num#DEC)
          
          alt_avg_last := 1034 'debug
          alt_avg_str  := num.ToStr(alt_avg_last, num#DEC)
    
          debug.cls
          debug.str(heading_avg_str)
    
    

    num = Numbers

    Basically all three variables take on the value of the last conversion...

    Regards

    Richard
  • MagIO2MagIO2 Posts: 2,243
    edited 2012-03-10 09:36
    Quick reply:
    I think the problem is the numToStr. As the propeller/SPIN does not have dynamic memory allocation the function numToStr will always return the address of the same buffer which is used for conversion. So, you should simply use numToStr just before doing the output of one value.

    If you really need more than one string in parallel these have be stored somewhere from the calling program, you have to call a bytemove and copy the string into another buffer.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-03-10 09:36
    num.ToStr puts its result into a buffer within the num. object and returns the address of the buffer. If you call num.ToStr again without using the contents of the buffer for something, the buffer just gets overwritten with the new data. You must display or move the contents of the buffer somewhere else. Just putting the returned address in a different variable is not enough.
  • g3cwig3cwi Posts: 262
    edited 2012-03-10 09:59
    Thanks. I tried:
    heading_avg_last := 180 'debug
    heading_avg_str  := num.ToStr(heading_avg_last, num#DEC)
    bytemove(@StrBuf, @heading_avg_str, 50)
    

    That did not work. StrBuf is the Numbers buffer array (50 bytes). I have not explored bytemove before and now seems like a good time to try!

    Regards

    Richard
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-03-10 10:18
    Richard,

    You don't want the "@" symbol in front of "heading_avg_str" since holds the location you want to copy from (it isn't the location itself).

    Can you describe what you're trying to do.
  • turbosupraturbosupra Posts: 1,088
    edited 2012-03-10 10:22
    Hi Richard,

    I had this same problem and it took a few forum members to school me. I still have this problem, but now I can usually fix it after I realize.

    Here is the format you can use, hopefully I can save you some of the hard knocks I went through

    Some rules to remember about bytemove, it always wants a variable address ( @variable), not a variable name (variable). You can use a variable name if you've previous assigned it to another variables address (variableName := @variable) because at that point, the variable (variableName) is just acting as a liaison/pointer to the other variable address (@variable).

    In my code below

    byte b_suffix[_byteLimit] is a byte array and RxStringAddr is a long containing the address pointer to a byte array

        'addr of byte array   'long holding addr of byte array   ' size of addr of byte array + 1 for the terminating zero for the spin interpreter
    bytemove(@b_suffix, RxStringAddr, strsize(RxStringAddr) +1)                   ' copy tail including the existing terminator (+1)
    
    

    If you give me your whole code, something that I can compile, and what you expect to see, I can try and get it working to show you how it should work
  • turbosupraturbosupra Posts: 1,088
    edited 2012-03-10 10:23
    Duane is a great resource and is one of the people that helped me wrap my head around this and hopefully I can pass what I learned on to you

    Duane Degn wrote: »
    Richard,

    You don't want the "@" symbol in front of "heading_avg_str" since holds the location you want to copy from (it isn't the location itself).

    Can you describe what you're trying to do.
  • g3cwig3cwi Posts: 262
    edited 2012-03-10 10:31
    Hi Duane

    On a macroscale this is it:

    http://aprs.fi/?call=G3CWI-12&_s=ll

    I will take the modified code out for a spin(!) in a few minutes.

    Regards

    Richard
Sign In or Register to comment.