Com Ports and the MSComm Control

MSComm Control Settings

There is quite a large number of settings for the Comm Control, none of which are really explained in the Visual Basic help. 

Settings

The first property that you can set is the Settings property(!). The Settings property allows you to specify the baud rate, parity, and the number of data bits and stop bits. By default, the baud rate (which is the maximum speed of the COM port) is set at 9600. The parity setting is for data validation. It is commonly not used, and set to "N". The data bits setting specifies the number of bits that represent a chunk of data. The stop bit indicates when a chunk of data has been received.

The settings string takes the following format.

"BBBB,P,D,S"

Where BBBB is the baud rate, P is the parity, D is the number of data bits, and S is the number of stop bits. The default value of value is:

"9600,N,8,1"

If the settings property is invalid when you attempt to open the port, an error 380 (Invalid property value) occurs.

Click here for the valid settings for each of these.

Buffer Memory Allocation

The InBufferSize and OutBufferSize properties specify how much memory is allocated to the receive and transmit buffers (which store the in-coming and out-going data). The larger you make the number, the less memory you have available to your application. If, however, your buffer is too small, you run the risk of overflowing the buffer unless you use handshaking.However, given the amount of memory available to most PCs at this time, buffer memory allocation is less crucial because you have more resources available. In other words, you can set the buffer values higher without affecting the performance of your application.

The RThreshold and SThreshold Properties

The RThreshold and SThreshold properties set or return the number of characters that are received into the receive and transmit buffers before the OnComm event is fired. The OnComm event is used to monitor and respond to changes in communications states. Setting the value for each property to 0 (which is the default value) prevents the OnComm event from firing. Setting the value to something other than 0 (1, for instance) causes the OnComm event to be fired every time a single character is received into either buffer.

The InputLen and EOFEnable Properties

Setting the InputLen property to 0 causes the Communications control to read the entire contents of the receive buffer when the Input property is used. When reading data from a machine whose output is formatted in fixed-length blocks of data, the value of this property can be set appropriately.

The EOFEnable property is used to indicate when an End of File (EOF) character is found during data input. Setting this to True causes data input to stop and the OnComm event to fire to inform you that this condition has occurred.

The InputMode Property

The InputMode property determines how data will be retrieved through the Input property. This data will either be a string (plain text), or as a byte array (binary data). Use comInputModeText for data that uses the ANSI character set (plain text). Use comInputModeBinary for all other data such as data that has embedded control characters, Nulls, etc.

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves” - Alan Kay