Solved

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

Posted on 2007-11-28
5
233 Views
Last Modified: 2010-04-21
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
Comment
Question by:jasdak
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:TreyH
ID: 20368026
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20368250
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
 

Author Comment

by:jasdak
ID: 20369056
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
 
LVL 11

Accepted Solution

by:
TreyH earned 500 total points
ID: 20371048
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
 

Author Closing Comment

by:jasdak
ID: 31411490
Superb, Excellent, Above and Beyond!   THANK YOU!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Sometimes people don't understand why download speed shows differently for Windows than Linux.Specially, this article covers and shows the solution for throughput difference for Windows than a Linux machine. For this, I arranged a test scenario.I…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question