How to access serial ports by using Visual Basic 2005 (904795)



The information in this article applies to:

  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft Visual Basic 2005

INTRODUCTION

For a Microsoft Visual Studio .NET version of this article, see 823179.
This step-by-step article describes how to access serial ports by using Microsoft Visual Basic 2005. This article also contains code examples that illustrate the concepts that are discussed.

Note You cannot use the Microsoft .NET Framework classes to directly access other types of ports, such as parallel ports or USB ports.

MORE INFORMATION

To access serial ports by using Visual Basic 2005, follow these steps:
  1. Start Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, expand Visual Basic, and then click Windows.
  4. Under Templates, click Console Application.
  5. In the Name box, type MyConsoleApplication, and then click OK.

    By default, the Module1.vb file is created.
  6. To write data to a serial port, add the following SendSerialData method to the Module1.vb file.
    Sub SendSerialData(ByVal data As String)
        ' Send strings to a serial port.
        Using com1 As IO.Ports.SerialPort = _
                My.Computer.Ports.OpenSerialPort("COM1")
            com1.WriteLine(data)
    	com1.Close()
        End Using
    End Sub
  7. To read data from a serial port, add the following ReceiveSerialData function to the Module1.vb file.
    Function ReceiveSerialData() As String
        ' Receive strings from a serial port.
        Dim returnStr As String = ""
    
        Using com1 As IO.Ports.SerialPort = _
                My.Computer.Ports.OpenSerialPort("COM1")
            Do
                Dim Incoming As String = com1.ReadLine()
                If Incoming Is Nothing Then
                    Exit Do
                Else
                    returnStr &= Incoming & vbCrLf
                End If
            Loop
    	com1.Close()
        End Using
    
        Return returnStr
    End Function
  8. To read data from and write data to a serial port, add the following code to the Sub Main procedure.
    Dim Data As String
    Data = "Test"
    
    Console.WriteLine("Writing the following data to COM1: " & Data)
    SendSerialData(Data)
    
    Console.WriteLine("Read the following data from COM1: " & ReceiveSerialData())
    
    Console.WriteLine("Press ENTER to quit")
    Console.ReadLine()
  9. To run the solution, press CTRL+F5.

REFERENCES

For more information about how to access serial ports and parallel ports in Microsoft Visual Basic .NET, click the following article number to view the article in the Microsoft Knowledge Base:

823179 How to access serial ports and parallel ports by using Microsoft Visual Basic .NET

For more information about how to access serial ports in Visual Basic 2005, visit the following Microsoft Developer Network (MSDN) Web sites:

Port operations in the .NET Framework with Visual Basic
http://msdn2.microsoft.com/en-us/library/ms172760.aspx

My.Computer.Ports object
http://msdn2.microsoft.com/en-us/library/e4560dx9.aspx


Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbinfo kbhowto kbProgramming KB904795 kbAudDeveloper