Public Class Form1
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Console.WriteLine("key hit" + e.KeyChar)
SerialPort1.Write(e.KeyChar)
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'Console.WriteLine("serial port receive" + SerialPort1.ReadExisting())
'Console.WriteLine(SerialPort1.ReadExisting())
TextBox1.AppendText(SerialPort1.ReadExisting())
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim port As Integer = 0
Dim COMmenuitem As ToolStripMenuItem = MenuStrip1.Items.Item(1)
' go thru COM ports adding them to the list of available ports
For port = 0 To 20
'Console.WriteLine("form1 load")
SerialPort1.PortName = "COM" + port.ToString()
Try
SerialPort1.Open() ' attempt to open serial port
TextBox1.AppendText("Found " + SerialPort1.PortName + vbNewLine)
Console.WriteLine("serial port open OK {0}", SerialPort1.PortName)
SerialPort1.Close() ' open OK, close it
Dim item As ToolStripMenuItem = New ToolStripMenuItem(SerialPort1.PortName) ' create a new menu item
AddHandler item.Click, AddressOf Me.COMToolStripMenuItem_Click ' add event handler
COMmenuitem.DropDownItems.Add(item) ' add to the list of COM ports
Catch ex As Exception
Console.WriteLine("serial port open fail {0}", SerialPort1.PortName)
End Try
Next
End Sub
Private Sub COMToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Console.WriteLine("item selected {0} {1}", e.ToString(), sender.ToString())
SerialPort1.Close()
SerialPort1.PortName = sender.ToString()
SerialPort1.Open()
TextBox1.AppendText("Opened Port " + sender.ToString() + " baud rate " + SerialPort1.BaudRate.ToString() + vbNewLine)
Text = "Port " + sender.ToString() + " baud rate " + SerialPort1.BaudRate.ToString()
End Sub
Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click, ToolStripMenuItem9.Click, ToolStripMenuItem8.Click, ToolStripMenuItem7.Click, ToolStripMenuItem6.Click, ToolStripMenuItem5.Click, ToolStripMenuItem4.Click, ToolStripMenuItem3.Click
Console.WriteLine("item selected {0} {1}", e.ToString(), sender.ToString())
SerialPort1.BaudRate = sender.ToString()
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Close()
End Sub
End Class