Com Ports and the MSComm Control

Receiving Data

To receive data, you need to check the Input property. However, the problem is, knowing when the data is there. This is because the data only stays in the Input property for a few seconds. I find that the OnComm event is the best way to do this. In order for the OnComm event to fire, you need to set RThreshold and SThreshold to 1. So, to get the result of the command we sent the modem in the last section, we need the following code to be inserted into the OnComm event:

Select Case MSComm1.CommEvent
' Event messages.
Case comEvReceive
    txtOutput.Text = txtOutput.Text & StrConv(MSComm1.Input, vbUnicode)
EndSelect

This checks to see what event has just occured. If it was a receive event, we add the contents of the Input property to the end of the txtOutput textbox. When we send the command above, we should receive something like

ATDT01992245365

BUSY

The first line is our command echoed back at us. The next is the result of the dial, in this case Busy (because we were dialing our own number). On my modem, you can turn the command echo off by sending ATE0. For a list of other commands, click here

You can also use the OnComm event to monitor other communication events or errors. Because communications (especially over telephone lines) can be unpredictable, trapping these events and errors allows you to respond to them appropriately.

The following table lists the communication events that will trigger the OnComm event. The values will then be written to the CommEvent property.

Constant Value Description
comEvSend 1 There are fewer than SThreshold number of characters in the transmit buffer.
comEvReceive 2 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 3 Change in Clear To Send line.
comEvDSR 4 Change in Data Set Ready line. This event is only fired when DSR changes from 1 to 0.
comEvCD 5 Change in Carrier Detect line.
comEvRing 6 Ring detected. Some UARTs (universal asynchronous receiver-transmitters) may not support this event.
comEvEOF 7 End Of File (ASCII character 26) character received.
 

The OnComm event is also triggered, and a value is written to the CommEvent property, when the following errors are encountered.

SettingValueDescription
comEventBreak1001A Break signal was received.
comEventFrame1004Framing Error. The hardware detected a framing error.
comEventOverrun1006Port Overrun. A character was not read from the hardware before the next character arrived and was lost.
comEventRxOver1008Receive Buffer Overflow. There is no room in the receive buffer.
comEventRxParity1009Parity Error. The hardware detected a parity error.
comEventTxFull1010Transmit Buffer Full. The transmit buffer was full while trying to queue a character.
comEventDCB1011Unexpected error retrieving Device Control Block (DCB) for the port.

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.

“If Java had true garbage collection, most programs would delete themselves upon execution.” - Robert Sewell