Sending Data from Arduino to C# Windows Forms

So I'm quite confused on how to send any kind of information from Arduino to C#. I've seen a post which explained how to do for Console App. Now I'm trying to "convert" this code to Windows Forms, since it's a bit different, but I'm quite a newbie in programming, so I don't really know how. What I need is basically that when I press a button (PULL-UP), it sends to C# a text (such as "You've pressed the button), by using label. Basically editing "label1" text to "You've pressed the button" text.

Here's the code:

internal class Program
     {
         static SerialPort serialPort;
         static void Main(string[] args)
         {
             serialPort = new SerialPort();
             serialPort.PortName = "COM5";
             serialPort.BaudRate = 9600;
             serialPort.Open();
             while(true)
             {
                 string a = serialPort.ReadExisting();
                 Console.WriteLine(a);
                 Thread.Sleep(1000);
             }
         }
    }

Looking at his code in Excel might give some hints?

1 Like

Not really, I'm sorry...

this is a VERY basic C# application using the SerialPort component it opens COM4 at 115200baud then in a loop reads lines of text and displays on console

using System;
using System.IO.Ports;

namespace ConsoleTerminal
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort serialPort = new SerialPort() ;
            serialPort.PortName = "COM4";
            serialPort.BaudRate = 115200;
            serialPort.Open();
            while (true)
            {
                string s = serialPort.ReadLine();
                Console.WriteLine(s);
            }
         }
    }
}

you can create a form and when characters are received from a COM port display them in a TextBox. You can create menus to select the COM port, setup baudrate, etc etc etc

Edit: realised code is similar to yours of post #1 - what exactly do you require hlep with the Arduino code or the C# code?

Edit: have a look at terminal emulators implemented in VB and C#

It's with the C#. In the C# Console App, yes that code works. but the problem is with the TextBox that I don't know how to make the data, that recieved (in this case a simple button pressing) in a text. Literally when I press a button, I want a text to appear on the TextBox

this is a screen shot the C# terminal emulator I gave the github link too in post #4

Csharp_terminalEmulator

you can see the TextBox which is displaying the text received from a ESP8266 on COM11
you can download the complete Visual Studio project and modify it to suit your requirements

Edit: I am building it (hit Ctrl/F5 to build and run) with
Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.1.2

Thank you, I'll look into it

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