|
|
View previous topic :: View next topic |
Author |
Message |
micro_man
Joined: 05 Jun 2013 Posts: 14
|
putc and printf |
Posted: Fri Oct 09, 2015 12:20 am |
|
|
Hi,
I have a very simple query. I want to send a number using serial port.
e.g
i want to receive 01 instead of 1 at the receiver side.for this purpose i use putc command like that
but nothing displayed.
then i tried printf
but only 1 is printed out. then i tried
Then i get 01
I want to know is it the right way to send this?
Also what is the purpose of putc command. Is it only print the characters but not interger? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19485
|
|
Posted: Fri Oct 09, 2015 12:46 am |
|
|
Probably not.
Your %x, prints the number in hexadecimal. If you number gets to 10 (decimal), it'll output as 0a. Is this what you want?....
Start at the beginning '1' (number), sent using putc, is the ASCII character 'SOH' which means 'start of heading'. Not the number '0' (this is ASCII 0x31).
You don't want to use putc to send numbers unless you have already turned them into ASCII.
Printf will turn number into ASCII for you. %d does this exactly as you have found, but '1', is the digit "1", not the two digits "01". The answer to this is in the manual for printf (read a C textbook which will explain much more). You can use: %02d which says 'print this result in a field 2 digits wide, with leading zero(s)'. So:
printf("%02d",a);
will output "01", and when 'a' gets to 10, will output "10". When a is '0' this will output "00".
Probably what you want. |
|
|
|
|
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
|