1MSCOMM.VBX Communication Control Description
MSCOMM.VBX communication controls can be used to communicate directly from the toolbox of VB by adding form forms. In the absence of this control in toolbox, use the CustomControls of tools to add MSCOMM.VBX from the Windows System subdirectory to the VB Toolbox.
1.1 Communication Mode
MSCOMM.VBX has 2 different ways to deal with and solve all kinds of communication software development and design problems
1, event-driven. It is similar to the window callback function when writing Windows software, and is 1 powerful ways to deal with problems. In the actual work, often have to deal with a lot of communication related events, for example, when the line data reaches the local or CD and RTS signal line States, we are required to use the appropriate events to track and process, the control is implemented using the OnComm event, it also includes the detection and handling of communication errors and other aspects of the problem , the CommEvent value returns the most recent communication event or error's numeric code. Examples of detailed errors and events for communication controls are:
Mscomm-er-break received 1 breaksignal
MSCOMM-ER-CDTOCD Signal Timeout
MSCOMM-EV-CDCD Signal Change
2, inquiry method. The program designer is responsible for reading the value of the commevent and handling any errors or events that occur. This approach is typically used for simple application design.
1.2 Properties of communication controls
The key of using communication control to make communication program is to understand the properties of setting communication control accurately. MSCOMM.VBX provides 27 properties on communication controls, such as:
Commport: Sets or returns the communication port number.
Settings: Sets or returns the data communication format that appears as a string: Baud rate, checksum, data bits, and stop bits.
PortOpen: Sets or returns the state of the communication port (including opening and closing 1 communications ports)
3. Example
The application background for the dcc95 electrostatic precipitator automatic monitoring system software, to solve 1 PC industrial control computer (main station) and 32 single chip microcomputer (sub station) communication between the problem. The bus-type network structure between the main station and the sub station uses the RS-485 communication standard to conduct data communication in question and answer mode. Since 32 sub stations Send communication commands (downlink commands) to the master station, the main station will continue to send the downlink command after the corresponding answer command (Uplink command) sent back by the receiving sub station. According to the requirements of the system function, the main station needs to send 2 kinds of commands: (1) The same time command, it is triggered by the timer, every LS cycle to send 1 times, (2) aperiodic command, it by the operator press the corresponding command button, not periodically sent. 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 lists only the basic demo programs that are used for testing when debugging a communication program. Test, with 1 PCs as the main station, another PC simulation of the work of 32 sub stations, two PCs using rs232c serial communication. To the main station communication demo program forms (form) to add 1 communication controls, 2 timer controls and a command button control, communication controls (MSCOMM1) used to access the serial port, send and receive data; Periodic timer control (periodic) Used to control the sending of periodic commands per second from the primary station to each child station; the command button control (Nonperiodic-command) and the Nonperiodic Timer control (nonperiodic) are used to send aperiodic commands. The data transmission uses the event-driven communication method, sets the Rtreshlod property according to the different sending commands, thus causes the OnComm event to receive the data.
2.1 Forms each Control initialization program
Set the communication serial working parameters, set the periodic timer in the break interval for the ls,nonperiodic timer interrupt interval of 0.5s.
Subform-load ()
mscomm1.commport=2 ' Select COM2 serial port
mscomm1.settings= "9600,n8,1" baud rate 9600, no parity bit, 8 bit data bit 1 bit stop bit
Mscomm1.inputlen=0 ' input will read the entire contents of the receive buffer
mscomm1.inbuffersize=1024 ' Sets the byte length of the receive buffer
Mscomm1.portopen=true ' Open the communication port
Mscomm1.inbuffercount=0 ' clears send buffer data
Mscomm1.outbuffercount=0 ' clears receive buffer data
periodic.inteval=100 ' Set LS timed interval so that telemetry commands are sent 1 times per second LS
nonperiodic.inteval=500 ' Sets the 0.5s timed interval to query whether the command button is active to determine whether to send periodic commands
Command-pressed=false ' command button is not active
During-periodic=false ' Periodic command data transfer has not yet started
During-nonperiodic=false ' non-cyclical command data transfer has not yet started
Endsub
2.2 Non-periodic command sender
Sends aperiodic commands in the interrupt program of the nonperiodic timer, depending on the status of the command button and the periodic command data transfer status.
Subnonperiodic-command-click ()
Command-pressed=true ' command button activation
Endsub
Subnonperiodic-timer ()
Ifduring-periodic=trueorcommand-pressed=false
Thenexitsub ' If a periodic command data transfer has not ended or the command button is active, quit sending a aperiodic command program.
Command-pressed=false ' command button back to inactive
Callsenddata (Nonperiodic-command) ' Send non-periodic command
Mscomm1.rthreshold=r-nonperiodic-byte ' after sending a aperiodic command, set the RThreshold property so that the primary station receives the set number of bytes and raises the OnComm event
Endsub
2.3periodic Timer Program
Send periodic commands in the interrupt program for the periodic timer:
Subperiodic-timer ()
Ifduring-nonperiodic=truethenexitsub ' quits sending a aperiodic command program if the data transfer has not ended with a periodic command.
During-periodic=true ' Set periodic command data transfer status is in progress
Callsenddata (Periodic-command) ' Send periodic commands
Mscomm1.rthreshold=r-periodic-byte ' sends periodic command, the main station receives R-remot-edata-byte byte, can raise OnComm event
Endsub
2.4oncomm Event Program
Sets the value according to the RThreshold property, raises the OnComm event, receives the data in the interrupt program when the corresponding byte character is received within the receiving buffer.
Submscomm1-oncomm ()
Selectcasemscomm1.commevent ' Here you can insert code to handle a variety of different errors or events
Casemscomm-ev-receive
Receivestring$=mscomm1.input
Selectcasemscomm1.rthreshold
Caser-periodic-byte ' response data for periodic commands
Calldisposedata (Periodic-command) ' processing receive data
Duringperiodic=false ' Set periodic command data transfer status to end
Caser-nonperiodic-byte ' response data for non-periodic commands
Calldisposedata (Nonperiodic-command) ' processing receive data
During-nonperiodic=false ' Set non-cyclical command data transfer status to end
Endselect
Endselect
Endsub
With the continuous upgrading of VB Version, VB will become the fastest, Easy-to-use, strong application development tools, Enterprise-class client/server application software development of the preferred tool.