-2

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?

5
  • Just because another, different app is able to communicate doesn't mean your C# app is connecting to the serial port, or reading or writing serial I/O correctly. Q: What is ListenSerial doing? Commented May 11, 2022 at 20:56
  • @paulsm4 i just added the complete code. The ListenSerial should listen continuously for new data from the arduino Commented May 11, 2022 at 21:11
  • 1
    Q: So what's the deal with the empty "catch{}" block? Don't you WANT to detect legitimate exceptions??? Q: I assume you at least see "open port "? Commented May 11, 2022 at 21:14
  • @paulsm4 i could add a warning here. But it doesn't matter, the code doesn't get to this point. Commented May 11, 2022 at 21:16
  • Maybe this article will help you? WinForms Applications: Where is console.writeline() output rendered? Commented Sep 6, 2023 at 23:50

1 Answer 1

0
  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.
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for your feedback. nope, no output on console...
OK: maybe the problem isn't "Serial I/O" at all. Maybe you're just expecting to see "console output" that simply isn't there :) Look here: stackoverflow.com/a/5706780/421195. Q: Are you using Visual Studio (MSVS)? Q: Do you see anything - EVER - in the MSVS "Console" window"? Q: Have you tried setting a breakpoint in Form1_Load() and stepping through the code in the MSVS debugger?
i'm using visual studio 2022 and i already received data in the console while trying other things in the same project. So it should be that the code doesn't reach this point right now
I'm glad you're using Visual Studio 2022 (the "MSVS" compiler IDE, NOT the "Visual Studio Code" editor, correct?) Sooooo ... Q: You moved "Port.Open();" into Form1_Load(), per my suggestion above. Correct? Q: Have you stepped through the code in the debugger? Q: Does it get into "Form1_Load()"? Q: How far does it get after that?
PS: I looked at the tutorial you cited. I have several problems with it, including "eating exceptions". Exceptions are your Friend: you NEED to know if something goes wrong. As quickly as possible, with as much information as possible.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.