MSComm DCD line change counter problem
MSComm DCD line change counter problem
MSComm DCD line change counter problem
MSComm DCD line change counter problem
MSComm DCD line change counter problem
MSComm DCD line change counter problem MSComm DCD line change counter problem MSComm DCD line change counter problem MSComm DCD line change counter problem MSComm DCD line change counter problem MSComm DCD line change counter problem MSComm DCD line change counter problem MSComm DCD line change counter problem
MSComm DCD line change counter problem MSComm DCD line change counter problem
MSComm DCD line change counter problem
Go Back  Xtreme Visual Basic Talk > > > MSComm DCD line change counter problem


Reply
 
Thread Tools Display Modes
  #1  
Old 09-01-2011, 03:51 AM
ikaracic ikaracic is offline
Newcomer
 
Join Date: Sep 2011
Posts: 3
Default 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!
Reply With Quote
  #2  
Old 09-04-2011, 01:33 AM
mkaras's Avatar
mkarasMSComm DCD line change counter problem mkaras is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Mar 2004
Location: Beaverton, OR
Posts: 1,874
Default

Hi ikaracic, Welcome to the forum.

Quote:
Originally Posted by ikaracic View Post
I am very new to vb programing so I was wondering if someone could please tell me if this is possible!
It appears that you may also be somewhat new to switches as well. Particularly how the contacts of switches behave when they open and close.

Quote:
Originally Posted by ikaracic View Post
but the problem is that the label1 sometimes show +2 or even +3 after button is pressed only once.
This is totally expected. Switch contacts have a tendancy to bounce for a few milliseconds (that a few thousandths of a second). The computer and its UART logic on the COMM port are able to follow and respond to the switch contact bounce almost as if you were pushing and releasing the switch very very quickly.

You will have to come up with either a hardware of software solution to this problem. I personally believe that the software solution will be easier since you would not have to rebuild your circuit.

The software solution is to use a different technique to detect the switch closure. I show some code I wrote a long while ago that detected a foot switch connected up to CTS input of the COMM port. Similar logic can be used for the CD input or you can move your switch over to the CTS pin.

Code:
Dim CTS_Flag0 As Integer            ' flags showing foot switch was pressed
Dim CTS_Flag1 As Integer


Public Function Comm_Open() As YouPick

  . . .

  Call Comm_RTS(True)				' these output signals provide an active level for the CTS
  Call Comm_DTR(True)               ' through a 4.7K resistor to the CTS input. The switch connects
                                    ' between the CTS pin and GND pin.
  
  
  ' initialize the state of the foot switch input via the CTS line
  CTS_Flag0 = 0
  CTS_Flag1 = 0
 
  . . .

End Function

' This function sets the state of the comm port RTS line true or
' false according to the entry argument. A TRUE state sets the
' line on the RS232 interface to a +V level while a FALSE puts
' the line to the -V level.

Sub Comm_RTS(RTS_State As Boolean)

  MSComm.RTSEnable = RTS_State
  
End Sub

' This function sets the state of the comm port DTR line true or
' false according to the entry argument. A TRUE state sets the
' line on the RS232 interface to a +V level while a FALSE puts
' the line to the -V level.

Sub Comm_DTR(DTR_State As Boolean)

  MSComm.DTREnable = DTR_State

End Sub

' this subroutine receives control when there is an event on the
' MS Comm control. This specifically deals with the CTS input which
' activates when the test box foot switch is activated. This response
' routine will set a global variable flag indicating that the CTS was
' detected pressed. A subsequent timer event will note if the flag is
' set is the CTS still in the same state. If so the CTS is deemed to be
' "debounced" and logic from the timer routine will the action that is 
' meant to happen when the switch has been pressed.

Private Sub MSComm_OnComm()

  Select Case MSComm.CommEvent

    Case comEvCTS   ' Change in the CTS line.

      If MSComm.CTSHolding = False Then
         CTS_Flag0 = 1           ' foot switch seen pressed
      Else
         CTS_Flag0 = 0           ' foot switch not seen pressed
         CTS_Flag1 = 0           ' so clear both of the flags
      End If
      
  End Select

End Sub

' timer response routine that supports the "debounce" of the
' foot switch input on the CTS line. The first notification of
' the foot switch comes from the MSComm event processing routine.

Private Sub Comm_Timer_Timer()

  ' check if to see a "debounced" foot switch has been detected.
  If CTS_Flag0 <> 0 Then
    
    If CTS_Flag1 <> 0 Then
    
	   ' place here the code that is supposed to execute when the switch detection has happened.
      
      CTS_Flag0 = 0
      CTS_Flag1 = 0
    End If

    If MSComm.CTSHolding = False Then
      CTS_Flag1 = 1
    End If
    
  End If
  
End Sub
Michael Karas
Reply With Quote
  #3  
Old 09-05-2011, 03:14 AM
ikaracic ikaracic is offline
Newcomer
 
Join Date: Sep 2011
Posts: 3
Default

Thank You for reply.
That solved my 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.
Some code, please!
Reply With Quote
  #4  
Old 09-21-2011, 01:20 AM
ikaracic ikaracic is offline
Newcomer
 
Join Date: Sep 2011
Posts: 3
Default

Anyone knows?
Reply With Quote
  #5  
Old 09-21-2011, 11:57 AM
Cerian Knight's Avatar
Cerian KnightMSComm DCD line change counter problem Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,191
Default

This should help:
Code:
'Setting RThreshold to 1, for example, causes the MSComm control to generate
'the OnComm event every time a single character is placed in the receive buffer.
Private Sub MSComm1_OnComm()
  Select Case MSComm1.CommEvent 'many different external events trigger OnComm
    Case comEvRing   ' Change in the Ring Indicator.
            MsgBox "Ring Indicator (RI) changed state"
  End Select
End Sub
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
MSComm DCD line change counter problem
MSComm DCD line change counter problem
MSComm DCD line change counter problem MSComm DCD line change counter problem
MSComm DCD line change counter problem
MSComm DCD line change counter problem
MSComm DCD line change counter problem MSComm DCD line change counter problem MSComm DCD line change counter problem MSComm DCD line change counter problem MSComm DCD line change counter problem MSComm DCD line change counter problem MSComm DCD line change counter problem
MSComm DCD line change counter problem
MSComm DCD line change counter problem
 
MSComm DCD line change counter problem
MSComm DCD line change counter problem
 
-->