Monday, January 3, 2011
【 Weak current College 】 Windows environment for serial data communication programming 】
Summary article discusses in detail the MSCOMM.VBX communication control in Windows platform software design method of communication, with the help of Visual Basic the attached communication control can easily develop advanced and professional communication software to meet the different business needs.
In the Windows operating system platform development communication software, although Windows 3.1 SDK provides a complete range of API application programming interface function and disruptive driven COMM.DRV communications device driver, which you can use C or C ++ to do this kind of application design, but its complexity and the high degree of specialization. Therefore, at present, many software developers are using Microsoft Visual Basic to develop Windows applications. This article will detail VB environment communication software development methods, through the analysis of the communication control MSCOMM.VBX, we can see that using VB to develop Windows platform of communication software is very simple. Finally the combination of our work in one instance completely describe the class of software design methods.
I. Description of the MSCOMM.VBX
Serial communication in VB controls have been provided using RS-232 for data communications software development and design of all rules, Microsoft Visual Basic 3.0 MSCOMM.VBX (need to be installed in indowsystem\ directory) is a 1992 Crescent software Inc. company designed specifically for Microsoft's VB Platform design, it uses two different ways to handle and solve various types of communication software development and design issues.
(1) event-driven, and we use the C/C + + write Windows software Windows callback function is similar, is a powerful approach. In practice, often requires that we deal with many communication related events. For example: when the line reaches the end or data CD (Carrier Detect) and RTS (Request To Send) signal cable status change, this requires us to use the appropriate events to track and handle. In the control that is implemented using the OnComm, it also includes a communication error detection and processing. The following is the MSCOMM detailed error and event list (readers may see SKD or MSC/C + + 7.0 etc in Windows.h related communication section constants and function prototypes):
MSCOMM_ER_BREAKA received a Break signal.
MSCOMM_ER_CDTOCD signal time out
MSCOMM_ER_CTSTOCTS signal time out
MSCOMM_ER_DSRTODSR signal time out
MSCOMM_ER_FRAME frame errors
MSCOMM_ER_OVERRUN receive data loss
MSCOMM_ER_RXOVER receive buffer overflow
MSCOMM_ER_RXPARITY correcting wrong inspection
MSCOMM_ER_TXFULL send buffer full
MSCOMM_EV_CDCD signal change
MSCOMM_EV_CTSCTS signal change
MSCOMM_EV_DSRDSR signal change
MSCOMM_EV_EOF received a text file ending characters (26)
MSCOMM_EV_RING detected a ring signal
MSCOMM_EV_RECEIVE receives a set number of characters in the event
MSCOMM_EV_SEND place the number of characters in the buffer is less than the value set by the production of the event the above constants defined in the CONSTANT.TXT file.
(2) query mode, program designers to read the value and handle the CommEvent by errors or events.
Usually simple application designed to adopt this approach.
Use communication control development communication software, can very easily design a simultaneous access to multiple communication ports, this is Windows. For example, using Commi.OnComm 1 ≤ I ≤ n (n: currently available communication port), you can design multiple serial interface handler. For example, a telephone network use to retrieve information on the background of a small server software application is a good example. MSCOMM.VBX provides 27 on communication control aspects of the property, the following lists some of the most important:
CommPort sets and returns the number of mouth communication
Settings set and returns a string representation of the data communication format: baud rate, parity, data bits, and stop bits
PortOpen sets and returns the status of the communication port (including opening and closing of a communication port)
Input reading of serial data reception buffer
Send a string Output
In the MSCOMM.VBX, there are two Input and Output properties are equal with function;
(1) input function, from the serial communication receive buffer with a string, the sentence is read as follows:
Comlnput(By Val hWnd As Integer, lpData As Any,By Val cbData As Integer) A
@ @ T8S14200.GIF; figure 1 @@
Return value: the read operation on the number of characters actually read
(2) output functions, the communication port to send a string, the sentence as follows: ComOutput(By Val hWnd As Inte
ger, lpData As Any,By Val cbData As Integer) As integer
@ @ T8S14201.GIF; figure 2 @@
Return value: This writes the actual number of characters sent
Second, event-driven and query modes in two communications software development template
Both correspond we usually use Assembly or C/C ++ to write communications programs and query methods.
(1) event driven
The following example is an event driven style design communication program templates, we simply according to own actual situation to select in the following Case statement to insert the appropriate code.
Select Case Commi.CommEvent'i=1,2...
' Error section
To insert processing Case MSCOMM-ER_BREAK ' BREAK code
To insert processing Case MSCOMM-ER_CDTO ' CD (RLSD) timeout code
To insert processing Case MSCOMM-ER_CTSTO ' CTS timeout code
To insert processing Case MSCOMM-ER_DSRTO ' DSR timeout code
Case insert processing frame MSCOMM-ER_FRAME ' error code
To insert processing Case MSCOMM-ER_OVERRUN ' data lost code
Case insert processing receive MSCOMM-ER_RXOVER ' buffer overflow code
Case insert processing check MSCOMM-ER_RXPARITY ' error code
To insert processing Case MSCOMM-ER_TXFULL ' send buffer full code
' Event section
To insert processing Case MSCOMM-EV_CD ' CD signal change code
To insert processing Case MSCOMM-EV_CTS ' CTS signal change code
To insert processing Case MSCOMM-EV_DSR ' DST signal change code
Case MSCOMM-EV_EOF ' insert processing received a text word (26) end code
To insert processing Case MSCOMM-EV_RING ' detected a ringing signals code
Case MSCOMM-EV_RECEIVE ' insert received a set number of characters the event code
Case MSCOMM-EV_SEND ' insert processing occurs on the number of characters in the buffer is less than the value set by the production of the
Event code End Select
(2) query mode
Query mode, each command (with data block) has to query wait on it. Detailed usage please see below for instance. Third, a complete example
The program's design background is the army's medical information online phone dial-up user front-end software automatic dialing section, to save space and description of the problem, here's a re-edit, but itself is intact. The program helps to master in order to pass the phone dial-up is called.
Debug environment: LEO 486/50, HiTec MODEM, phone pulse dialing, hospital use serial port 1, PWIN 3.1
Program Form consists of the following parts: List1 phone number display list; Label1 dial-up status display; Dial, Canc
Quit three button; MSCOMM.VBX communication control.
' Visual Basic 3.0 MSCOMM.VBX programming: using dial-up MODEM, artificial call
' Program name: MDIALER.TXT
' Academy of military medical Sciences Centre Summer cloud, 1995.2.12
' Run: using VB FILE Load in the Text entry into, or press F5 to run the generated .exe file
Dim PhoneNumbers $ () ' phone number stored variable
Dim CancelFlag'Cancel button status flags
Sub Dial (Number $) ' dial-up routines
' According to the pulse/DTMF dialing to select DT/DP;
' Dialed number stored in the variable $ PhoneNumbers (Index);
' Command in the semicolon represents the MODEM after the dial-up and return to the command State;
' Each command by carriage Chr $ (13);
Commi.Settings = "2400, N, 8, 1" ' set the frame format
On Error Resume Next ' error recovery
Commi.PortOpen = True ' to start the interface works
If Err Then ' if the port is not started, it displays information
MsgBox "the current serial communication port does not work, please choose to use another port. ”
Commi.InBufferCount=0'Clear the receive buffer
DialString Commi.Output = $ ' to begin dialing
Do ' wait for the MODEM returns the answer "OK"
Dummy = DoEvents () ' to enter the wait mode, will resources submission system management
If Commi.InBufferCount Then ' if the receive buffer has data exists, then the read
If InStr (FromModem $, "OK") Then ' if the receive the "OK"
Beep ' has received the "OK", the sound of the bee-uh
MsgBox "the other phones already dialed, please pick the phone and press enter or OK button"
If CancelFlag Then ' if the Cancel button by pressing the handle
Commi.Output = "ATH" + Chr $ (13) ' hang up command
Commi.PortOpen = False ' close the mouth
Sub DialButton_Click () ' Dial button handler
DialButton.Enabled = False'Dial, Quit second button at the same time invalid
CancelButton.Enabled = True ' open Cancel button
$ Number = PhoneNumbers $ (List1.ListIndex)) ' get a phone number
$ Temp = Status ' Save State
$ Status = "dialing number:" + Number $
Dial Number $ ' dial-up routine implementation
DialButton.Enabled = True ' dial-up ends open Dail, Quit button
CancelButton.Enabled = False ' prohibition of the Cancel button
Status = Temp $ ' recovery status
Commi.InputLen = 0 ' set a value of 0: read all of the receive buffer content
MDialer.Show ' display window
The Institute of Medicine of the "fourth List1.AddItem Computing Center"
List1.AddItem "Academy of military medical sciences information Zhang Hua researcher"
ReDim PhoneNumbers$(0 To List1.ListCount-1)
List1.ListIndex = 0 ' default first number
Sub List1-DblClick () ' list box to select the number to start a dial-up
Sub CancelButton-Click () ' Cancel button handler
CancelFlag = True ' identifies the Cancel button is pressed
CancelButton.Enabled = False ' cancel the spin button to continue activities
Sub QuitButton-Click () ' Quit button
End to End '
MSCOMM.VBX on development communication under Windows platform software easy indeed, use event driven to design good background communication programs, develop terminal emulation software and to solve more complex problems. The communications control not only includes all of the Windows API on serial communication of 16 function the function, and has opened up more enables users to design a convenient object properties to meet the needs of different users of different business needs. Therefore we can apply the content and discussed above, the use of Windows environment method, use VB tools to develop the design of all serial communication software. In addition, the MSCOMM in order to lower a high-speed modem equipment directly used in the standards, it offers a rate of first-class 14.4Kbps interface.