Reading negative values in strings from a GUI
T Chap
Posts: 4,223
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.
The GUI sends out :
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
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
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
best regards
Stefan