Pages: [1]   Go Down
Author Topic: Visual Basic 2008 & Arduino Uno  (Read 1363 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys,

I would like your help in this, perhaps small matter, please.  smiley

Beforehand I say that I'm not an advanced programmer in both these languages, that's why I'm posting here.

I'm trying to send some values through the interface I've created in VB2008 to the Arduino.

First off, I seem to don't be able to use the serial scope on Arduino when I have an open connection between VB2008 and Arduino which is very inconvenient, is that normal? Is there any practical way around this?

And second, then I want to send some values let's say 70 20 0.6. So VB2008 will send them all in a row and the problem on the receiver's side (Arduino) is that using the following code:

int incomingByte = 0;   // for incoming serial data

void setup() {
   Serial.begin(9600);   // opens serial port, sets data rate to 9600 bps
}

void loop() {

   // send data only when you receive data:
   if (Serial.available() > 0) {
      // read the incoming byte:
      incomingByte = Serial.read();

      // say what you got:
      Serial.print("I received: ");
      Serial.println(incomingByte, DEC);
   }
}

it saves every number to a variable therefore separating the values I want.

So the approach I take next is to send each value terminated with a character, lets say '-'. Now, on the receiver's side I try to collect the numbers I'm receiving in a string until I get the '-'. And this is working. So finally, the problem is being to store these strings with the value I want in an int or a float variable.

Can you give me some advice, please? And also if there is a much better way to solve this, please let me know.  smiley

Thanks
Logged

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

Quote
First off, I seem to don't be able to use the serial scope on Arduino when I have an open connection between VB2008 and Arduino which is very inconvenient, is that normal?
Yes, it is. Only one application can listen to a serial port at a time.

Quote
Is there any practical way around this?
No. And there really doesn't need to be. You can get the Arduino working, writing to it using the Serial Monitor, and seeing its responses. When the Arduino works, you make the VB app send the same data you were typing in the Serial Monitor, and read the returned data. Since you have a known output format, and known responses (the same ones you saw in the serial monitor) this should be relatively easy.

Quote
And second, then I want to send some values let's say 70 20 0.6. So VB2008 will send them all in a row and the problem on the receiver's side (Arduino) is that using the following code:
<snip>
it saves every number to a variable therefore separating the values I want.
To be pedantic, it saves every character to the same variable. The "separation" you speak of occurs in the output stream.

Quote
So the approach I take next is to send each value terminated with a character, lets say '-'. Now, on the receiver's side I try to collect the numbers I'm receiving in a string until I get the '-'.
Not with that code you don't.

Quote
And this is working.
Not with that code.

Quote
So finally, the problem is being to store these strings with the value I want in an int or a float variable.
How to parse the string, and convert the values to numbers so that they can be stored in variables depends on how you are collecting the data.

Post that code for real help.

The indexOf() and substring and toInt() functions look useful, if you are using String.

The strtok() and atoi() and atof() functions are useful if you are doing it right and using a NULL terminated char array.

Logged

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

Thank you for the prompt reply PaulS.

Well the code I showed was the one I used to develop upon. What I have so far is something like this:

byte data = 0;   // for incoming serial data
float a = 0;
float b = 0;
float c = 0;
int count = 0;
String value = '\n';

void setup() {
   Serial.begin(9600);   // opens serial port, sets data rate to 9600 bps
}

void loop() {

   // send data only when you receive data:
   if (Serial.available() > 0) {
      // read the incoming byte:
      data = Serial.read();

      // say what you got:
      Serial.print("I received: ");
      Serial.println(data);

                if(data != '-'){
                 
                  value += data;
                                   
                  }
         
                else{
                  count++;
                  if(count == 1) a = float(value);
                  if(count == 2) b = float(value);
                  if(count == 3){
                    c = float(value);
                    count = 0;
                    //Serial.print("value = ");Serial.println(value);  //for testing
                    Serial.print("a = ");Serial.println(a);
                    Serial.print("b = ");Serial.println(b);
                    Serial.print("c = ");Serial.println(c);
                }
                }
          
        }
}

The 'float(value)' are of course failing.
Logged

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

Quote
The 'float(value)' are of course failing.
Well, of course they are. The float macro is a crutch for people that don't understand casts. It evaluates to
Code:
    if(count == 1) a = (float)value;
which, of course, is nonsense, since a String object can not be cast to a float.

You could use the value.toCharArray() method to extract the char array from the String object, then use atof() on the char array.
Logged

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

OK, thanks for the help PaulS. I'm going to try it out.

Is this the way used to receive values with more than one digit? Or is there another practical, more used, way to deal with these situations?
Logged

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

Quote
Is this the way used to receive values with more than one digit?
No.

Quote
Or is there another practical, more used, way to deal with these situations?
Collect the data received, up to an end-of-packet marker, in an array. Deal with the contents of the array when the end-of-packet marker arrives.
http://jhaskellsblog.blogspot.com/2011/05/serial-comm-fundamentals-on-arduino.html
Logged

Pages: [1]   Go Up
Jump to: