Shop OBEX P1 Docs P2 Docs Learn Events
I need to learn how to make a string — Parallax Forums

I need to learn how to make a string

Brian CarpenterBrian Carpenter Posts: 728
edited 2009-03-31 06:26 in Propeller 1
i have several variables that i need to make into a string with commas separating the values. eed to then count the length of the string and send the sting out serially. I can send the data but i am not sure how to create the string. See below
    Digi.str(string("$THERM,"))
    Digi.hex(mach_ID,4)
    Digi.str(string(",")) 
    Digi.dec(Therm1H)
    Digi.str(string(","))
    Digi.dec(Therm2H)
    Digi.str(string(","))
    Digi.dec(Therm3H)
    Digi.str(string(","))
    Digi.dec(Therm4H)
    Digi.str(string(","))
    Digi.dec(CSP1)
    Digi.str(string(","))
    Digi.dec(CSP2)
    Digi.str(string(","))
    Digi.dec( Amps1)
    Digi.str(string(","))
    Digi.dec(Amps2)
    Digi.str(string(","))
    Digi.dec(contact1)
    Digi.str(string(","))
    Digi.dec(contact2)
    Digi.str(string(","))
    Digi.dec(contact3)





Any ideas or threads to read would be great.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


It's Only A Stupid Question If You Have Not Googled It First!!

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-03-30 04:33
    Why do you need to create the string? Is it only because you don't know the length of the string? That's pretty easy. You know the lengths of all the character and string segments and the hexadecimal segment. You just need the length of the decimal numbers. How about:
    PUB decLength(v)
       if v < 0   ' If negative, account for sign
          result++
          ||v
       repeat while v => 10   ' Reduce to < 10
          result++   ' How many digits needed?
          v /= 10
       result++   ' Last digit
    
  • Brian CarpenterBrian Carpenter Posts: 728
    edited 2009-03-30 05:07
    Mike
    So i would run this routine for each dec value and then add it all together with the known number of other characters?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    It's Only A Stupid Question If You Have Not Googled It First!!
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-03-30 06:07
    Why do you need the length at all?

    You could simply define one "LineEnd" character. For direct terminal-output this could be the 13 (Carriage Return).
    The receiver has a buffer which is long enough to carry the max. number of characters you expect and receives until the buffer-length has been reached or it receives the line-end character. If the receiver reached the buffer-end it continues with reading until line-end, but does not store the characters anywhere. This should never happen in normal operation, but this way your receiver is failsafe and should not have buffer overflow bugs.

    If the string is not for direct output, you could transmit binary values instead. Then you have a fixed size for each transmission. Say your values have word size, then you have to transmit 12*2 bytes.
  • Brian CarpenterBrian Carpenter Posts: 728
    edited 2009-03-31 05:21
    i am using it in an HTTP POST application and must know the length

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    It's Only A Stupid Question If You Have Not Googled It First!!
  • Mike GreenMike Green Posts: 23,101
    edited 2009-03-31 05:43
    There are all sorts of ways to do this. What I suggested is, in some ways, the simplest. Another thing to do would be to take the .str / .hex / .dec / .bin code from one of the display objects and modify it to put the characters in a buffer using a zero terminator. Each routine would add to the end of the string and you'd have a method to return the address of the buffer and another to clear it (by storing a zero byte at the beginning. This would let you edit the text line the way you're used to and accumulate the characters to use in one single output string and you could precompute the length as well.
  • jazzedjazzed Posts: 11,803
    edited 2009-03-31 06:26
    Brian, Have a look at the Format object http://obex.parallax.com/objects/230/·You can use the sprintf method to create a string with numeric conversions, etc... then use strsize on the resulting formatted string. It is somewhat expensive in memory terms, but very useful in the right hands.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve


    Propalyzer: Propeller PC Logic Analyzer
    http://forums.parallax.com/showthread.php?p=788230
Sign In or Register to comment.