Opening/Closing the Serial Port - Visual Basic 2005 Serial Communication Program

This example program uses the COM-1(USB)H RS-232C 1-port type assigned to COM1 over a USB connection and opens and closes the COM port and performs exception handling.

Used COM port COM1
Transmission rate 9600 bps
Parity setting None
Data bit length 8 bits
Stop bits 1 bit
Handshake Do not use
RTS line Disabled

Creating the form and configuring properties

Start Visual Basic 2005, create a new project, and create a form like that shown to the left.Paste the SerialPort component on the form.Check the SerialPort component properties (communication settings such as the baud rate).

Object TextBox1 COM1
Object Button1 Connect
Object Button2 Disconnect

Error handling (exception handling)

If an exception error occurs when the program does not perform exception handling, the application is forcibly terminated.In order to avoid this, the program will perform exception handling.When exceptions are handled, the application is not forcibly terminated.The image to the left is an example of alerting the user by displaying a message box when an exception is handled.

Error Not setting the PortName

Program code example

Add the processing for when the connect button and the disconnect button are clicked.Write the following code on Form1.vb.When you double-click each object (button, etc.), if the object is a button, the procedure to code the processing when the button is pressed will open.Write the code for the processing you want to perform here.

Opening the port

Private Sub Button1_Click(...  : Processing when the connect button is pressed
SerialPort1.PortName = TextBox1.Text    ' Stores the name of the port to open
SerialPort1.Open()                          ' Opens the port
End Sub

Closing the port

Private Sub Button2_Click(...  : Processing when the disconnect button is pressed
    If SerialPort1.IsOpen = True Then   ' Port has been opened
       SerialPort1.Close()                         ' Closes the port
    End If
End Sub

Exception handling

This code example adds exception handling to the code above and displays the details of the exception error

Private Sub Button1_Click(...  : Processing when the connect button is pressed
    Try                     ' Start of exception handling 
     If SerialPort1.IsOpen = True Then          ' Port has been opened
        MessageBox.Show("Error", MessageBoxButtons.OK, MessageBoxIcon.Error)                
        Exit Sub
     End If
        SerialPort1.PortName = TextBox1.Text    ' Stores the name of the port to open
        SerialPort1.Open()                          ' Opens the port        
    Catch ex As Exception           ' Exception handling            
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,MessageBoxIcon.Error)        
    End Try 
End Sub
Private Sub Button2_Click(...  : Processing when the disconnect button is pressed
    If SerialPort1.IsOpen = True Then   ' Port has been opened
       SerialPort1.Close()                         ' Closes the port
    End If
End Sub

For the Try - Catch - End Try syntax, check the Visual Basic 2005 reference.

To PageTop