Reading real-time serial port in csharp

Asked

Viewed 60 times

1

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

This information is of the type string and format VX1.987or VY0.123 and are sent at high speed. After being filtered and treated, update textbox in my show, and that way what I see in textbox is the last information received.

What happens to my code is that at a certain point, for example, they were sent 1000 lines by Arduino, my program keeps updating until 600 and then stop showing updates. I know that on buffer of reception are the other 400 lines, but were not shown.

I searched the internet and I saw many commands but could not align with my need.

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;
               }

            }                     

        }
    }
No answers

Browser other questions tagged

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