Launch Visual Studio, select [New]-[Project] from [File] menu.
From the [Templates] of [Create a new project] dialog box, select [Windows Forms App] of [C#], click the [Next] button.
From the [Configure your new project] dialog box, specify the project name and save location, and click the [Next] button.
[Additional information] dialog box, select [.NET 8.0 (Long Term Support)] / [.NET 6.0 (Long Term Support)] 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.
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
The Visual C# default settings do not include the System.IO.Ports class of the .NET8 / .NET6 in your project.
For this reason, we explicitly include the external class.
Select [Project] from the Visual Studio menu bar, and select [Manage NuGet Packages...] from the pull-down menu.
From the [NuGet Packages Manager], select the Browse tab and enter "System.IO.Ports" in the browse box.
As a result of the search, select "System.IO.Ports: Provides classes for controlling serial ports." provided
by Microsoft displayed at the top and install it.
In the tree view in the [Solution Explorer] window of Visual Studio, you can check whether there are installed modules in [Dependencies/Packages].
The above is for Microsoft Visual Studio Professional 2022 (64-bit).
Item names and commands may differ depending on the version.
Thank you for your understanding.
To use the SerialPort class in Visual C#, the System.IO.Ports namespace must be available in your project.
Use the Using statement to declare the use of the System.IO.Ports package.
It also shows an example of creating an instance of System.IO.Ports.SerialPort using the new declaration.
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
{
....
System.IO.Ports.SerialPort serialPort1 = new System.IO.Ports.SerialPort();
//Initialize when this sample is loaded here
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.
....
}
....
}
}
SerialPort1 declared with System.IO.Ports.SerialPort serialPort1 = new System.IO.Ports.SerialPort(); can handle events of the SerialPort class with the following description.
First, declare to use the DataReceived event of serialPort1 with
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
Then add the necessary event handling program to 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
{
System.IO.Ports.SerialPort serialPort1 = new System.IO.Ports.SerialPort();
//Initialize when this sample is loaded here
private void Form1_Load(object sender, EventArgs e)
{
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
....
}
//Member to start when received
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
....
}
}
}
In general, an EXE application built with "Any CPU" specified in the [Active solution platform] item in the Visual Studio settings [Configuration Manager] window can be executed in both 64-bit environment and 32-bit OS environment.
However, when developing an EXE application that supports the .NET8 / .NET6 framework, it is necessary to be aware of the target OS.
For example, if the development PC is 64-bit OS environment,
EXE applications can only be executed in a 64-bit OS environment.
If you start it in a 32-bit OS environment, the following message is displayed and it cannot be executed normally.
[This app can't run on your PC. To find a version for your PC, check with the software publisher.]
There are two ways to build an EXE application that can be executed in a 32-bit OS environment.
・If the development PC is 32-bit OS environment, specify "Any CPU" in [Active Solution Platform] and build the EXE application.
・If the development PC is 64-bit OS environment, change "Any CPU" to "x86" in [Active Solution Platform] and build the EXE application.
Reference: .NET execution environment for .NET6 or later