Use VB to develop a serial communication program in Windows

Source: Internet
Author: User
With the popularization of windows in China, especially the launch of Windows 95, more and more computer users have switched to Windows operating systems. However, the development of Windows application software is not easy for technical personnel in the general engineering field. It is not only difficult to understand the message loop mechanism of windows, but also requires the development of an Application Software with strong functions and beautiful interfaces, the programming and debugging workload is very large. Many engineering and technical personnel who are not engaged in software are daunting for Windows programming. However, it is very convenient to use Microsoft's Visual Basic to develop Windows application software in these aspects. Generally, engineers and technicians are easy to master and design the software interface is very convenient. The programming workload is relatively small, you only need to program the main application functions and programming a small amount of interface control. This article introduces some gains from the use of VB in serial communication programming. Most of the introduction to Windows serial communication programming in newspapers and magazines is microsoftc ++ or BorlandC ++, therefore, I am willing to discuss with you the application of VB in this aspect. For the use of RS-232 interface serial communication of computer measurement and control system software development, has a certain reference value.

In this system, the 80C31 single chip microcomputer is used as the lower computer to collect the measurement data of inertial sensitive parts. After some pre-processing, the data is transmitted to the upper PC through the RS-232 serial port. Run the detection software in a Windows environment on a PC. It receives data from a serial port for data processing, display, and storage. The data processing result is printed and output as a table.

In vb3.0, many professional manufacturers provide custom controls with the suffix ". vbx". MSComm. vbx is the control used for serial communication. For vb3.0, the control is included in the toolbox after it is started. Otherwise, you can add the control to the control toolbox through file | AddFile. Then you can use this control to design a program for serial communication.

The following is a simple example to illustrate how to design a program for serial communication. This program has been implemented using vb3.0 in Windows 95:

1. Create a project file first, and its control is shown in the following table:

2. The Code is as follows:

Subform_load ()

Comm1.commp ORT = 1' use the serial port 1

'Baud rate 9600, even verification, 8 data bits, 1 Stop bits.

Comm1.settings = "9600, e"

When comm1.input is used, one byte is taken from the receiving buffer every time.

Comm1.inputlen = 1

Timer1.interval = 20' the timer interrupt interval is 20 ms, used to update the display time.

Endsub

Subscribe comm_click ()

Dimechoch % 'command variable

Staticmaxf01 &, maxf02 &, minf01 &, minf02 & 'maximum value, minimum value

Onerrorresumenext 'simple error handling

Maxf01 & = maxf02 & = 0' initialization variable

Minf01 & = 32767

Minf02 & = 32767

Label2.caption = comm1.settings 'display the configuration of the serial port.

Ifcmdcomm. Caption = "communication" then' if the prompt of the current command is "communication", send and

Upload comm. Caption = "stop" 'Accept the data, and change the command prompt to "stop ",

Cmdexit. Enabled = false' allow communication to stop. Invalidate the exit command in the communication.

Ifnotcomm1.portopenthen: If the serial port is not opened, the serial port is opened.

Comm1.portopen = true

Endif

Ifcomm1.portopenthen

Ifecho. Text <> "" then "if the command input box is not empty, convert it to a hexadecimal value.

Echoch % = Val ("& H" + echo. Text)

Else

Echoch % = & hf0' otherwise, the command f0h is saved.

Endif

Comm1.output = CHR (echoch % )'

Endif

Label3.caption = hex (echoch %) 'displays the issued command

Dowhilecomm1.portopen

Ifcomm1.inbuffercount> 0then' if comm1.inbuffercount> 0, the buffer is accepted.

Ch $ = comm1.in' contains data and reads one character,

Ifasc (Ch $) = echoch % then 'exit the current loop if it is the same as the command

Exitdo 'enters the next loop

Endif

Else

Comm1.output = CHR (echoch % )'

Endif 'send data

Dummy = doevents () 'allows the system to process other messages

Loop

'The lower computer receives the correct command and sends back the command correctly.

I = 1

Dowhilecomm1.portopen

Ifcomm1.inbuffercount> 5 then

Ch $ = comm1.input

Dowhileasc (Ch $) <> echoch % 'synchronize test data with command bytes

Ch $ = comm1.input

Loop

Accept $ = comm1.in' read test data from the Windows serial data buffer

CH2 $ = comm1.input

CH3 $ = comm1.input

Methane $ = comm1.input

Newf01 & = clng (ASC (CH2 $) * 16*16 + ASC (cost $) 'is merged into an integer

Newf02 & = clng (ASC (_4 $) * 16*16 + ASC (CH3 $ ))

Ifnewf01 &> maxf01 & then': determines the maximum and minimum values.

Maxf01 & = newf01 &

Endif

Ifnewf01 & <minf01 & then

Minf01 & = newf01 &

Endif

Ifnewf02 &> maxf02 & then

Maxf02 & = newf02 &

Endif

Ifnewf02 & <minf02 & then

Minf02 & = newf02 &

Endif

Labf01.caption = STR (newf01 &) 'show data

Labf02.caption = STR (newf02 &)

Labmaxf01.caption = STR (maxf01 &)

Labminf01.caption = STR (minf01 &)

Labmaxf02.caption = STR (maxf02 &)

Labminf02.caption = STR (minf02 &)

Else

Comm1.output = CHR (echoch %)

Endif

Dummy = doevents ()

Loop

During the communication process, the else stops the communication by pressing the complete comm button and issues the stop test.

Disable comm. Caption = "communication" command to disable the serial port so that the exit button is valid.

Comm1.output = CHR (& h5f)

Comm1.portopen = false

Cmdexit. Enabled = true

Endif

Endsub

Subcmdexit_click ()

Ifcomm1.portopenthen: confirm to close the serial port

Comm1.portopen = false

Endif

End 'terminate the entire program

Endsub

Subtimer1_timer () 'update display time

Iflabtime. Caption <> time & then

Labtime. Caption = time $

Endif

Endsub

3. program description:

A. The communication protocol of the above program is: baud rate 9600, even verification, 8 data bits, and 1 Stop bits. The upper computer sends the test command, and the lower computer receives the command and sends the detected data to the upper computer every Ms. Each time five bytes are sent, the first byte is the command received by the lower computer, the second three byte is the 16 byte value, and the fourth Five byte is the 16 byte value. The upper computer accepts and processes and displays data cyclically;

B. in the control software, the data sent and received by serial communication is mostly in hexadecimal format, so the command input is in hexadecimal format, in the program, use echoch % = Val ("& H" + echo. text) returns the input echo. convert text to a hexadecimal number and assign it to the variable echoch %. Comm1.output requires accept of variable type when sending data through a serial port, so it is implemented through the statement comm1.output = CHR (echoch %;

C. When receiving data, you must determine whether the first byte is the command sent. Only the last four bytes are the correct data. Otherwise, you can view the next byte. Set comm1.inputlen = 1 to read one byte from the accept buffer every time;

D. When exiting the program, you must close the serial port through comm1.portopen = false;

E. If the upper computer needs to regularly query the lower computer, serial communication can be carried out in the timer Timer1-Timer event;

F. To implement complete functions, you must further improve the program and even use windowsapi and OLE. The above program is just a simple example;

The above introduces some gains from the development of measurement and control software in Windows using VB, based on the above principle, the author develops a satellite control system inertial sensitive part detection software with friendly interface, comprehensive functions and convenient use. The actual use of this software is good and the work efficiency is improved, even automated testing and data processing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.