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 CCS Technical Support

Multi-character input from hyperterminal

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







Multi-character input from hyperterminal
PostPosted: Sun Jul 04, 2004 2:08 am     Reply with quote

Hola -

I have been programming numerical stuff on PICs for a while, and never used RS-232/hyperterminal except to send data from the PIC to a PC to be sure my code was working OK. My input data were always digital or ADC inputs to the appropriate PIC pins. So recently, I started tinkering with using hyperterminal to input data to the PIC, and I am stuck on something really simple. Please note I never used "C" before I started toying with the PIC., and all my "C" experience has never involved strings....

OK, so I want to send numerical data to the PIC from a PC using hyperterminal. I want to type in numbers up to several digits long, hit the carriage return, and have the PIC recognize these numbers. To get started, I wanted to send the PIC a 4 digit number - like the year using the following code:

main {
char data[4];

while(1) {
gets(data);
printf{"%4u\n\r", atol(data)};
}
}

I am sure you know the result already - when I type in a number greater than 255 (decimal), the number "wraps." Type in 255, you get 255, type in 256, you get 0, type in 257, you get 1, etc.... This makes sense as a char is 8 bits - a value up to 255. I am assuming I need to read the number in as a string and convert it using atoi. But how do I input an arbitrary - or even fixed length - number of digits? This seems so simple, yet I am stuck.....

TIA
Guest








RS232 ... PC to PIC
PostPosted: Sun Jul 04, 2004 3:09 am     Reply with quote

char data[10]; // remember that CR count one and the '0' indicating the end of string also need one byte

while(1)
{
gets(data); for(i=0;i<10;i++) printf("%3x ", data[i]);
printf("%s\n\r", data);
printf("%3u\n\r", atoi(data));
printf("%5lu\n\r", atol(data));
printf("%f\n\r", atof(data));
};

Best wishes
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