C# - Receive data via serial and pause receiving

Asked

Viewed 30 times

0

Hello

I am developing a software in C# that receives data via serial communication, everything is working but I have a little problem. My software receives the data via serial and displays in Text Box, but soon after the data is deleted from Text Box, I believe it is due to the Timer that I entered in the Software to check if something was received by Serial. I would like to receive the data and that they remain in Text Box, how to fix this problem ?

This is the part of the code that receives and displays the data:

private void timerReceive_Tick(object sender, EventArgs e)
    {
        if (serialPort1.IsOpen == true)
        {
           if (serialPort1.ReadBufferSize >= 0)
           {
               RxString = serialPort1.ReadExisting();
               textBoxReceber.Text = RxString;

            }
        }
    }

Thank you

  • 1

    As you are checking that the value is equal to 0 (zero) then it accepts and overwrites the value of the field. leave only when it is greater than 0. if (serialPort1.ReadBufferSize >= 0)

  • Store content in a larger scope and concatenate

1 answer

0

I want to thank you for the help and share how I solved the problem in my code.

inserir a descrição da imagem aqui

The problem was in my variable Rxstring that is inside the Timer, which aims to keep updating and collecting everything received by the serial, so if I receive for example the phrase ( Hello World ) it stores in variable Rxstring and if I don’t receive nothingness he was also storing emptiness in my variable, then I inserted the following:

if(RxString != "")
{
  textBoxReceber.Text = RxString;
}
else
{

}

It checks if the variable has any value thus displaying in Textbox, if the value is emptiness, does nothing. In reality he nay was deleting the contents of Textbox, he was displaying the empty value in the Text Box. Now everything is working perfectly.

Complete Code:

private void timerReceive_Tick(object sender, EventArgs e)
{
  if (serialPort1.IsOpen == true)
  {
    if (serialPort1.ReadBufferSize > 0)
    {
      RxString = serialPort1.ReadExisting();

      if(RxString != "")
      {
        textBoxReceber.Text = RxString;
        navegador.DocumentText = RxString;
      }
      else
      {

      }
    }
  }
}

Thank you all.

  • Tests serialPort1.ReadBufferSize > 0 no need to test RxString != ""

  • So Ramaral I tested here, but it didn’t work. Because it will be ?

  • If you change the code you put in the question, replacing >= for >, I see no reason why it shouldn’t work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.