pcbway




Update: Few bugs were reported by some readers, which I have corrected and updated the code. Now, its fully tested and 100% working.


How to use Serial Port in VB 2010, serial port in vb, serial port vb 2010Hello friends, hope you all are enjoying the start of winter season. By the way, I really hate winter season and I just want to hibernate in this season . 🙂 Well coming to our today’s lecture, my today tutorial, serial port in VB 2010, is actually based on a request made by one of the member on my Facebook Page and as it is a really good topic so i thought to share it.

Today we will make a software on Microsoft Visual Basic 2010 in which we will send data through the serial port in VB 2010. In this software we will send the data and also receive it. Simply follow all the given steps carefully and you can easily interface the Serial Port in VB 2010, its a fully working project with code so don’t do any mistake. Moreover check these two complete tutorials on Microsoft Visual Studio 2010 as well, these are quite fascinating.

First of all download the Microsoft Visual Basic 2010. The installer can be freely downloaded from Microsoft. After installing the software follow these simple steps. So ,let’s get started with How to use Serial Port in VB 2010:

How to use Serial Port in VB 2010 ???

Step 1 : Creating a New Project
  • Open your installed Microsoft Visual Studio 2010 software. The first interface will be something like that :
serial port in vb,Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010
  •  Now click on the New Project and select Windows Form Applications.
  • In the project name box, add name of your project as I have added Serial Port Interface.
Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010
  • Click OK and a new window will be opened as shown in below image which contains a blank Form1.
  • In this Form1 we are gonna add our controls buttons etc.
Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010
Step 2 : Changing Name of Form
  • Now click on the form1 and the properties panel will be open on the right side. Now, in the properties tab shown on the right side change its name to frmMain (for easier identification specially when adding more forms).
Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010
  • Also change the text of the form to something you like as Serial Terminal. This will be shown on the title bar of your application.
Step 3 : Adding Controls To The Project
  • Lets start to add some controls in our software like buttons,combo box and labels etc.
  • So from the Common Controls tab add two buttons, two combo boxes and two labels into your Form1 and  align them as shown below :
Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010
  • For Button 1, change the text to Connect and change the name to btnConnect.
  • For Button 2, change the text to Disconnect and change the name to btnDisconnect.
  • For Combo Box 1, change the name to cmbPort.
  • For Combo Box 2, change the name to cmbBaud.
  • For Label 1, change the text to Comm Port.
  • For Label 2, change the text to Baud Rate.

Note :

  • Keep the names and texts of same character as i wrote them.
  • They are case sensitive so be careful. I will recommend to just copy paste them.
  • If you make even a one letter mistake the code will not run.
  • btn and cmb are just to remind that they are button and combo box respectively. Its better to do neat programming.
Step 4 : Adding Serial Port & Boxes
  •  Now from Container tab, add two Group Boxes in the forum.
  • Change the name of Group Box 1 to Transmit Data.
  • Change the name of Group Box 2 to Received Data.
  • Now add a Text Box and a Button in the Transmit Data Group Box.
  • Change the name of the Button to Send and text to btnSend.
  • Change the name of the Text Box to txtTransmit.
  • Now add a Rich Text Box in the Received Data Box and change its text to rtbReceived.
  • Arrange all these components as shown in the below image :
serial port in vb,Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010




  •  Lastly and i think its the most important part of this tutorial, add a Serial Port Block into your forum. It will appear at the bottom. Don’t change any of its parameters just leave it as it is.
Step 5 : Coding Section
  • Now we come to the coding part of our project. If you double click on your forum, it will open a new window something like that :
Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010
  • This is the place where we add our code and in other words add functionality to our project, this window is called Code Editor.
  • If you double click on any button or box, its respective code will created in this region automatically.
  • Now what you need to do is copy the below code and paste it in your code editor window.
  • Just remove all the previous code in your Code Editor Window.
  • Here’s the code for Serial Port in VB 2010:

 

  • After adding the code your Code Editor Window will look something like this one :



serial port in vb,Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010
Step 6 : Compile Your Project
  • After adding all the code, now you are ready to compile your code and run your application.
  • To compile go to Debug -> Build SerialPortInterface and if everything’s going right then your project will pop up.
Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010
  •  To test your application, just add some LCD to your serial port or simply short your Rx Tx pins and whatever you send you will receive it.
  • Here’s the image of my final application. I have converted it to .exe file, in my coming tutorials i will tell you how to convert a project to .exe file.
serial port in vb,Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010

Note :

  • The project exe file and the complete code has already been emailed to all our subscribed members.
  • If someone didn’t get it or want to get these files then first Subscribe to Our Newsletter and then post your email here and I will email it to them.

So that was all on How to use Serial Port in VB 2010. I hope you guys have enjoyed it and are gonna design it on your own. Take care !!! 🙂

