USBUART print hex values to terminal

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

cross mob
nibec_3605396
Level 2
Level 2
First like given

I have a char array, example char c[] = {0xC0, 0x02, 0x00, 0x01, ...}

I want to send it over uart to another device via a while loop and UART_putchar(*c) but I also want to send it over USBUART so I can see it over terminal but  USBUART_putchar doesn't seem to work. My guess is it's trying to change it to an ASCII character. I've found other discussions that did something similar but not quite the same thing and the same thing goes just trying to find stuff on stackexchange.

What I have so far is something like:

char str[3];

    for(int i=sizeof(c); i>0; i--){

        sprintf(&str[1],"%2x",c);

        USBUART_PutString(str);       

    }

to make sure that it has a null termination on the string but that doesn't seem to work. I just want termial to print out "C0020001...." and no matter what tweaks I've done can't seem to figure out why it's not working.

Any help would be greatly appreciated.

(using CY8KIT-050 i.e. PSoC 5LP and PSoC Creator 4.2)

0 Likes
1 Solution

Not sure what I'm doing wrong then because I ran both at the same time and the results are:

MESSAGE 1:     C0020001030B02003633C0

MESSAGE 1:     C0020336C0

subsequent tests have similar results with dropping packets. USBUART_putstring must have some issue with flooding the terminal so it can't parse it quick enough so instead I'm instead using a function I borrowed from another example which adds the USBUART_CDCisReady() condition to avoid the flooding issue:

void PrintToUSBUART(char8 * outText)

{

    /* Wait till the CDC device is ready before sending data */

    while(USBUART_CDCIsReady() == 0u);

    /* Send strlen number of characters of wrBuffer to USBUART */

    USBUART_PutData((uint8 *)outText, strlen(outText));

}

char str[80];

    for(uint i=0; i<sizeof(c); i++){      

        sprintf(str,"%02x ",c);

        PrintToUSBUART(str);

    }

works perfectly. Thanks for helping me process everyone.

now that it works I've moved to have it encapsulated in a function that works perfectly to how I need to see my messages and don't need to worry about length:

void send_packet_USBUART(char *p, int len){

    char c[80];

    PrintToUSBUART("C0 ");

    /* for each byte in the packet, send the appropriate character

    * sequence

    */

    while(len--){

        switch(*p) {

            case END:

                /*escape END character

                */

                sprintf(c,"%02X ",ESC);

                PrintToUSBUART(c);

                sprintf(c,"%02X ",ESC_END);

                PrintToUSBUART(c);

                break;

            case ESC:

                /*escape ESC character

                */

                //USBUART_PutString("DB");

                sprintf(c,"%02X ",ESC);

                PrintToUSBUART(c);

                sprintf(c,"%02X ",ESC_ESC);

                PrintToUSBUART(c);

                break;

            default:

                sprintf(c,"%02X ",*p);

                PrintToUSBUART(c);

        }   

        p++;

    }

    PrintToUSBUART("C0"); 

}

View solution in original post

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

sprintf(&str[1],"%2x",c);

This will not work, index starts at zero 0, not at one. Stacked variables will get overwritten Better use

sprintf(str,"%2x",c);

or even

sprintf(str,"%2x \n\r",c);

Give the variable str a bit more size as char str[80];

Bob

Thank you for that. I realized my for loop was backwards because I was copying from something on stack exchange so it was going the end to zero so my output was backward. I tried

char str[80];

    for(uint i=0; i<sizeof(c); i++){

        sprintf(str,"%02x ",c);

        USBUART_PutString(str);

    }

 

it should be "02 00 01 03 00 36 33" but when printing with each iteration of the loop goes is "02 01 03 33". Any reason why it might be dropping when putstring is inside the loop?

All in all for some reason trying to print each char in the loop doesn't work but building the array out and then printing works:

char str[80];

    for(uint i=0; i<sizeof(c); i++){

        sprintf(&str[i*2],"%02x ",c);

    }

    USBUART_PutString(str);

It would be nice for the function I'm building this into to be able to just print in the loop so I don't have to make an exorbiantly big array to store a bunch of chars in and then print.

0 Likes

Hello,

I checked with the below statements I didn't find any dropping of the values during printing.

char str[80];

    for(uint i=0; i<sizeof(c); i++)

{       sprintf(str,"%02x ",c);

        USBUART_PutString(str);

    }

Thanks,

P Yugandhar.

Not sure what I'm doing wrong then because I ran both at the same time and the results are:

MESSAGE 1:     C0020001030B02003633C0

MESSAGE 1:     C0020336C0

subsequent tests have similar results with dropping packets. USBUART_putstring must have some issue with flooding the terminal so it can't parse it quick enough so instead I'm instead using a function I borrowed from another example which adds the USBUART_CDCisReady() condition to avoid the flooding issue:

void PrintToUSBUART(char8 * outText)

{

    /* Wait till the CDC device is ready before sending data */

    while(USBUART_CDCIsReady() == 0u);

    /* Send strlen number of characters of wrBuffer to USBUART */

    USBUART_PutData((uint8 *)outText, strlen(outText));

}

char str[80];

    for(uint i=0; i<sizeof(c); i++){      

        sprintf(str,"%02x ",c);

        PrintToUSBUART(str);

    }

works perfectly. Thanks for helping me process everyone.

now that it works I've moved to have it encapsulated in a function that works perfectly to how I need to see my messages and don't need to worry about length:

void send_packet_USBUART(char *p, int len){

    char c[80];

    PrintToUSBUART("C0 ");

    /* for each byte in the packet, send the appropriate character

    * sequence

    */

    while(len--){

        switch(*p) {

            case END:

                /*escape END character

                */

                sprintf(c,"%02X ",ESC);

                PrintToUSBUART(c);

                sprintf(c,"%02X ",ESC_END);

                PrintToUSBUART(c);

                break;

            case ESC:

                /*escape ESC character

                */

                //USBUART_PutString("DB");

                sprintf(c,"%02X ",ESC);

                PrintToUSBUART(c);

                sprintf(c,"%02X ",ESC_ESC);

                PrintToUSBUART(c);

                break;

            default:

                sprintf(c,"%02X ",*p);

                PrintToUSBUART(c);

        }   

        p++;

    }

    PrintToUSBUART("C0"); 

}

0 Likes