Jose Paulo Serra
Jose Paulo Serra

Reputation: 1

Read a serialport in real time

I am trying to perform a control where I receive information sent by Arduino.

This information is of type string and format VX 1.987 or VY 0.123 and are sent at high speed. After they are filtered and treated, they update the textbox in my program, and in this way what I see in the TextBox is the last information received.

What happens with my code is that at a certain point, for example, were sent 1000 lines by the Arduino, my program will update up to 600 and then stop showing the updates. I know that in the receive buffer are the other 400 rows, but they were not shown. Summarizing how do I make memo time be receiving the information by serial and reading and putting in the textbox? Could this be a processing time issue? How to solve?

namespace ControleCaseiro
{
    private void Form1_Load(object sender, EventArgs e)
    {
         serialPort1.DataReceived += new    SerialDataReceivedEventHandler(serialPort1_DataReceived);
    } 

    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {                   
                    DataIn = serialPort1.ReadLine();
                    this.Invoke(new EventHandler(MostraDados));                
            }
            catch(Exception ex)
            { }
        }

     private void MostraDados(object sender, EventArgs e)
      {
        switch (DataIn)
            {
                case "19\r":
                    textcontrole.Text = "XOFF";
                    break;
                case "17\r":
                     textcontrole.Text = "XON";
                    if (botãoEnviaGcode)
                    {
                        LinhaArquivo++;
                        EnviaGcode();
                    }
                    break;
                default:
                    FiltroDadosRecebidos(DataIn);                 
                    break;
            }      
      }

    private void FiltroDadosRecebidos(string valorRecebido)
     {        
        string eixo="";
        int tamanho=0;
        string valorEixoAtual="";
        if (valorRecebido.Length > 1)
            {               
                    tamanho = valorRecebido.Length;           
                     eixo = valorRecebido.Substring(0, 2);                   
            switch (eixo)
               {                             
                  case "VX":
                              valorEixoAtual = valorRecebido.Substring(2, tamanho - 3);           
                              txtPosAtualX.Text=valorEixoAtual;
                              break;
                  case "VY":
                              valorEixoAtual = valorRecebido.Substring(2, tamanho - 3);
                              txtPosAtualY.Text=valorEixoAtual;
                              break;
                  case "VZ":
                              valorEixoAtual = valorRecebido.Substring(2, tamanho - 3);
                              txtPosAtualZ.Text=valorEixoAtual;
                              break;
                   default :
                              DadosRecebidos.Items.Add(DataIn);// se não for dados referentes aos eixos, vai mostrar na listbox “DadosRecebidos”
                              break;
               }

            }                     

        }
    }

As I have already described in the beginning, what it is that the Arduino is sending the information and after a while, everything to and the information is not shown in the TextBox. What it looks like is that while OA SerialPort is receiving data, the event "SerialDataReceivedEventArgs " causes the program to show the information, when it is out of receive, no longer shows and the information is accumulated in the serialport buffer.

Upvotes: 0

Views: 547

Answers (1)

bwing
bwing

Reputation: 781

The issue is likely that you're not handling all the data in the receive buffer. You're only extracting one line of the receive buffer even though there may be multiple lines waiting to be processed. You could try changing your handler to something like this:

    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        try
        {
            while (serialPort1.BytesToRead > 0)
            {
                DataIn = serialPort1.ReadLine();
                this.Invoke(new EventHandler(MostraDados));
            }
        }
        catch (Exception ex)
        { }
    }

Upvotes: 1

Related Questions