Some notes on the MSCOMM control
The MSComm communication control of VB5.0/6. provides a series of standard communication command interfaces, which allows the establishment of serial port connections and can be connected 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 the communication process, allowing you to create full-duplex, event-driven
Dynamic, 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 imagined. Especially in Chinese Wln
There will be more problems when communicating under 95/98. The following will start with the basic introduction, and then gradually discuss the problems that arise in the programming of the MSComm control and the programming techniques.
1. Communicate with MSComm control
1. Basic knowledge of serial communication
Generally, computers have one or more serial ports, which are com1, com2,..., these serial ports also provide external devices and PC for data transmission and
The passage of the letter. These serial ports act as interpreters between the CPU and peripherals. When character data is sent from the CPU to the peripheral, these character data are converted into serial bits
Stream data; when receiving data, the bit stream data is converted into character data and passed to the CPU. Furthermore, in terms of the operating system, Windows uses a communication driver
(COMM.DRV) calls API functions to send and receive data. When API functions are called with communication controls or declarations, they are interpreted by COMM.DRV and passed to the device driver.
As a vB programmer, you need to write communication programs. You only need to know the interface provided by the communication control to the Windows communication AP1 function. In other words, just set up and monitor the
Just trust the properties and events of the control.
2. Using Mscomm control
Before starting to use the MSComm control. Need to first understand its properties, events or errors
Property description
CommPort sets or returns the communication port number
Settings sets or returns the baud rate, parity, data bits, and stop bits as strings
PortOpen sets or returns the status of the communication port. Ports can also be opened and closed
Input returns and deletes characters in the receive buffer
Output writes the string into the send buffer
The CommEvent property returns one of the following values for a communication event or error. These constants can also be found in the control's object library.
Constant value description
ComEventBreak1001 received a break signal
ComEventCTSTO1002ClearToSendTimeout. When sending characters, within the event specified by the system, the CTS (ClearToSend) line is low level
ComEventDSRTO1003DataSetReadyTimeout. When sending characters, within the event specified by the system, the DSR (DataSetReady) line is low level
ComEventFrame1004 data frame error. The hardware detected a data frame error
ComEventOverrun1006 port overflow. The character in the hardware has not been read yet, the next character arrives and is lost
ComEventCDTO1007CarrierDetectTime. When sending characters, the CD (CarrierDetect) line is low level within the event specified by the system. CD
Also called RLSD (ReceiveLineSingalDetect, receive line signal detection)
ComEventRxOver1008 receive buffer overflow. No space in receive buffer
ComEventRxParity1009 Parity error. Hardware detected parity error 7
ComEventTxFull1010 The sending buffer is full. While queuing send characters, the send buffer is full.
ComEventDCB1011 An unexpected error occurred when retrieving the port DCB (DeviceControlBlick).
Communication events include the following settings:
Constant value description
The number of characters in the ComEvSend1 send buffer is lower than the Sthreshold value
ComEvReceive2 received Rthreshold characters. This event continues to be generated until the data in the receive buffer is deleted using the Input property.
ComEvCTS3CTS(ClearToSend) line changed
ComEvDSR4DSR(DataSetReady) line changed. This event occurs when DSR changes from 1 to 0
ComEvCD5CD (CarrierDetect) line changes when ComEvRing6 detects a ring signal. Some URAT(UniversalAsynchronousReciver-
-Transmitters, Universal Asynchronous Receiver-Transmitter) does not support this event
ComEvEOF7 received an EOF character (ASCII character 26)
Error messages (MSComm control) The following table lists the error messages that the MSComm control can capture:
Constant value description
ComInvalidPropertyValue380 Invalid property value
ComSetNotSupported383 property read-only
ComGetNotSupported394 property read-only
This presence is invalid when the ComPortOpen8000 port is opened.
8001 timeout setting must be greater than 0
ComPortInvalid8002 Invalid port number
8003 attributes are only valid at runtime
8004 Properties are read-only at runtime
ComPortAleadyOpen8005 port has been opened
8006 Device identifier is invalid or not supported
8007 The device's baud rate is not supported
8008 The specified byte size is invalid
8009 Default parameter error
8010 Hardware unavailable (locked by other devices)
8011 function cannot allocate queue
ComNoOpen8012 device is not open
8013 The device is already open
8014 Communication notification cannot be used
ComSetCommStateFailed8015 cannot set communication status
8016 Unable to set communication event shielding
ComPortNotOpen8018 This presence is only valid when the port is open
8019 Device busy
ComReadError8020 Communication device read error
ComDCBError8021 An internal error occurred while retrieving the port device control block
After understanding the above basic properties, you can start writing communication permission programs. Create a new project file in VB5.0/6.0. Add MicrosoftCommControl5.0 group
File, add the Command button to Simplified Form1 and name it CmdTest, name the MSComm control MSComm1, and add the following program code.
PrivateSubcmdTestClick()'Open the serial port
MSComml.CommPort=2'Set Com2
IfMSComml.PortOpen=FalseThen
MSComm1.Settings="9600,n,8,1"'9600 baud rate, no parity, 8 data bits, 1 stop bit
MSComm1.PortOpen=True'Open the serial port
Endif
MSComm1.OutBufferCount=0'Clear the send buffer
MSComm1.InBufferCount=0'Slip the receive buffer
'When sending character data, be sure to end it with a carriage return (vbcr)
MSComm1.Output="Thisisaqoodbook!"&vbCr
'Splash phone number or send AT command
MSComm1.Output="ATDT05778191898,&vbCr
'When sending character array data, note that ByteArray must be assigned a value in advance
DimByteArrayasbyte()
'Define dynamic array
ReDimByteArray(1)
'Redefine the array size
ByteArray(0)=0
ByteArray(1)=1
MSComm1.Output=ByteArray
EndSub
privateSubMScommEvent()
SelectCaseMSComm1.CommEvent
CasecomEvReceive
DimBufferAsVariant
MSComm1.InputLen=0
'Receive binary data
MSComm1.InputMode=ComInputModeBinary
Buffer=MSComm1.Input
'Receive character data
MSComm1.InputMode=comInputModeText
Buffer=MSComml.Input
Caseelse
EndSelect
Endsub
(Procedure 1)
1 2Read the full text on the next page