• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

How to use an external hardware button trigger an event in windows

i have a physical, hardware button mounted on the outside of our business, next to our front door.  is there any way i can connect it to a windows computer so that every time it is pressed it appends the date and time to a text file?  i can do all the programming either via batch file or visual basic but i do not know where to start interfacing a physical switch to a windows computer.  is there some way to do it via serial, usb, parallel?  any help would be great.  thanks!
0
jasdak
Asked:
jasdak
  • 2
  • 2
1 Solution
 
TreyHCommented:
You could use the MSComm control and raise/lower DTR to fire an event. It's just a matter of the button shorting the correct pins on the serial port when the button is pressed. If interested I can give you the pinouts.
0
 
Jaime OlivaresCommented:
since the distance is long, maybe a direct switching is not recommended, you can use a relay to make switching phisically near to the computer.
0
 
jasdakAuthor Commented:
treyH,
i would like the pinouts and any url's that you know that may give me some tips, syntax, code snippets, etc for implementing this solution.  THANKS!
0
 
TreyHCommented:
Here are the pinouts for a 9 pin serial port:

Pin - Label, Signal Name, Signal Type
----------------------------------------
1 - CD, Carrier Detect, Control
2 - RD, Received Data, Data
3 - TD, Transmitted Data, Data
4 - DTR, Data Terminal Ready, Control
5 - GND, Signal Ground, Ground
6 - DSR, Data Set Ready, Control
7 - RTS, Request to Send, Control
8 - CTS, Clear to Send, Control
9 - RI, Ring indicator, Control

This is some very rough code to get you started.

-Start a standard exe project (VB6)
-Reference the 'Microsoft Comm Control 6.0' component for your VB Project   (MSComm1) and place it on a form.
-Place a text box (text1) on the form

-Place this code in the Form_Load Sub:
Private Sub Form_Load()
    With MSComm1
        .CommPort = 1 '<-- change this to the com port you are using.
      .DTREnable = True
        .PortOpen = True
    End With
End Sub

Place this code in the MSComm1_OnComm Sub:
Private Sub MSComm1_OnComm()
    Select Case MSComm1.CommEvent
   ' Handle each event or error by placing
   ' code below each case statement

   ' Errors
      Case comEventBreak   ' A Break was received.
      Case comEventFrame   ' Framing Error
      Case comEventOverrun   ' Data Lost.
      Case comEventRxOver   ' Receive buffer overflow.
      Case comEventRxParity   ' Parity Error.
      Case comEventTxFull   ' Transmit buffer full.
      Case comEventDCB   ' Unexpected error retrieving DCB]

   ' Events
      Case comEvCD   ' Change in the CD line.
      Case comEvCTS   ' Change in the CTS line.
      Case comEvDSR   ' Change in the DSR line.
        '****************************
      'This is where you would place code for when the DSR Line changes
      Text1.Text = MSComm1.DSRHolding
      
      '****************************
      Case comEvRing   ' Change in the Ring Indicator.
      Case comEvReceive   ' Received RThreshold # of
                        ' chars.
      Case comEvSend   ' There are SThreshold number of
                     ' characters in the transmit
                     ' buffer.
      Case comEvEOF   ' An EOF charater was found in
                     ' the input stream
   End Select

End Sub

Now,if you connect pins 4 and 6 on the serial port the MSComm1.DSRHolding should be 'True' and the text box should display 'True'. When you disconnect them, DSRHolding should be 'False' and the text box should change to false.

If the button is not to far away from the computer, you could simply use it to connect pins 4 and 6 to raise the DTR and fire whatever code you wish. If it's very far away the voltage will not carry. To get by this you could keep pins 4 and 6 connected so DSRHolding is always 'True', then use the button to ground either pin 4 or 6 to pin 5(ground) which would lower DTR and change DSRHolding to 'False'.

One other pitfall is that due to the button contacts, the MSComm1_OnComm event will probably fire multiple times with just one push of the button so you will have to compensate for that within code. I usually just use a module level variable and a Timer control to limit the 'OnComm' code to ~1 sec intervals.
Here's an example:

-Place a timer control on your form (Timer1) and set the Enabled property to 'False'
 and the interval to 1000.
-Declare a module level boolean variable bFlag
-Set bFlag to True in the Form_Load sub.

Dim bFlag   As Boolean

Private Sub Form_Load()
   
    bFlag = True
End Sub

Private Sub Timer1_Timer()
    Timer1.Enabled = False
    bFlag = True
End Sub
Private Sub MSComm1_OnComm()
    Select Case MSComm1.CommEvent
   
      Case comEvDSR   ' Change in the DSR line.
      'Now you can use the bFlag variable to limit how often the code executes.
        If bFlag Then
            Text1.Text = MSComm1.DSRHolding
            bFlag = False
            Timer1.Enabled = True
            End If
   End Select

End Sub
0
 
jasdakAuthor Commented:
Superb, Excellent, Above and Beyond!   THANK YOU!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now