Berikut ini adalah komponen-komponen yang digunakan :
- ComboBox (2 buah)
- Button (2 buah)
- Label (3 buah)
- TextBox (1 buah)
- SerialPort (1 buah)
Buat tampilan program menjadi seperti gambar dibawah ini.
Berikut ini adalah program lengkapnya.
Public Class Form1Dim myPort As Array
Delegate Sub SetTextCallback(ByVal As String)Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadmyPort = IO.Ports.SerialPort.GetPortNames()ComboBox2.Items.Add(9600)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))NextComboBox1.Text = ComboBox1.Items.Item(0)ComboBox2.Text = ComboBox2.Items.Item(0)Button1.Enabled = TrueButton2.Enabled = FalseEnd SubPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickSerialPort1.PortName = ComboBox1.TextSerialPort1.BaudRate = ComboBox2.TextSerialPort1.Open()Button1.Enabled = FalseButton2.Enabled = TrueEnd SubPrivate Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.ClickSerialPort1.Close()Button1.Enabled = TrueButton2.Enabled = FalseEnd SubPrivate Sub ReceivedText(ByVal As String) ‘compares the ID of the creating Thread to the ID of the calling ThreadIf Me.TextBox1.InvokeRequired ThenDim x As New SetTextCallback(AddressOf ReceivedText)Me.Invoke(x, New Object() {(text)})Else‘Me.TextBox2.Text = ” “Me.TextBox1.Text =End If End SubPrivate Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived‘Automatically called every time a data is received at the serialPortReceivedText(SerialPort1.ReadExisting())End SubEnd Class
Untuk menguji program, kita dapat menggunakan Program Pengirim yang sudah dibahas pada artikel sebelumnya atau bisa juga menggunakan Virtual Terminal.
No comments:
Post a Comment