Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 156,210 times

Contents

Related Categories

Com Ports and the MSComm Control - Receiving Data

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.

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [21] Com Ports and the MSComm Control

    Posted by raxe on 10 Jul 2006

    Hi. I need to comunicate with a money colector using VB 6.0 Pro. I had installed the MsComm control.


    How can i send this comands to the  money collector and how i can read the answers ...

  • Great Work!!!!

    Posted by zeezee on 09 Feb 2006

    First of all, I have to thank you for giving out this lovely tutorial:) This is a great help for folks like who has no idea in using the Ms comm Control, Thank you very much and keep up the good work....

  • Posted by vimal_up on 12 Dec 2005

    Dear Friend Virag,
    I have done lots of R&D in this matter, and what i found is, it is best option to go with third party controls & components to work with these type of apps. So search web u will ha...

  • how to boild IVR system usding VB

    Posted by viragonnet on 11 Dec 2005

    dear friend,

    i m looking for new tips for controling hardware using visual basic code.
    i also want to build interactive voice response system by which i can control my computer from anywhere. just...

  • IVR Application with MS Comm Control

    Posted by vimal_up on 15 Oct 2005

    Is it possible to develop simple IVR Apps with ms comm control in which step of wave file prompts guide the caller and he/she can choose option from phone buttons. By detecting caller choice programme...