4 Replies Latest reply on Aug 23, 2018 9:48 AM by nik_berry_3605396

    USBUART print hex values to terminal

    nik_berry_3605396

      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[i]);

              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)

        • 1. Re: USBUART print hex values to terminal
          user_1377889

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

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

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

          or even

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

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

           

          Bob

          2 of 2 people found this helpful
          • 2. Re: USBUART print hex values to terminal
            nik_berry_3605396

            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[i]);

                    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[i]);

                }

                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.

            • 3. Re: USBUART print hex values to terminal
              PY_21

              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[i]);

                      USBUART_PutString(str);

                  }

               

               

              Thanks,

              P Yugandhar.

              1 of 1 people found this helpful
              • 4. Re: USBUART print hex values to terminal
                nik_berry_3605396

                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[i]);

                        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"); 

                }