Good Day,
I have an application that will send text out of a serial port if i hold down the "1" key for 2 seconds. I am currently just turning a chime on and off but want to expand this to the ability to turn my amplifier on/off etc. I want this to be running in the background but to respond at any time by just hitting a certain key. The program works great one time when i execute, but goes into hide and i never see it again. Any suggestions are appreciated.
Public Class frmKeyCatcher
Dim WithEvents serialPort As New IO.Ports.Serial Port
Private Sub DataReceived( _
ByVal sender As Object, _
ByVal e As System.IO.Ports .SerialDataRece ivedEventArgs) _
Handles serialPort.Data Received
txtDataReceived .Invoke(New myDelegate(Addr essOf updateTextBox), _
New Object() {})
End Sub
Public Delegate Sub myDelegate()
Public Sub updateTextBox()
With txtDataReceived
.SelectionColor = Color.Red
.AppendText(ser ialPort.ReadExi sting)
.ScrollToCaret( )
End With
End Sub
Private Sub frmKeyCatcher_F ormClosing(ByVa l sender As Object, ByVal e As System.Windows. Forms.FormClosi ngEventArgs) Handles Me.FormClosing
serialPort.Clos e()
End Sub
Private Sub frmKeyCatcher_K eyDown(ByVal sender As Object, ByVal e As System.Windows. Forms.KeyEventA rgs) Handles Me.KeyDown
If e.KeyCode = 49 Then
Timer1.Start()
End If
End Sub
Private Sub frmKeyCatcher_K eyUp(ByVal sender As Object, ByVal e As System.Windows. Forms.KeyEventA rgs) Handles Me.KeyUp
If e.KeyCode = 49 Then
Timer1.Stop()
End If
End Sub
Private Sub Timer1_Tick(ByV al sender As Object, ByVal e As System.EventArg s) Handles Timer1.Tick
Me.Show()
Me.BackColor = Color.LightGray
If serialPort.IsOp en Then
serialPort.Clos e()
End If
Try
With serialPort
.PortName = "COM3"
.BaudRate = 96000
.Parity = IO.Ports.Parity .None
.DataBits = 8
.StopBits = IO.Ports.StopBi ts.One
End With
serialPort.Open ()
Timer4.Start()
Catch ex As Exception
MsgBox(ex.ToStr ing)
End Try
End Sub
Private Sub Timer2_Tick(ByV al sender As Object, ByVal e As System.EventArg s) Handles Timer2.Tick
Me.Hide()
Timer2.Stop()
End Sub
Private Sub Timer4_Tick(ByV al sender As Object, ByVal e As System.EventArg s) Handles Timer4.Tick
Try
serialPort.Writ e("C")
With txtDataReceived
.SelectionColor = Color.Black
.AppendText("C" & vbCrLf)
.ScrollToCaret( )
End With
Me.BackColor = Color.white
Catch ex As Exception
End Try
Timer4.Stop()
Timer2.Start()
End Sub
End Class