|
|
View previous topic :: View next topic |
Author |
Message |
Guest Guest
|
Multi-character input from hyperterminal |
Posted: Sun Jul 04, 2004 2:08 am |
|
|
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 |
Posted: Sun Jul 04, 2004 3:09 am |
|
|
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 |
|
|
|
|
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
|