Coding and general discussion relating to the compiler
Moderators: David Barker, Jerry Messina
-
SHughes_Fusion
- Posts: 219
- Joined: Wed Sep 11, 2013 1:27 pm
- Location: Chesterfield
Post
by SHughes_Fusion » Fri Oct 16, 2015 11:03 am
I've got two functions which do the same thing but on different data types:
Code: Select all
Sub SendData(Suffix As String, Data As Word)
FTextAddress = @TStr // Initialise pointer to start of the string
FTextIn = Suffix(0) // Put the suffix as the first characters
FTextIn = Suffix(1)
FTextIn = GH_ETB // Send end of transmission block character
NumToStr(Data) // Append number to string
FTextIn = null // Terminate
RS485Comms.Write(TStr)
End Sub
Sub SendData(Suffix As String, Data As Integer)
FTextAddress = @TStr // Initialise pointer to start of the string
FTextIn = Suffix(0) // Put the suffix as the first characters
FTextIn = Suffix(1)
FTextIn = GH_ETB // Send end of transmission block character
If Data.IsSigned Then // If this negative?
FTextIn = "-" // Add a minus sign
Data = -Data // Invert
EndIf
NumToStr(Data) // Append number to string
FTextIn = null // Terminate
RS485Comms.Write(TStr)
End Sub
The first compiles fine. The second gives me a 'Constant Expression violates subrange bounds' error on the
line. The program compiles and runs if I comment this out but of course for integers I only get the first character, not both. (The string passed is always two characters)
Any ideas what is going on?
FTextAddress is FSR2 and FTextIn is POSTINC2, the string being passed is from an array of consts.
-
Jerry Messina
- Swordfish Developer
- Posts: 1473
- Joined: Fri Jan 30, 2009 6:27 pm
- Location: US
Post
by Jerry Messina » Fri Oct 16, 2015 1:00 pm
There must be something more to it.
I tried the following test and it compiled. Maybe I made some wrong assumptions? Can you show a simple example where it fails?
Code: Select all
// FTextAddress is FSR2 and FTextIn is POSTINC2, the string being passed is from an array of consts
dim FTextAddress as FSR2
dim FTextIn as POSTINC2
const Cstr() as string = ("AB", "CD", "EF", "GH")
const GH_ETB = $17
dim TStr as string
dim s as string
dim w as word
dim i as integer
// just guessing here... won't actually work but it should do
sub NumToStr(Data As Word)
FTextIn = data.bytes(0) // this is just dummy
FTextIn = data.bytes(1) // this is just dummy
end sub
sub NumToStr(Data As Integer)
FTextIn = data.bytes(0) // this is just dummy
FTextIn = data.bytes(1) // this is just dummy
end sub
Sub SendData(Suffix As String, Data As Word)
FTextAddress = @TStr // Initialise pointer to start of the string
FTextIn = Suffix(0) // Put the suffix as the first characters
FTextIn = Suffix(1)
FTextIn = GH_ETB // Send end of transmission block character
NumToStr(Data) // Append number to string
FTextIn = null // Terminate
'RS485Comms.Write(TStr)
End Sub
Sub SendData(Suffix As String, Data As Integer)
FTextAddress = @TStr // Initialise pointer to start of the string
FTextIn = Suffix(0) // Put the suffix as the first characters
FTextIn = Suffix(1)
FTextIn = GH_ETB // Send end of transmission block character
If Data.IsSigned Then // If this negative?
FTextIn = "-" // Add a minus sign
Data = -Data // Invert
EndIf
NumToStr(Data) // Append number to string
FTextIn = null // Terminate
'RS485Comms.Write(TStr)
End Sub
w = 1
SendData(cstr(0), w)
i = -1
SendData(cstr(1), i)
s = "12"
SendData(s, 1)
-
SHughes_Fusion
- Posts: 219
- Joined: Wed Sep 11, 2013 1:27 pm
- Location: Chesterfield
Post
by SHughes_Fusion » Fri Oct 16, 2015 1:37 pm
Thanks for trying, Jerry. All assumptions seem valid and your code compiles for me as well.
It must be just one of those odd things - I couldn't find a way to replicate it without posting my whole code so I've done a work-around and moved the routine to copy the suffix in to a sub and that works fine now.