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.

API Serial Port Software

Visual Basic for Applications API Serial Port Software Example

We will continue the serial port control demonstration using the windows API functions.  We only need the CommIO.bas file to continue this demonstration, unpack this file in a blank directory so it will be easy to find.  Our screen should look like the following picture, with a blank user form loaded.

Note: as stated before, this example is tailored to the first time Visual Basic for applications 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.

VBA - blank form API serial port example- for Pencom Design, Inc. products

Click on the command button icon in the tool box (rectangular box) and place two buttons on the user form. Click on CommandButton1 and the options will appear in the properties window. Click to the right of the Caption in the properties window and change commandButton1 to "All Relays ON".  Click on the commandButton2 or select this name from the pull down at the top of the properties window, and change this to read "All Relays OFF" as shown in the picture below.

VBA - API serial port software - example for Pencom Design, Inc. products

Right Click in the Project window, select >> Import File... >> and navigate to the CommIO.bas file.  Click OK to add this file to the project window.  If you expand the tree by clicking on the + box you will see that the modCOMM module is loaded, as shown below.

VBA modcomm module loaded using the windows API for Pencom Design, Inc. products.

From the menu at the top of the Visual Basic for Applications screen select View >> Code or press F7, 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 for Applications and click inside the code window and highlight any information in this window -- press Ctrl "V" to paste the contents into the code window.  The window should now look like the following picture.

VBA API serial port code using the windows API 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 the winows API, it will work on the 1, 2 & 8 Channel relay boards.  Select File >> Save Book1 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 your 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 your 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 that everything is connected up properly, press the VBA press to run blue right arrow at the top of the screen and our program will run.  To stop the program click the VBA Netcomm close buttonbox on the top right of the user form,  DO NOT close the program using the blue VBA stop programsquare box in the VBA editor.  If you would close the program using the blue box the serial port will not be closed properly, and it may not operate properly if you would restart the program.

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.

VBA serial port running using the windows API for Pencom Design, Inc. products.

Congratulations on your first Visual Basic For Applications control program using the Windows API 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