有关于MSComm控件详解

VB 专栏收录该内容
7 篇文章 0 订阅
  Visual Basic: MSComm Control
 
      The MSComm control provides serial communications for your application by allowing the transmission and reception of data through a serial port.
 
1.Error Messages (MS Comm Control)

The following table lists the trappable errors for the MSComm control.

 

ConstantValueDescription
comInvalidPropertyValue380Invalid property value
comSetNotSupported383Property is read-only
comGetNotSupported394Property is read-only
comPortOpen8000Operation not valid while the port is opened
8001Timeout value must be greater than zero
comPortInvalid8002Invalid Port Number
8003Property available only at run time
8004Property is read only at runtime
comPortAlreadyOpen8005Port already open
8006The device identifier is invalid or unsupported
8007The device's baud rate is unsupported
8008The specified byte size is invalid
8009The default parameters are in error
8010The hardware is not available (locked by another device)
8011The function cannot allocate the queues
comNoOpen8012The device is not open
8013The device is already open
8014Could not enable comm notification
comSetCommStateFailed8015Could not set comm state
8016Could not set comm event mask
comPortNotOpen8018Operation valid only when the port is open
8019Device busy
comReadError8020Error reading comm device
comDCBError8021Internal error retrieving device control block for the port
 
2.Properties Description

    Although the MSComm control has many important properties, there are a few that you should be familiar with first.

 

CommPort     Sets and returns the communications port number.
Settings     Sets and returns the baud rate, parity, data bits, and stop bits as a string.
PortOpen     Sets and returns the state of a communications port. Also opens and closes a port.
Input        Returns and removes characters from the receive buffer.
Output       Writes a string of characters to the transmit buffer.

 


CommEvent

      Returns the most recent communication event or error. This property is not available at design time and is read-only at run time.

Syntax

object.CommEvent

The CommEvent property syntax has these parts:

Part Description
object An object expression that evaluates to an object in the Applies To list.


Remarks

Although the OnComm event is generated whenever a communication error or event occurs, the CommEvent property holds the numeric code for that error or event. To determine the actual error or event that caused the OnComm event, you must reference the CommEvent property.

The CommEvent property returns one of the following values for communication errors or events. These constants can also be found in the Object Library for this control.

Communication errors include the following settings:

Constant               Value                              Description
comEventBreak           1001                       A Break signal was received.
comEventFrame           1004         Framing Error. The hardware detected a framing error.
comEventOverrun         1006     Port Overrun. A character was not read from the hardware                                       before the next character arrived and was lost.
comEventRxOver          1008        Receive Buffer Overflow. There is no room in the                                                     receive buffer.
comEventRxParity        1009         Parity Error. The hardware detected a parity error.
comEventTxFull          1010          Transmit Buffer Full. The transmit buffer was full                                              while trying to queue a character.
comEventDCB             1011             Unexpected error retrieving Device Control Block                                                           (DCB) for the port.


Communications events include the following settings:

Constant               Value                              Description
comEvSend                               There are fewer than Sthreshold number of                                               characters in the transmit buffer.
comEvReceive                   Received Rthreshold number of characters. This event is                                  generated continuously until you use the Input property to                                            remove the data from the receive buffer.
comEvCTS                                     Change in Clear To Send line.
comEvDSR                         Change in Data Set Ready line. This event is only fired                                                    when DSR changes from 1 to 0.
comEvCD                                         Change in Carrier Detect line.
comEvRing                           Ring detected. Some UARTs (universal asynchronous                                        receiver-transmitters) may not support this event.
comEvEOF                            End Of File (ASCII character 26) character received.


Data Type   Integer


EOFEnable 
     The EOFEnable property determines if the MSComm control looks for End Of File (EOF) characters during input. If an EOF character is found, the input will stop and the OnComm event will fire with the CommEvent property set to comEvEOF.

Syntax

object.EOFEnable [ = value ]

The EOFEnable property syntax has these parts:

Part Description
object An object expression that evaluates to an object in the Applies To list.
value A boolean expression that determines whether the OnComm event is fired when an EOF character is found, as described in Settings.


Settings

The settings for value are:

Setting Description
True The OnComm event is fired when an EOF character is found.
False (Default) The OnComm event isn't fired when an EOF character is found.


Remarks

     When EOFEnable property is set to False, the control will not scan the input stream for EOF characters. 

InBufferCount


Returns the number of characters waiting in the receive buffer. This property is not available at design time.

Syntax

object.InBufferCount[ = value ]

The InBufferCount property syntax has these parts:

Part Description
object An object expression that evaluates to an object in the Applies To list.
value An integer expression specifying the number of characters waiting in the receive buffer.


Remarks

InBufferCount refers to the number of characters that have been received by the modem and are waiting in the receive buffer for you to take them out. You can clear the receive buffer by setting the InBufferCount property to 0.

Note   Do not confuse this property with the InBufferSize property. The InBufferSize property reflects the total size of the receive buffer.

Data Type   Integer 

 

InBufferSize


Sets and returns the size of the receive buffer in bytes.

Syntax

object.InBufferSize[ = value ]

The InBufferSize property syntax has these parts:

Part Description
object An object expression that evaluates to an object in the Applies To list.
value An integer expression specifying the size of the receive buffer in bytes.


Remarks

InBufferSize refers to the total size of the receive buffer. The default size is 1024 bytes. Do not confuse this property with the InBufferCount property which reflects the number of characters currently waiting in the receive buffer.

Note   Note that the larger you make the receive buffer, the less memory you have available to your application. However, if your buffer is too small, it runs the risk of overflowing unless handshaking is used. As a general rule, start with a buffer size of 1024 bytes. If an overflow error occurs, increase the buffer size to handle your application's transmission rate.

Data Type  Integer

 

