/admin

Serial Port Communication Visual Basic

VISUAL BASIC SERIALPORT CLASS ( COMPONENT ) WITH ADR ( RS232 ) DEVICES IMPORTANT: For a tutorial on using the MSCOMM control with older versions of Visual Basic See: NOTE1: This programming guide assumes the user has a basic knowledge of Visual Basic programming. The teaching method used is to show a basic example of a VB2010 program which communicates with an ADR board by sending and receiving ASCII data, and then dissect the program to understand its operation. NOTE2: The procedure shown is identical for VB2008, VB2010 and also runs on Express versions. NOTE3: The entire project can be downloaded here.

Base on my understanding, our buddy wants to fetch weight scale from COM port. Actually, we need not care about what we purposed to get from the port (MSCOMM is no more than the listener), and OnComm is the key 'event' of MSCOMM component. To communicate with the ADR boards using Visual Basic, the SerialPort component must be utilized to allow serial data transfer via a serial port ( Com1-Com99). SerialPort is a component shipped with VB2008 and VB2010 and must be loaded using the Toolbox/Components menu.

(98K) require the sending and receiving of ASCII data via RS232 to operate. To communicate with the ADR boards using Visual Basic, the SerialPort component must be utilized to allow serial data transfer via a serial port ( Com1-Com99). SerialPort is a component shipped with VB2008 and VB2010 and must be loaded using the Toolbox/Components menu.

The following SerialPort VB2010 Example program is used to communicate with an. The is an RS232 based data acquisition interface that uses simple CR ( Chr(13) ) terminated ASCII strings to read two 12-bit analog input channels and control an 8-bit digital I/O port. The program can be modified to work with any ONTRAK or other manufacturers RS232 based product.

Figure 1: SerialPort VB2010 Example Form1 The program was built using a pull down menu for COM port selection, an OPEN COM Port button, three Send command buttons, and a text box to display received ASCII data. When run, the port is selected using the Pull-Down Menu and enabled when the OPEN COM Port button is clicked. The Send RDO button sends an ASCII ' RDO' + Chr(13) out on the COM port. The Chr(13) is required by the ADR112 to tell it a command has been received. Two additional buttons send SETPA0 and RESPA0 commands ( also terminated with Chr(13)) to set or reset Bit PA0 on PORTA of the ADR112. Figure 2 shows the program running with an ADR112 connected to COM3.

When the Send RD0 button was pressed, the ADR112 returned ASCII 1711. This shows the ADR112 analog port 0 is at 1711/4095 X 5 =2.0891V. The number 4095 is the full scale input of the ADR112 which is 5VDC. Figure 2: SerialPort VB2010 Example Running The entire program code is shown in Figure 3 and Figure 4. The code is heavily commented and a few explanations are provided below each figure. Figure 3: Opening the COM Port and Some Housekeeping.

In Figure 3, we first declare a COM port assign it a label of ADRport. Next, we check to see if the port is already open and if so we close it.

The next block of code creates our pull down combobox with an array of com ports from COM1 to COM20. This value can be increased if desired. The last block of code is for the 'OPEN COM Port' button. When the button is pressed, the value in the combobox is used to open the selected COM port and give it a label of ADRport.

Further, the text string 'CPA11111110' + Chr(13) is sent out on the COM port to configure the Digital I/O port so that PA0 is an output. This is required by the ADR112 and depending on your hardware, initialization may or may not be required. Figure 4: Sending Commands and Receiving Data Figure 4 is where the sending and receiving of data happens under control of the buttons on Form1. The first block of code uses TRY CATCH to send an 'RDO' + Chr(13) to the ADR112 to retrieve the value of analog port AN0. The received data is then placed in the text box on Form1.

Buku bahasa arab surya alpindo kelas 9 semester gasal a. 07 28kb29 Thread.Sleep(20) is used to force the thread to wait 20ms. This gives time for the command to be sent out on COM3 to the ADR112 and, for the response string to be received. Catch ex As TimeoutException is used to detect if there has been a timeout in the read of the COM port.

If there is a timeout, we print 'No Data' in the text box and enable a flash screen to show a timeout has occurred. We have set the timeout to 100ms in our example. If you are running the program without an ADR112 connected these events will be triggered.

The following two subroutines simply send either 'SETPA0' +Chr(13) or 'RESPA0'+Chr(13) to set or reset PA0 on the depending on what button has been pressed. FURTHER NOTES and Programming Hints; 1.USING VARIABLES - In many cases it may be desired to send a string incorporating a command and some variable. For example, the 'MAddd' command outputs to port A, the integer value ddd.