Visual C# (.NET Framework)

 

Create Project

Launch Visual Studio, select [New]-[Project] from [File] menu.
From the [Templates] of [Create a new project] dialog box, select [Windows Forms App (.NET Framework)] of [C#], click the [Next] button.

From the [Configure your new project] dialog box, specify the project name and save location,
select [.NET Framework 4.8] from the Framework item and click the [Create] button to create the project.

* The above is for Microsoft Visual Studio Professional 2022 (64-bit). Item names and commands may differ depending on the version.

 

Add SerialPort component to the project

When performing serial communication with Visual C#, there is a method of using the SerialPort class.

Reference : [SerialPort class] (Since this is an external link, the link may change without notice)
    https://learn.microsoft.com/en-us/dotnet/api/system.io.ports.serialport?view=netframework-4.8

The Visual C# default settings include the System.IO.Ports.SerialPort component of the .NET Framewrok in your project.

Select [View / Designer] from the menu bar to display the main form of the project.
A list of common controls and components built into the toolbox window is displayed.

Among the components in the Toolbox window is a SerialPort component.
By dragging and dropping this SerialPort component onto the form, the SerialPort component will be added.

In the tree view in the [Solution Explorer] window of Visual Studio, you can check whether serialPort1 : SerialPort exists in [Form1.cs].

* The above is for Microsoft Visual Studio Professional 2022 (64-bit). Item names and commands may differ depending on the version.

 

Configure communication settings for the SerialPort component

You can set the properties of the SerialPort component added to the project in the Properties window on Visual C#.

You can also reference and assign each property value in your program code.
When using enum parameters for SerialPort component (StopBit.one, Parity.None, etc.), use the Using statement to make the System.IO.Ports namespace visible.

For details, refer to the source code of the sample program.

Program example :

Using System.IO.Ports;     // Namespace for System.IO.Ports

namespace SerialPort

{

    public partial class Form1 : Form

    {

        ....

 

        private void Form1_Load(object sender, EventArgs e)

        {

            ....

            

            serialPort1.BaudRate = 9600;           //Setting Baudrate.

            serialPort1.DataBits = 8;              //Setting Databits.

            serialPort1.StopBits = StopBits.One;   //Setting Stopbits.

            serialPort1.Parity = Parity.None;      //Setting Paritybits.

 

            ....

        }

 

        ....

    }

}

 

Event handling for the SerialPort component

You can set events for the SerialPort component added to the project in the Properties window on Visual C#.
When you set the event, the following event handling function will be created automatically.

Add the required event handling program to the automatically generated event handling function serialPort1_DataReceived().
For details, refer to the source code of the sample program.

Program example :

Using System.IO.Ports      // Namespace for System.IO.Ports

namespace SerialPort

{

    public partial class Form1 : Form

    {

        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)

        {

            ....

        }

    }

}