Shop OBEX P1 Docs P2 Docs Learn Events
Reading negative values in strings from a GUI — Parallax Forums

Reading negative values in strings from a GUI

T ChapT Chap Posts: 4,223
edited 2009-08-08 18:05 in Propeller 1
What would you guys do to read values from a PC app that outputs strings. I had this combo Spin and GUI project that worked great when outputting only strings from 0 and up. Now I am adapting it to exchange negative numbers and things are not working right. When the numbers from the strings in the GUI are negative going to the Prop, they no longer appear right(once below '0').

Here is an example of the existing code that translates the strings to integers for use in motor positioning, and offsets the Prop by -20000 to avoid using negative numbers as strings. The GUI assumes 0 - 40000 is the same as the Prop -20000 to 20000. 20000 being = 0 in the GUI.


//wait for a string, then convert
PUB Start
 repeat              
   if receiveStr(@array,24) <> TAB
     quit
   Temp1 := convertstr(@array)
   if receiveStr(@array,24) <> CR
     quit
   Temp2 := convertStr(@array)
   Temp2 := Temp2 - 20000        '<<<<<<subtract 20000 to offset what the GUI is using as all positive.
   CaseTemp     



PRI convertStr(address) | C
  repeat while c := byte[noparse][[/noparse]address++]
   result := result * 10 + (c - "0")   





The GUI sends out :
Dim OutputString As String
  Dim Tab, Space, CR as String
  Dim SliderValueY as integer
  Tab=Chr(9)
  Space=Chr(32)
  CR=Chr(13)  
  X = Xslider.value
  Xposition.text = str((X-20000)/1000)         .<<<<<<<<Offset -20000 for the GUI display values
  OutputString = "XID" + TAB + str(X) + CR
  Serial1.Write outputString





I started a workaround, whereas the GUI keeps two sets of books, one for it's own internal and display purposes by simply subtracting a value from the original which results in negative numbers, and one that does not reflect negative numbers, so that the values sent to the Prop are still all positive. For example, the GUI might consider 0 to 40000 to be the same as -20000 to 20000.

This is turning into a chore to manage two sets of math.

Any suggestions on how to keep my integers in the GUI naturally negative and positive, and send the strings in such a way as the Prop can interpret? Maybe there is already an existing object that does a conversion?


Edit: Seems the "-" is being calculated in the convertStr, so that when "-1" is the string, -29 is the result. Then -290, -2900, -29000 onwards. I think the solution is to filter the "-" from the conversion, so the result just subtracts the result from 0 if there is a "-" present to produce a negative number.

The fix is Numbers from OBX FromStr (@array, DEC) converts the negative string to Decimal

Post Edited (TChapman) : 8/8/2009 6:04:12 PM GMT

Comments

  • StefanL38StefanL38 Posts: 2,292
    edited 2009-08-08 18:05
    Hello Tracy,

    take a look into the code of the method "RxDec" in the Extended_FDSerial-object or
    the method "StrToDec" in the FullDuplexSerialPlus-object to see how this can be done

    PUB rxDec : Value | place, ptr, x
    {{
       Accepts and returns serial decimal values, such as "1234" as a number.
       String must end in a carriage return (ASCII 13)
       x:= Serial.rxDec     ' accept string of digits for value
    }}   
        place := 1                                           
        ptr := 0
        value :=0                                             
        dataIn[noparse][[/noparse] ptr] := RX       
        ptr++
        repeat while (DataIn[noparse][[/noparse] ptr-1] <> 13) and (DataIn[noparse][[/noparse] ptr-1] <> Delimiter)                     
           dataIn[noparse][[/noparse] ptr] := RX                             
           ptr++
        if ptr > 2 
          repeat x from (ptr-2) to 1                            
            if (dataIn[noparse][[/noparse] x] => ("0")) and (datain[noparse][[/noparse] x] =< ("9"))
              value := value + ((DataIn[noparse][[/noparse] x]-"0") * place)       
              place := place * 10                               
        if (dataIn[noparse][[/noparse]0] => ("0")) and (datain[noparse][[/noparse]0] =< ("9")) 
          value := value + (DataIn[noparse][[/noparse]0]-48) * place
        elseif dataIn[noparse][[/noparse]0] == "-"             '<=== here the negative sign is managed                     
             value := value * -1
        elseif dataIn[noparse][[/noparse] 0] == "+"                               
             value := value 
    
    
    
    



    PUB StrToDec(stringptr) : value | char, index, multiply
    
        '' Converts a zero terminated string representation of a decimal number to a value
    
        value := index := 0
        repeat until ((char := byte[noparse][[/noparse] stringptr][noparse][[/noparse] index++]) == 0)
           if char => "0" and char =< "9"
              value := value * 10 + (char - "0")
        if byte[noparse][[/noparse] stringptr] == "-"
           value := - value
    
    
    



    best regards

    Stefan
Sign In or Register to comment.