All of the electronics engineering students mostly want to create something which has a nice GUI control for their device. But they see softwares like VB6, VB.NET as pure software language. On the other side, Computer engineering students see also these software as pure GUI. Electronics geeks know the hardware and computer geeks know software. But Actually these languages such as VB6, VB.NET, Java can provide us very useful features to control our hardware connected to PCs. Here i am presenting a very basic tutorial to access serial port of PC in the VB6 software.
We will create the GUI (Graphical User Interface) which contains some buttons. Pressing them we can send some data to our PC’s serial port. Though it is a very basic tutorial but this is a must learn step. Further you can receive, send, monitor data, take some decisions based on that, store hardware data in some databases in your software. We can start with basic mirocontrollers by connecting them with our PC’s serial Ports. We are creating a software which has 2 buttons. Pressing button 1 it sends value ‘A’ to the serial port and Pressing button 2 it sends value ‘B’ to the serial port. You can monitor them via virtual serial port pairing method. If you dont know virtual serial port emulating then see this .
Step 2: On the main form add two command buttons. If you see carefully then you will find that most of all components are available in the left panel. But as pointed out in the below screen you can see that by default there is no component for serial port available. Hence we need to select it from the project menu, shown in next screen. Step 3: Select the Microsoft Comm Control 6.0 as shown here.
So as in below screen now you will get serial port component in your project.
Step 4: Write code for each component individually.
This MSComm component is not visible at run time on the GUI. But it operates in the background.
The code for configuring the serial port is as shown below. This code should be executed at the time of loading the form.
Private Sub Form_Load()
‘ Fire Rx Event Every single Bytes
MSComm1.RThreshold = 1
‘ When Inputting Data, Input 1 Byte at a time
MSComm1.InputLen = 1
‘ 9600 Baud, No Parity, 8 Data Bits, 1 Stop Bit
MSComm1.Settings = “9600,N,8,1”
‘ Disable DTR
MSComm1.DTREnable = False
‘ Open COM1
MSComm1.CommPort = 1
MSComm1.PortOpen = True
We have created 2 buttons, both can be programmed as follows to send ‘A’ or ‘B’ as per the button.
Private Sub Command1_Click()
‘ Send ‘A’ in ascii means 65 in decimal
MSComm1.Output = Chr$(65)
Private Sub Command2_Click()
‘ Send ‘B’ in ascii means 65 in decimal
MSComm1.Output = Chr$(66)
Note: Dont forget to write code to close the serial port you used here. So that after you close this software your other softwares be able to use that serial port. Code for that is shown below.
Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
Step 5: Run the project, monitor data on your serial port physically with your hardware circuits like microcontrollers or you can also do that virtually using VSPE software
Congratulations. Your task is done. Now further you can do more work related to this by making few modifications.
To see serial port hardware interfacing projects with VB, see this section containing VB6 projects with 8051, PIC microcontrollers. Click here to view.
Comment below if you have any queries or doubts.
If you found this helpful please give your 5 seconds to rate and press Like button on my post.