Skip to content

Latest commit

 

History

History
43 lines (29 loc) · 2.19 KB

how-to-send-strings-to-serial-ports.md

File metadata and controls

43 lines (29 loc) · 2.19 KB
description title ms.date helpviewer_keywords ms.assetid
Learn more about: How to: Send Strings to Serial Ports in Visual Basic
How to: Send Strings to Serial Ports
07/20/2015
ports, sending strings to
strings [Visual Basic], sending to serial ports
My.Computer.Ports object
serial ports, sending strings to
6ebf46cd-b2d0-4b2c-9a1f-be177b22ad52

How to: Send Strings to Serial Ports in Visual Basic

This topic describes how to use My.Computer.Ports to send strings to the computer's serial ports in Visual Basic.

Example

This example sends a string to the COM1 serial port. You may need to use a different serial port on your computer.

Use the My.Computer.Ports.OpenSerialPort method to obtain a reference to the port. For more information, see xref:Microsoft.VisualBasic.Devices.Ports.OpenSerialPort%2A.

The Using block allows the application to close the serial port even if it generates an exception. All code that manipulates the serial port should appear within this block or within a Try...Catch...Finally block.

The xref:System.IO.Ports.SerialPort.WriteLine%2A method sends the data to the serial port.

[!code-vbVbVbalrMyComputer#33]

Compiling the Code

  • This example assumes the computer is using COM1.

Robust Programming

This example assumes the computer is using COM1; for more flexibility, the code should allow the user to select the desired serial port from a list of available ports. For more information, see How to: Show Available Serial Ports.

This example uses a Using block to make sure that the application closes the port even if it throws an exception. For more information, see Using Statement.

See also