Jumat, 22 November 2013

Belajar interface serial menggunakan Visual Basic .Net (2012)

Ok, langsung saja Sy akan share bagaimana cara komunikasi menggunakan serial port dengan Visual Basic 2012. Codingnya Sy buat dengan mengacu dari beberapa referensi yg Sy peroleh, dengan modifikasi seperlunya.

Program ini Sy buat karena Sy ingin komunikasikan Arduino dengan VB 2012.

Berikut Contoh desain formnya ya:




Public Class Form1

    Dim readBuffer As String

    Private Sub SendBtn_Click(sender As Object, e As EventArgs) Handles SendBtn.Click
        Try
            SerialPort1.Write(TextBox1.Text + Environment.NewLine)

            TextBox3.AppendText(">" + TextBox1.Text + Environment.NewLine)
            TextBox3.ScrollToCaret()

            TextBox1.Text = String.Empty

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim portName() As String
        Dim i As Integer

        Try
            portName = System.IO.Ports.SerialPort.GetPortNames()
            For i = 0 To i >= (portName.Length)
                ComList.Items.Add(portName(i))
            Next i

            ComList.SelectedText = ComList.Items(0)
            disBtn.Enabled = False
        Catch ex As Exception
            MsgBox("Belum Ada Serial Port Aktif, Silahkan Cek Koneksi Serial Anda")
        End Try
    Private Sub ConBtn_Click(sender As Object, e As EventArgs) Handles ConBtn.Click
        If SerialPort1.IsOpen Then
            SerialPort1.Close()
        End If

        Try
            With SerialPort1
                .PortName = ComList.Text
                .BaudRate = 9600
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
                .Open()
            End With
            Label1.Text = ComList.Text + " Connected"
            ConBtn.Enabled = False
            disBtn.Enabled = True

        Catch ex As Exception
            MsgBox(ex.ToString)

        End Try
    End Sub

    Private Sub disBtn_Click(sender As Object, e As EventArgs) Handles disBtn.Click
        Try
            SerialPort1.DiscardInBuffer() 'mengosongkan input buffer
            SerialPort1.Close()
            ConBtn.Enabled = True
            disBtn.Enabled = False
            Label1.Text = "  Disconnected"
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    ''' <summary>
    ''' async read on secondary thread
    ''' </summary>
    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, _
                                         ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
                                         Handles SerialPort1.DataReceived
        If SerialPort1.IsOpen Then
            Try
                readBuffer = SerialPort1.ReadLine()
                'data to UI thread
                Me.Invoke(New EventHandler(AddressOf DoUpdate))
            Catch ex As Exception
                MsgBox("read " & ex.Message)
            End Try
        End If
    End Sub

    ''' <summary>
    ''' update received string in UI
    ''' ini subrutine tambahan
    ''' </summary>

    Public Sub DoUpdate(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim temp As Decimal
        temp = CInt(readBuffer) / 1024 * 5 * 100
        TextBox2.Text = Int(temp)
        TextBox4.Text = CInt(readBuffer)

    End Sub

End Class


Jangan lupa dihubungkan dulu port serialnya dengan mirkrokontrol sehingga akan terdetek port yang aktif. Untuk kode mikrokontrolernya Sy menggunakan Arduino. Bisa pakai kode di example nya. Untuk komunikasi serial.

Selamat berkreasi :)

Sumber referensi:
http://students.netindonesia.net/blogs/sapikuda/archive/2012/07/25/membuat-program-koneksi-ke-plc-menggunakan-c.aspx

http://code.msdn.microsoft.com/windowsdesktop/SerialPort-Sample-in-VBNET-fb040fb2

http://msdn.microsoft.com/en-us/library/vstudio/s2dy91zy.aspx

Tidak ada komentar:

Posting Komentar

Selamat berinovasi :D Salam berbagi..

Cara mengetahui ip address raspberry atau perangkat lain yg terhubung pada wifi yg sama

1. Install nmap [jika belum ada]: sudo apt install nmap 2. Cek ip address komputer (yg akses ke wifi yang sama): ip addr misal hasilnya 192....