View previous topic :: View next topic |
Author |
Message |
mvanvliet
Joined: 02 Jun 2009 Posts: 123 Location: The Netherlands
|
Ledbar & string |
Posted: Thu Jan 31, 2013 9:49 am |
|
|
I'm trying to generate text on a Led message display. The ledbar can be controlled with RS232 but has a checksum on the end. The checksum is: all characters XOR-ed and added that value with the startadres of 0x74.
I've an example code with arduino:
http://arduino.sundh.com/2012/01/arduino-ethernet-shield-hooked-up-to-led-message-display/
My code is:
Code: |
int checksum = 0x74;
char type[6];
type = "hello!";
int i = 0;
for(i = 0; i <= strlen(Type); i++)
{
checksum = checksum ^ type[i];
}
printf("<ID00><L1><PA><FE><MA><WC><FE>");
printf(type);
printf(checksum);
printf("<E>");
|
Can somebody check this code? When this is working I want some different strings, so the checksum must be generated automaticly from the string.
CCS version: 4.132 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Thu Jan 31, 2013 9:59 am |
|
|
Your printf's are wrong.
To print the string 'type', you need:
printf("%s",type);
The checksum, wants to be printed in hex, so the second printf needs to be
printf("%02X",checksum);
This does what the 'hexstr' and 'touppercase' functions do.
Best Wishes |
|
|
mvanvliet
Joined: 02 Jun 2009 Posts: 123 Location: The Netherlands
|
|
Posted: Thu Jan 31, 2013 10:03 am |
|
|
I've changed the printf functions. But you think the for instruction is ok? |
|
|
mvanvliet
Joined: 02 Jun 2009 Posts: 123 Location: The Netherlands
|
|
Posted: Wed Feb 13, 2013 2:29 am |
|
|
The checksum is working after the changes Ttelmah wrote. But now step 2:
I find out that the 0x74 start adress of the checksum is the XOR function of <L1><PA><FE><MA><WC><FE>, so now I want to add these characters to the "hello!" string, so my checksum will be completely generated on the complete string and I can start with a checksum of 0. I saw something with strcat, is that the easiest way?
Is it ok to write:
Code: |
char type[10]
char pretype[25]
char totaltype[50]
pretype = "<L1><PA><FE><MA><WC><FE>";
type = "hello!";
totaltype = strcat(pretype, type);
|
And then generate the checksum of "totaltype".
I also want to have some variables INT16 in the string, how can I convert these into strings and add these also to my string "totaltype"?
Thanks in advance! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Wed Feb 13, 2013 2:45 am |
|
|
Code: |
pretype = "<L1><PA><FE><MA><WC><FE>";
type="hello!";
|
won't work. '=' can't handle strings like this.
You can _initialise_ the string with:
Code: |
char type[] = "hello!";
char pretype[] = "<L1><PA><FE><MA><WC><FE>";
char totaltype[50]
//length is optional. The compiler will automatically create an array just
//long enough. If you are going to want to put longer items in later, then
//have the length.
//To put a value into a string later, use:
strcpy(type,"hello!");
//not '='
//yes, strcat does catenate strings as you want. However it does it _into_
//the first string. So you need:
strcpy(totaltype,pretype); //totaltype now contains a copy of pretype
strcat(totaltype,type); //it is now the two strings one after the other
|
sprintf. Depends on the format you want the numbers in.
Best Wishes |
|
|
mvanvliet
Joined: 02 Jun 2009 Posts: 123 Location: The Netherlands
|
|
Posted: Wed Feb 13, 2013 8:08 am |
|
|
Thank you very much for your help. It becomes much clearer every time.
Code: |
int checksum = 0;
int i = 0;
char pretype[];
char type[] = "hello!";
char totaltype[]
strcpy(pretype,"<L1><PA><FE><MA><WC><FE>");
strcpy(totaltype,pretype); //totaltype now contains a copy of pretype
strcat(totaltype,type); //it is now the two strings one after the other
for(i = 0; i <= strlen(totaltype); i++)
{
checksum = checksum ^ totaltype[i];
}
printf("<ID00>");
printf("%s",totaltype);
printf("%02X",checksum);
printf("<E>"); |
I generated this code and try this in a few hours on my hardware.
For the sprintf function:
I would like to have a percent value on my ledbar, so let's say 3.1(%).
Is this ok:
Code: |
char percent[];
int percent2 = 31
sprintf(percent, "%2.1w>", percent2
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Wed Feb 13, 2013 8:32 am |
|
|
If you are going to write something 'to' a string, you need to make sure it is big enough to take it. Note how I gave a size to 'totaltype'. You only don't need a size, when you are initialising the string when declared, then the compiler knows it needs space for the initialisation string, and makes it large enough. As I said in the comments:
"If you are going to want to put longer items in later, then have the length."
You need a length or no space is actually allocated to the array.
You might want to use %3.1. The character in front of the decimal in C, is the 'total field width', so %2.1, does not give any space in front of the decimal (printf will cope with this and make the field longer than asked), but better to have the size you need there.
Best Wishes |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Feb 13, 2013 8:41 am |
|
|
From the CCS manual on the Printf function: Quote: | A %% will output a single %. |
|
|
|
mvanvliet
Joined: 02 Jun 2009 Posts: 123 Location: The Netherlands
|
|
Posted: Wed Feb 13, 2013 9:00 am |
|
|
Ok, it is clear to me that I must declare the length of the string when I fill it later and I don't have to declare the length.
For the sprintf function I want 3.1% in my display, so I assume I need %3.1.
Ckielstra, what do you mean with %%. I see it in the manual, but I don't know what they mean with this text. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Wed Feb 13, 2013 9:20 am |
|
|
% is a command character to printf. It says 'what follows is a command'. So to display/output the character '%', you have to use the command '%%'.
So the format string wants to be "%3.1w%%", to display 'n.n%'.
Best Wishes |
|
|
mvanvliet
Joined: 02 Jun 2009 Posts: 123 Location: The Netherlands
|
|
Posted: Wed Feb 13, 2013 9:46 am |
|
|
Ok, clear. I also added the %% to the end of the sprintf function.
It will be like this:
Code: | char percent[10];
int percent2 = 31
sprintf(percent, "%3.1w%%", percent2
|
|
|
|
|