0% found this document useful (0 votes)
15 views

Form1 Object Eventargs: 'Lấy Danh Sách Các Cổng Có Trên Máy'

This document contains the code for a Windows Forms application that allows serial communication. It defines event handlers for controls on the form including labels, buttons, text boxes, and combo boxes. The code opens and closes the serial port, writes data to the port, and handles receiving data to display messages in the rich text box. When the form loads it populates the combo box with available serial ports and sets initial button states.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Form1 Object Eventargs: 'Lấy Danh Sách Các Cổng Có Trên Máy'

This document contains the code for a Windows Forms application that allows serial communication. It defines event handlers for controls on the form including labels, buttons, text boxes, and combo boxes. The code opens and closes the serial port, writes data to the port, and handles receiving data to display messages in the rich text box. When the form loads it populates the combo box with available serial ports and sets initial button states.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Public Class Form1

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Label1.Click
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Label2.Click
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
If SerialPort1.IsOpen Then
SerialPort1.Close()
End If
'Ly danh sch cc cng c trn my'
For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
ComboBox1.Items.Add(My.Computer.Ports.SerialPortNames(i))
Next
Button2.Enabled = False
Button3.Enabled = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Try
With SerialPort1
.PortName = ComboBox1.Text
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
End With
SerialPort1.Open()
Label2.Text = " Kt ni ti " & ComboBox1.Text & " thnh cng!"
Button1.Enabled = False
Button2.Enabled = True
Button3.Enabled = True
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
Try
SerialPort1.Close()
Label2.Text = "Ngt kt ni vi " & ComboBox1.Text
Button1.Enabled = True
Button2.Enabled = False
Catch ex As Exception
MsgBox(ex.ToString)

End Try
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button3.Click
Try
If SerialPort1.IsOpen Then
SerialPort1.Write(TextBox1.Text & vbCrLf)
With RichTextBox1
.AppendText("Ti: " & TextBox1.Text & vbCrLf)
.ScrollToCaret()
End With
TextBox1.Text = String.Empty
Else
Label2.Text = "Vui lng chn cng kt ni " & ComboBox1.Text
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try

End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Control.CheckForIllegalCrossThreadCalls = False
With RichTextBox1
.AppendText("Khch: " & SerialPort1.ReadExisting())
.ScrollToCaret()
End With
End Sub

Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As


System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
If SerialPort1.IsOpen Then
SerialPort1.Close()
End If
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles
ComboBox1.SelectedIndexChanged
End Sub
Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles
RichTextBox1.TextChanged
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles
TextBox1.TextChanged
End Sub

Private Sub Label6_Click(sender As Object, e As EventArgs) Handles Label6.Click


End Sub
End Class

You might also like