Const strings and the LCD module

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
blackcattech
Posts: 113
Joined: Mon Jan 11, 2010 10:39 pm
Location: Chesterfield

Const strings and the LCD module

Post by blackcattech » Tue Sep 11, 2012 1:30 pm

I'm having a very odd issue with writing text to the LCD. I'm setting up some messages using Consts and using LCD.Write to display them on a text LCD.

Some are working fine, others are throwing up compiler error messages. If, for example, I do:

Code: Select all

Const InitPages(6) As String = ( "   Some Text    ", "Some more Text",
                                 "Texting, Texting", "Blah, Blah, Blah",
                                 "Firmware Version", Ver )
LCD.Write(InitPages(0))
Then this works fine. However, if I do:

Code: Select all

Const TxtAlarmFaultAlarm = #127+" Yes "+#4+#5+#6+#7+"     "+#126
LCD.Write(TxtAlarmFaultAlarm)
Then I get an 'Incompatible Types' error message.

If I try and force it to recognise the Const as a string by using

Code: Select all

Const TxtAlarmFaultAlarm as String
or

Code: Select all

Const TxtAlarmFaultAlarm as String(17)
(The string s 16 characters plus terminator gives a 17 byte string)

Then I get an 'Invalid constant typecast' error.

Am I doing something wrong or is this a compiler limitation or bug? The help files say you can define string constants so I can't see why const ... as string throws an error, but I can't see what else they would be defined as anyway, and LCD.Write should accept strings so I'm just confused!

User avatar
Senacharim
Posts: 139
Joined: Tue Aug 10, 2010 5:19 pm
Location: Ventura, CA

Post by Senacharim » Tue Sep 11, 2012 2:59 pm

Code: Select all

Const TxtAlarmFaultAlarm = #127+" Yes "+#4+#5+#6+#7+"     "+#126 
That's not a valid string, which is likely where your error lay.

My advice: look at the LCD module code, and write your LCD init routine such that it sends one character at a time. Clever solutions are rarely the best solutions, just keep it simple.
Surviving Member
Bermuda Triangle Battalion
from 2026 to 1992

Voted "Most likely to time travel"--Class of 2024.

blackcattech
Posts: 113
Joined: Mon Jan 11, 2010 10:39 pm
Location: Chesterfield

Post by blackcattech » Tue Sep 11, 2012 3:44 pm

Senacharim wrote:

Code: Select all

Const TxtAlarmFaultAlarm = #127+" Yes "+#4+#5+#6+#7+"     "+#126 
That's not a valid string, which is likely where your error lay.
Erm... Yes it is....

I've had that working as part of an array of strings. I try and change from an array to a single string and it falls over.

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Tue Sep 11, 2012 4:00 pm

I've just building this full program here (SF 2.2.1.4)

Code: Select all

include "LCD.bas"
Const TxtAlarmFaultAlarm = #127+" Yes "+#4+#5+#6+#7+"     "+#126
LCD.Write(TxtAlarmFaultAlarm)
and it compiles fine

blackcattech
Posts: 113
Joined: Mon Jan 11, 2010 10:39 pm
Location: Chesterfield

Post by blackcattech » Tue Sep 11, 2012 9:08 pm

Well, I've found the problem and it was one of those that is totally unrelated..

One problem with moving between C and Swordfish is you get used to using char to mean an 8 bit variable whereas Swordfish consider this a character, not a value. I'd been working on a C program earlier in the day and had got the two mixed up in SF, and was referencing an array using a char rather than a byte.

Quite why the editor threw up the error it did I don't know as it was in a different place... It was only by chance when I scrolled down the list of errors to try and correct at least some of them that I spotted the problem. Correcting it cleared all the other error messages. Really, I guess you could call this a preprocessor bug as the error given was wrong...

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Tue Sep 11, 2012 10:46 pm

If you think it's a compiler error, please supply a full example so I can take a look and fix the problem, if required.

blackcattech
Posts: 113
Joined: Mon Jan 11, 2010 10:39 pm
Location: Chesterfield

Post by blackcattech » Fri Sep 21, 2012 10:18 am

Hi David,

Ok, I've looked in to this in more depth and I think I was wrong, it isn't so much a compiler error, just the error message isn't so clear. It was correctly telling me there was an error on that line of code, I'd misinterpreted this as being with the LCD write, it was actually an array reference where I was trying to use a char rather than a byte as the reference.

As Chars and Bytes could quite easily get confused by people who are used to other programming languages it might be worth putting something in the preprocessor to highlight this as a probable cause of the error when the fault is 'Incompatible Types' and the incompatible type is a char.

Post Reply