1 2 Previous Next 15 Replies Latest reply on Aug 7, 2020 12:36 AM by MoTa_728816

    Getting unexpected character

    abho_4730071

      Hi Team,

      I am using serial port- DB9 connection. I am using below code. But i am getting the below output.

      int main(void)

      {

          uint8_t c;

         

          CyGlobalIntEnable; /* Enable global interrupts. */

       

       

          UART_Start() ;

          UART_PutString("\x1b[2J\x1b[;H") ; /* clear screen */

          UART_PutString("This is an ECHO program\n\r") ;

         

          for (;;) {

            c= UART_GetChar();

          if(c=='L')

          UART_PutString("Light");

          else

          UART_PutString("Dark");

              //if (UART_GetRxBufferSize() > 0) { /* received some data */

             //     c = UART_GetByte() ;

             //     if ((c != 0) && (c != 0xFF)) {

                     // UART_PutChar(c) ;

              //    }

            //  }

          }

      }

       

        • 1. Re: Getting unexpected character
          SwathiS_86

          Hello,

           

          Please have a look at the attached example project. Make the corrections in the API commands as per the Example code. Make sure that you have configured the Serial Terminal's (TeraTerm) Baud rate same as that of the UART component's Baud rate. Also, right click on the UART module on the schematic to find the component datasheet for more information.

           

          Regards,

          Swathi

          • 2. Re: Getting unexpected character
            abho_4730071

            Hi Swathi,

            I am getting below error.

            When i tried to find the device in device selector i could not found this device.

             

            • 3. Re: Getting unexpected character
              SwathiS_86

              Hi,

               

              You got that error because the code example was for PSoC 4 device. Looking at the error message, I realised that you are using PSoC 5LP device, so, I have attached an example code compatible with PSoC 5LP. Please follow all the instructions in the pdf document provided along with the project.

               

              The example project attached, displays the transmitted characters on the Serial Terminal as well as on the LCD. Let me know if it works.

               

              Regards,

              Swathi

              • 4. Re: Getting unexpected character
                MoTa_728816

                Hi,

                 

                UART_GetChar() returns something, even if it has note received data.

                So I used a test to check any incoming data arrived, which you commented out.

                     if (UART_GetRxBufferSize() > 0) { /* received some data */

                 

                Meantime, in case data is not valid or some errro happens,

                UART_GetChar() tends to return 0 or 0xFF, in my experience,

                so I wrote below, which you also commented out

                c = UART_GetByte() ;

                if ((c != 0) && (c != 0xFF)) {

                UART_PutChar(c) ;

                }

                 

                After all, if you want to write Dark or

                 

                    for (;;) {

                          if (UART_GetRxBufferSize() > 0) { /* received some data */

                          c = UART_GetByte() ;

                          if ((c != 0) && (c != 0xFF)) {

                               if ((c == 'L') || (c == 'l')) {

                                    UART_PutString("Light") ; // I wonder if this should be "Light\n\r"

                               } else {

                                    UART_PutString("Dark") ; // again, could be "Dark\n\r"

                               }

                           }

                  }

                    }

                 

                moto

                 

                P.S. And as I have asked you not a once, please try a loop-back test to make sure that PC to DB9 path is working.

                To do that you short the TXD and RXD of your USB-Serial Converter and check if what you are typing are echoed back to the PC.

                If loop-back test is not working, the rest is just a waste of time.

                Meantime, if the loop-back test is working and you are getting something like above, may be you connected TXD and RXD to wrong pin.

                It is work trying to swap TXD and RXD if it cause any improvment.

                • 5. Re: Getting unexpected character
                  MoTa_728816

                  Hi,

                   

                  So finally similar USB-Serial converter I ordered arrived.

                  IMG_4316.JPG

                  I soldered pin-sockets for easy connection with jumper wires.

                   

                  The first thing I tried was test the voltage of TXD when the jumper setting is 3.3V on this board.

                  And, Yes, TXD was 3.3V.

                  So DO NOT connect this device to the pins of DB9 on CY8CKIT-050.

                   

                  Next I performed the loop-back test.

                  To do this test we need to short TXD and RXD of the USB-Serial Converter like below.

                  loop_back_connection.JPG

                  On my board

                  IMG_4317.JPG

                  Then connect the USB cable to PC and start Tera Term.

                  Then all the keys typed in the Tera Term were echoed back.

                  Note: This requires no program.

                  001-loop-back-tera-term.JPG

                  If this does not work, probably your USB-Serial Converter is damaged.

                   

                  Now for the program, I wrote another program.

                   

                  The schematic

                  002-schematic.JPG

                  pins

                  003-pins.JPG

                  main.c

                  ======================

                  #include "project.h"

                   

                  int main(void)

                  {

                      uint8_t c ;

                   

                      CyGlobalIntEnable; /* Enable global interrupts. */

                   

                      UART_Start() ;

                      UART_PutString("\x1b[2J\x1b[;H") ; /* clear screen */

                      UART_PutString("This is an ECHO program\n\r") ;

                    

                      for(;;)

                      {

                          if (UART_GetRxBufferSize() > 0) { /* received some data */

                              c = UART_GetByte() ;

                              if ((c != 0)&&(c != 0xFF)) {

                                  if ((c == 'L') || (c == 'l')) {

                                      UART_PutString("Light\n\r") ;

                                  } else if ((c == 'D') || (c == 'd')) {

                                      UART_PutString("Dark\n\r") ;

                                  }

                              }

                          }

                      }

                  }

                  ======================

                   

                  Then the hardware connection was

                  TXD of the USB-Serial to P0[0] of CY8CKIT-050 (White Jumper Wire)

                  RXD of the USB-Serial to P0[1] of CY8CKIT-050 (Green Jumper Wire)

                  GND of the USB-Serial to  GND (or VSSD) of CY8CKIT-050 (Black Jumper Wire)

                  IMG_4318.JPG

                  Then I ran the debugger.

                  In the screen shot below, I typed L, D, L, L, D, D

                  002-ProgramTest.JPG

                  moto

                  (Edited) some typos were taken care of.

                  • 6. Re: Getting unexpected character
                    abho_4730071

                    Hi,

                    I write the below code. When i type any code the LED is turned off and also 'L' the LED is not turning on. Working fine but while getting character to teraterm is not working.The character are unrecognized.

                    • 7. Re: Getting unexpected character
                      MoTa_728816

                      Hi,

                       

                      > I write the below code. When i type any code the LED is turned off and also 'L' the LED is not turning on.

                      > Working fine but while getting character to teraterm is not working.The character are unrecognized.

                       

                      I assume that you used my sample code attached in my previous response,

                      namely "uart_test_050_200730", right?

                       

                      I could not understand why LED does not turned on/off, then I noticed that in that project I was not connecting LED.

                       

                      Meantime, about the printed letters, your tera term setting may be wrong.

                       

                      The following is my Tera Term Setting(s).

                       

                      Menu: Setup > Terminal...

                      003-TeraTerm-Terminal-setup.JPG

                       

                      Menu: Setup > Serial Port...

                      Note: As you know, Port may be different depending on the environment.

                      004-TeraTerm-Serial-port-setup.JPG

                       

                      And my Tera Term output is

                      000-TeraTerm-log.JPG

                       

                      Meantime to take care of LED, I modified the project as below.

                       

                      schematic

                      001-schematic.JPG

                      pins

                      002-Pins.JPG

                       

                      main.c

                      ===========================

                      #include "project.h"

                       

                      #define LED_ON 1u

                      #define LED_OFF 0u

                       

                      int main(void)

                      {

                          uint8_t c ;

                         

                          CyGlobalIntEnable; /* Enable global interrupts. */

                       

                          UART_Start() ;

                          UART_PutString("\x1b[2J\x1b[;H") ; /* clear screen */

                          UART_PutString("This is an ECHO program\n\r") ;

                         

                          for(;;)

                          {

                              if (UART_GetRxBufferSize() > 0) { /* received some data */

                                  c = UART_GetByte() ;

                                  if ((c != 0)&&(c != 0xFF)) {

                                      if ((c == 'L') || (c == 'l')) {

                                          LED_Write(LED_ON) ;

                                          UART_PutString("Light\n\r") ;

                                      } else if ((c == 'D') || (c == 'd')) {

                                          LED_Write(LED_OFF) ;

                                          UART_PutString("Dark\n\r") ;

                                      }

                                  }

                              }

                          }

                      }

                      ===========================

                       

                      moto

                      • 8. Re: Getting unexpected character
                        abho_4730071

                        Hi I am getting the output but sometimes some special character is coming as below hilighted.

                        • 9. Re: Getting unexpected character
                          MoTa_728816

                          Hi,

                           

                          Usually we see that symptom when Tera Term Terminal setup's New-line "Receive" is incorrect.

                          005-new-line.JPG

                           

                          But in your case I'm suspecting the followings

                          (1) The USB-Serial Converter (especially RX) is damaged as you may have connected the pin to the 15V output.

                          (2) Connection of CY8CKIT-050 and rx of UART-Serial Converter is not reliable.

                          (3) The Font of Character Set is set to some special language.

                               But if you see the output without problem, when you use USBFS (USBUART),

                              I would suspect (1) or (2)

                           

                           

                          moto

                          • 10. Re: Getting unexpected character
                            abho_4730071

                            Hi ,

                            If the USB - serial converter damaged then how LED functionality is working in the same program and also see the initial string ""This is an ECHO Program" is showing as expected?

                            #include "project.h"

                             

                             

                            int main(void)

                            {

                                //uint8_t c;

                                char c;

                              

                                CyGlobalIntEnable; /* Enable global interrupts. */

                             

                             

                                UART_Start() ;

                                UART_PutString("\x1b[2J\x1b[;H") ; /* clear screen */

                                UART_PutString("This is an ECHO program\n\r") ;

                              

                                for (;;) {

                             

                                    if (UART_GetRxBufferSize() > 0) { /* received some data */

                                        c = UART_GetByte() ;

                                      

                                       // UART_PutString("This is an ECHO program\n\r") ;

                                      //  c = UART_GetChar() ;

                                        CyDelay(1000);

                                        if ((c != 0) && (c != 0xFF))

                                        {

                                            if(c=='L' || c=='l')

                                            {

                                            UART_PutString("Light \n\r") ;

                                            LED_Write(1);

                                            }

                                            else

                                            {

                                                UART_PutString("Dark \n\r") ;

                                                LED_Write(0);

                                            }

                                        }

                                    }

                                }

                            }

                             

                            Thanks,

                            Abinash

                            • 11. Re: Getting unexpected character
                              MoTa_728816

                              Hi,

                               

                              > If the USB - serial converter damaged then how LED functionality is working in the same program?

                              That part requires only the TXD port of the USB Serial converter,

                              so it will work even if the RXD port is not working perfectly.

                               

                              And if you have connected the USB-Serial Converter to the DB9/RS232C level signal,

                              there should have been over voltage applied to the RXD of the USB Serial Converter.

                               

                              But as you are seeing at least "Light" and "Dark", I hope that the TXD port is working

                              (or almost working).

                               

                              Meantime, if you changed the Tera Term's new line / receive to AUTO

                              and still having unexpected letters along with Light and Dark

                              there may be problem with the TXD port.

                               

                              Are you using the project I attached or have you manually copied the program?

                              If you are using your own project, can you attach the archive here,

                              so that I can test it in my side of the planet.

                               

                              moto

                              (edited) some typos and RXD Part -> RXD Port

                              • 12. Re: Getting unexpected character
                                abho_4730071

                                Please find an attachment of my project.

                                • 13. Re: Getting unexpected character
                                  MoTa_728816

                                  Hi,

                                   

                                  I downloaded your project and tested.

                                   

                                  As you have changed the baud rate of UART component to 19200

                                  003-Uart-Config.JPG

                                  I needed to change the TeraTerm Serial setup

                                  002-Tera-Term-Serial-port-setup.JPG

                                  Then with the following setup

                                  IMG_4336.JPG

                                  Your program was working OK.

                                  Except that I needed to wait 1 sec for each key press.

                                  001-TeraTerm-log1.JPG

                                  IMHO, you don't need the following line

                                              CyDelay(1000);

                                   

                                  The I did an experiment.

                                  To simulate the condition in which the RX port of USB Serial Converter is not working.

                                  I disconnected the jumper between the RXD of USB Serial Converter and the P0_0 (Tx_1) of CY8CKIT-050.

                                  IMG_4337.JPG

                                  Needless to mention, there were no feed back to TeraTerm,

                                  but when I typed 'L' the LED turned on

                                  and when I typed 'D' the LED turned off.

                                   

                                  Now for the summary of the day.

                                  I modified you program as below

                                  ===========================

                                  #include "project.h"

                                   

                                  int main(void)

                                  {

                                      //uint8_t c;

                                      char c;

                                     

                                      CyGlobalIntEnable; /* Enable global interrupts. */

                                   

                                      UART_Start() ;

                                      UART_PutString("\x1b[2J\x1b[;H") ; /* clear screen */

                                      UART_PutString("CY8CKIT-050 USB-Serial Test 6-Aug-2020\n\r") ;

                                     

                                      for (;;) {

                                    

                                          if (UART_GetRxBufferSize() > 0) { /* received some data */

                                              c = UART_GetByte() ;

                                             

                                             // UART_PutString("This is an ECHO program\n\r") ;

                                            //  c = UART_GetChar() ;

                                             // CyDelay(1000); // this line is not necessary

                                              if ((c != 0) && (c != 0xFF))

                                              {

                                                  if(c=='L' || c=='l')

                                                  {

                                                  UART_PutString("Light \n\r") ;

                                                  LED_Write(1);

                                                  }

                                                  else

                                                  {

                                                      UART_PutString("Dark \n\r") ;

                                                      LED_Write(0);

                                                  }

                                              }

                                          }

                                      }

                                  }

                                  ===========================

                                   

                                  And the TeraTerm log was

                                  010-TeraTerm-log2.JPG

                                   

                                  moto

                                  • 14. Re: Getting unexpected character
                                    abho_4730071

                                    Ya that is fine i am also getting special character whenever i am typing something along with my output.

                                    Not sure why i am getting that. Can you please help me on that?

                                    1 2 Previous Next