Teraterm showing correctly but in c# console getting unnecessary data

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

cross mob
lock attach
Attachments are accessible only for community members.
abho_4730071
Level 4
Level 4
First like received

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.

pastedImage_0.png

pastedImage_3.png

0 Likes
1 Solution

Abinash,

The code you desire is already attached to the posts.  Here are the links.

PC SW: https://community.cypress.com/servlet/JiveServlet/download/247419-57536/SerialPortConsole%20(Len).zi...

PSoC SW: https://community.cypress.com/servlet/JiveServlet/download/247419-57529/Lession3%20(Len).cyprj.zip

I have a further modified version that doesn't use the "ON  " and "OFF  ".  It uses only one character each '1' and '0' respectively.  Here are the links.

PC SW: https://community.cypress.com/servlet/JiveServlet/download/247420-57537/SerialPortConsole%20(Len)%20...

PSoC SW: https://community.cypress.com/servlet/JiveServlet/download/247420-57538/Lession3%20(Len)%201%20char....

Enjoy!

Len

Len
"Engineering is an Art. The Art of Compromise."

View solution in original post

16 Replies
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

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

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?

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

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

0 Likes

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?

pastedImage_0.png

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

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

0 Likes
Kenshow
Level 8
Level 8
Distributor - Marubun (Japan)
50 solutions authored 25 solutions authored 10 solutions authored

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

MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

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

lock attach
Attachments are accessible only for community members.

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

Len
"Engineering is an Art. The Art of Compromise."
0 Likes
lock attach
Attachments are accessible only for community members.

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

Len
"Engineering is an Art. The Art of Compromise."
0 Likes

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.

Len
"Engineering is an Art. The Art of Compromise."

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(); pastedImage_1.png

0 Likes

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

Len
"Engineering is an Art. The Art of Compromise."
0 Likes

Hi ,

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

Thanks,

Abinash

0 Likes

Abinash,

The code you desire is already attached to the posts.  Here are the links.

PC SW: https://community.cypress.com/servlet/JiveServlet/download/247419-57536/SerialPortConsole%20(Len).zi...

PSoC SW: https://community.cypress.com/servlet/JiveServlet/download/247419-57529/Lession3%20(Len).cyprj.zip

I have a further modified version that doesn't use the "ON  " and "OFF  ".  It uses only one character each '1' and '0' respectively.  Here are the links.

PC SW: https://community.cypress.com/servlet/JiveServlet/download/247420-57537/SerialPortConsole%20(Len)%20...

PSoC SW: https://community.cypress.com/servlet/JiveServlet/download/247420-57538/Lession3%20(Len)%201%20char....

Enjoy!

Len

Len
"Engineering is an Art. The Art of Compromise."

Hi ,

I tried in the same way. While debugging i am getting the ascii value "63" always , which means the value coming from controller is "?"

pastedImage_0.png

0 Likes

abinash,

I'm not sure why you're getting '?' always.

    static void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)

    {

        string str = "String is : ";

        int c = _serialPort.ReadChar();    // change from ReadLine() to ReadChar()

        if (c == '1') str = str + "ON";

        else if(c == '0') str = str + "OFF";

        Console.WriteLine(str + "    Input (dec) = " + c);

    }

I changed  to add more debugging info.

I get what I'm suppose to get:

Type QUIT to exit

String is : ON    Input (dec) = 49

String is : OFF    Input (dec) = 48

String is : ON    Input (dec) = 49

String is : OFF    Input (dec) = 48

String is : ON    Input (dec) = 49

String is : OFF    Input (dec) = 48

String is : ON    Input (dec) = 49

String is : OFF    Input (dec) = 48

String is : ON    Input (dec) = 49

String is : OFF    Input (dec) = 48

String is : ON    Input (dec) = 49

String is : OFF    Input (dec) = 48

String is : ON    Input (dec) = 49

String is : OFF    Input (dec) = 48

String is : ON    Input (dec) = 49

String is : OFF    Input (dec) = 48

String is : ON    Input (dec) = 49

String is : OFF    Input (dec) = 48

String is : ON    Input (dec) = 49

String is : OFF    Input (dec) = 48

String is : ON    Input (dec) = 49

String is : OFF    Input (dec) = 48

String is : ON    Input (dec) = 49

String is : OFF    Input (dec) = 48

String is : ON    Input (dec) = 49

String is : OFF    Input (dec) = 48

String is : ON    Input (dec) = 49

String is : OFF    Input (dec) = 48

String is : ON    Input (dec) = 49

Check to make sure the both sides (PC and PSoC) are set to the same baud rate and 8N1.

Len

Len
"Engineering is an Art. The Art of Compromise."
0 Likes