Serial communication program in a Windows environment using VB

zhaozj2021-02-11  200

With Windows popularization in China, especially Windows 95, more and more microcomputer users turn to the Windows operating system. However, the development of Windows applications is not easy to do with technical personnel in general engineering technicians, but not only Windows's message circulation mechanism is not easy to understand, but also develop a function of strong, beautiful applications, and its programming and debugging workload is very huge. Many engineers who do not engage in software are daunting for Windows programming. However, using Microsoft's VisualBasic to develop these aspects of Windows applications are very convenient, and general engineers are easy to master, the design software interface is very convenient, programming workload is relatively small, only programming and small interface control of primary application functions Program. This article describes some gains that the author uses VB in serial communication programming. Due to the introduction of the serial communication programming in the newspapers and magazines, the introduction of Microsoft C or Borlandc is used, so the author is willing to discuss the application of VB in this area. . The development of computer measuring system software for serial communication using the RS-232 interface has a certain reference value.

In this system, an 80C31 single-chip microcomputer is used as a low-bit machine to collect the inertial sensitive components. The data is transmitted to the upper PC through the RS-232 serial port after a certain pre-treatment. Run the detection software in the Windows environment on the PC, accept data from the serial port, perform data processing, display, storage, etc. The data processing results are printed in the form of table.

In VB3.0, there are many professional manufacturers to provide user-customized controls, which are .vbx suffix, where Mscomm.vbx is a control for serial communication. For VB3.0, the control is included in the control toolbox (Toolbox), otherwise the control can be added to the control toolbox with File | AddFile. Then you can use the control for serial communication.

Here, a simple example will illustrate how to perform serial communication programming, which uses VB3.0 with VB3.0 in a Windows 95 environment:

1. Create a project file first, its control is as follows:

2. The code is as follows:

Subform_load ()

Comm1.commport = 1 'uses serial port 1

'Baud rate 9600, doll verification, 8 data bits, 1 stop bit.

Comm1.settings = "9600, e, 8, 1"

When using Comm1.input, take one byte each time you use the receive buffer.

Comm1.inputlen = 1

The interrupt interval of the Timer1.Iterval = 20 'timer is 20ms for updating the display time.

EndSub

Subcmdcomm_click ()

DIMECHOCH% 'Command variable

StaticMaxF01 &, Maxf02 &, Minf01 &, Minf02 & 'maximum, minimum

OnErrorResumenext 'simple error handling

MAXF01 & = Maxf02 & = 0 'initialization variable

Minf01 & = 32767

Minf02 & = 32767

Label2.caption = Comm1.Settings' Displays the settings of the serial port.

Ifcmdcomm.caption = "Communication" THEN 'If the prompt of the current command button is "communication", start sending and

cmdcomm.caption = "Stop" "accepts data, and change the command button to" stop "

cmdexit.enabled = false 'Allows to stop communication. The exit command has failed in communication.

IfNotcomm1.Portopenthen 'is not open if the serial port is not open, turn on the serial port comm1.portopen = true

ENDIF

IFcomm1.Portopenthen

Ifcho.text <> "" "If the command input box is not available, turn it into a hexadecimal value

Echoch% = VAL ("& H" echo.text)

Else

Echoch% = & HF0 'Otherwise, the provincial command is F0H

ENDIF

Comm1.output = chr (echoch%) 'writes a byte command to the serial port, by Windows

Endif 'moved the command

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

DowhileComm1.Portopen

Ifcomm1.inbuffercount> 0then 'If comm1.inbuffercount> 0 indicates a buffer

CH $ = Comm1.input 'has data and reads a character,

If the IFASC (CH $) = echoch% Then 'is the same as the issued command, then exits the current loop

EXITDO 'enters the next loop

ENDIF

Else

Comm1.output = chr (echoch%) 'Otherwise, continue to order to the next machine, notify the next machine

ENDIF 'Sending Data

Dummy = doevents () "Let the system handle other messages

Loop

'The lower machine accepts the correct command, and the return command is correct, go to the following cycle

i = 1

DowhileComm1.Portopen

IFcomm1.inbuffercount> 5then

CH $ = comm1.input

DowhileASC (CH $) <> echoch% 'Test data in command byte

CH $ = comm1.input

Loop

CH1 $ = Comm1.input 'Accept buffer reading test data from Windows serial data

CH2 $ = comm1.input

CH3 $ = comm1.input

CH4 $ = comm1.input

NEWF01 & = ClNG (CLNG (ASC (CH2 $) * 16 * 16 ASC (CH1 $)) 'merged into an integer

NEWF02 & = ClNG (ClNG (ASC (CH4 $) * 16 * 16 ASC (CH3 $))

IfNewF01 &> Maxf01 & kiln 'judgment maximum, minimum

MAXF01 & = Newf01 &

ENDIF

IFNewF01 &

Minf01 & = newf01 &

ENDIF

IFNewF02 &> Maxf02 & Ten

MAXF02 & = Newf02 &

ENDIF

IFNewF02 &

Minf02 & = newf02 &

ENDIF

Labf01.caption = Str (newf01 &) 'Display 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

Else 'During the communication process, press the CMDCOMM button to stop communication and stop testing

CMDComm.caption = "Communication" command, turn off the serial port, make the exit button valid.

Comm1.output = chr (& H5f)

Comm1.portopen = false

cmdexit.enabled = true

ENDIF

EndSub

SUBCMDEXIT_CLICK ()

Ifcomm1.portopenthen 'confirms close serial port

Comm1.portopen = false

ENDIF

End 'termination of the entire program

EndSub

Subtimer1_timer () 'Update Display Time

Iflabtime.caption <> Time & Then

Labtime.caption = Time $

ENDIF

EndSub

3. Program instructions:

a. The communication protocol of the above program is: the baud rate 9600, even check, 8 data bits, 1 stop bit. The start test command is sent by the host computer, and the detected data is transmitted to the upper machine every 500 ms after receiving the command. Each time you send 5 bytes, the first byte is the command accepted by the next machine, the value of the second three bytes of 16Byte, the value of the fourth-fifth byte is 16byte. The upper computer is recycled and processed and displayed data;

b. Due to the data transmitted and accepted by the serial communication, the data transmitted and accepted is more than hexadecimal, so the number of hexadecimal numbers in the program, in the program via echoch% = Val ("& H" Echo. Text) Convert the entered echo.text to a hexadecimal number and assign the variable echoch%. When the serial port is sent, Comm1.output requires accepting character type variables, so it is achieved by statement comm1.output = chr (echoch%);

c. When you accept data, each time you want to judge whether the first byte is a command, only the 4 bytes of the following are the correct data, otherwise check the next byte. Implementing a byte from accepting buffer from accepting buffer by setting Comm1.inputlen = 1;

d. When exiting the program, be sure to turn off the serial port and complete it through the comm1.portopen = false;

e. If you need the upper machine to query the lower machine, serial communication can be performed in the timer Timer1-Timer event;

f. To achieve a complete function, we must further improve the program, even to use WindowsAPI, OLE, etc., the above procedure is just a simple example;

The author introduces the author's harvest in the use of VB development Windows environment, the author develops an interface-friendly, comprehensive and convenient satellite control system inertial sensitive component detection software using the above principles. The actual use effect is good, improved work. Efficiency, and even automation of testing and data processing.

转载请注明原文地址:https://www.9cbs.com/read-3906.html

New Post(0)