Solved

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

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

If you have done a reformat of your hard drive and proceeded to do a successful Windows XP installation, you may notice that a choice between two operating systems when you start up the machine. Here is how to get rid of this: Click Start Clic…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

758 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

21 Experts available now in Live!

Get 1:1 Help Now