How to: Dial Modems Attached to Serial Ports in Visual Basic
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. 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 My.Computer.Ports.OpenSerialPort Method.
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.
Using com1 As IO.Ports.SerialPort = _ My.Computer.Ports.OpenSerialPort("COM1", 9600) End Using
Set the DtrEnable property to indicate that the computer is ready to accept an incoming transmission from the modem.
com1.DtrEnable = True
Send the dial command and the phone number to the modem through the serial port by means of the Write method.
com1.Write("ATDT 555-0100" & vbCrLf)
Sub DialModem() ' Dial a number via an attached modem on COM1. Using com1 As IO.Ports.SerialPort = _ My.Computer.Ports.OpenSerialPort("COM1", 9600) com1.DtrEnable = True com1.Write("ATDT 555-0100" & vbCrLf) ' Insert code to transfer data to and from the modem. End Using End Sub
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 How to: Insert Snippets Into Your Code (Visual Basic).
Compiling the Code
This example requires a reference to the System namespace.
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 in Visual Basic.
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 (Visual Basic).
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 in Visual Basic.