Question

I have a system that sends an "at" command to a serial port and displays the return on a MessageBox. But I needed to do this in all available serial ports. So I created a List and I'm adding all the ports on it. I managed to send the command, but could not continue the rest of the code to catch the return because I am having trouble handling the lists. I am a beginner in C #. Below is my current code. The part that is commented out is what I'm struggling to continue. This part belongs to the old code (when it was just one serial port).

public partial class Form1 : Form
    {
        List<SerialPort> serialPort = new List<SerialPort>();

        // delegate is used to write to a UI control from a non-UI thread
        private delegate void SetTextDeleg(string text);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var portNames = SerialPort.GetPortNames();
            foreach (var port in portNames) {
                SerialPort sp;
                sp = new SerialPort(port, 19200, Parity.None, 8, StopBits.One);
                sp.Handshake = Handshake.None;
                //sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
                sp.ReadTimeout = 500;
                sp.WriteTimeout = 500;

                serialPort.Add(sp);
                listPorts.Items.Add(port);
            }
        }

        private void listPorts_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (var sp in serialPort) {
                // Open port
                try
                {
                    if (!sp.IsOpen)
                        sp.Open();

                    MessageBox.Show(sp.PortName + " aberto!");
                    sp.Write("at\r\n");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
                }
            }
        }

        /* HELP START

        void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(500);
            string data = sp.ReadLine();
            this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
        }

        private void si_DataReceived(string data)
        {
            String retorno = data.Trim();
            MessageBox.Show(retorno);
            // Fecha a porta após pegar o retorno
            sp.Close();
        }

        HELP END */

    }

What to put in place 'sp.ReadLine ();' and 'sp.Close ();'? I don't know do this because of the List <>

Was it helpful?

Solution

The simplest approach would be to use a lambda expression which would capture the port you're using. A lambda expression is a way of building a delegate "inline" - and one which is able to use the local variables from the method you declare it in.

For example:

foreach (var port in portNames)
{
    // Object initializer to simplify setting properties
    SerialPort sp = new SerialPort(port, 19200, Parity.None, 8, StopBits.One)
    {
        Handshake = Hanshake.None,
        ReadTimeout = 500,
        WriteTimeout = 500
    };
    sp.DataReceived += (sender, args) =>
    {
        Thread.Sleep(500); // Not sure you need this...
        string data = sp.ReadLine();
        Action action = () => {
            MessageBox.Show(data.Trim());
            sp.Close();
        };
        BeginInvoke(action);
    };
    serialPort.Add(sp);
    listPorts.Items.Add(port);
}

A few notes about this:

  • Just because some data has been received doesn't mean that a whole line has, so ReadLine may still block
  • If you only need to show a message box, you may not need Control.BeginInvoke. (If you need to do more here, you might want to extract most of that code into a separate method which just takes a string, then create an action which would call that method.)
  • Are you sure you want to close the serial port as soon as the first line has been received?

OTHER TIPS

You can chage your sp_DataReceived method as,

       void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(500);
            SerialPort sp = (SerialPort)sender;
            string data = sp.ReadLine();
            this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
            sp.Close();
        }

and remove the sp.Close(); from si_DataReceived method.

If you want to have a Serial port value in your si_DataReceived method, you should pass it there:

  // First, add port into your delegate
  private delegate void SetTextDeleg(SerialPort port, string text);
  ...
  /* HELP START */

  void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
  {
    Thread.Sleep(500);
    SerialPort sp = (SerialPort) sender; // <- Obtain the serial port
    string data = sp.ReadLine();

    // Pass the serial port into  si_DataReceived: SetTextDeleg(sp, ...
    this.BeginInvoke(new SetTextDeleg(sp, si_DataReceived), new object[] { data }); 
  }

  // "SerialPort sp" is added
  private void si_DataReceived(SerialPort sp, string data) {
    String retorno = data.Trim();
    MessageBox.Show(retorno);
    // Fecha a porta após pegar o retorno
    sp.Close();
  }

  /* HELP END */

See also:

http://msdn.microsoft.com/library/system.io.ports.serialport.datareceived.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top