Shop OBEX P1 Docs P2 Docs Learn Events
hell of a problem using data types or adding an array — Parallax Forums

hell of a problem using data types or adding an array

grasshoppergrasshopper Posts: 438
edited 2008-05-22 21:06 in Propeller 1
Basically i am trying to take an array that a user can change by storing a baud rate in EEPROM. My problem is that if i add up the string that the user inputs i can seem to get it to be a correct value.
here is an example if a user inputs 9600


 FLASH[noparse][[/noparse] 2 ] := ((RxString[noparse][[/noparse] 9 ] * 1000) + (RxString[noparse][[/noparse] 10 ] * 100) + ( RxString[noparse][[/noparse] 11 ] * 10) + (RxString[noparse][[/noparse] 12 ]))





In the code the array Flash[noparse][[/noparse] 2 ] is where i would expect to find 9600 logicaly i thought adding RxString [noparse][[/noparse] 9 - 12] but it is not the case

9600 where the 9 is RxString[noparse][[/noparse] 9 ] and the 6 is RxString[noparse][[/noparse] 10 ] and the 0 is RxString [noparse][[/noparse] 11 ] and the 0 is RxString [noparse][[/noparse] 12 ]

My other foreseeable problem is that if a user inputs a number 19200 then the code will have to be modified


 FLASH[noparse][[/noparse] 2 ] := ((RxString[noparse][[/noparse] 9 ] * 10000) + (RxString[noparse][[/noparse] 10 ] * 1000) + ( RxString[noparse][[/noparse] 11 ] * 100) + (RxString[noparse][[/noparse] 12 ] *10 ) + (RxString[noparse][[/noparse] 12 ]  ))





I could address this with an If statement but some help would be nice

Comments

  • grasshoppergrasshopper Posts: 438
    edited 2008-05-22 18:59
    Any one ?

    Did i explain it enough?

    I really need help on this one
  • Mike GreenMike Green Posts: 23,101
    edited 2008-05-22 19:22
    You basically want to convert a digit string into a binary value. There is already a routine (rxDec) in the Extended FullDuplexSerial object that does this. You could also look at the "getAnyNumber" method near the end of FemtoBasic.spin which allows for decimal, hexadecimal, and binary numbers.
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-05-22 20:00
    {
    You've forgotten that the "Character" recieved will be in ASCII.· The ASCII character for "0" (zero) is 30.
    }
    PUB GetNum(InStrAddr) : Result | I, MyLen
    · I := 0
    · MyLen := STRSIZE(InStrAddr)
    · repeat I from MyLen TO 0 STEP -1
    ··· Result := Result * 10
    ··· Result := Result + InStrAddr[noparse][[/noparse]I] - 30
    · RETURN Result
  • grasshoppergrasshopper Posts: 438
    edited 2008-05-22 20:16
    allanlane5 said...


    PUB GetNum(InStrAddr) : Result | I, MyLen
    I := 0
    MyLen := STRSIZE(InStrAddr)
    repeat I from MyLen TO 0 STEP -1
    Result := Result * 10
    Result := Result + InStrAddr - 30
    RETURN Result


    Nice work thanks a million!!

    This is what I was looking for .
    tongue.gif
  • StefanL38StefanL38 Posts: 2,292
    edited 2008-05-22 21:06
    Hello,

    the ASCII-Code for "0" zero is DECIMAL 48 and hexadecimal this is 30

    a number without a leading "$" is interpreted as DECIMAL

    so code has to be

    PUB GetNum(InStrAddr) : Result | I, MyLen
    I := 0
    MyLen := STRSIZE(InStrAddr)
    repeat I from MyLen TO 0 STEP -1
      Result := Result * 10
      Result := Result + InStrAddr - 48 'intead of 30
    RETURN Result
    
    
    



    best regards

    Stefan

    Post Edited (StefanL38) : 5/22/2008 9:29:47 PM GMT
Sign In or Register to comment.