Can't display serial port data in vb.net

I want to display serial port data which sent by arduino in vb.net desktop applications,but failed. :( Please help me. The data display correctly in the Arduino serial monitor like "-5.46/5.78/67.98" etc

I have used the below code to send data to serail port by arduino:

    //Serial.print("Roll: ");
    Serial.print(rollAngle*RadianToDegree);
    Serial.print("/");
    //Serial.print(" Pitch: ");
    Serial.print(pitchAngle*RadianToDegree);
    Serial.print("/");
    //Serial.print(" Yaw: ");
    Serial.println(yawAngle*RadianToDegree);

below is my vb.net code:

Imports System.Text

Public Class Form1

    Dim buffB(50) As Byte  
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        SerialPort1.BaudRate = 115200

        SerialPort1.PortName = "com6" 

        If SerialPort1.IsOpen = False Then SerialPort1.Open()
    End Sub

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

        SerialPort1.Read(buffB, 0, 50)

        BeginInvoke(New EventHandler(AddressOf ffunction), SerialPort1.BytesToRead())

    End Sub

    Sub ffunction(ByVal sender As System.Object, ByVal e As System.EventArgs)

        TextBox1.Text = Encoding.ASCII.GetString(buffB, 0, 30)
    End Sub

End Class

Did you use the correct baud rate and COM Port?

What does your VB program receives from the serial port?

baud rate and COM Port are all right.Maybe the problem is the data type?

please don't make a "poll" like this just to attract attention to your post. If people can help they will help. No monkey tricks needed !

Try this:

Dim WithEvents serialPort As New IO.Ports.SerialPort

Public Sub serialPortOpen()

        'Configure and open the serial port

        'If the port is already open, close it first
        If serialPort.IsOpen Then
            serialPort.Close()
        End If

        Try
            With serialPort
                .PortName = settings.getSetting("serialPortName")
                .BaudRate = CInt(settings.getSetting("serialPortbaud"))
                .Encoding = System.Text.Encoding.ASCII
                .NewLine = Chr(13) + Chr(10)
            End With

            'Open the port and clear any junck in the input buffer
            serialPort.Open()
            serialPort.DiscardInBuffer()

        Catch Ex As Exception
           'errorhandeling here
        End Try
    End Sub

These two lines from the code above are important:

.Encoding = System.Text.Encoding.ASCII .NewLine = Chr(13) + Chr(10)

Sorry for the "Poll". I used your code,but still display nothing. could you give a full code to me? thanks.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SimpleSerial
{
    public partial class Form1 : Form
    {
        // Add this variable 
        string RxString;

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM6";
            serialPort1.BaudRate = 115200;
            serialPort1.Encoding = System.Text.Encoding.ASCII;
            serialPort1.NewLine = Chr(13) + Chr(10);
            serialPort1.Open();
            if (serialPort1.IsOpen)
            {
                buttonStart.Enabled = false;
                buttonStop.Enabled = true;
                textBox1.ReadOnly = false;
            }
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
                buttonStart.Enabled = true;
                buttonStop.Enabled = false;
                textBox1.ReadOnly = true;
            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (serialPort1.IsOpen) serialPort1.Close();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // If the port is closed, don't try to send a character.
            if (!serialPort1.IsOpen) return;

            // If the port is Open, declare a char[] array with one element.
            char[] buff = new char[1];

            // Load element 0 with the key character.
            buff[0] = e.KeyChar;

            // Send the one character buffer.
            serialPort1.Write(buff, 0, 1);

            // Set the KeyPress event as handled so the character won't
            // display locally. If you want it to display, omit the next line.
            e.Handled = true;
        }

        private void DisplayText(object sender, EventArgs e)
        {
            textBox1.AppendText(RxString);
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            RxString = serialPort1.ReadExisting();
            this.Invoke(new EventHandler(DisplayText));
        }
    }
}

I can’t get to the rest of the program right now, but here’s a link to a little VB program that reads data from the serial port.

http://mikmo.dk/misc/vbarduino.zip

The original code was a sample program i found on the net, it was kind of a serial chat program.

still can not display anything. datareceived event don't start. :'(

I used serialport control to receive string data from serial port,but failed.
I have checked Baudrate, portname,but still no usefull.
And i tried serial port test programme,the data all correctly received,so i downloaded vb.net & c# codes included serialport control,but still can’t receive anything.
Could anybody tell me what happened. thanks very much.

did you try the program i linked to in my previous post. I know it works with Arduino. I used it to recieve data from arduino and display it frm VB.

YES, i tried.but dispaly nothing.
My god.wuwu

Now i attach my full code. There is a serialport control an textbox control only. please check it for me.thanks.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Encoding = System.Text.Encoding.ASCII
        SerialPort1.NewLine = Chr(13) + Chr(10)
        SerialPort1.Open()
        If SerialPort1.IsOpen Then
            TextBox1.Text = "Serial port is opend!"
        End If
    End Sub

    Delegate Sub myMethodDelegate(ByVal [text] As String)
    Dim myD1 As New myMethodDelegate(AddressOf myShowStringMethod)

    Sub myShowStringMethod(ByVal myString As String)
        TextBox1.AppendText(myString)
    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim str As String = SerialPort1.ReadExisting()
        Invoke(myD1, str)
    End Sub
End Class

You're not specifying a port name in the call to open.

I set the portname at the property grid.

Try not to use a serialport control, but declare the serialport in the code like i do in the sample code.

I had some problems with using a serialport control and the datarecievd event.

Or try to poll the serialport instead of using the datarecieved event, just for starters to make sure it works.

I know it’s an old topic, but maybe my note can help someone who found this page googling.

I had similar issue with vb and serial port, and i fixed it using 8 bit encoding (UTF8) instead of 7 bit (ASCII); so change this line:

.Encoding = System.Text.Encoding.ASCII

with this one:

.Encoding = System.Text.Encoding.UTF8

Acually with the old 7bit encoding, the data received <128 was correctly acquired, while the others always result in a unexpectd one.