Solved

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

Posted on 2007-11-28
5
224 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
shortcuts on desktops 14 61
SLMGR Switches Are Not Working On KMS Host 3 69
VBA Shell can't Find Word document 11 76
Added a column screws up code 5 49
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now