1 2 Previous Next 16 Replies Latest reply on Aug 23, 2020 11:35 AM by LePo_1062026

    Teraterm showing correctly but in c# console getting unnecessary data

    abho_4730071

      Hi team,

       

      I attach my both PSOC Program and c# program. I am getting proper data in TeraTerm, but when i tried the same in C# console application i am getting garbage data. See the below screenshot.I am using PSOC 5LP.

      I attached my both program.

      1st image is C# Console and 2nd image is Teraterm.

       

       

        • 1. Re: Teraterm showing correctly but in c# console getting unnecessary data
          MoTa_728816

          Hi,

           

          I downloaded and tested your project.

          First of all it is working "as is".

          000-CS_0.JPG

           

          So your problem seems to be your system environment related, such as locale, font, language, etc.

           

          Having written above I'd like to point out a few things.

          (1) "Line" usually supposed to end with an EOL character.

          So IMHO, "ON " is a word, "ON\r" is a line.

           

          (2) As I repeatedly pointed out, in general do not use functions such as UART_PutString() in an ISR.

          Instead just set a flag inside the ISR and take care of the printing in the main().

           

          (3) UART_PutString() is a slow function, and your C# program is slower.

          So I'd recommend to put some delay after calling UART_PutString() to allow both UART

          and the C# program to handle/complete the communication.

           

          So I modified your program as follows

           

          main.c

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

          #include "project.h"

           

          volatile uint8_t button_value = 0 ;      

          volatile int     button_pressed = 0 ;

           

          CY_ISR(INPUT_PIN_SW2_Handler)

          {

              if (OUTPUT_LED_Read() == 0) {

                  button_value = 1 ;

              } else {

                  button_value = 0 ;

              }

              OUTPUT_LED_Write(button_value);

              button_pressed = 1 ;

              INPUT_PIN_SW2_ClearInterrupt();

          }

           

          int main(void)

          {

              CyGlobalIntEnable; /* Enable global interrupts. */

             

              PIN_SW2_int_StartEx(INPUT_PIN_SW2_Handler);

              UART_Start() ;

             

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

          //  UART_PutString("Test 200813B\n\r") ;

           

           

              for(;;)

              {

                  if (button_pressed) {

                      if (button_value == 1) {

                          UART_PutString("ON\r");

                      } else if (button_value == 0) {

                          UART_PutString("OFF\r");               

                      }

                      CyDelay(10) ;

                      button_pressed = 0 ;

                  }

              }

          }

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

           

          The result Tera Term log was

          005-CS_3.JPG

           

          moto

          • 2. Re: Teraterm showing correctly but in c# console getting unnecessary data
            abho_4730071

            Hi ,

            I am not sure why then i am getting correct value in TeraTerm and not getting in C# Console?

            Am i coding wrongly? If yes please help me where is the mistake?

            • 3. Re: Teraterm showing correctly but in c# console getting unnecessary data
              MoTa_728816

              Hi,

               

              I wonder if you ever READ my response?

              I don't mind trying to use Google Translation as far as the language is supported in the translator.

              So please let me know which language you are willing to read.

               

              When I tested on my PC, your c# program worked fine or at least it showed the correct strings.

               

              So I think that the problem is not in your C# Console program but in your environment setting.

              What language and/or font are you using as default for your Visual Studio?

              Or can you just test creating a new account (user) in your PC, whose language is "English"

              and test the program as that user?

               

              moto

              • 4. Re: Teraterm showing correctly but in c# console getting unnecessary data
                abho_4730071

                Hi,

                If there is no environment setting required for c#. In PSOC side looking like settings are ok as i am getting proper output in TeraTerm. I am using English. I am using C#. Please let me know the issue?

                Now i am getting this pattern. Not sure why?

                • 5. Re: Teraterm showing correctly but in c# console getting unnecessary data
                  MoTa_728816

                  Hi,

                   

                  Right now the issue is happening only on your side,

                  and I could not reproduce the problem with your program(s).

                   

                  So I can not tell what is the cause.

                   

                  BTW, have you tried my sample code?

                  And did it also generated corrupted strings in your C# console?

                   

                  moto

                  • 6. Re: Teraterm showing correctly but in c# console getting unnecessary data
                    MoTa_728816

                    Hi,

                     

                    Apparently your PC is receiving some corrupted letters along with the correct string from PSoC.

                     

                    I'm still suspecting that your USB-Serial Converter has been damaged or has some problem,

                    especially at RX port.

                     

                    Although I can not remember how many times I have suggested you to perform "loop-back" test.

                     

                    Let me beg you to perform "loop-back" test and let me know the result.

                     

                    moto

                    • 7. Re: Teraterm showing correctly but in c# console getting unnecessary data
                      NoTa_4591161

                      Hello,

                       

                      Which do you think the cause of this issue is PSoC output issue or C# program issue?

                       

                      If you think this is a PSoC output issue, please observe the UART output and point out the observed waves.

                      If you think this issue is a C# program issue, you should discuss it in a forum that discusses C#, such as MS, rather than this communication site.

                       

                      Regards,
                      Kenshow

                      • 8. Re: Teraterm showing correctly but in c# console getting unnecessary data
                        LePo_1062026

                        abho,

                         

                        What kit/eval board are you using for this project?

                         

                        I found a few issues with your code.

                         

                        When I run your code here is an example of the output I received:

                        String is : ? ?  ?  ?     ? ON

                         

                         

                        String is : O

                        String is : FF

                         

                         

                        String is : ON

                        String is :

                         

                         

                        String is : O

                        String is : F

                        String is : F

                        String is :

                         

                         

                        String is : O

                        String is : N

                        String is :

                        String is :

                        String is :

                         

                         

                        String is : O

                        String is : F

                        String is : F

                        String is :

                        String is :

                        String is :

                         

                         

                        String is : ON

                        String is :

                        String is :

                        String is :

                        I have diagnosed the reason for sporadic output of the "ON" and "OFF" is that you are using the following code in the SerialPortConsole sp_DataReceived() event handler:

                            static void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
                            {
                                SerialPort sp = (SerialPort)sender;
                                string str = sp.ReadExisting();
                                Console.WriteLine("String is : " + str);
                            }
                        

                         

                        The sp_DataReceived() event handler will be called if ANY data is in the Rx buffer.  Because you are using sp.ReadExisting(), it gets what characters are in the buffer at the time to output in Console.WriteLine("String is : " + str);   This means the entire string you are trying to send ("ON  " or "OFF  ") may not be sent from the PSoC by the time you process the output string to the Console on the PC.

                         

                        There are two very quick fixes you need to make:

                        • To the SerialPortConsole\Program.cs change ReadExisting() to ReadLine() shown below:
                        static void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
                        
                            {
                                SerialPort sp = (SerialPort)sender;
                                string str = sp.ReadLine();     // change from ReadExisting() to ReadLine()
                                Console.WriteLine("String is : " + str);
                            }
                        
                        
                        • To the Lesson3.cydsn\main.c change:
                          • UART_PutString("OFF  "); to UART_PutString("OFF  \n");''
                          • UART_PutString("ON  "); to UART_PutString("ON  \n");''

                         

                        Adding the "\n" to these PutString()s will send a line feed to the SerialPortConsole program to better process the sp.ReadLine() change which is looking for the line feed.

                        Now your output show improve with much less skipped or incomplete data from the PSoC.

                         

                        Len

                         

                        See my next post for general improvements in your coding to help it to become more reliable.

                        1 of 1 people found this helpful
                        • 9. Re: Teraterm showing correctly but in c# console getting unnecessary data
                          LePo_1062026

                          adho,

                           

                          I hope you don't mind some coding improvements to make your code tighter and more reliable from a real-time perspective.

                           

                          PSoC5 Code (Lesson3.cydsn)

                           

                          Add a Switch Debouncer

                           

                          By adding a debouncer component after your switch input false triggers due to a 'bouncing' switch will basically eliminate extra (and wrong) button press events.

                          Here's a snip of the new circuit in TopDesign:

                           

                          No Start() or Init() code is needed in main.c

                           

                          Move the PutString() out of the ISR

                           

                          Move the PutString() (and the OUTPUT_LED_Write() calls from the INPUT_PIN_SW2_Handler() ISR and place in main().

                          PutString() is a potentially blocking function.  This means if all the data being sent is not put into the Tx FIFO, the function will loop until done,  This will block other valid interrupt servicing since you placed the PtuString() in the ISR routine.

                           

                          I realize that your code is VERY simple and in essence you have no other interrupts this should not be a problem.  The general principle if interrupt coding is to "get in and get out" quickly.   If you use this project to build on in the future, you will appreciate the more efficient use of your ISR handling especially if you use other interrupts.

                           

                          Here are code snips of these changes:

                          CY_ISR(INPUT_PIN_SW2_Handler)
                          {
                              button_pressed = 1 ;
                          //    INPUT_PIN_SW2_ClearInterrupt();
                          }
                          
                          
                          int main(void)
                          {
                              CyGlobalIntEnable; /* Enable global interrupts. */
                               UART_PutString("\x1b[2J\x1b[;H") ; /* clear screen */
                              PIN_SW2_int_StartEx(INPUT_PIN_SW2_Handler);
                          
                              UART_Start() ;
                          
                              //UART_PutString("Test B2\n\r") ;
                          
                              for(;;)
                              {
                                  if (button_pressed)
                                  {
                                      // toggle the LED state
                                      if( OUTPUT_LED_Read() == 0) 
                                      {
                                          UART_PutString("ON  \n");
                                          OUTPUT_LED_Write(1);
                                      }
                                      else 
                                      {
                                          OUTPUT_LED_Write(0); 
                                          UART_PutString("OFF  \n");
                                      }
                                      button_pressed = 0 ;   // clear button press flag
                                  }
                              }
                          }
                          

                           

                          PC Code (SerialPortConsole.cydsn)

                           

                          Changing ReadExisting() to ReadLine()

                           

                          This is the working suggestion listed in the previous post.

                           

                          Redundant var sp

                           

                          Opening a SerialPort var sp in sp_DataReceived() is somewhat redundant.  You allocated _serialPort as a Global to the class therefore it could be used instead of the local declared var sp.

                              static void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
                              {
                                  string str = _serialPort.ReadLine();     // change from ReadExisting() to ReadLine()
                                  Console.WriteLine("String is : " + str);
                              }
                          

                           

                          I've attached modified versions of your Lesson3 and SerialPortConsole projects

                           

                          Len

                          • 10. Re: Teraterm showing correctly but in c# console getting unnecessary data
                            LePo_1062026

                            abho,

                             

                            If you're interested, I've further modified your PSoC and PC code to be slightly more efficient.  The major change is to pass only one char between the PSoC and the PC.  Instead of sending "ON  \n" I send '1'.  Instead of "OFF \n" I send '0'.  It's short and simple and reduces potential data transfer errors.

                             

                            Len

                            • 11. Re: Teraterm showing correctly but in c# console getting unnecessary data
                              abho_4730071

                              I changed as above. but my application getting Hanged.

                              Only Rx is working and after sometime i am getting error.

                              I am getting the error in _serialPort.ReadLine();

                              • 12. Re: Teraterm showing correctly but in c# console getting unnecessary data
                                LePo_1062026

                                abho,

                                 

                                I've double checked my code recommendation for sp_DataReceived() and the recommended PutString()s with the '\n' at the end of the string.  It's working for me.

                                 

                                Are you sure you added to Lesson3.cydsn\main.c change:

                                UART_PutString("OFF  "); to UART_PutString("OFF  \n");''

                                UART_PutString("ON  "); to UART_PutString("ON  \n");''  ???

                                 

                                If I don't make the change in the PutString()s, I get the same error you report after 8.33 minutes of a button press.

                                 

                                Len

                                • 13. Re: Teraterm showing correctly but in c# console getting unnecessary data
                                  abho_4730071

                                  Hi ,

                                  I am getting the same error i mentioned. Can you please send PSOC program and C# Application where you are testing?

                                   

                                  Thanks,

                                  Abinash

                                  1 2 Previous Next