Developing Serial Terminal for Arduino in C#

In this video I show you how to create your own serial terminal in Microsoft Visual Studio using C#. It is very simple and you can have the same functionality as you have in the Arduino's developer environment. Later on you can further improve the functions to tailor it to your needs. To fully understand the code and the communication with the Arduino, you should check my previous video where I explain how to communicate with the Arduino through the serial terminal.

Arduino Serial Communication



C# source code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO.Ports;  //use the ports for serial communications (1)
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
 
namespace ArduinoSerialTutorial
{
    public partial class Form1 : Form
    {
        private SerialPort SerPort; //serial port
        private string ReceivedData; //received data
 
 
 
 
        public Form1()
        {
            InitializeComponent();
 
            FetchAvailablePorts(); //run the port-fetching
        }
 
        void FetchAvailablePorts()
        {
            String[] ports = SerialPort.GetPortNames();//Put the name of the available USB ports into the ports string array
            AvailablePortsBox.Items.AddRange(ports); //Add the string into the dropdown list we made
        }
 
        private void ConnectToPort_Click(object sender, EventArgs e)
        {
            //This is some predefined thing that you can get from Google
            SerPort = new SerialPort();
            SerPort.BaudRate = 9600;
            SerPort.PortName = AvailablePortsBox.Text;
            SerPort.Parity = Parity.None;
            SerPort.DataBits = 8;
            SerPort.StopBits = StopBits.One;
            SerPort.ReadBufferSize = 20000000;
            SerPort.DataReceived += SerPort_DataReceived;
 
 
            try
            {
                SerPort.Open(); //We open the port
                Thread.Sleep(1000); //We wait a sec
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error!");
 
            }
        }
 
        private void SerPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            ReceivedData = SerPort.ReadLine(); //We read the serial port
 
            this.Invoke(new Action(ProcessingData)); //execute the delegate (ProcessingData)
        }
 
        private void SendSerialButton_Click(object sender, EventArgs e)
        {
            SerPort.WriteLine(SenderTextBox.Text.ToString()); //send the content of the textbox to the serial port
        }
 
        private void ProcessingData()
        {
            ReceivedDataBox.Text += ReceivedData.ToString() + Environment.NewLine;
            //We put the received data in the textbox and add a linebreak
 
        }
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                SerPort.Close(); //close the port on from closing
            }
            catch
            {
            }
        }
    }
}

Previous
Previous

Plotting data from Arduino

Next
Next

Two-way serial communication with Arduino