ActiveXperts » Serial Port Component » How to Use Serial Port Component » Visual Basic 6.x
Excel can communicate in and out the serial port both with API calls and with the MSCOMM.OCX control. Both techniques require some fairly serious VBA code as well as a deep understanding of either the Windows. VBA for the Serial Port; Convert code from excel 2000 vba to excel 2003 vba; Serial port comunication with Excel; Access 7 to Access 2003 using Excel VBA; serial port access; Serial Port in Ms Access Restarting the program after processing the second invoice; Excel VBA application: Porting to Visual Studio 2005?
ActiveXperts Serial Port Component is a software development kit (SDK) that enables the user to communicate to a device over a serial interface.
Such a device can be: a weight indicator, a modem, a scanner, or any other device that is equiped with a serial port. It can even be another PC, connected via a NULL modem cable.
ActiveXperts Serial Port Component features the following:
- Direct COM port support (like 'COM1')
- TAPI (Windows Telephony Device) support (like 'Standard 56000 bps Modem');
- Support for RS-232/RS422/RS485, up to 256 simultaneous ports;
- Support for all types of Hayes compatible modems;
- Support for serial cable as well as USB cable or Bluetooth connections;
- Support for Virtual COM ports (i.e. COM ports redirected through the network);
- Hardware flow control (RTS/CTS, DTR/DSR) and software flowcontrol (XON/XOFF) support;
- Configurable baudrate/parity/stopbits, full buffered data transfer, text/binary data transfer.
Step 1: Download and install the ActiveXperts Serial Port Component
Download the ActiveXperts Serial Port Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
Step 2: Create a new Visual Basic project
Launch 'Microsoft Visual Basic' from the Start menu, and choose 'New' from the 'File Menu'. The 'New Project' dialog appears.
Select 'Standard Exe' and click 'OK':
(Click on the picture to enlarge)
Step 3: Refer to the ActiveXperts Serial Port Component Library and create the objects
A new Project is created, with a blank form.
First, you must add a reference to Serial Port Component in the project to be able to use the object. To do so, choose 'References..' from the 'Project' menu. In the 'References' dialog that pops up, enable the 'Serial Port Component 2.2 Type Library' reference as shown in the following picture:
(Click on the picture to enlarge)
Click 'OK' to close the 'References..' dialog.
Then, select the Project form and choose 'View Code' from the context menu:
(Click on the picture to enlarge)
![Visual basic serial port read Visual basic serial port read](https://i.ytimg.com/vi/KqlHhIgACRk/maxresdefault.jpg)
On top of your code, declare the following object:
Step 4: Create the object
From the Code window, select 'Form'. The Private Sub 'Form_Load()' will be displayed now.
In the 'Form Load' function, create the object in the following way: Street fighter iv launcher crack.
Step 5: Send an AT command to a connected Hayes compatible modem
You can now send and/or receive data to and/or from a serial device.
The following code shows how to query a modem:
There are many working samples included with the product. You can also find them on the ActiveXperts FTP site: ftp.activexperts-lab.com/samples/serial-port-component.
Visual Basic for Applications Serial Port Software Example
For this demonstration we will be using the mscomm32.ocx driver in VBA to control a RS232 serial port and USB virtual serial port that is created with the USB driver installed on your system for our USB products. This allows a seamless transition between our RS232 and USB devices.
For example, you can be in Access, Excel, or Word and start a macro that would power up an external data acquisition unit and receive data into the software utilizing VBA, serial control, and one of our serial relay boards with I/O. You could control room lighting, or any other device that you would like to turn ON or OFF from within an application, just think of the many possibilities.
Visual Basic for Applications has many of Visual Basic's commands and controls, that can be utilized for control applications. We will show you how to use this powerful application to control our serial relay boards.
Note: 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.
Let's get started:
For this demonstration we will use Excel's VBA editor, but any of the software that has built in VBA support will work. Start Excel and select >> Tools >> Macro >> Visual Basic Editor, from the pull down menus as shown in the picture below, or use Alt+F11 shortcut.
For this demonstration we will use Excel's VBA editor, but any of the software that has built in VBA support will work. Start Excel and select >> Tools >> Macro >> Visual Basic Editor, from the pull down menus as shown in the picture below, or use Alt+F11 shortcut.
Depending on which version of Excel you are using, this and the following screen may look different. The Visual Basic Editor will appear as shown below.
After VBA is loaded, Right Click in the project window, Select >> Insert >> Userform and a blank form will be loaded, with a control tool box as shown below.
Now we are going to add a serial port control to the control box so that we may use it in our application. For this demonstration we are going to use the mscomm32.ocx communication driver, depending on what other MS applications you have loaded on your system, this driver may or may not be available on your system.
Right click in the controls box and select >> Additional Controls… Scroll down to you see Microsoft Communications Control, and click on the check box on the left, as shown below.
Visual Basic Excel Serial Portfolio
If you don't see this driver available in the additional controls window you can do a file search using windows Start >> Find >> File or Folder to search for mscomm32.ocx. If this file is found you need to copy this file into the c:windowssystem directory so the VBA can find it. If this driver is not found continue to the other options available instead of using the mscomm32.ocx driver
Click OK to continue. The control box will now have an icon of a telephone sitting on a modem displayed in the controls window as shown below.
Select this control and place your cursor on the user form, click the left button and the control will be placed on the screen. If you see a warning like shown in the picture below you either have a unlicensed copy of the control or the control is not licensed properly.
If you downloaded the mscomm32.ocx driver from the internet and you don't have Visual Basic, Visual Fox, or one of the other programs that it is supplied with, you don't have a registered copy and will not be able to use this driver. Don't worry, there are two other ways to accomplish serial communications that don't require this driver, continue to the other two options than don't require the mscomm32.ocx driver.
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.
Click on the MScomm icon or use the properties pull down menu to view the properties for this control as shown below.
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 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.
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 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:
Visual Studio Serial Port Example
![Visual studio serial port communication Visual studio serial port communication](https://s-media-cache-ak0.pinimg.com/originals/e0/33/e5/e033e59d2c1670918d316629d0081797.jpg)
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 blue right arrow at the top of the screen and our program will run. To stop the program click the box on the top right of the user form, DO NOT close the program using the blue square 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.
Congratulations on your first Visual Basic For Applications control program using mscomm and relays boards manufactured by Pencom Design, Inc.
Visual Basic Serial Port Read
We're sure you will find many practical applications for using your RS232 or USB serial controlled relay board.