87 Comments

  1. @kashif Thanks bro for appreciation and yups try it and let me know howz you find it.

    @Seol This Invoke function is used actually to automatically called every time a data is received at the Serial Port. Its kind of an interrupt for the coming data.

    @saim lodhi The code and the software has been sent bro, check your email id. and stay connected … :))

    • Check the last line of Step4 ….. ” Lastly and i think its the most important part of this tutorial, add a Serial Port Block into your forum. It will appear at the bottom. Don’t change any of its parameters just leave it as it is.”

      I think you didn’t add the serial port block in your project that’s what causing problem and if you have added then make sure the name of your serial port block is SerialPort1 …. you can change it in the properties.

      • I also did same mistake. Now corrected it. But facing another problem… Com port sends data( led on usb converter blinked) but is not received on hyperterminal.. Also from other side if i send some data then vb application is not receiving data

  2. I’m have a problem: When I start your program there are no ports in the pulldown. I have tried using the ‘myPort = IO.Ports.SerialPort.GetPortNames()’ before without getting any ports.
    Could this be computer related?

    • Before running the project …. make sure that you have connected the COM Port with your computer …. As it automatically detects all the ports so if there’s no port already available then it creates problem.

      Moreover, have you changed the name of Form1 to frmMain ? Double check it.

      If still having problem, let me know.

    • Yeah it will detect that usb port …. I think you are using serial to usb converter …. make sure that you have installed the drivers correctly and some COM has been assigned to your cable like COM3 etc ….

      The best way to test the software is via virtual port …. the port you are taking is not virtual …. virtual is actually a software port which doesn’t exist but operates virtually (software level) …

  3. Hi. Can i ask something. I already follow all your instructions. I can debug the code. But when i click on Connect button, error message popup stated that “UnauthorizedAccessException was unhandled. Access to the port ‘COM4’ is denied. It is because i used virtual serial port? I used proteus, eltima virtual serial port and microc pro for PIC.

  4. Hi bro,
    i am using visual studio 2010 c++,i have try to made terminal using c++ windows form applications.i tried run exe file on other pc which don’t have studio it showing errors.i changed the settings project->properties-> cofiguration->c++->codegeneration->multi threaded(mt). after changing settings also i’m getting errors.what is the solution for this? give reply asap.
    thanks

  5. Hi Nasir,

    I have got this problem as below,
    The PortName cannot be empty.
    Parameter name: PortName
    The list of com port does not drop down as well. It is blank when I run the program. I really appreciate your help.

    Regards,
    Teh

    • Hi,

      Actually you are getting the problem because you haven’t attached any COM Port with the computer on which you are running it …..

      So, before running this software first attach some COM Port or create a virtual COM Port.

      Thanks.

  6. Hi
    Sir I follow the procedure and make the program and compile it successfully, but before that i miss to rename the transmit text box, however after compiling the program i start the program but it does not shows any thing in combo box nor port no neither baud rate, i also try virtual serial port but it does not works, i repast the whole code again and i got an error and form in design tab also vanishes.
    I just forget the what error was because while writing this i repast and test it, and it works launch the program it works, i wonder what happens, Its like charm , Can u explain what happened, first it does not work but later it works

    • change this code
      myPort = IO.Ports.SerialPort.GetPortNames()

      to
      myPort = SerialPort.GetPortNames()

      Also the transmit is not included Character return and line feed…so if you are using modem it doesn’t work.
      Please add the character return yourself. ….I am testing the receiving part, since I can get message response

  7. This is really pretty cool, i compiled and controlled Arduino succesifully. But make sure the port is short circuited/or there is handshake between the transmitter and the receiver in order to see how it send as well as it receive data.

  8. I have tried code using VB2010 on Windows 7, the program runs perfectly without any error BUT its NOT detecting any serial/com ports on my machine. Checking the Device Manager, under (PORTS & LPT) communication port (COM1) is listed there.

    What do I missed?

    Please help.

    Thanks

  9. This is a very interesting tutorial/project. for learning purposes, can I have a copy of this project? thanks! my email add is ddprasad [at] gmail [dot] com

    Thanks,

  10. Hi Im working with FT232r
    After I finished your tutorial and Implement it with Visual basic All the section except Received field work currently
    I shortened Rx and Tx pins of Ft232r but What I sent through “Send button did not appear In Received Field
    Both Received and Transmit LED on FT232r module became on when I sent my string but why I couldn’t see any string in Received Text?

    Thanks in advance

  11. Sir I Using PLC With Port Connection, But When I Connect Using COM1 The Device Connection Is Off.., When I Closed Connection, Device Connection Is On…
    Why…??
    Can You Give me Solution..??
    Thankyou…

    • Hi,

      No idea here,it seems like the port is using at two different places, I think any other program also accessing the port. Can’t say anything for sure.

      Thanks.

    • Hi,

      The simple code is attached in this post and if you need its advanced for then you need to buy it from our site.

      Thanks.

  12. I compiled this code fine. Connect it to an Arduino board project.
    On initial connection there seems to be trash characters being sent to the Arduino. After about 3 or 4 seconds it stops. Happens every time the connection is established. After that it works great.
    Any ideas what might cause such a thing? I have not modified the code in any way.
    This behaviour does not happen if I connect with Putty to the Arduino board.

  13. Sorry my last comment was incomplete.. I am sending a pic to you.. I want to create this form.. Can you tell me what will be code?? How can I store data coming from usb??

  14. Sir,
    How to make this program in java?
    How to rx & tx data through JAVA application?
    may you please send me the logic and code?

    • Hi,

      No this code won’t work with GSM. Its just a COM Port code. If you wanna work on GSM then add me on Skype and I will help you out. My Skype id is theenggprojects.

      Thanks.

  15. Hi Syed, liked your tutorial, I did everything you said, but the comm port drop down box is empty, does not pick up any ports and there are definetly ports open on my PC , I can open them with Hyper terminal or Clear terminal, I use Windows7, what can be the problem?
    Willie

  16. Good morning
    could you please send me your tutorial ” How to use Serial Port in VB 2010″ program ?
    Thank you.
    Angelo.

  17. actually i want to send command from visual studio to xbee and receive data comming on serial port i need code for this purpose , i dont want to use ardujino for decrypt the receive string , i have to do all operation from visual studio related xbee please help me out ..
    thanks ….

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.