Visual Basic .NET (.NET8 / .NET6)

 

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] of [Visual Basic], click the [Next] button.

From the [Configure your new project] dialog box, specify the project name and save location, and click the [Next] button.
From the [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.

 

Use the external class

When performing serial communication with Visual Basic .NET, 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 Basic .NET 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.

 

Declare the use of the SerialPort class in the source code

To use the SerialPort class in Visual Basic .NET, the System.IO.Ports namespace must be available in your project.
Use the Imports 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 Dim customer As New application() declaration.
In addition, the Dim WithEvents customer As application() declaration is used together with System.IO.Ports.SerialPort() event handling.

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

Program example :

Imports System.IO.Ports      'Namespace for System.IO.Ports

Public Class Form1

    Dim WithEvents SerialPort1 As New System.IO.Ports.SerialPort()    'SerialPort class instance creation and event handling declaration

    

    ....

    

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

    

        ....

        

        SerialPort1.BaudRate = 9600            'Setting Baudrate.

        SerialPort1.DataBits = 8               'Setting Databits.

        SerialPort1.StopBits = StopBits.One    'Setting Stopbits.

        SerialPort1.Parity = Parity.None       'Setting Paritybits.

        

        ....

    

    End Sub

 

    ....

 

End Class

 

 

Event handling for the SerialPort class

SerialPort1 declared with Dim WithEvents serialPort1 As New System.IO.Ports.SerialPort() can handle events of the SerialPort class with the following description.

Add the necessary event handling program to SerialPort1_DataReceived().

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

Program example :

Imports System.IO.Ports      'Namespace for System.IO.Ports

Public Class Form1

    Dim WithEvents SerialPort1 As New System.IO.Ports.SerialPort()    'SerialPort class instance creation and event handling declaration

 

    'Member to start when received

    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        ....

    End Sub

 

End Class

 

 

Notes on building EXE applications in "Any CPU"

  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