Serial Port Vb6 Mscomm Example

Sending Commands inVISUAL BASIC to ADR ( RS232 ) interfaces using mscommIMPORTANT:For a tutorial on VB2008 or VB2010 ( VB.NET ) including Express, See: The MSComm ControlNOTE1: This programming guide assumes the user hasa basic knowlege of Visual Basic programming. The teaching method used is to show a basicexample of a VB4.0 program which communicates with an ADR board by sending and receivingASCII data, and then disect the program to understand its operation.NOTE2: The procedure shown is identical for VB Ver4 and VB Ver.
5.ADR serial data acquisition interfaces require the sending andreceiving of ASCII data via RS232 to operate. Avery label templates business cards. 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 loadedusing the Tools menu.Form1 of a sample program controlling anisshown below.The program was built using radio buttons for port control, threecommand buttons, a text box to display analog data and the MSComm control for serialcommunications. When run, the port is enabled using the radio buttons and then AN0 is readevery time the 'Read AN0' button is clicked. The analog data is then displayedin text box 1. PA0 can be set or reset using the 'SET PA0' or 'RESETPA0' buttons. When run, the program appears as follows,The MSComm properties allow the setting of communication parametersincluding port selection and port enabling functions.

The properties window is shownbelow. Note the default settings for MSComm allow communication with the ADR interfaces bymerely selecting a com port ( com2 in our example ), and enabling the port. Thecommunication parameters of 9600,n,8,1 are the default parameters and need not be alteredto communicate with ADR products.The com port must be enabled to allow communication with the ADRboard. The code for the 'Port Enable' radio button isThe port is enabled by setting MSComm1.PortOpen to TRUE. A'CPA00000000' command is then sent using the MSComm1.Output fuction to configureport 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 thedata in text box 1. The code for this button is shown below.The RD0 command is sent using the MSComm1.Output function and thenthe program loops until five characters are recievied into the serial buffer.
( The ADR112returns four ascii characters ranging from 0000 to 4095 and a carriage return in responseto a RD0 command. If an ADR101 is used, the loop should be set to 4 characters as a valueof 000 to 255 will be returned along with a carriage return.) The analog value is thenretrieved and displayed in text box 1.The SETPA0 button simply sends a SETPA0 command to the ADR112.
Serial Port Vb6 Mscomm Example Pdf

Thecode for this button is shown below.The RESPA0 button simply sends a RESPA0 command to the ADR112. Thecode for this button is shown below.Before exiting the program, the comm port must be disabled. This isdone with the 'Port Disable' radio button. The code for this button is shownbelow.NOTES and Programming Hints;1. PORT ENABLING- Port selection and enablefunctions can be programmed in the 'load' and 'unload' subroutines inthe 'form' object or they may be controlled by radio buttons or pull down menus.2.RECIEVING DATA- When recieving data fromthe ADR board, be sure to wait for the correct number of characters to be recieved in theserial buffer.
Check your ADR programming manual for the correct number of characters tobe recieved and add one for the carriage return. The ADR1000 sends both a carriage returnand line feed thus two must be added to the number of characters expected.3.USING VARIABLES - In many cases it may be desiredto 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 variablenamed 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 ASCIIstring and it is appended to 'MA'. The Chr(13) is a carriage return.