Você está na página 1de 2

How to access serial ports by using Visual Basic 2005

http://support.microsoft.com/kb/904795

Article ID: 904795 - Last Review: December 6, 2006 - Revision: 2.3

How to access serial ports by using Visual Basic 2005


INTRODUCTION

For a Microsoft Visual Studio .NET version of this article, see 823179 (http://support.microsoft.com/kb/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. 2. 3. 4. 5. Start Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Under Project Types, expand Visual Basic, and then click Windows. Under Templates, click Console Application. 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 (http://support.microsoft.com/kb/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 (http://msdn2.microsoft.com/en-us/library/ms172760.aspx) My.Computer.Ports object http://msdn2.microsoft.com/en-us/library/e4560dx9.aspx (http://msdn2.microsoft.com/en-us/library/e4560dx9.aspx)

APPLIES TO

1 of 2

10/2/2009 9:51 PM

How to access serial ports by using Visual Basic 2005

http://support.microsoft.com/kb/904795

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

Keywords: kbinfo kbhowto kbprogramming KB904795

Get Help Now


Contact a support professional by E-mail, Online, or Phone

Help and Support

2009 Microsoft

2 of 2

10/2/2009 9:51 PM

Você também pode gostar