reyntjensm
reyntjensm

Reputation: 23

Communication from Arduino to C# forms app

I'm following this tutorial. The goal is to receive periodic data from the Arduino via serial port.

The com port connection is fine as I'm unable to connect with another terminal program to the Arduino when my c# app is running (port is already connected). At this point the SerialListen thread should start but this doesn't happen.

namespace TestReceiveArduino
{
public partial class Form1 : Form
{
    //object serialport to listen usb
    System.IO.Ports.SerialPort Port;

    //variable to check if arduino is connect
    bool IsClosed = false;

    public Form1()
    {
        InitializeComponent();


        //configuration of arduino, you check if com3 is the port correct, 
        //in arduino ide you can make it
        Port = new System.IO.Ports.SerialPort();
        Port.PortName = "COM11";
        Port.BaudRate = 9600;
        Port.ReadTimeout = 500;

        try
        {
            Port.Open();
            Console.WriteLine("open port ");


        }
        catch { }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //A Thread to listen forever the serial port
        Console.WriteLine("start thread ");
        Thread Hilo = new Thread(ListenSerial);
        Hilo.Start();
    }


    private void ListenSerial()
    {
        Console.WriteLine("start listener");
        while (!IsClosed)
        {
            Console.WriteLine("in while");

            try
            {
                //read to data from arduino
                string AString = Port.ReadLine();

                //write the data in something textbox
                txtSomething.Invoke(new MethodInvoker(
                    delegate
                    {
                        txtSomething.Text = AString;
                    }
                    ));

            }
            catch { }
        }
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        //when the form will be closed this line close the serial port
        IsClosed = true;
        if (Port.IsOpen)
            Port.Close();
    }
}
}

My Arduino is sending data, I've checked this with terminal software. I'm also using the correct COM port. I have some experience with C# but I'm new to threads. What could be the reason for this?

Upvotes: -2

Views: 1123

Answers (1)

paulsm4
paulsm4

Reputation: 121849

  1. See if these changes help:

    namespace TestReceiveArduino
    {
        public partial class Form1 : Form
        {
            //object serialport to listen usb
            System.IO.Ports.SerialPort Port;
    
            //variable to check if arduino is connect
            bool IsClosed = false;
    
            public Form1()
            {
                InitializeComponent();  
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                try
                {
                    // Connect to Arduino
                    Port = new System.IO.Ports.SerialPort();
                    Port.PortName = "COM11";
                    Port.BaudRate = 9600;
                    Port.ReadTimeout = 500;
                    Port.Open();
                    Console.WriteLine("Port successfully opened: Name: {0}, Baud: {1}, ReadTimeout: {2}", Port.PortName, Port.BaudRate, Port.ReadTimeout);
    
                   //A Thread to listen forever the serial port
                   Console.WriteLine("start thread ");
                   Thread Hilo = new Thread(ListenSerial);
                   Hilo.Start();
                }
                catch (Exception ex)
                { 
                    Console.WriteLine(ex.Message);
                }
            }
            ...
    
  2. I moved "Open Serial Connection" and "Start Listener Thread" to the same place (Form1_Load), and wrapped both in the same try/catch block. You might even want to move the "try/catch" to a higher level (e.g. so you can display any exceptions in your Windows GUI).

  3. I assume you're using Microsoft Visual Studio (e.g. MSVS Express 2019) as your GUI, correct? Definitely familiarize yourself with your IDE's debugger; definitely get in the habit of stepping through the code as you're developing it.

  4. Your next steps:

    • Verify the code gets to "Open Serial Connection", and verify that it opens correctly (e.g.prints "Port successfully opened...").
    • Verify the code then gets to ListenSerial(), and prints "start listener" and "in while" at least once.

Upvotes: 0

Related Questions