Surplus Parts
How to Order
Terms and Conditions
Product Design
Contact Us
Press Release
International Sales

Visual Basic Serial Port Software Example

Visual Basic is one of the easiest languages to use for serial port communications, the mscomm32.ocx driver controls all the serial port communications and it's included with Visual Basic.  We will show you in this example how easy it is to control our line of serial relay board products using this serial port com driver.

If you need to control our relay boards, but do not have any programming experience you now have an option.  Our relay boards can now be controlled directly from a desktop shortcut using our command-line software.  This eliminates having to deal with the mscomm32.ocx driver!

Note: this example is tailored to the first time Visual Basic user, but the information is useful for the advanced user that is unfamiliar with serial port control.  Click here if you want to jump directly to the code segment.

Let's get started:
First start Visual Basic and select File >> New Project >> and select the Standard EXE Icon.  Visual basic will display a blank form as shown in the picture below.  The look of the screen may vary due to the version of Visual Basic you are using.

Visual Basic screen showing a blank form

Since the Microsoft communication component is not loaded in this view we need to load this component to use it in our project.  Right click on the icon area to the left of the form window and select Components on the menu.  The following screen will be displayed.

Visual Basic add component screen

Scroll down and select the Microsoft Comm Control.  The version shown may differ from the version you are using.  Click OK to add this component to the icon menu as shown in the picture below.

Graphic showing the mscomm32.ocx icon

Double click on the telephone icon and a copy of it will be placed on the blank form.  Next double click on the push button icon near the top of the component menu and a button will be copied to the blank form.  Do this again to add a second button to the screen.  You will need to drag the buttons apart since they will be placed on top of each other.  Your blank screen will now look like the following picture except the name on the buttons face will say "Command1" (All Relays ON) and "Command2" (All Relays OFF).

Visual Basic form showing the serial relay board control buttons

 Single click on the command1 button and the properties for this button will be displayed in the properties window as shown below.  Click on the property labeled Caption and change the name Command1 to "All Relays ON".  Using the pull down in at the top of the properties window, select the command2 button and the properties for this button will be displayed.  Change the Caption name to "All Relays OFF"

Visual Basic command button properties

Using the pull down at the top of the properties window, select the mscomm1 control and the properties for this com port will be displayed.  Using the scroll bar on the right, scroll down to the bottom of this screen as shown below.

Visual Basic mscomm serial port control properties

Click on the property labeled RThreshold, Change this value from a "0" to a "1".  Do the same for the SThreshold property.  Everything else in this window can remain the same for this example.  

Note: If you need to read the port status on the board (which is not shown in this example) you need to change the InputMode property to  "1-comInputModeBinary" by selecting the right pull down.

We are now finished with the configuration of the serial port and command buttons.  The default settings are already set for com port 1, 9600 baud, 8 bits, no parity, and one stop bit.

From the menu at the top of the Visual Basic screen select View >> Code, and a code window will be displayed where the form currently resides.  Click in the following box and select the contents of the box by holding the left mouse button and dragging down over the contents.  Hold the Ctrl Button down and press "C'' to copy the contents to the clipboard.

Switch to Visual Basic and click inside the code window and press Ctrl "V" to paste the contents, the window should now look like the following picture.

Visual Basic serial relay boards control software code

That's all the code that is required to turn all the relays ON and OFF on our relay boards, it will work on the 1, 2 & 8 Channel relay boards.  Select File >> Save Project from the pull down menu to save your project before you attempt to run the program.

All that's left to do is run our program, but first make sure the relay board is connected to com1 on the the back of your computer using the 9 pin serial to modular cable adapter.  Connect the modular cable from the adapter to the input of the relay board.  Plug the wall transformer cable into the board and the wall transformer into the wall receptacle or apply power to the board if your using the internal transformer version.

Assuming that everything is connected up properly, press the blue right arrow at the top of the screen and our program will run.  To stop the program press the blue square box to the right.  The running program is shown below.

 Click the "All Relays ON" button and you will hear an audible click from the relays that is turning them ON and pressing the "All Relays OFF" button will turn them OFF.

Congratulations on your first control program!  We're sure you will find many practical applications for your serial controlled relay board.

If you have any other questions you may contact the support dept at or you may request information using our feedback form.

[ Home ] [ Products ] [ Surplus Parts ] [ Terms & Conditions ] [ How To Order ] [ PDF Order Form ] [ HTML Order Form ]

Copyright 2000-2016 Pencom Design, Inc. All rights reserved. Made in USA