visual basic serial communication

Here is a very simple VB prog I wrote to test a connection to an Arduino.

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Long
        Dim s As String

        If Not SerialPort1.IsOpen() Then
            SerialPort1.Open()
        End If

        TextBox1.Text = ""
        TextBox1.Refresh()
        Do While i < 50
            SerialPort1.Write("<hello>")
            s = SerialPort1.ReadLine()
            TextBox1.Text = TextBox1.Text + s + vbCrLf
            TextBox1.Refresh()
            i = i + 1
        Loop

    End Sub

    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        If SerialPort1.IsOpen() Then
            SerialPort1.Close()
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.PortName = "Com33"
        SerialPort1.Open()
    End Sub
End Class

Just have a button and testbox plus drag a Serialport object onto the form. You don't need code to set the serial parameteres, it's all in the parameters window.


Rob