Home Parallel Port Serial Port USB .NET Webboard I/O Today API Project Link

Thai Page Click Here
 Hardware Interfacing in VB.NET framework programming.

Serial Port Communication in VB.NET Programming

For this article, I will suggress you an introduction on how to do serial port communication on .NET platform using Visual Basic. How to using how to control a communications port from .NET. Since the .NET Framework does not support direct control of the Comm Port, a separate class is used that provides direct Win32 API calls. (Note: Comm Port is synonymous with COM Port, Serial Port or RS232 port.)
We will be using RS 232 class that create from visual basic programming for communication between PCs. In full duplex mode, I will use modem connection for communication.

This article show how to use the COMM Port for communicate. With follow thus below
  • To test for see if the Comm Port is available for use.
  • To use the Comm Port for communicate with a modem.
  • Using Win32 API in calls it to control communication with the Comm Port.

The SerialPort Class

Rs232.vb is the file provides the structure for the class which is used to control access to the Comm Port that incoulde in this project.
The SerialPort class provides a framework for synchronous and event-driven I/O, access to pin and break states, and access to serial driver properties. It can be used to wrap Stream objects, allowing the serial port to be accessed by classes that use streams. That is, SerialPort class represents a serial port resource.

To create a Serial Port object

Declare necessary Rs232 class variables for make communication with the Comm Port possible.

Private m_CommPort As New Rs232

How to Using Connect Comm Port

we will use are

  • Open(Comm Port,BauRate,DataBit,Parity,StopBit,DataStopBit,BufferSize) : Opens a new Comm Port connection.
  • Write(Buffer Size,Buffer String) : Writes the specified string and the New Line value to the output buffer.
  • Read(Byte to Read) : Read string from output buffer.
  • Close()

Example Code

' Here ,We want to work with default values for the public properties .
m_CommPort. Open(m_ModemPort, 115200, 8, Rs232.DataParity.Parity_None, Rs232.DataStopBit.StopBit_1, 4096)

' Write an AT Command to the Port.
m_CommPort.Write(Encoding.ASCII.GetBytes("AT" & Chr(13)))

' Sleep long enough for the modem to respond and the timer to fire.

' DoEvents for free CPU usage.

' Close Comm Port

For reading the data form COM port, press Read button. If there is any data in read buffer, it will be displayed in the textbox. Code show thus below

Private Sub tmrReadCommPort_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrReadCommPort.Tick
   Try    ' As long as there is information, read one byte at a time and output it.
     While (m_CommPort. Read(1) <> -1)
     ' Write the output to the screen.
       WriteMessage(Chr(m_CommPort.InputStream(0)), False)
     End While
      Catch exc As Exception
      ' An exception is raised when there is no information to read.
      ' Don't do anything here, just let the exception go.

   End Try
End Sub

Example Program

Main Form

  • Click command button "Check for Ports" for verify about comm port 's computer.
  • Message List show in Textbox , The following form will be displayed:.

  • Click command button "Check for Moderm" for verify about Moderm 's computer.
  • Message List show in Textbox after checking moderm .
  • For starting communication, press Comm. As soon as you press "Send AT Command" the button, the following form will be displayed:

  • Microsoft Visual Studio.NET Professional or greater.
  • Windows 2000 or Windows XP.
  • A modem installed on one of the Comm Ports. (for example most laptops include a modem).
Running the Project:
To run the Project:
  • Download Source Code of This Project
  • Unzip the code into a directory of your choice.
  • Open VB.NET Comm Port.sln file.
  • Click the buttons on the form to test the functionality.
  • Examine the code in the project to understand how the code works.

Copyright (c) 2000 thaiio.com all rights reserved.  Contact webmaster :webmaster@thaiio.com