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 support@ccsinfo.com

About printf

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



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

About printf
PostPosted: Tue Oct 01, 2019 5:46 pm     Reply with quote

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: 122
Location: Bombay, India

View user's profile Send private message Visit poster's website

PostPosted: Tue Oct 01, 2019 9:29 pm     Reply with quote

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: 19262

View user's profile Send private message

PostPosted: Tue Oct 01, 2019 11:13 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 02, 2019 10:26 am     Reply with quote

Jerson wrote:
...


oh yes, now work perfect.

Thank you to both Very Happy
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