View previous topic :: View next topic |
Author |
Message |
cvargcal
Joined: 17 Feb 2015 Posts: 134
|
About printf |
Posted: Tue Oct 01, 2019 5:46 pm |
|
|
Hi,
I send a data from one pic to other across uart with hex 0x02 as start and hex 0x03 as end for easy control receive.
Code: | printf("%x%x%x%x%x",0x02,RX.code[0],RX.code[1],RX.code[2],0x03 );
printf("\n\r" ); |
out
Quote: |
02332c1b03
02f7fcf703
02e173cf03
02afbe9e03
0260461f03
02e7fff903
0208028c03
02fc802003
02664d7003
02f863d603
|
....
in other side with the other pic I receive this way:
Code: | #int_RDA2
void RDA_isr2(void) {
bufferByte = getchar(uart2);
// See what we got
if (bufferByte == 0x02){
start=1 ;
}else if ((start)&&(bufferByte =='0x03')) {
moduleResonseBuffer[moduleBufferIndex] = 0x00; // Add Null terminator
messageReady=1;
start=0;
moduleBufferIndex = 0; // Prepare index for next message
disable_interrupts(INT_RDA2);
}else
{
if (start) moduleResonseBuffer[moduleBufferIndex++] = bufferByte; // Add Byte to Buffer
}
}
|
The problem is how do i send 0x02 and 0x03 as hex ? or how do i receive as hex, because the pic receives "02" and "03" as strings not as hex ?
thanks |
|
|
Jerson
Joined: 31 Jul 2009 Posts: 126 Location: Bombay, India
|
|
Posted: Tue Oct 01, 2019 9:29 pm |
|
|
Try it this way
Code: | printf("%c%x%x%x%c",0x02,RX.code[0],RX.code[1],RX.code[2],0x03 );
printf("\n\r" );
|
this will send 0x02 and 0x03 as single characters |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19601
|
|
Posted: Tue Oct 01, 2019 11:13 pm |
|
|
Understand, that 'hex', _is_ an ASCII representation of numbers.
Inside the processor everything is binary.
You want to just send the raw byte '2' and the raw byte '3'.
As Jerson says, %c is the printf code to send a raw byte.
It will _not_ be 'hex'.
0b00000010 = 2 = 0x02
These are all just different ways of representing numbers. Internally
the value is the same. Printing as 'hex' converts the number from this
raw form into the ASCII version.
At the receiving end, you compare with the number 2 (which you happen to be inputting in hex), so the comparison will be fine. |
|
|
cvargcal
Joined: 17 Feb 2015 Posts: 134
|
|
Posted: Wed Oct 02, 2019 10:26 am |
|
|
oh yes, now work perfect.
Thank you to both |
|
|
|