CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2011
    Posts
    5

    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!

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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.
    Last edited by WoF; September 1st, 2011 at 07:36 AM.

  3. #3
    Join Date
    Sep 2011
    Posts
    5

    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

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: MSComm DCD line change counter problem

    If memory serves a ring condition triggers the receive event with the word RING
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: MSComm DCD line change counter problem

    Quote Originally Posted by DataMiser View Post
    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...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  6. #6
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

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

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: MSComm DCD line change counter problem

    Quote Originally Posted by Eri523 View Post
    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.
    Always use [code][/code] tags when posting code.

  8. #8
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: MSComm DCD line change counter problem

    Quote Originally Posted by WoF View Post
    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)
    Last edited by Eri523; September 2nd, 2011 at 06:46 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  9. #9
    Join Date
    Sep 2011
    Posts
    5

    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.

  10. #10
    Join Date
    Sep 2011
    Posts
    5

    Re: MSComm DCD line change counter problem

    Anybody?
    Some code please.

  11. #11
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: MSComm DCD line change counter problem

    Have you tried this?

    Code:
    Case comEvRing ' Change in the Ring Indicator.
    Always use [code][/code] tags when posting code.

  12. #12
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured