Arduino board: communicate with it using serial port.
Arduino board: communicate with it using serial port.
Arduino board: communicate with it using serial port.
Arduino board: communicate with it using serial port.
Arduino board: communicate with it using serial port.
Arduino board: communicate with it using serial port. Arduino board: communicate with it using serial port. Arduino board: communicate with it using serial port. Arduino board: communicate with it using serial port. Arduino board: communicate with it using serial port. Arduino board: communicate with it using serial port. Arduino board: communicate with it using serial port. Arduino board: communicate with it using serial port.
Arduino board: communicate with it using serial port. Arduino board: communicate with it using serial port.
Arduino board: communicate with it using serial port.
Go Back  Xtreme Visual Basic Talk > > > Arduino board: communicate with it using serial port.


Reply
 
Thread Tools Display Modes
  #1  
Old 01-27-2011, 02:46 PM
andrebr andrebr is offline
Newcomer
 
Join Date: May 2010
Posts: 13
Question Arduino board: communicate with it using serial port.


Hi.
I have a problem with something.
I'm new to visual basic 2010, and I have a Arduino board I wan't to communicate with by serialport.

In "Form1", I'll retrieve values from the Arduino board, and I want it to calculate and display data in TextBoxes.

How can I do this?
What I've done is:
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort2.Write("B")
End Sub
This sends value to Arduino from computer.

From Arduino I send this:
Serial.print(sensor);

This is what I wanna display in my "Form".

How is this possible?

Here is my whole code:
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

        Dim intX As Integer = Screen.PrimaryScreen.Bounds.Width
        Dim intY As Integer = Screen.PrimaryScreen.Bounds.Height
        Dim resolution = intX & " * " & intY

        With SerialPort2
            'Set the serial port you want to use.
            .PortName = "COM6" 'You MUST put 'COM' before the COM number.

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

            'Set DTR and RTS
            .DtrEnable = True
            .RtsEnable = True

            'Set timeout to 2 seconds.
            .ReadTimeout = 2000

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

            'Open serial port.
            .Open()

        End With

        'TextBox3.Text = SerialPort2.ReadLine()

    End Sub

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

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SerialPort2.Write("A")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        SerialPort2.Write("B")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        SerialPort2.Write("sensor1")
    End Sub

    Private Sub SerialPort2_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort2.DataReceived
        ' Read and process your data
        TextBox3.Text = SerialPort2.ReadLine()
    End Sub

End Class
Annyone?

Last edited by Cerian Knight; 01-27-2011 at 03:07 PM. Reason: Added [code]...[/code] tags.
Reply With Quote
  #2  
Old 01-28-2011, 03:56 AM
Orca44 Orca44 is offline
Centurion
 
Join Date: Nov 2004
Location: Cape Town, South Africa
Posts: 124
Default

What is the problem exactly? Does the DataReceived event not fire?
Reply With Quote
  #3  
Old 01-28-2011, 09:13 AM
AtmaWeapon's Avatar
AtmaWeaponArduino board: communicate with it using serial port. AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Orca44's right. "Form1" is not an adequate one-sentence summary of the problem (I got a moderator to fix the title), nor does the post ask a question. You state that you want to display some values in a form, and I see code that sets up the serial port for reading, but I don't see a description of what's going wrong. Since I don't have an Arduino (or even a serial port), I can't run your code to see if I can figure out what's happening, so a detailed description of what seems to be wrong would be nice. Give How to Ask Questions the Smart Way a read; it's a nice article by ESR and following it makes forums more productive.

I'm pretty sure I know it's one of two problems, but let's talk about how you could figure this out.

I can only guess that the text box doesn't have the text you want it to have. The first thing you should do when things aren't working is verify that all of your assumptions are correct. Is the event even being raised? Set a breakpoint on the event handler by putting your cursor on the "TextBox3.Text = SerialPort2.ReadLine()" line and pressing F9. Now press F5 to start debugging the application, and do whatever to get some data sent to your application. If the event is being raised, Visual Studio should force itself to the front and highlight the line with the breakpoint; this indicates that code has halted so you can have a peek around at everything. If the breakpoint isn't being hit, the event isn't being raised and you know that's your problem and you need to figure out why. If it is being raised, you have to make sure you're getting the data you expect.

