1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports
Public Class Form1
Dim myPort As Array 'COM Ports detected on the system will be stored here
Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.
myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available
ComboBox2.Items.Add(9600) 'Populate the cmbBaud Combo box to common baud rates used
ComboBox2.Items.Add(19200)
ComboBox2.Items.Add(38400)
ComboBox2.Items.Add(57600)
ComboBox2.Items.Add(115200)
For i = 0 To UBound(myPort)
ComboBox1.Items.Add(myPort(i))
Next
ComboBox1.Text = ComboBox1.Items.Item(0) 'Set cmbPort text to the first COM port detected
ComboBox2.Text = ComboBox2.Items.Item(0) 'Set cmbBaud text to the first Baud rate on the list
Button2.Enabled = False 'Initially Disconnect Button is Disabled
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.PortName = ComboBox1.Text 'Set SerialPort1 to the selected COM port at startup
SerialPort1.BaudRate = ComboBox2.Text 'Set Baud rate to the selected value on
'Other Serial Port Property
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8 'Open our serial port
SerialPort1.Open()
Button1.Enabled = False 'Disable Connect button
Button2.Enabled = True 'and Enable Disconnect button
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort1.Close() 'Close our Serial Port
Button1.Enabled = True
Button2.Enabled = False
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.PortName = ComboBox1.Text 'pop a message box to user if he is changing ports
Else 'without disconnecting first.
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting()) 'Automatically called every time a data is received at the serialPort
'compares the ID of the creating Thread to the ID of the calling Thread
End Sub
Private Sub ReceivedText(ByVal [text] As String)
If Me.RichTextBox1.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.RichTextBox1.Text &= [text]
End If
End Sub
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.BaudRate = ComboBox2.Text 'pop a message box to user if he is changing baud rate
Else 'without disconnecting first.
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub
End Class |
Partager