View previous topic :: View next topic |
Author |
Message |
John_Lintern
Joined: 30 Sep 2004 Posts: 14
|
RS232 printf - how do I send a hex value instead of ASCII |
Posted: Thu Apr 21, 2005 7:10 am |
|
|
I am trying to send the hex value 1B to an external LED display unit.
I've used the line...
printf ("%02X",0x1B);
I then monitored the RS232 comms using the Serial Port Monitor tool in CCS.
But instead of sending the hex value 1B, it sends the ASCII characters 1 and B (which is hex 31 and 42).
How do I make it send the actual hex value 1B ?
I am using a PIC16C74B running at 20MHz.
The baud rate is 1200, 8 bits, no parity.
Thanks. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Apr 21, 2005 7:12 am |
|
|
|
|
|
Guest
|
|
Posted: Fri Apr 22, 2005 5:31 am |
|
|
|
|
|
lucky
Joined: 12 Sep 2003 Posts: 46 Location: South Coast - England
|
|
Posted: Fri Apr 29, 2005 8:46 am |
|
|
printf ("%X",0x1B); _________________ Lucky
www.mpic3.com - MPIC3 player project, Forum, Downloads, Online Shop |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Apr 29, 2005 9:20 am |
|
|
lucky wrote: | printf ("%X",0x1B); |
Didn't you read his post? That isn't what he wants and is what he did to begin with! |
|
|
lucky
Joined: 12 Sep 2003 Posts: 46 Location: South Coast - England
|
|
Posted: Fri Apr 29, 2005 9:29 am |
|
|
I read it.
Code: | printf ("%X",0x1B); |
will do as he wants.
Code: | printf ("%02X",0x1B); |
however will not. This is what he had in the original post.
I use this in lots of products for PIC - GUI coms. _________________ Lucky
www.mpic3.com - MPIC3 player project, Forum, Downloads, Online Shop |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Apr 29, 2005 9:31 am |
|
|
lucky wrote: | I read it.
Code: | printf ("%X",0x1B); |
will do as he wants.
Code: | printf ("%02X",0x1B); |
however will not. This is what he had in the original post.
I use this in lots of products for PIC - GUI coms. |
And he said
Quote: | But instead of sending the hex value 1B, it sends the ASCII characters 1 and B (which is hex 31 and 42).
|
He does not want the value to be converted to text. He just wants the value sent. A single char which in ascii would be the ESC key. |
|
|
lucky
Joined: 12 Sep 2003 Posts: 46 Location: South Coast - England
|
|
Posted: Fri Apr 29, 2005 9:43 am |
|
|
Mark,
Code: |
printf ("%02X",0x1B);
|
Will print the ASCII characters 1 and B. This is what he had.
Code: |
printf ("%X",0x1B);
|
Will send the Single HEX byte 0x1B. This is what he wants to do.
Note the lack of '02' in my code post. _________________ Lucky
www.mpic3.com - MPIC3 player project, Forum, Downloads, Online Shop |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Apr 29, 2005 9:51 am |
|
|
lucky wrote: | Mark,
Code: |
printf ("%02X",0x1B);
|
Will print the ASCII characters 1 and B. This is what he had.
Code: |
printf ("%X",0x1B);
|
Will send the Single HEX byte 0x1B. This is what he wants to do.
Note the lack of '02' in my code post. |
Yeah, and pigs fly right. Nope wrong. It will convert it to text. The 02 will just force 2 chars like 0x0B would get printed as 0B where as in your case would just be B. Take a look in the manual as the difference between 'X' and 'x'. Note the difference being the output is either upper or lower case. Now that should make you think, hummm, numbers don't have case so Mark must be right |
|
|
lucky
Joined: 12 Sep 2003 Posts: 46 Location: South Coast - England
|
|
Posted: Fri Apr 29, 2005 10:03 am |
|
|
OK,
You are rite (as always).
In feable defence, I was thinking of my debug code that does indeed print the Hex in character readable form. _________________ Lucky
www.mpic3.com - MPIC3 player project, Forum, Downloads, Online Shop |
|
|
Ttelmah Guest
|
|
Posted: Sat Apr 30, 2005 7:43 am |
|
|
It is perhaps worth reflecting on the 'reason' for the slight confusion that did apply in this thread. This is simply that a 'Hex' vaue, is _inherently) ASCII!. Hex is a way of representing the raw binary value, using less ASCII characters, than the binary itself. The original poster, actually wanted to send the 'raw' value, represented by a pair of hex digits, not it's hex representation, but asked how to send a hex value....
Best Wishes |
|
|
densimitre
Joined: 21 Dec 2004 Posts: 45
|
|
Posted: Sat Apr 30, 2005 4:01 pm |
|
|
what about print a string like this in hexa format??
Code: | char test[]="Hi, This is a Test"
printf("%x",test); |
Ist this posible?'
I want this string be printed in hexa format...
Tnahk you |
|
|
Ttelmah Guest
|
|
Posted: Sun May 01, 2005 2:27 am |
|
|
densimitre wrote: | wahat about print a string like this in hexa format??
Code: | char test[]="Hi, This is a Test"
printf("%x",test); |
Ist this posible?'
I want this string be printed in hexa format...
Tnahk you |
Remember that a 'string', is just an array of characters. So:
Code: |
char test[]="Hi, This is a test";
int8 ctr;
for (ctr=0;ctr<strlen(test);ctr++)
printf("%02x",test[ctr]);
putc('\n');
|
If you don't force each character to use two digits (the '02x' defintion), if you have a character with a leading '0', this will be dropped, and the receiver will get confused as to where it is in the string. I am sending a 'line feed' at the end of the string, but this is optional (up to you to work out how you want the other end to know you have reached the end of the string...).
You can also do the 'same', but instead of using strlen, simply take each character, test for it being '0' (the 'string terminator' character), and stop when this happens. So:
Code: |
char test[]="Hi, This is a test";
int8 cval,ctr=0;
while(true) {
cval=test[ctr++];
if (cval==0) break;
printf("%02x",cval);
}
|
Though this looks 'bulkier', it actually gives slightly smaller and quicker code, since the 'strlen' function, scans through the whole string to find the length, then the output scans the string again. This code combines the scan, and only performs one array access for each byte (array accesses like 'test[ctr]', are much slower than a direct variable access like 'cval').
Best Wishes |
|
|
densimitre
Joined: 21 Dec 2004 Posts: 45
|
|
Posted: Sun May 01, 2005 8:56 am |
|
|
Hi, thank you for yor answers...
I did smilutae this code , and send only lower case character...what about ABDCDEF???
example:
Code: | char test[]="j";
int8 ctr;
for (ctr=0;ctr<strlen(test);ctr++)
printf("%02x",test[ctr]);
putc('\n'); |
The output is:
must be "6A", not "6a"
and changing "j" to "J":
Code: | char test[]="J";
int8 ctr;
for (ctr=0;ctr<strlen(test);ctr++)
printf("%02x",test[ctr]);
putc('\n'); |
The outpu is:
mut be "4A" not "4a"
What ist wrong???
Thank you |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Sun May 01, 2005 11:38 am |
|
|
Use a capital "X" instead of lowercase "x".
Go invest in a copy of "The C Programming Language", 2nd ed. by Kernighan and Ritchie. ISBN 0-13-110362-8 _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
|