Using C# .NET with Arduino

Hi,

I would like to have the serial monitor output from the Arduino IDE show on a .NET C# GUI, however, I am having trouble getting an output to show on the GUI. The program doesn't seem to be receiving any data at all.

I have made sure that the COM's are the same as well as the baud rate, and I have also ensured that the serial monitor is receiving data to output by checking the serial monitor on the Arduino IDE prior to running the C# program. I would really appreciate some advice on this issue.

Below is the C# code on Visual Studio and the Arduino code separately.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace test
{
    public partial class Form1 : Form
    {
        private SerialPort myport;
        string in_data;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            myport = new SerialPort();
            myport.BaudRate = 9600;
            myport.PortName = "COM3";
            myport.DataReceived += Myport_DataReceived;
            try
            {
                myport.Open();
                tbxOut.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }

        private void Myport_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string in_data = myport.ReadLine();
            this.Invoke(new EventHandler(displaydata_event));
        }

        private void displaydata_event(object sender, EventArgs e)
        {
            tbxOut.AppendText("Received data: "+in_data);
            Console.WriteLine("Received data: " + in_data);
        }
    }
}
#include "HX711.h"

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;

HX711 scale;

void setup() {
  Serial.begin(9600);
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
}

void loop() {

  if (scale.is_ready()) {
    long reading = scale.read();
    String readingStr = String(reading);
    Serial.println(reading);
  } else {
    Serial.println("HX711 not found.");
  }

  delay(1000);
}

I haven't tested your code, but IMHO it looks like a matter of variable scope.
Inside "Myport_DataReceived" you define a local variable with the same name of the global one, so "displaydata_event" won't ever read and print the received data, but just the global (empty) variable.

Try this fix and see if solves your issue:

        private void Myport_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            //string in_data = myport.ReadLine(); // NO!!!
            in_data = myport.ReadLine();
            this.Invoke(new EventHandler(displaydata_event));
        }

Thank you so much. I completely missed that. I fixed the issue and it works now. :smile:

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