pertama kita siapkan objectnya,apa saja object yang digunakan ??
1. Command Button
2. RichTextBox
3. SerialPort
4. Combo Box
udah tau kan carinya dimana ?? yup..benar sekali,ada di Object Box disebelah kiri ituuuu..hehehehe..untuk Propertiesnya biarkan standard saja..tidak ada yang perlu digantikan..
Next Step..
kalo kalian Double Click di Salah satu objecnya,nanti kalian akan melihat Coding Form ( bahasa ane sendiri seh..hehehe.. ) nah dari sanalah perintah - perintah kita tulisakan..oke..mana Codingnya ?? ini dia :
Public Class Form1
Dim comPorts As Array ‘menampung Port ke dalam array
Dim rxBuff As String ‘Buffer untuk menerima data
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
‘Saat Form load
‘Menampilkan PORT ke dalam Combo Box
comPorts = IO.Ports.SerialPort.GetPortNames()
For i = 0 To UBound(comPorts)
ComboBox1.Items.Add(comPorts(i))
Next
‘Set ComboBox1 text untuk menampilkan port yg paling pertama
ComboBox1.Text = ComboBox1.Items.Item(0)
SerialPort1.PortName = ComboBox1.Text
‘Set Attribut port
SerialPort1.BaudRate = 19200
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8
TextBox1.Enabled = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
‘saat button di click..buffer menjadi hilang
rxBuff = “”
‘If port tertutup,maka langsung dibuka
If SerialPort1.IsOpen = False Then SerialPort1.Open()
‘Tulis data ke dalap Port
SerialPort1.Write(TextBox1.Text & vbCr)
‘Pause untuk 800ms
System.Threading.Thread.Sleep(800)
‘saat port terbuka,langsung di tutup
If SerialPort1.IsOpen = True Then SerialPort1.Close()
‘saat buffer tidak ada data maka end sub
If rxBuff = “” Then GoTo ends
‘tampilakn data ke RichTextBOx
RichTextBox1.Text = rxBuff
ends:
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.PortName = ComboBox1.Text
Else : MsgBox(“Operation only valid when port is closed.”, MsgBoxStyle.Exclamation, “Error”)
End If
RichTextBox1.Clear()
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
System.Threading.Thread.Sleep(300)
rxBuff = (SerialPort1.ReadExisting)
End Sub
End Class
No comments:
Post a Comment