Made in USA Flag All board products Proudly made in USA!

Manufacturer of computer controlled relay, I/O and custom boards for commercial and industrial applications.

Visual Basic Software

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 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.  If you're familiar with VBA and want to skip this demo, you may 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 blank form serial port example - for Pencom Design, Inc. products

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 components form serial port software for Pencom Design, Inc. products

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.

Visual Basic Com icon displayed serial port software for Pencom Design, Inc. products

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 completed form serial port software for Pencom Design, Inc. products

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 button properties - serial port software for Pencom Design, Inc. products

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 comm properties - serial port software for Pencom Design, Inc. products

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 the 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 code window - serial port software for Pencom Design, Inc. products

That's all the code that is required to turn all the relays ON and OFF on our relay boards using Visual Basic, 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.

On the RS232 versions:

Connect the board to com1 on 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 AC adapter cable into the board and plug the AC adapter into the wall receptacle or apply power to the board if you’re using the internal transformer version.

On USB versions:

If you have not done this already - install the USB driver

Connect the board to your computer with the USB cable. Plug the AC adapter cable into the board and plug the AC adapter into the wall receptacle or apply power to the board if you’re using the internal transformer version.  The USB driver will recognize the board and install a virtual serial port (com port)  To determine what port the board is connected to - open the device manager.  Enter "devmgmt.msc" in the run dialog box in windows, select ports and the device should be displayed as "USB serial port (number)".  Change the port number in the program you just created to match this port.

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

The running program is shown below:

 Click the "All Relays ON" button and you will hear an audible click from the relays or the LED indicator will light - this is activating all the relays on the board and pressing the "All Relays OFF" button will turn all them OFF.

Congratulations on your first Visual Basic For Applications control program using Visual Basic and relays boards manufactured by Pencom Design, Inc.

We're sure you will find many practical applications for using your RS232 or USB serial controlled relay board.

Return to the previous page