A value of 123.125 will output correctly, however, 123.12 will output as 123.119.
I'm guessing this will be due to the way numbers are represented, but I'm wondering is there any way to avoid this?
I'm thinking that the obvious solution of adding 0.001 will simply mean other numbers will output values slightly higher than they should?
I spotted this as I'm inputting and returning a float and every time it was 0.01 low. Initially I thought the problem was my StrToFloat routine but now I'm suspecting the output routine..
Code: Select all
Dim
FDigitsIn As PREINC1, // pre incrementing pointer to digits array
FDigitsAddress As FSR1, // address of digits array
FDigitsOut As POSTDEC1, // post decrementing pointer to digits array
FTextAddress As FSR2, // address of result string
FTextIn As POSTINC2, // post incrementing pointer to result string
FTextDec as POSTDEC2 // post decrementing pointer to result string
Dim TStr As String(12) // Temp string to hold output
// Convert a number to a string. Set FTextIn = @TStr before calling these routines.
Sub Num2Str(pValue As Word)
Dim Index As Byte
Dim Number As Word
Dim Digits(6) As Byte
FDigitsAddress = @Digits
Index = 0
Repeat
Number = pValue Mod 10
FDigitsIn = Number + 48
pValue = pValue / 10
Inc(Index)
Until pValue = 0
While Index > 0 // String created will be inverted so this swaps it back to the correct order
FTextIn = FDigitsOut
Dec(Index)
Wend
End Sub
Sub SendData(CmdRef As Byte, Data As Float)
Dim TB As Byte
Dim TW As Integer
FTextAddress = @TStr // Initialise pointer to start of the string
AddCommandCode(CmdRef)
FTextIn = GH_ETB // Send end of transmission block character
If Data.IsSigned Then // Is number negative?
FTextIn = "-" // Add a minus sign
Data = -Data // Invert
EndIf
TW = Data
NumToStr(TW) // Append number to string
FTextIn = "."
Data = Data - TW
Data = Data * 1000
TW = Data
If TW < 10 Then FTextIn = "0" EndIf // If less than 10 add a padding zero
IF TW < 100 then FTextIn = "0" Endif // If less than 100 add two padding zeros
NumToStr(TW) // Append to string
FTextIn = RS485EOT
FTextIn = null // Terminate
RS485Comms.Write(TStr)
End Sub