Input


Returns and removes a stream of data from the receive buffer. This property is not available at design time and is read-only at run time.

Syntax

object.Input

The Input property syntax has these parts:

Part Description
object An object expression that evaluates to an object in the Applies To list.


Remarks

The InputLen property determines the number of characters that are read by the Input property. Setting InputLen to 0 causes the Input property to read the entire contents of the receive buffer.

The InputMode property determines the type of data that is retrieved with the Input property. If InputMode is set to comInputModeText then the Input property returns text data in a Variant. If InputMode is comInputModeBinary then the Input property returns binary data in an array of bytes in a Variant.

Data Type  Variant


InputLen


Sets and returns the number of characters the Input property reads from the receive buffer.

Syntax

object.InputLen [ = value ]

The InputLen property syntax has these parts:

Part Description
object An object expression that evaluates to an object in the Applies To list.
value An integer expression specifying the number of characters the Input property reads from the receive buffer.


Remarks

The default value for the InputLen property is 0. Setting InputLen to 0 causes the MSComm control to read the entire contents of the receive buffer when Input is used.

If InputLen characters are not available in the receive buffer, the Input property returns a zero-length string (""). The user can optionally check the InBufferCount property to determine if the required number of characters are present before using Input.

This property is useful when reading data from a machine whose output is formatted in fixed-length blocks of data.

Data Type  Integer


InputMode


Sets or returns the type of data retrieved by the Input property.

Syntax

object.InputMode [ = value ]

The InputMode property syntax has these parts:

Part Description
object An object expression that evaluates to an object in the Applies To list.
value A value or constant that specifies the input mode, as described in Settings.


Settings

The settings for value are:

Constant Value Description
comInputModeText 0 (Default) Data is retrieved through the Input property as text.
comInputModeBinary 1 Data is retrieved through the Input property as binary data.


Remarks

The InputMode property determines how data will be retrieved through the Input property. The data will either be retrieved as string or as binary data in a byte array.

Use comInputModeText for data that uses the ANSI character set. Use comInputModeBinary for all other data such as data that has embedded control characters, Nulls, etc.


OutBufferCount


Returns the number of characters waiting in the transmit buffer. You can also use it to clear the transmit buffer. This property is not available at design time.

Syntax

object.OutBufferCount [ = value ]

The OutBufferCount property syntax has these parts:

Part Description
object An object expression that evaluates to an object in the Applies To list.
value An integer expression specifying the number of characters waiting in the transmit buffer.


Remarks

You can clear the transmit buffer by setting the OutBufferCount property to 0.

Note   Do not confuse the OutBufferCount property with the OutBufferSize property which reflects the total size of the transmit buffer.

Data Type  Integer


OutBufferSize


Sets and returns the size, in bytes, of the transmit buffer.

Syntax

object.OutBufferSize [ = object ]

The OutBufferSize property syntax has these parts:

Part Description
object An object expression that evaluates to an object in the Applies To list.
value An integer expression specifying the size of the transmit buffer.


Remarks

OutBufferSize refers to the total size of the transmit buffer. The default size is 512 bytes. Do not confuse this property with the OutBufferCount which reflects the number of bytes currently waiting in the transmit buffer.

Note   The larger you make the transmit buffer, the less memory you have available to your application. However, if your buffer is too small, you run the risk of overflowing unless you use handshaking. As a general rule, start with a buffer size of 512 bytes. If an overflow error occurs, increase the buffer size to handle your application's transmission rate.

Data Type  Integer


Output


Writes a stream of data to the transmit buffer. This property is not available at design time and is write-only at run time.

Syntax

object.Output [ = value ]

The Output property syntax has these parts:

Part Description
object An object expression that evaluates to an object in the Applies To list.
value A string of characters to write to the transmit buffer.


Remarks

The Output property can transmit text data or binary data. To send text data using the Output property, you must specify a Variant that contains a string. To send binary data, you must pass a Variant which contains a byte array to the Output property.

Normally, if you are sending an ANSI string to an application, you can send it as text data. If you have data that contains embedded control characters, Null characters, etc., then you will want to pass it as binary data.

Data Type  Variant

 

RThreshold


Sets and returns the number of characters to receive before the MSComm control sets the CommEvent property to comEvReceive and generates the OnComm event.

Syntax

object.Rthreshold [ = value ]

The Rthreshold property syntax has these parts:

Part Description
object An object expression that evaluates to an object in the Applies To list.
value An integer expression specifying the number of characters to receive before generating the OnComm event.


Remarks

Setting the RThreshold property to 0 (the default) disables generating the OnComm event when characters are received.

Setting RThreshold to 1, for example, causes the MSComm control to generate the OnComm event every time a single character is placed in the receive buffer.

Data Type  Integer

 

SThreshold


Sets and returns the minimum number of characters allowable in the transmit buffer before the MSComm control sets the CommEvent property to comEvSend and generates the OnComm event.

Syntax

object.SThreshold [ = value ]

The SThreshold property syntax has these parts:

Part Description
object An object expression that evaluates to an object in the Applies To list.
value An integer expression representing the minimum number of characters in the transmit buffer before the OnComm event is generated.


Remarks

Setting the SThreshold property to 0 (the default) disables generating the OnComm event for data transmission events. Setting the SThreshold property to 1 causes the MSComm control to generate the OnComm event when the transmit buffer is completely empty.

If the number of characters in the transmit buffer is less than value, the CommEvent property is set to comEvSend, and the OnComm event is generated. The comEvSend event is only fired once, when the number of characters crosses the SThreshold. For example, if SThreshold equals five, the comEvSend event occurs only when the number of characters drops from five to four in the output queue. If there are never more than SThreshold characters in the output queue, the event is never fired.

Data Type  Integer

 

 

 

 

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值