printf () functions how to redirect serial port on the PSOC BLE

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hi,all

   

  I want to use the Printf () function to print the serial port information.

   

For example:

   

   Normal:

   

        UART_SpiUartPutArray("Helon Test\r\n",sizeof("Helon Test\r\n"));//Uart can print the serial port information.

   

  Now,i want to do like this:

   

        printf("Helon Test\r\n");//"Helon Test\r\n" receviced by serial tool.

   

so,my question is how can i do to print the serial port information by printf() function?And the _Write () function has been rewrited,but printf("Helon Test\r\n") still can't print the serial port information.the _Write () function as follow:

   

int _write(int file, char *ptr, int len)
{
    int i;
    file = file;
    for (i = 0; i < len; i++)
    {
        UART_SpiUartWriteTxData(*ptr++);
    }
    return len;
}

0 Likes
1 Solution
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Yes, but when using printf() it must call your new _write() function sooner or later. So when you set a breakpoint there, you can find out whether its called at all (then something in your code is wrong) or not (then its not the rioght way to change printf).

View solution in original post

0 Likes
12 Replies
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

When you debug your project, is your _write() function called at all?

0 Likes
Anonymous
Not applicable

Hi,hli

   

 if the _Write () function has been rewrited,the linker will be link the New _Write () function instead of the Old.So i do nothing with it.

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Yes, but when using printf() it must call your new _write() function sooner or later. So when you set a breakpoint there, you can find out whether its called at all (then something in your code is wrong) or not (then its not the rioght way to change printf).

0 Likes
Anonymous
Not applicable

Hi,hli

   

  you are right,when set a breakpoint in new _write() function,it has not been called.So how to set the project?And the new _write() function is ok,can you give some ideas?

   

  Million thanks!

0 Likes
Anonymous
Not applicable

Hi,hli

   

  I find the cause of the problem is the Heap Size(bytes) is too small.Previous value is 0x80, I changed to 0x200,Now i can use printf() function print serial port information.I have not changed anything, just change the Heap Size(bytes).

   

  Thanks for your help.

Anonymous
Not applicable

Hi,Helon_chen_1478911

   

Can you share your demo for me please, I have some problem for printf() function, and i change the Heap Size to 0x200, but the printf() function print any thing……

   

Thanks!

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Welcome in the forum, Eric!

   

Easiest is to use sprintf(Buffer,...) and UART_PutString(Buffer). Your PSoC hasn't got an OS, so prinf() does not know where to send the data to. There is a solution by supplying an own function, but my experiences are that this consumes more resources and CPU time.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Hi Bob

   

Thanks for your help!

   

I was use UART_PutString(Buffer) only can print string, and use sprintf(Buffer,...), it must use array[]; and i found iprintf(buffer), but it can print %s/%d/%c/%x only...

   

So i want to use printf(buffer) to print more, but it can not work.

   

Do you have a demo for printf() API? or
do you have any advice for it? thanks!

   

 

   

Eric

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

", and use sprintf(Buffer,...), it must use array[]" Tat is not quite right.

   

In the above case, "Buffer" receives the conversion of your formatted data, so you can  use UART_PutString(Buffer).

   

try:

   

char Buffer[80];

   

int Data = 4277;

   

sprintf(Buffer,"Data is %d",Data);

   

UART_PutString(Buffer) ;

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Hi BOB

   

The problem is solved, thanks for your support!

   

 

   

Eric

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

I'm sorry,so long to see your question,this is my learning experience as shown in Annex.And you may have to know something about Chinese or you can access my bbs(www.alongble.com),but most of my tutorials are Chinese,so i don't know if i can help you.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You are always welcome, Eric!

   

 

   

Bob

0 Likes