-->![Port Port](/uploads/1/2/5/7/125755915/766729441.png)
This topic describes how to use
My.Computer.Ports
to dial a modem in Visual Basic.How to: Dial Modems Attached to Serial Ports in Visual Basic.; 2 minutes to read +5; In this article. This topic describes how to use My.Computer.Ports to dial a modem in Visual Basic. Typically, the modem is connected to one of the serial ports on the computer. PC's serial data acquisition interfaces require the sending and receiving of ASCII data to operate. To communicate with the serial port using Visual Basic, the MsComm control must be utilized to allow serial data transfer via a serial port ( Com1-Com4). MSComm is a custom control shipped with VB5.0 and VB6.0 and must be loaded using the Tools menu.
Typically, the modem is connected to one of the serial ports on the computer. For your application to communicate with the modem, it must send commands to the appropriate serial port.
To dial a modem
- Determine which serial port the modem is connected to. This example assumes the modem is on COM1.
- Use the
My.Computer.Ports.OpenSerialPort
method to obtain a reference to the port. For more information, see OpenSerialPort.TheUsing
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 aTry...Catch...Finally
block. - Set the
DtrEnable
property to indicate that the computer is ready to accept an incoming transmission from the modem. - Send the dial command and the phone number to the modem through the serial port by means of the Write method.
Example
This code example is also available as an IntelliSense code snippet. In the code snippet picker, it is located in Connectivity and Networking. For more information, see Code Snippets.
![Port Port](/uploads/1/2/5/7/125755915/766729441.png)
Compiling the Code
This example requires a reference to the System namespace.
Robust Programming
This example assumes the modem is connected to COM1. We recommend that your code 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.In this example, the application disconnects the serial port after it dials the modem. Realistically, you will want to transfer data to and from the modem. For more information, see How to: Receive Strings From Serial Ports.