CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Ledbar & string

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
mvanvliet



Joined: 02 Jun 2009
Posts: 123
Location: The Netherlands

View user's profile Send private message

Ledbar & string
PostPosted: Thu Jan 31, 2013 9:49 am     Reply with quote

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: 19245

View user's profile Send private message

PostPosted: Thu Jan 31, 2013 9:59 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jan 31, 2013 10:03 am     Reply with quote

I've changed the printf functions. But you think the for instruction is ok?
mvanvliet



Joined: 02 Jun 2009
Posts: 123
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Feb 13, 2013 2:29 am     Reply with quote

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: 19245

View user's profile Send private message

PostPosted: Wed Feb 13, 2013 2:45 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Feb 13, 2013 8:08 am     Reply with quote

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: 19245

View user's profile Send private message

PostPosted: Wed Feb 13, 2013 8:32 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Feb 13, 2013 8:41 am     Reply with quote

From the CCS manual on the Printf function:
Quote:
A %% will output a single %.
mvanvliet



Joined: 02 Jun 2009
Posts: 123
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Feb 13, 2013 9:00 am     Reply with quote

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: 19245

View user's profile Send private message

PostPosted: Wed Feb 13, 2013 9:20 am     Reply with quote

% 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

View user's profile Send private message

PostPosted: Wed Feb 13, 2013 9:46 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group