MSComm DCD line change counter problem
Hello!
I am working on a little program that counts changes in the serial port CD line.
I have connected pin 4 (DTR) and pin 1 (DCD) to a button and I am trying to make a program that counts how many times button is pressed. Here is the code:
Code:
Private Sub Form_Load()
With MSComm1
.CommPort = 1
.Settings = "9600,n,8,1"
.SThreshold = 1
.RThreshold = 1
.Handshaking = comXOnXoff
.PortOpen = True
End With
End Sub
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvCD
If MSComm1.CDHolding = True Then
Label1.Caption = Val(Label1.Caption) + 1
End If
End Select
End Sub
When button is pressed the label1 should show +1, but the problem is that the label1 sometimes show +2 or even +3 after button is pressed only once. I am very new to vb programing so I was wondering if someone could please tell me if this is possible, and if it is some code would be very helpful. Thank You in advance!
Re: MSComm DCD line change counter problem
The answer is quite easy to give: Your button is "bouncing" as we say.
It has a mechanical contact. When contacts are closing they produce a series of open-close-open-close intervals within the first miliseconds before they completely close.
Programmers live in the world of very short time periods, because electronics detect these very short puls trains. The MSComm detects not all but some of these, producing multiple events.
What you can do is "debounce" the button.
With the first event you start a timer of say 100ms and do not react on further comEvCD events while the timer is running.
In fact after the first pule has detected, you wait a period of 100 (or better 200) ms, letting the contacts time to close.
Another way would be to connect a capacitor of about 5-10uF parallel to the switch to debounce it electrically.
Re: MSComm DCD line change counter problem
Thank You Mate!!
That helped!
I just need a little bit more of Your knowledge. Can You please write me the code to do the same thing with ringing indicator (pin 9)? There is no RIHolding property.
Best regards
Re: MSComm DCD line change counter problem
If memory serves a ring condition triggers the receive event with the word RING
Re: MSComm DCD line change counter problem
Quote:
Originally Posted by
DataMiser
If memory serves a ring condition triggers the receive event with the word RING
I'm afraid the RING is sent by the modem as character sequence when it detects a ring event. So unless MSComm generates another one on its own I doubt that helps...
Re: MSComm DCD line change counter problem
I don't understand what you actually want.
The Ring is no hardware line you can activate with a pushbutton...
Re: MSComm DCD line change counter problem
Quote:
Originally Posted by
Eri523
I'm afraid the RING is sent by the modem as character sequence when it detects a ring event. So unless MSComm generates another one on its own I doubt that helps...
It has been a while, makes sense though as there would be no ring unless connected to a phone line via a modem. :blush:
Re: MSComm DCD line change counter problem
Quote:
Originally Posted by
WoF
The Ring is no hardware line you can activate with a pushbutton...
Really? AFAIK it is. I had to look that up though, since I wasn't sure whether it might have been dropped in the transistion to the now common DE-9 connector but it seems to still be there on pin 9. (See http://en.wikipedia.org/wiki/Serial_port#Pinouts)
Re: MSComm DCD line change counter problem
I found this site:
http://www.danielandrade.net/2005/11...inamp-control/
This guy used four pins to control winamp, DCD, DSR, CTS and RI.
DCD, DSR, CTS have the holding property, but I don't know what to do with RI.
Re: MSComm DCD line change counter problem
Anybody?
Some code please.
Re: MSComm DCD line change counter problem
Have you tried this?
Code:
Case comEvRing ' Change in the Ring Indicator.
Re: MSComm DCD line change counter problem
Well, yes Eri, you are right. the RING is a hardware line, being able to be activated with a pushbutton.