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.
Setting | 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. |
Comments