Reading Serial Data from Arduino in Windows Forms Application

I am currently trying to build a windows forms app that gets sensor data from an arduino via the serial com.

when checking in the arduino IDE the data gets writen into the serial port correctly. But i can't figure out how to read the data via c#.

class Program
{
    static SerialPort SP;
    static void Main(string[] args)
    {
        SP = new SerialPort();
        SP.PortName = "COM7";
        SP.BaudRate = 9600;
        SP.Handshake = System.IO.Ports.Handshake.RequestToSend;
        SP.Open();

        while (true)
        {
            Console.WriteLine(DateTime.Now.ToString() + " : " + SP.ReadLine());
        }

    }
}

My guess is that the Port is not properly set up, but i have no idea what i am missing.

The Goal is just to receive strings from the arduino, i do not necessarily need to send any data to the arduino. custom battery

edit: i am working with an arduino micro

Seems like a C# question may be posting this on a Microsoft forum would get you more expert eyes

That was discussed before for example here

1 Like

Try it without the handshake.

@joni78, your topic has been moved to a more suitable location on the forum.

Please show your Arduino code; as is, your C# code works with the below Arduino code (on a Nano). I'm using VS2017.

void setup()
{
  Serial.begin(57600);
}

void loop()
{
  Serial.println("Hello world");
  delay(2000);
}

Here is a rudimentary example of using an event handler to capture and display data from an Arduino (I used a Uno and code similar to what sterretje posted above). The C# project is simply windows form with a richtext box control. Do not copy and paste the event handlers instead access them from the properties window in design view. This is a good basis to start creating your own custom Arduino serial port monitor.

namespace Serial_Port_Reader
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        SerialPort SP = new SerialPort();
        public delegate void spDelegate(string sData);


        private void Form1_Load(object sender, EventArgs e)
        {
            SP.DataReceived += SP_DataReceived;
            SP.PortName = "COM7";
            SP.BaudRate = 9600;
            SP.DtrEnable = true;              //reset Arduino
            SP.Open();
            SP.ReadExisting();                //clear any junk that might be in the serial buffer
        }

        private void SP_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SP.ReadTimeout = 20;

            try
            {
               
                string str = SP.ReadLine();
                this.BeginInvoke((new spDelegate(stringDisplay)), str);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

         private void stringDisplay(string sData)
        {
            DateTime localDate = DateTime.Now;              //snippet copied from docs.Microsoft.com
            richTextBox1.AppendText(localDate + "   ");
            richTextBox1.AppendText(sData + "\n");
        }

    }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.