Pages: [1]   Go Down
Author Topic: Receive and Display Serial Data in Visual Basic?  (Read 1617 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Im having trouble receiving/displaying the data sent by my arduino sketch in viual basic, was wondering if someone could give me a bit of help in this issue, heres my arduino and VB codes:

Arduino:
Code:
void setup()
{
  Serial.begin(115200);
  pinMode(1, INPUT);
}


void loop()
{
   
    float inputReading = analogRead(1);
    float voltage = (inputReading);                             
    Serial.println(voltage); 
 
}

VB:
Code:
Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1

    Shared serialportused As SerialPort
    Dim looop As Boolean

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

        With serialportused

            'Temporarily closes port
            .Close()

            'Set the serial port to port 6
            .PortName = "COM6"

            'Set baud rate, parity, data bits, and stop bits.
            .BaudRate = 115200
            .Parity = Parity.None
            .DataBits = 8
            .StopBits = StopBits.One

            'Set number of bytes before DataRecieved event is raised.
            .ReceivedBytesThreshold = 1

            'Open serial port.
            .Open()

            'Sets loop true
            looop = True

            'Loops reading serial data and displaying in textbox
            Do While looop = True
                tbxSerial.Text = serialportused.ReadLine() + vbCrLf
            Loop

        End With
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        'Closes program
        Me.Close()

    End Sub

End Class

Kept everything as simple as i could for now, and would expand on it later. I have confirmed the arduino is using COM6 on the PC and that it is sending serial data.
« Last Edit: March 08, 2013, 01:26:03 pm by NickPatey » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 642
Posts: 50365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  Serial.begin(115200);
  pinMode(1, INPUT);
The Serial instance uses pins 0 and 1. DO NOT DIDDLE WITH THEM.

Code:
    float inputReading = analogRead(1);
The analogRead function does not return a float.

Code:
    float voltage = (inputReading);                             
The parentheses are unnecessary. The white space at the end is unnecessary. In fact, the whole statement is unnecessary.

Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        With serialportused

            'Temporarily closes port
            .Close()
The port wasn't opened. Don't close something you didn't open.

Code:
            'Sets loop true
            looop = True

            'Loops reading serial data and displaying in textbox
            Do While looop = True
                tbxSerial.Text = serialportused.ReadLine() + vbCrLf
            Loop
An infinite loop in the Form_Load event is a really bad idea. Don't even consider it.

The VB app runs an event loop. Let that event loop get started, and let it do the infinite looping.

Actually implementing the DataReceived event would be a good idea.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Fixed my arduino program accordingly, also coincidentally i did change to using the data received handler as i got some similar advice from a colleague, my new VB code is listed below:

Code:
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Imports System.ComponentModel

Public Class Form1

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

        'Opens port
        spu.Open()

        'Defines data holder as string, and moves in serial data
        Dim indata As String = spu.ReadExisting()

        'Displays string in textbox
        tbxSerial.Text = indata

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        'Closes program
        Me.Close()

    End Sub
End Class

Still no serial data, also not sure if im supposed to loop my read/write function or if thats done automatically, or if i need to open the port or if thats done simply by placing the component. Note: i have a serial port component layed out with all the same specs i listed before, and i named it as "spu".

Also tried without the serial port component, and still no luck:

Code:
Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1

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

        'Defines port
        Dim spu As New SerialPort("COM6")

        'Sets port details
        spu.BaudRate = 115200
        spu.Parity = Parity.None
        spu.StopBits = StopBits.One
        spu.DataBits = 8
        spu.Handshake = Handshake.None

        'Opens port
        spu.Open()

    End Sub

    Private Sub spu_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles spu.DataReceived

        'Moves data into string holder
        Dim indata As String = spu.ReadExisting()

        'Moves string from holder into textbox
        tbxSerial.Text = indata

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        'Closes program
        Me.Close()

    End Sub
End Class
« Last Edit: March 08, 2013, 03:33:42 pm by NickPatey » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Never mind, i got my data in, i just need to set up a proper loop, thanks anyways.
Logged

Pages: [1]   Go Up
Jump to: