Page 1/2 about MSCOMM controls

Source: Internet
Author: User

MSCOMM control
The MSComm communication control of VB5.0/6 provides interfaces for a series of standard communication commands. It allows you to establish a serial port connection and connect to other communication devices (such as Modem ).
You can also send commands, exchange data, and monitor and respond to various errors and events that may occur during communication, so that you can use it to create full-duplex, event-driven
An efficient and practical communication program. However, in the actual communication software design process, the MSComm control is not as perfect and easy to control as expected, especially in the Chinese Wln.
95/98 communication problems may occur. Next we will start from the basics, and gradually discuss the problems and programming skills in the compilation of the MSComm control.
1. Communicate with MSComm
1. Basic knowledge of Serial Communication
Generally, the computer has one or more serial ports, which are com1, Com2 ,..., These serial ports also provide external devices and PCs for data transmission and
The channel of the dish. These serial ports act as interpreters between the CPU and peripherals. When the character data is sent from the CPU to the peripherals, the character data is converted to a serial bit.
Streaming Data. When receiving data, bit stream data is converted to character data and transferred to the CPU. Further, in terms of the operating system, Windows uses a communication driver.
(COMM. DRV) call an API function to send and receive data. When a communication control or declaration call an API function is used, it is explained by COMM. DRV and passed to the device driver,
As a vB programmer, to write a communication program, you only need to know the interface that the communication control provides to the Windows communication AP1 function. In other words, you only need to set and monitor
The attributes and events of the email control.
2. Use the Mscomm control
Before you start using the MSComm control. You must first understand its attributes, events, or errors.
Attribute description
Set or return the communication port number.
Settings sets or returns the baud rate, parity, data bit, and stop bit in the form of a string.
Set or return the status of the communication port. You can also enable or disable ports.
Input returns and deletes characters in the receiving buffer.
Output writes the string to the sending buffer.

If the CommEvent attribute is a communication event or error, one of the following values is returned. These constants can also be found in the object library of the control.
Constant Value description
ComEventBreak 1001 received the disconnection Signal
ComEventCTSTO 1002 Clear To Send Timeout. When sending characters, the CTS (Clear To Send) line is low within the first thing specified by the system.
ComEventDSRTO 1003 Data Set Ready Timeout. When sending characters, the DSR (Data Set Ready) line is low in the event specified by the system.
ComEventFrame 1004 data frame error. The hardware detects a data frame error.
ComEventOverrun 1006 port overflow. The characters in the hardware are not read, the next character arrives again, and are lost.
ComEventCDTO 1007 Carrier Detect Time. When sending characters, the CD (Carrier Detect) line is low within the event specified by the system. CD
Also known as RLSD (Receive Line Singal Detect, Receive Line signal detection)
ComEventRxOver 1008 receives Buffer Overflow. No space in the receiving buffer
ComEventRxParity 1009 parity error. Hardware detected parity error 7
ComEventTxFull 1010 the sending buffer is full. When queuing for sending characters, the sending buffer is full
An unexpected error occurred while checking the port DCB (Device Control Blick) in ComEventDCB 1011.

Communication events include the following settings:
Constant Value description
The number of characters in the sending buffer of ComEvSend 1 is lower than the value of Sthreshold
ComEvReceive 2 receives Rthreshold characters. This event continues until the data in the receiving buffer is deleted using the Input attribute.
ComEvCTS 3 CTS (Clear To Send) Line change
The ComEvDSR 4 (Data Set Ready) line is changed. This event occurs when the DSR changes from 1 to 0.
The ComEvCD 5 CD (Carrier Detect) line changes the ComEvRing6 to Detect the ringing signal. Some URAT (Universal AsynchronousReciver-
-Transmitters, universal asynchronous transceiver) does not support this event
ComEvEOF 7 received the EOF character (ASCII character 26)

The following table lists the Error messages that can be captured by the MSComm control:
Constant Value description
ComInvalidPropertyValue 380 is invalid.
ComSetNotSupported 383 attribute read-only
ComGetNotSupported 394 attribute read-only
Port 8000 is invalid when port is enabled.
8001 the timeout value must be greater than the value 0.
The port number of ComPortInvalid 8002 is invalid.
8003 the attribute is valid only at run time.
8004 the attribute is read-only at run time.
Port ComPortAleadyOpen 8005 is enabled
8006 the device identifier is invalid or not supported
8007 the device baud rate is not supported
8008 the specified byte size is invalid
8009 default parameter error
8010 hardware unavailable (locked by other devices)
8011 the function cannot allocate a queue.
The ComNoOpen 8012 device is not enabled
8013 the device has been enabled
8014 communication notifications cannot be used.
ComSetCommStateFailed 8015 cannot set the communication status
8016 communication event blocking cannot be set.
ComPortNotOpen 8018 is valid only when the port is opened.
8019 devices busy
ComReadError 8020 communication device read error
An internal error occurs when ComDCBError 8021 detects the port device control block.

After figuring out the above basic attributes, you can start to write the communication program. Create a project file in VB5.0/6.0. Add Microsoft Comm Control 5.0 Group
Add the Command button in the simplified Form1 and name it "release test". The MSComm control is named MSComm1 and the following program code is added.
Private Sub branch testclick () 'Open the serial port
MSComml. commp ORT = 2' set Com2
If MSComml. PortOpen = False Then
MSComm1.Settings = "9600, n, 8, 9600" 'baud rate, no verification, 8-Bit Data bit, 1-bit stop bit
MSComm1.PortOpen = true' open the serial port
End if
MSComm1.OutBufferCount = 0' clear the sending Buffer
MSComm1.InBufferCount = 0' slip the blank receiving buffer

'Note that the carriage return (vbcr) must be used to end character data sending.
MSComm1.Output = "This is a qood book! "& VbCr

'Splash call number or send AT command
MSComm1.Output = "ATDT 05778191898, & vbCr

'When sending character array data, note that ByteArray must be pre-defined with a value assigned.
Dim ByteArray as byte ()

'Define a dynamic array
ReDim ByteArray (1)

'Redefine the array size
ByteArray (0) = 0
ByteArray (1) = 1
MSComm1.Output = ByteArray
End Sub

Private Sub MScommEvent ()
Select Case MSComm1.CommEvent
Case comEvReceive
Dim Buffer As Variant
MSComm1.InputLen = 0
'Receive binary data
MSComm1.InputMode = ComInputModeBinary
Buffer = MSComm1.Input
'Receive character data
MSComm1.InputMode = comInputModeText
Buffer = MSComml. Input
Case else
End Select
End sub
(Procedure 1)

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.