Professional Documents
Culture Documents
Hướng Dẫn Giao Tiếp Serial Port Sử Dụng VB
Hướng Dẫn Giao Tiếp Serial Port Sử Dụng VB
Sonbui1903
TUT 1
Sonbui1903
TUT 1
Sonbui1903
TUT 1
Sonbui1903
II: Cc v d
Cc cu lnh s dng vi Serialport c th xem ti:
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx#Y3078
1: Chat gia hai my tnh s dng cng Com n gin
1.1: Xy dng giao din
M mt d n mi v ko th cc cng c cn thit trong tool box
Clip hng dn 1
Clip hng dn chnh sa cc i tng
Giao din chng trnh sau khi lm xong
Cng c
Form
Name
Form1
ComboBox
Button
Button
Button
Label
Label
Label
ComboBox 1
Button 1
Button 2
Button 3
Label 1
Label 2
Label 6
Text
Demo Chat qua cng
Com dng VBSonbui.haui@gmail.com
Kt ni
Ngt kt ni
Gi
Chn Cng
Trng thi kt ni
Metechvn.com
TUT 1
Sonbui1903
TextBox1
RichTextBox
SerialPort
TextBox
RichTextBox1
SerialPort1
1.2: Lp trnh
lp trnh cho cc i tng bn double click vo cc i tng
1.2.1: ng/ m cng khi bt hoc tt chng trnh
Khi m chng trnh
Button2.Enabled = False
Button3.Enabled = False
End Sub
TUT 1
Sonbui1903
1.2.2: Kt ni v tt kt ni
Khi bm vo kt ni
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
TUT 1
Sonbui1903
Khi bm vo tt kt ni:
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
End Sub
Nhn:
Chn s kin khi nhn
Chn serialport 1 -> DataReceived
TUT 1
Sonbui1903
Code:
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("Friend: " & SerialPort1.ReadExisting())
.ScrollToCaret()
End With
End Sub
1.2.4: Chy th
TUT 1
Sonbui1903
TUT 1
Sonbui1903