As your code is structured right now, you can't tell what the ReadLine() call returns. Rewrite it this way:
Code:
Private Sub SerialPort2_DataReceived(...
    Dim theText As String = SerialPort2.ReadLine()
    TextBox3.Text = theText
End Sub
Now put a breakpoint on the line that sets theText and run the program again. When the breakpoint is hit, push "F10" to step over that line; now the next line will be highlighted. Hover your mouse cursor over theText and you should get a popup window that displays its value. Is the value what you expected? Is it something that should display in the text box? Hit F5 to continue running the program and close it.

Now we know whether the event is being raised and, if it is being raised we know whether the data that's coming in is what you want. If it's not what you want, maybe you're reading the data wrong. It's time to look at the documentation. SerialPort.DataReceived has some interesting information in the documentation.

The first thing of interest is the example. If your code is different than the example, that might be indicative of a problem. Here's how the example reads data:
Code:
Dim sp As SerialPort = CType(sender, SerialPort)
Dim indata As String = sp.ReadExisting()
Huh, what's ReadExisting() do that's different than ReadLine()? Read the documentation for each. I had a look at it and don't know that it's anything significant. ReadExisting() may even be the wrong thing based on what I read. You might want to have a look at the BytesToRead property, but I'm not sure how it applies. I'm not sure setting ReceivedBytesThreshold to 1 is a good idea, you might want to play around with that.

If the event is being raised and the data is being properly read, the only thing left to assume is that setting the text box's text is broken somehow. That leads me to the last bit of information that's quite useful from the documentation, emphasis mine:
Quote:
The DataReceived event is raised on a secondary thread when data is received from the SerialPort object. Because this event is raised on a secondary thread, and not the main thread, attempting to modify some elements in the main thread, such as UI elements, could raise a threading exception. If it is necessary to modify elements in the main Form or Control, post change requests back using Invoke, which will do the work on the proper thread.
It is illegal to do anything with a control from a thread that is not the main thread. You can't directly set TextBox3.Text from this event handler. It could be that it's not working because of this; in my experience you don't always get an exception and sometimes things just don't work. This tutorial discusses the problem and includes information about how to solve it.

Figure out which step along that path is the one you're having trouble with, then we might be able to figure out a solution. It's especially helpful for you to include as much detail as possible, since hardware is involved and the chances of someone else being able to run and test your code are more slim than when writing general code.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 01-28-2011, 11:44 AM
andrebr andrebr is offline
Newcomer
 
Join Date: May 2010
Posts: 13
Smile

Hi Guys.
Thanks for your quick response.
I'm applying a image showing my error code.

What I wanna do is this.
I have a arduino board with several tempsensors.
I want to show these sensors on my screen, but I just can't get it to work...

Here is my code for serial data:

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

        Dim intX As Integer = Screen.PrimaryScreen.Bounds.Width
        Dim intY As Integer = Screen.PrimaryScreen.Bounds.Height
        Dim resolution = intX & " * " & intY

        With SerialPort2
            'Set the serial port you want to use.
            .PortName = "COM6" 'You MUST put 'COM' before the COM number.

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

            'Set DTR and RTS
            .DtrEnable = True
            .RtsEnable = True

            'Set timeout to 2 seconds.
            .ReadTimeout = 2000

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

            'Open serial port.
            .Open()

        End With

    End Sub


    Private Sub SerialPort2_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort2.DataReceived
        Dim theData As String
        theData = SerialPort2.ReadLine()
        Sensor1.Text = theData
    End Sub
Thanks for your further help
Attached Images
File Type: bmp error.bmp (726.7 KB, 4 views)
Reply With Quote
  #5  
Old 01-28-2011, 12:02 PM
PlausiblyDamp's Avatar
PlausiblyDampArduino board: communicate with it using serial port. PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Did you read Atma's post? He went to the trouble of explaining exactly what this problem means and what you need to / investigate to avoid it.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #6  
Old 01-29-2011, 04:17 AM
andrebr andrebr is offline
Newcomer
 
Join Date: May 2010
Posts: 13
Default

Hi guys.
I still can't get it...
When I hit F5, I get an error with:
Code:
TextBox3.Text = indata
It's hilighted, and when I put my mouse over it, I get the value I've expected...

I've tried several things.
I eaven put in a pushbuttom:
Code:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        SerialPort2.Write("sensor1")

        Dim theText As String = SerialPort2.ReadExisting()
        TextBox1.Text = theText

    End Sub
When I did this I did got the values from arduino, but only one time...
The value got "stuck" in 20'C and did not change when I pressed the buttom again...

But I want the values to "Show" all time, without pressing anny buttoms.

But since I get some values from arduino, I have to do something righ thougt...

And since "TextBox3.Text = indata" is hilighted and shows some values, this also have to be right, BUT What do I do wrong!!!

Reply With Quote
  #7  
Old 01-29-2011, 12:02 PM
AtmaWeapon's Avatar
AtmaWeaponArduino board: communicate with it using serial port. AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

My post explains why it doesn't work and makes a suggestion. You've done the work to get this far in:
Quote:
If the event is being raised and the data is being properly read, the only thing left to assume is that setting the text box's text is broken somehow.
Now read the explanation that follows it, read the article it asks you to read, make the changes it tells you to make, and see if that changes things.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #8  
Old 01-29-2011, 12:03 PM
andrebr andrebr is offline
Newcomer
 
Join Date: May 2010
Posts: 13
Default

PS:
I've tested some more codes.

Code:
    Private Sub SerialPort2_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort2.DataReceived

        Dim buff As String = SerialPort2.ReadLine

        While SerialPort2.BytesToRead > 0
            buff &= SerialPort2.ReadLine
        End While

        If buff.Length > 0 Then
            MsgBox(buff)
        End If

    End Sub
Now I get the value from serialport.
But still no data to textbox...

Why?
Reply With Quote
  #9  
Old 01-30-2011, 04:48 AM
andrebr andrebr is offline
Newcomer
 
Join Date: May 2010
Posts: 13
Default

Thanks guys.
Now I've got it. I receive data from serial port and split up the data and distribute data to where I need it.

Again, thanks for your help and guidance
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Arduino board: communicate with it using serial port.
Arduino board: communicate with it using serial port.
Arduino board: communicate with it using serial port. Arduino board: communicate with it using serial port.
Arduino board: communicate with it using serial port.
Arduino board: communicate with it using serial port.
Arduino board: communicate with it using serial port. Arduino board: communicate with it using serial port. Arduino board: communicate with it using serial port. Arduino board: communicate with it using serial port. Arduino board: communicate with it using serial port. Arduino board: communicate with it using serial port. Arduino board: communicate with it using serial port.
Arduino board: communicate with it using serial port.
Arduino board: communicate with it using serial port.
 
Arduino board: communicate with it using serial port.
Arduino board: communicate with it using serial port.
 
-->