Sending Commands in VISUAL BASIC to ADR ( RS232 ) interfaces using mscomm 

 

IMPORTANT:  For a tutorial on VB2008 or VB2010 ( VB.NET ) including Express, See:

Using Visual Basic with ADR ( RS232 ) Devices Using the SerialPort Component

 

The MSComm Control

NOTE1 : This programming guide assumes the user has a basic knowlege of Visual Basic programming. The teaching method used is to show a basic example of a VB4.0 program which communicates with an ADR board by sending and receiving ASCII data, and then disect the program to understand its operation.

NOTE2 : The procedure shown is identical for VB Ver 4 and VB Ver. 5.

ADR serial data acquisition interfaces require the sending and receiving of ASCII data via RS232 to operate. To communicate with the ADR boards using Visual Basic, the MsComm control must be utilized to allow serial data transfer via a serial port ( Com1-Com4). MSComm is a custom control shipped with VB4.0 and VB5.0 and must be loaded using the Tools menu.

Form1 of a sample program controlling an ADR112 is shown below.

The program was built using radio buttons for port control, three command buttons, a text box to display analog data and the MSComm control for serial communications. When run, the port is enabled using the radio buttons and then AN0 is read every time the "Read AN0" button is clicked. The analog data is then displayed in text box 1. PA0 can be set or reset using the "SET PA0" or "RESET PA0" buttons. When run, the program appears as follows,

The MSComm properties allow the setting of communication parameters including port selection and port enabling functions. The properties window is shown below. Note the default settings for MSComm allow communication with the ADR interfaces by merely selecting a com port ( com2 in our example ), and enabling the port. The communication parameters of 9600,n,8,1 are the default parameters and need not be altered to communicate with ADR products.

The com port must be enabled to allow communication with the ADR board. The code for the "Port Enable" radio button is

The port is enabled by setting MSComm1.PortOpen to TRUE. A "CPA00000000" command is then sent using the MSComm1.Output fuction to configure port A as output. The Chr (13) variable is a carriage return required by the ADR board.

The "Read AN0" button reads analog port 0 and displays the data in text box 1. The code for this button is shown below.

The RD0 command is sent using the MSComm1.Output function and then the program loops until five characters are recievied into the serial buffer. ( The ADR112 returns four ascii characters ranging from 0000 to 4095 and a carriage return in response to a RD0 command. If an ADR101 is used, the loop should be set to 4 characters as a value of 000 to 255 will be returned along with a carriage return.) The analog value is then retrieved and displayed in text box 1.

The SETPA0 button simply sends a SETPA0 command to the ADR112. The code for this button is shown below.

The RESPA0 button simply sends a RESPA0 command to the ADR112. The code for this button is shown below.

Before exiting the program, the comm port must be disabled. This is done with the "Port Disable" radio button. The code for this button is shown below.

.

NOTES and Programming Hints;

1. PORT ENABLING- Port selection and enable functions can be programmed in the "load" and "unload" subroutines in the "form" object or they may be controlled by radio buttons or pull down menus.

2.RECIEVING DATA-  When recieving data from the ADR board, be sure to wait for the correct number of characters to be recieved in the serial buffer. Check your ADR programming manual for the correct number of characters to be recieved and add one for the carriage return. The ADR1000 sends both a carriage return and line feed thus two must be added to the number of characters expected.

3.USING VARIABLES - In many cases it may be desired to send a string incorporating a comand and some variable. For example, the "MAddd" command outputs to port A, the integer value ddd. If ddd is a variable named PV, a string to set the port to the value of this variable would look like;

MSComm1.Output = " MA " & Str(PV) + Chr(13)

The "Str" function converts the variable to an ASCII string and it is appended to  "MA".  The Chr(13) is a carriage return.

 

Back to Programming Page