Share via


Intoduction to Serial Communications in VB.NET(Visual 2005) using SerialPort

In this exercise, you’ll be writing a simple Windows Application in VB.NET that enables simple 2-way communication or data transfer via the serial communications port (COM1).

 

1. Open Visual Studio 2005

 

2. Go to File -> New -> Project

 

3. Name your Project SpAPP and place it on any location you desire as long as it easily accessible.

 

4. Next, you’ll see Form1.vb[Design] in the main window of Visual Studio.

 

5. Rollover your mouse on the toolbox, scroll down to the Components section. Drag and drop SerialPort into your Form. This will instantiate an object of SerialPort within your application.

SerialPort would appear at the bottom of Form1.vb

 

6. If you right click on SerialPort and go to Properties. On the bottom right hand side of Visual Studio there will be a Properties pane that’ll enable you to change the settings of SerialPort. For now the PortName should be COM1. WriteTimeOut and ReadTimeOut at around 500ms.

 

7. After that go over to toolbox again and drag a button to the form. Set the Text of the Button (using the properties pane) to Open Port.

 

8. Double click on the Open Port Button and it’ll bring you to the code view of your application Form.

 

9. Cut and paste these lines of code into the Button1_Click Sub. This code essentially open and closes the serial com port by clicking the button. It will also change states as in the text on the button will match the current action that will be allowed. It will also enable or disable the Send Button(added in the next step) to avoid an illegal operation.

 

     If Button1.Text Is "Open Port" Then

            SerialPort1.Open()

            Button1.Text = "Close Port"

            Button2.Enabled = True

        ElseIf Button1.Text Is "Close Port" Then

            SerialPort1.Close()

            Button1.Text = "Open Port"

            Button2.Enabled = False

        End If

 

10. After that go back to the Design View of Form1.vb. Drag and drop another Button into the form. Set the Text of the Button to Send.

 

11. Double click on the Send Button and fill this code within the Button2_Click Sub. This code snippet functions as the data sending portion of the application and it’ll log to the ListBox that we will add later.

 

  SerialPort1.WriteLine(TextBox1.Text)

        ListBox1.Items.Add("Sent: " + TextBox1.Text)

12. Next, go back to the design view and we will drag and drop a ListBox into the Form. Resize it as necessary to fit the Form.

 

13. Drag and drop a TextBox into the Form.

 

14. Lastly left click once on the SerialPort at the bottom. Then, go to the Properties pane, click on the lightning symbol. You’ll see Misc, DataReceived, ErrorReceived, PinChanged. Double click on DataReceived and fill the sub with this code.

 

ListBox1.Items.Add("Received: " + SerialPort1.ReadLine())

 

This function will be called whenever there are data stored in the input buffer. It is to display incoming data from the Serial Communication Port.

15. You’ve completed the coding section. Now press Ctrl-F5 to Start Without Debugging. The application should run. Test the application by clicking Open Port, keying in some data in the TextBox and then click Send. Make sure the Rs232 cable is connected between 2 computers or via a null modem. If you have connected to a modem, you can key in “ATDT” to get a response from the modem. It’ll reply “ATDT” to the ListBox <-- this proves that your program is communicating through the serial port. ATDT is a standard Modem protocol for diaing.

 

      Congratulations, you've just succesfully written a Serial Communications program that utilizes SerialPort class in VB.NET (Visual Studio 2005)!!