VBasic receiving serial data from Arduino?

greets,

I’m trying to find out how to receive serial data from an Arduino, using Visual Studio 2013, but the stuff I found on the net is confusing.

I already know how to send data from Visual Studio, i.e. vbasic windows form, but don’t know how to receive data.

Do I need an infinite loop to constantly read the serial port?

thanks

I found a tutorial showing how to create a console app in VBasic. It works but I can’t figure out how to receive and show data in raw binary format, instead I get weird characters, event though I changed the encoding to 1252, which works for sending data in binary format.

here is the code:

Module Module1

    Private WithEvents SerialPort1 As New System.IO.Ports.SerialPort("COM3")

    Sub main()
        SerialPort1.BaudRate = 31250
        SerialPort1.DataBits = 8
        SerialPort1.Encoding = System.Text.Encoding.GetEncoding(1252)
        SerialPort1.Open()
        Console.ReadKey()
        SerialPort1.Close()
    End Sub

    Private Sub SerialPort1_DataReceived(sender As Object, e As    IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        Dim msgString As String = SerialPort1.ReadExisting
        Console.Write(msgString)

    End Sub

End Module
        SerialPort1.Open()
        Console.ReadKey()
        SerialPort1.Close()

Open the port, resetting the Arduino.
Expect to read something immediately.
Close the port, resetting the Arduino again.

And, you got nothing, right?

Are you (still) surprised?

    Private Sub SerialPort1_DataReceived(sender As Object, e As    IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        Dim msgString As String = SerialPort1.ReadExisting
        Console.Write(msgString)

    End Sub

You've closed the port. How can you expect to read anything?

You claim to be sending binary data. Why would you be storing binary data in a string?

hi,

actually Console.ReadKey() leaves the console window open and will only close the Serial port if a key is pressed on the keyboard which will also quit the console window.

in any case I experimented a lot and finally got it to work, here's the code:

Module Module1

    Private WithEvents SerialPort1 As New System.IO.Ports.SerialPort("COM3")

    Sub main()

        SerialPort1.BaudRate = 31250
        SerialPort1.DataBits = 8
        SerialPort1.Encoding = System.Text.Encoding.GetEncoding(1252)

        SerialPort1.Open()
        Console.ReadKey()
        SerialPort1.Close()

    End Sub



    Private Sub SerialPort1_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        While SerialPort1.BytesToRead > 0
            Dim byteReceived As Byte = SerialPort1.ReadByte
            Console.Write(" " & Hex(byteReceived))
        End While

    End Sub
End Module