Receiving data via serial connection C #

9

I need to make a program that has to send a command to a radio connected via serial port and it returns its ID. The connection to the port and the sending of data is okay, when I send something the light flashes. However I need your feedback and from what I saw in the debug the program does not enter the serialPort1_DataReceived class. What could it be?

Follow the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;  // necessário para ter acesso as portas     


namespace serialteste
{
public partial class Form1 : Form
{
    string RxString;

    public Form1()
    {
        InitializeComponent();

    }


    private void atualizaListaCOMs()
    {
        int i;
        bool quantDiferente;    //flag para sinalizar que a quantidade de portas mudou

        i = 0;
        quantDiferente = false;

        //se a quantidade de portas mudou
        if (comboBox1.Items.Count == SerialPort.GetPortNames().Length)
        {
            foreach (string s in SerialPort.GetPortNames())
            {
                if (comboBox1.Items[i++].Equals(s) == false)
                {
                    quantDiferente = true;
                }
            }
        }
        else
        {
            quantDiferente = true;
        }

        //Se não foi detectado diferença
        if (quantDiferente == false)
        {
            return;                     //retorna
        }

        //limpa comboBox
        comboBox1.Items.Clear();

        //adiciona todas as COM diponíveis na lista
        foreach (string s in SerialPort.GetPortNames())
        {
            comboBox1.Items.Add(s);
        }
        //seleciona a primeira posição da lista
        comboBox1.SelectedIndex = 0;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //atualizaListaCOMs();

    }





    private void btConectar_Click(object sender, EventArgs e)
    {
        if (serialPort1.IsOpen == false)
        {
            try
            {


                serialPort1.PortName = comboBox1.Items[comboBox1.SelectedIndex].ToString();
                serialPort1.Open();

            }
            catch
            {
                return;

            }
            if (serialPort1.IsOpen)
            {
                btConectar.Text = "Desconectar";
                comboBox1.Enabled = false;

            }
        }
        else
        {

            try
            {
                serialPort1.Close();
                comboBox1.Enabled = true;
                btConectar.Text = "Conectar";
            }
            catch
            {
                return;
            }

        }
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        if (serialPort1.IsOpen == true)  // se porta aberta
            serialPort1.Close();            //fecha a porta


    }

    private void btEnviar_Click(object sender, EventArgs e)
    {
        if (serialPort1.IsOpen == true)          //porta está aberta
            serialPort1.Write(textBoxEnviar.Text);  //envia o texto presente no textbox
    }

    private void trataDadoRecebido(object sender, EventArgs e)
    {
        textBoxReceber.AppendText(RxString);
    }

    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {


        textBoxReceber.Text="alguma coisa recebeu";
        RxString = serialPort1.ReadExisting();              //le o dado disponível na serial
        this.Invoke(new EventHandler(trataDadoRecebido));   //chama outra thread para escrever o dado no text box
    }

    private void Atualizar_Click(object sender, EventArgs e)
    {
        atualizaListaCOMs();
    }      


}
}
    
asked by anonymous 19.06.2015 / 20:49

1 answer

0

To fire the process would be:

_serialPort.DataReceived += new SerialDataReceivedEventHandler(RecebeDadosSerial); 

But it would be interesting to send us a manual about your device. It may not be sending anything to your serial. It may be that the ENQ command needs to be sent to have a return, it can be done as follows:

string ENQ = "\u0005\r\n"; // -> \n padrão de Escape 
        _serialPort.Write(ENQ);
    
25.06.2015 / 15:40