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.SerialPort
Private Sub DataReceived( _
ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles serialPort.DataReceived
txtDataReceived.Invoke(New myDelegate(AddressOf updateTextBox), _
New Object() {})
End Sub
Public Delegate Sub myDelegate()
Public Sub updateTextBox()
With txtDataReceived
.SelectionColor = Color.Red
.AppendText(serialPort.ReadExisting)
.ScrollToCaret()
End With
End Sub
Private Sub frmKeyCatcher_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
serialPort.Close()
End Sub
Private Sub frmKeyCatcher_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = 49 Then
Timer1.Start()
End If
End Sub
Private Sub frmKeyCatcher_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = 49 Then
Timer1.Stop()
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.Show()
Me.BackColor = Color.LightGray
If serialPort.IsOpen Then
serialPort.Close()
End If
Try
With serialPort
.PortName = "COM3"
.BaudRate = 96000
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
End With
serialPort.Open()
Timer4.Start()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Me.Hide()
Timer2.Stop()
End Sub
Private Sub Timer4_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer4.Tick
Try
serialPort.Write("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