Visual Basic Resources

Visual Basic is our preferred language for communicating to the NCD product line.  We have written all of our manuals to extensively support Visual Basic.  Visual Basic stocks a powerhouse of features that make it one of the most appealing and easy languages to use.  Advanced users will find VB is ideally suited for intensely complex Multi-Media, Database, Mathematical, Video, and Imaging applications.  If you would like to get an idea of just how powerful Visual Basic can be, please visit www.vbxtras.com and have a look at the hundreds of corporations who are developing extensions that add powerful capabilities to this amazing language.

The following guide will teach ANYONE how to operate ANY of our products using Visual Basic.  The examples provided on this page were developed for Visual Basic 6 Professional.  These directions are about 99% compatible with older versions of Visual Basic as well.  You will need Professional or Enterprise edition for these directions to work.  Some "Lite" editions of Visual Basic do not include the MSComm control, which is used to send data from your computer to our devices.

Follow these simple steps and you will be on your way to controlling ANYTHING from your desktop computer in 30 minutes or less.  If you have questions, please contact us (click here).

 
Step 1: Create a New Project

Start Visual Basic and Select Open.

 
Step 2: Add Serial Communications to your Program
Right Click on the Tool Bar and Select "Components..."
 

The Above Dialog Box will Open.  Scroll Down and put a check next to "Microsoft Comm Control".  If it does not appear in the list, then you will need to obtain the Professional or Enterprise edition of Visual Basic.  Next, Select "OK".  This adds Serial Communications to your toolbox.
 
Your toolbox should now have the "Telephone" icon, which means serial communications can now be added to your project.  Double Click the Telephone Icon to add Serial Communications to your program. 
 
Step 3: Add Serial Communications to your Project
The Telephone Icon will Now Appear in the middle of your form.  Next, Double Click in the Gray Area next to the telephone Icon.  This will open the "Form Load" Window.
 
Step 4: Tell your Program to Open the Serial Port
Type the commands shown in the "Form Load" window above.  When you first run your program, these are the first lines of code that will be executed.  These lines of code configure the COM port and communication speed.  The CommPort setting may need to be changed to an available COM port on your computer.  Once the port is configured, the PortOpen command is used to begin communication from your program to any of our devices.  Once you have typed these lines of code, you can close the "Form Load" window.
 
Step 5: Send Data out the Serial Port to the NCD Device
In your Toolbox, double click the "Command Button" (as shown near the mouse pointer above).  This will add a "Button" to your program.  Double Click on the "Command1" Button you just created.
 
Double Clicking on the "Command1" button will open the "Command1 Click" window, which holds the lines of code that will be executed every time the user pushes the button.  Type the lines of code shown above.  These lines of code will activate the first relay on the R8x Pro series relay controller.  You may substitute these lines of code for lines of code shown in the product manuals for our devices.

Also note the position of the pointer in the photo above.  You can change the text on the button by changing the caption property in the "Properties" window.  Change the caption to read "Send Data" as shown above. 

You are now ready to run your program.  Simply click the Start button to run your program (small arrow pointing to the right on the upper tool bar).

That is all that is needed to control our products from Visual Basic.  In no time, you will be able to create several buttons capable of controlling anything from your desktop computer.

 
Step 6: Retrieve Data from the Device to your Computer
In some cases, you may want to read data from an NCD device and process the result.  This is particularly useful if you want to know the On/Off status of a relay, or if you want to read an A/D value from one of our devices that supports A/D conversion.  The example shown below demonstrates reading data from our device and display the result in a Visual Basic program.  We will use the R8x Pro to read the On/Off status of a relay.
Create a new button and label it "Receive Data".  Create a Label by double clicking the "A" in the toolbox (shown near the pointer).  The button will be used to send a request for the status of all 8 relays on an R8x Pro Relay Controller.  The status will appear as a number from 0-255 "Caption" to the "Label".

Double double click your new button, "Receive Data".  The "Command2 Click" window will appear.  Type the code shown above.  When this button is clicked, a command will be sent to the device, requesting data from the controller.  The program then monitors the receive buffer on your computer.  If data is received from the device, it will be stored in the receive buffer.  The program will wait indefinitely for data to appear in the receive buffer on your computer. 

The DoEvents command is VERY important in Visual Basic.  This command allows windows to Multi-Task.  Without this command, your computer would appear to lock up while waiting for data to appear in the receive buffer.  In actuality, your computer will not be locked up at all, instead, all windows tasks will not be serviced and all CPU power will be devoted to this portion of the program.  You should ALWAYS use the DoEvents command when waiting for data from the serial port.

The controller will send a byte of data back to your computer.  This byte of data is always a numeric value from 0-255.  The byte of data will travel from our controller into the receive buffer of your computer.  Once received, the Do/Loop portion of the program will detect the data.  The next line of code reads the byte of data from the receive buffer on your computer using the MSComm1.Input command.  The ASC portion of this command is used to convert the returned data into a numeric value from 0-255.  The Label1.Caption=Status is used to display the returned value to the user.

 

Conclusion

These are the basic requirements for sending and receiving data using Visual Basic and a typical NCD device.  NCD product manuals will have details regarding the commands the device is capable of accepting, as well as commands that send data back to the user.  You may download this example by clicking on the link below.  This example was written in VB6, and will not load in earlier versions of VB.  However, these instructions are nearly identical, so building an application with these directions will only take a few minutes.
Download VB6 Serial Test Program