The communication control in VisualBasic and the communication software design method under the windows platform are combined with specific examples to give the basic communication program.
In 1992, Crescent Software Inc. designed the mscomm.vbx user communication control specifically for VB. It provides serial communication capabilities for sending and receiving data through the serial port. It not only includes the functions completed by all 16 functions of serial communication in Windows API. , and has developed more object attributes that facilitate user design to meet the needs of different users and different businesses. At present, many domestic VB materials rarely involve the introduction of this communication control. This article will introduce the mscomm.vbx communication control and programming methods in detail, and give a basic communication program based on an example at work.
1mscomm.vbx communication control description
The mscomm.vbx communication control can be directly added to the form form from the VB toolbox, and can be used for communication. If there is no such control in the toolbox, use the customcontrols of tools to add mscomm.vbx from the system subdirectory of windows to the toolbox of vb.
1.1 Communication methods
mscomm.vbx has 2 different ways to handle and solve the development and design problems of various communication software
1. Event-driven. It is similar to the window callback function when writing Windows software in C/C, and is a powerful way to deal with problems. In actual work, we often have to deal with many related events in communication. For example: when the line data arrives at the local end or the status of the cd line and rts signal line changes, we are required to use the corresponding events to track and process. This control is used The oncomm event is implemented, which also includes detecting and handling communication errors and other issues. The commevent value returns the most recent communication event or error numeric code. Detailed error and event examples of communication controls include:
mscomm-er-break received 1 breaksignal
mscomm-er-cdtocd signal timeout
…
mscomm-ev-cdcd signal changes
…
2. Inquiry method. It is the programmer's responsibility to read the value of commevent and handle the errors or events that occur. Usually simple application design can adopt this approach.
1.2 Properties of communication controls
The key to using communication controls to compile communication programs is to accurately understand and set the properties of communication controls. mscomm.vbx provides 27 properties about communication controls, such as:
commport: Set or return the communication port number.
settings: Set or return the data communication format in the form of a string: baud rate, parity, data bits and stop bits.
portopen: Set or return the communication port status (including opening and closing a communication port)
…
3. Examples
The application background of this program is the dcc95 electrostatic precipitator automatic monitoring system software, which solves the communication problem between 1 pc industrial computer (master station) and 32 microcontrollers (substations). The bus network structure between the master station and the substation adopts the rs-485 communication standard and conducts data communication in a question-and-answer manner. Since the 32 substations send communication commands (downlink commands) to the master station, the master station continues to send downlink commands after receiving the corresponding reply commands (uplink commands) sent back by the substations. According to the requirements of system functions, the master station needs to send two types of commands: (1) Synchronous command, which is triggered by a timer and is sent once every ls cycle; (2) Aperiodic command, which is triggered by the operator Caused by moving the corresponding command button, it is sent aperiodically. The automatic monitoring system software is installed on the main station, and the communication program is also installed on the main station as part of the automatic monitoring system software.
This article only lists a basic demo program list for testing when debugging communication programs. During the test, one PC was used as the main station, and another PC simulated the work of 32 substations. The two PCs used rs232c serial port communication. Add 1 communication control, 2 timer controls and 1 command button control to the communication demonstration program form (form) of the main station. The communication control (mscomm1) is used to access the serial port, send and receive data; periodic timer control (periodic) is used to control the master station to send periodic commands to each substation every second; the command button control (nonperiodic-command) and the nonperiodic timer control (nonperiodic) are used to send non-periodic commands. Data transmission uses event-driven communication. The rtreshlod attribute is set according to different sending commands, thereby causing the oncomm event to receive data.
2.1 Initialization procedures for each control in the form
Set the working parameters of the communication serial port, set the interrupt interval of the periodic timer to ls, and the interrupt interval of the nonperiodic timer to 0.5s.
subform-load()
mscomm1.commport=2'Select com2 serial port
mscomm1.settings="9600,n8,1"'Baud rate 9600, no parity bit, 8 data bits and 1 stop bit
mscomm1.inputlen=0'input will read the entire contents of the receive buffer
mscomm1.inbuffersize=1024'Set the byte length of the receive buffer
mscomm1.portopen=true'Open the communication port
mscomm1.inbuffercount=0'Clear send buffer data
mscomm1.outbuffercount=0'Clear the receive buffer data
periodic.inteval=100'Set the ls timing interval so that the telemetry command is sent once every ls
nonperiodic.inteval=500'Set the 0.5s timing interval and query whether the command button is activated to determine whether to send periodic commands
command-PRessed=false'The command button is inactive
during-periodic=false'Periodic command data transmission has not yet started
during-nonperiodic=false'non-periodic command data transmission has not yet started
endsub
2.2 Aperiodic command sending program
According to the command button status and periodic command data transmission status, aperiodic commands are sent in the interrupt program of the nonperiodic timer.
subnonperiodic-command-click()
command-pressed=true'command button activation
endsub
subnonperiodic-timer()
ifduring-periodic=trueorcommand-pressed=false
thenexitsub' If the periodic command data transmission has not ended or the command button is activated, exit the program that sends aperiodic commands.
command-pressed=false'Command button returns to inactive state
callsenddata(nonperiodic-command)'Send non-periodic commands
mscomm1.rthreshold=r-nonperiodic-byte'After sending the aperiodic command, set the rthreshold attribute so that the master station triggers the oncomm event after receiving the set number of bytes.
endsub
2.3periodic timer program
Send periodic commands in the interrupt program of the periodic timer:
subperiodic-timer()
ifduring-nonperiodic=truethenexitsub'If the non-periodic command data transmission has not yet ended, exit the program for sending aperiodic commands.
during-periodic=true' sets the periodic command data transmission status to in progress
callsenddata(periodic-command)'send periodic commands
mscomm1.rthreshold=r-periodic-byte'After sending the periodic command, the master station receives r-remot-edata-byte bytes, which can trigger the oncomm event
endsub
2.4oncomm event program
According to the rthreshold attribute setting value, when the corresponding byte character is received in the receiving buffer, the oncomm event is triggered and the data is received in the interrupt program.
submscomm1-oncomm()
selectcasemscomm1.commevent'Here you can insert code to handle various errors or events.
casemscomm-ev-receive
receivestring$=mscomm1.input
selectcasemscomm1.rthreshold
caser-periodic-byte'response data of periodic command
calldisposedata(periodic-command)'process the received data
duringperiodic=false' sets the periodic command data transmission status to end
caser-nonperiodic-byte'non-periodic command response data
calldisposedata(nonperiodic-command)'process received data
during-nonperiodic=false' sets the non-periodic command data transmission status to end
endselect
endselect
endsub
With the continuous upgrading of vb version, vb will become the fastest, easiest to use, and powerful application development tool, and one of the first choice tools for enterprise-level client/server application software development. ->