Pages: [1]   Go Down
Author Topic: Arduino + VB 2010  (Read 1616 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all,
I need to both send and recieve data from vb to arduino and back.
Sending the data seems to work but I have no idea how to make vb read the serial port... I have tried adding a timer to do so but all it does is freeze my application.

Any ideas on how to make it work??

Thanks.
Logged

Copenhagen / Denmark
Offline Offline
Edison Member
*
Karma: 6
Posts: 2360
Do it !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The serial port class has a datarecieved event. You can use that to know when data has arrived

You will also need to look into delegates because the code servicing the event is running in another thread than your code.
Logged

Belgium
Offline Offline
Full Member
***
Karma: 0
Posts: 187
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This is my code to read from the serial port when something gets sent:
Code:
Sub EventHandler(sender As Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Arduino.DataReceived

        Dim datareceived As Boolean = False
        Dim recStr as String = ""

        While Not datareceived

            Dim recVal As Integer
            recVal = Arduino.ReadByte()
            If recVal = 10 Or recVal = 13 Then
                datareceived = True
            Else
                recStr = recStr + Chr(recVal)
            End If

        End While

    End Sub

For this you need a serialport named Arduino and that's it, this should make it work perfectly (it works for me)

EDIT: you have to use println() for this, if you use print() it get in an infinite loop!
Logged


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

Thanks Steen for you answer.

I have tried your code but it did not work...
I know I can send data to the arduino thru VB because I was able to control the led (turn it on and off...)

On the arduino side, all I have is a serial.println(integer here)...
And when I use the serial monitor I do get the propper value printed.

It is only when I am using vb that nothing happens smiley-sad
could it be because I am sending stuff and shortly after trying to recieve??

I make sure to close the port right after I send!

I am truly puzzeled!

Any help will be appreciated.

thanks.
Logged

Belgium
Offline Offline
Full Member
***
Karma: 0
Posts: 187
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

could you please post some example code, since now it is hard to tell what you might be doing wrong.

The code i posted works, but you have to set the correct baudrate, the parity etc to make it working.

And another thing, you say you read the serial port just before you send to it, does that mean you print on command of the PC? like if the pc sends 'A' then the arduino has to send the int ?

this is the complete code to receive data:
Code:

Imports System.IO.Ports

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With SerialPort1

            .Close()
            .PortName = "COM3"
            .BaudRate = 19200
            .Parity = Parity.None
            .DataBits = 8
            .StopBits = StopBits.One
            .ReceivedBytesThreshold = 1
            .Open()

        End With
    End Sub

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

        Dim datareceived As Boolean
        Dim recStr As String

        datareceived = False
        recStr = ""

        While Not datareceived

            Dim recVal As Integer
            recVal = SerialPort1.ReadByte()
            If recVal = 10 Or recVal = 13 Then
                datareceived = True
            Else
                recStr = recStr + Chr(recVal)
            End If

        End While

        TextBox1.Text = TextBox1.Text + vbNewLine + recStr

    End Sub
End Class

and the arduino code:
Code:
int cnt = 0;
int recVal;

void setup() {
  Serial.begin(19200);
}

void loop() {
  cnt++;
  recVal = Serial.read();
  Serial.println(cnt);
}
Logged


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

Here is the vb code:

Code:
Public Class Form1
    Shared _continue As Boolean
    Shared _serialPort As SerialPort
    Dim sendvar As Integer
    Dim somevariable As Integer


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

        diesizelabel.Hide()
        diecavitylabel.Hide()



        SerialPort1.Close()
        SerialPort1.PortName = "com5" 'change com port to match your Arduino port
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
    End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Stopautomaticcontrol.Click
        'sending 5002 is auto control stop
        SerialPort1.Open()
        SerialPort1.Write("5002")
        SerialPort1.Close()
        MessageBox.Show("Variable sent to the Microcontroller is: 5002")

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        somevariable = 0

        'moving to the left 1 step
        somevariable = 5011

        'variable to be sent is 5011
        sendvar = somevariable


        'sending the integer
        SerialPort1.Open()
        SerialPort1.Write(sendvar)
        SerialPort1.Close()
        MessageBox.Show(sendvar, "Variable sent to the Microcontroller is:")


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

        Dim datareceived As Boolean = False
        Dim recStr As String = ""

        While Not datareceived

            Dim recVal As Integer
            recVal = SerialPort1.ReadByte()
            If recVal = 10 Or recVal = 13 Then
                datareceived = True
            Else
                recStr = recStr + Chr(recVal)
            End If

        End While

        MessageBox.Show(recStr, "Message from the Microcontroller")

    End Sub


on the arduino side:

Code:
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int val = 0;

void setup() {
Serial.begin(9600);
        pinMode(13, OUTPUT);
}

void loop() {
       

if (Serial.available() == 4) {

a = Serial.read();
                b = Serial.read();
                c = Serial.read();
                d = Serial.read();
               
                val = a*1000 + b*100 + c*10 + d;
               
                Serial.println(val);
                if(val == 5011){
                  digitalWrite (13, HIGH);
                }
                if (val == 5002){
                  digitalWrite (13, LOW);
                }
}
Serial.flush();

   

}

so the arduino is recieving the trasmitions because the led operates as expected...

I do not however get anything in return...


Thanks a lot for your help!
Logged

Belgium
Offline Offline
Full Member
***
Karma: 0
Posts: 187
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why do you  do if (Serial.available() == 4) { ? that is just too explicit to function. You'd better use just if (Serial.available() > 1) {. This is explicit enough to make it work, but not restricted.


change the SerialPort1.Datareceived to SerialPort1.dataReceived (notice the lowercase d from data)

where do you close this sub:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click ?
since i don't see an End Sub anywhere

i think the rest of the code OK.. try these changes, and tell us what hapened

EDIT: and btw, if your port is closed, it will ont receive anything, will it? try to delete those SerialPort1.Open() and .Close() commands and maybe it will function
Since it needs to be activated inorder to be able to receive st..
« Last Edit: April 05, 2012, 11:33:40 am by Steen » Logged


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

Quote
Why do you  do if (Serial.available() == 4) { ? that is just too explicit to function. You'd better use just if (Serial.available() > 1) {. This is explicit enough to make it work, but not restricted.


I am using it that way to make it a filter to only accept 4 characters.

BTW, my problem was indeed the port being closed!! THANK YOU SO MUCH.

Now, it works fine but everytime I get a communication from the Arduino a get not only one message box but two: the first one has the variable that I have sent (great that was the goal) but the second one that comes up (not supposed to come up at all) has nothing in it.

Do you this it is the arduino side that sends an extra empty string or is it the vb side that is messing up??

Thanks again for your help!!
Logged

Belgium
Offline Offline
Full Member
***
Karma: 0
Posts: 187
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

change MsgBox(recVal or whatever variable here,,"received value) instead of MsgBox(recVal, "received value")

since the title is the third optional command, it might be something messing up with that. the second one, the one you give "title" is the button command...

Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I make sure to close the port right after I send!
Doing so resets the Arduino. Is that what you really want to do?
Logged

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

no i do not want to reset it.
that means that I should not close the port??

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
that means that I should not close the port??
Opening and closing the serial port resets the Arduino. You want to open the port once, then wait for the Arduino to finish resetting.

Close the port only in the Form_FormClosed event.
Logged

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

Thank you I did not know that.
So should I open the port as soon as I load the form or wait until I actually need to communicate with the Arduino?
Logged

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

So should I open the port as soon as I load the form or wait until I actually need to communicate with the Arduino?

in my understanding,
you could do either.
just make sure you aren't opening and closing it unnecessarily.
also make sure you at least do it once before communicating.
and do close it once you done communicating.
Logged

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

Thank you, you have been extreamly helpful!
Logged

Pages: [1]   Go Up
Jump to: