Arduino Forum

Using Arduino => Interfacing w/ Software on the Computer => Topic started by: backbone on Dec 20, 2012, 08:49 am

Title: 2 way serial arduino to vb6
Post by: backbone on Dec 20, 2012, 08:49 am
Hello,

I am almost there with the code but last part fails.

I switch the Arduino on and it reads data from the eeprom.
I switch on VB program and by USB cable it copies data to some text boxes from Arduino to the program.

Then I send by button command from VB6 a single string of 7 variables to the Arduino.

Arduino receives the data but not all data now changes correctly.

VB code
Code: [Select]
Private Sub cmdSendDataToController_Click()
strOutput = "10,2,3,4,5,6,11:"
MSComm1.Output = strOutput
End Sub


Arduino code
Code: [Select]


const int NUMBER_OF_FIELDS = 7; // how many comma separated fields we expect
int fieldIndex = 0;            // the current field being received
int values[NUMBER_OF_FIELDS];   // array holding values for all the fields

void loop
if( Serial.available())
  {
    char ch = Serial.read();
    if(ch >= '0' && ch <= '9') // is this an ascii digit between 0 and 9?
    {
      // yes, accumulate the value
      values[fieldIndex] = (values[fieldIndex] * 10) + (ch - '0');
    }
    else if (ch == ',')  // comma is our separator, so move on to the next field
    {
      if(fieldIndex < NUMBER_OF_FIELDS-1)
        fieldIndex++;   // increment field index
    }
    else
    {
      // any character not a digit or comma ends the acquisition of fields
      for(int i=0; i <= fieldIndex; i++)
      {
        deadbandxValue = (values[0]);
        //brakeregenValue = (values[1]);
        //regbrakeState = (values[2]);
        //regenhyValue = (values[3]);
        //brakespeedValue = (values[4]);
        //brakespeedState = (values[5]);
        modelNumber = (values[6]);
        values[i] = 0; // set the values to zero, ready for the next message
      }
      fieldIndex = 0;  // ready to start over
      }
  }


deadbandxValue = (values[0]); parses 0 back  to the vb program which should be 10!
modelNumber = (values[6]); correctly parses 11 back to the vb program.

Is there something I overlook as the array spliting is working partialy?
for some reason it only reads the last variable in the array.

Paco
Title: Re: 2 way serial arduino to vb6
Post by: coocooworld on Dec 20, 2012, 09:07 am
I haven't test your version of ASCII digits for 0-9, but here is what I used and it works:

Code: [Select]
if ((ch>47)&&(ch<58))
{
             int actual_digit = ch-48;
             // do something iwth actual digit
}
Title: Re: 2 way serial arduino to vb6
Post by: AWOL on Dec 20, 2012, 09:11 am
Or, more intuitively:
Code: [Select]
if ((ch>='0')&&(ch<='9'))
{
             int actual_digit = ch-'0';
             // do something with actual digit
}

or even
Code: [Select]
if (isdigit(ch))
{
             int actual_digit = ch-'0';
             // do something with actual digit
}
Title: Re: 2 way serial arduino to vb6
Post by: PaulS on Dec 20, 2012, 11:53 am
Code: [Select]
     for(int i=0; i <= fieldIndex; i++)
     {
       deadbandxValue = (values[0]);
       //brakeregenValue = (values[1]);
       //regbrakeState = (values[2]);
       //regenhyValue = (values[3]);
       //brakespeedValue = (values[4]);
       //brakespeedState = (values[5]);
       modelNumber = (values[6]);
       values[i] = 0; // set the values to zero, ready for the next message
     }

On the first pass through this loop, think about what is in values[0] that is being assigned to deadbandxValue.

Then, what happens to values[ i ] at the end of loop?

Then, what is assigned to deadbandxValue on the next pass? And the one after that.

Why is the use of values[ i ] done inside the loop? That makes no sense, really.
Title: Re: 2 way serial arduino to vb6
Post by: AWOL on Dec 20, 2012, 12:20 pm
Code: [Select]
const int NUMBER_OF_FIELDS = 7; // how many comma separated fields we expect
...
int values[NUMBER_OF_FIELDS];   // array holding values for all the fields
...
for(int i=0; i <= fieldIndex; i++)

Oops. try
Code: [Select]
for(int i=0; i < fieldIndex; i++)
Title: Re: 2 way serial arduino to vb6
Post by: backbone on Dec 20, 2012, 02:01 pm
For what it is worth.

here the link from where I got the lead source for getting the data to the array.
http://my.safaribooksonline.com/9781449399368/sending_multiple_text_fields_from_ard#X2ludGVybmFsX0h0bWxWaWV3P3htbGlkPTk3ODE0NDkzOTkzNjglMkZyZWNlaXZpbmdfbXVsdGlwbGVfdGV4dF9maWVsZHNfaW5fYV9zJnF1ZXJ5PQ== (http://my.safaribooksonline.com/9781449399368/sending_multiple_text_fields_from_ard#X2ludGVybmFsX0h0bWxWaWV3P3htbGlkPTk3ODE0NDkzOTkzNjglMkZyZWNlaXZpbmdfbXVsdGlwbGVfdGV4dF9maWVsZHNfaW5fYV9zJnF1ZXJ5PQ==)

I expected this to work flawlessly after some changes but it is not. :-(

Will check all your answers and the options to see if I can manage to solve it.

Let you know the result.

Thanks, Paco
Title: Re: 2 way serial arduino to vb6
Post by: AWOL on Dec 20, 2012, 02:05 pm
Someone should fire the proofreader.
Title: Re: 2 way serial arduino to vb6
Post by: PaulS on Dec 20, 2012, 02:11 pm
Quote
I expected this to work flawlessly after some changes but it is not.

The "changes" you made introduced some of the problems you are having.
Title: Re: 2 way serial arduino to vb6
Post by: billroy on Dec 20, 2012, 02:11 pm
Have we ruled out a problem on the sending side?

Opening the serial port from the PC side resets the arduino.  The Arduino isn't running your program for a couple seconds after reset.  So, if you open the port and write to it right away / without a delay, some  of the sent output gets dropped on the floor (by the bootloader).

If you don't delay after opening the port, it might be worth a try.  And could you post your whole sending-side code?

-br
Title: Re: 2 way serial arduino to vb6
Post by: backbone on Dec 20, 2012, 05:54 pm
OK,

Thanks for all replies and suggestions.
Two steps forward one step back.
PaulS his comment was something I already tried but did not solved the problem this morning while debugging.

Code: [Select]
values[i] = 0; // set the values to zero, ready for the next message


But reading once again he was rigth it does not belong there so I commented the line and it worked.

After start up of Arduino and VB program and I hit the VB data write button now all the values are set correctly in the Arduino variables and are returned to the VB program correctly.
So I though YES..........but,
When I in the VB program hit the button again with the same values it looks like the serial buffer was not cleared and random data is received and returned to the VB program.
From what I have read as soon as serial.read is called it clears the buffer and the next call to the buffer should be correct but it is not.

Paco

Title: Re: 2 way serial arduino to vb6
Post by: PaulS on Dec 20, 2012, 09:16 pm
Quote
From what I have read as soon as serial.read is called it clears the buffer and the next call to the buffer should be correct but it is not.

It removes the character read from the serial buffer. It does not clear all the rest of the data, and it does not clear the buffer you are writing to.

I don't know how you changed the code, but it should be:

Code: [Select]
        deadbandxValue = (values[0]);
        //brakeregenValue = (values[1]);
        //regbrakeState = (values[2]);
        //regenhyValue = (values[3]);
        //brakespeedValue = (values[4]);
        //brakespeedState = (values[5]);
        modelNumber = (values[6]);

        for(int i=0; i <= fieldIndex; i++)
        {
          values[i] = 0; // set the values to zero, ready for the next message
        }
Title: Re: 2 way serial arduino to vb6
Post by: AWOL on Dec 20, 2012, 09:23 pm
Code: [Select]
for(int i=0; i <= fieldIndex;
No, really, no.
Title: Re: 2 way serial arduino to vb6
Post by: PaulS on Dec 20, 2012, 09:25 pm
Yeah, yeah.

Code: [Select]
        for(int i=0; i < fieldIndex; i++)
        {
          values[i] = 0; // set the values to zero, ready for the next message
        }
Title: Re: 2 way serial arduino to vb6
Post by: backbone on Dec 20, 2012, 11:42 pm
With the current code this is the behavior.

Start arduino and data is read from EEPROM to variables.
I start the VB program and it reads the data that during setup of arduino is written from the EEPROM into the variables of the Arduino.
Sofar so good.
I press the VB button and voila the code changes in VB to the required setting. Still good.
Then I press the VB button again with same combobox settings and thevalues of the variables of the arduino are those as if they are read again from the EEPROM.
Strange as it does not read the EEPROM values in the void loop........only once in the void setup.

Code: [Select]
if( Serial.available())
  {
    char ch = Serial.read();
    if(ch >= '0' && ch <= '9') // is this an ascii digit between 0 and 9?
    {
      // yes, accumulate the value
      values[fieldIndex] = (values[fieldIndex] * 10) + (ch - '0');
    }
    else if (ch == ',')  // comma is our separator, so move on to the next field
    {
      if(fieldIndex <= NUMBER_OF_FIELDS-1)
        fieldIndex++;   // increment field index
    }
    else
    {
      deadbandxValue = (values[0]);
      brakeregenValue = (values[1]);
      regbrakeState = (values[2]);
      regenhyValue = (values[3]);
      speedbrakeValue = (values[4]);
      speedbrakeState = (values[5]);
      modelNumber = (values[6]);

      for(int i=0; i <= fieldIndex; i++)
      {
        values[i] = 0; // set the values to zero, ready for the next message
      }
    }
  }


Knipsel2.jpg shows the two collums with on the left hand side the text boxes that read the data from the variabels from the Arduino.
The right hand side collum has the combo boxes with the new values that need to be send to the Arduino and again need to be returned into the text boxes of the VB APP. This picture shows the data before the button is pressed and EEPROM data.
Knipsel.jpg is the after the button is pressed once and the EEPROm data is overwritten.
Title: Re: 2 way serial arduino to vb6
Post by: AWOL on Dec 21, 2012, 07:31 am
Code: [Select]
for(int i=0; i <= fieldIndex
When I said "no", I really meant it.
Title: Re: 2 way serial arduino to vb6
Post by: backbone on Dec 21, 2012, 08:51 am
PaulS and AWOL,

I tried all your options if I understand correctly what you both mean.

I changed from
Code: [Select]
for(int i=0; i <= fieldIndex; i++)
to
Code: [Select]
for(int i=0; i <= fieldIndex
but that gives an compile error as there is no round brakcet at the end.
So I made it
Code: [Select]
for(int i=0; i <= fieldIndex;) I even removed the whole line.
All  giving the same result as described in my previous post.

I might misunderstand your way of thinking and the way you try to descibe it to me. 

Paco
Title: Re: 2 way serial arduino to vb6
Post by: AWOL on Dec 21, 2012, 08:53 am
The test is "less than", not "less than or equal to".
Code: [Select]
for(int i=0; i < fieldIndex; i++)

If you use "less than or equal to", you will write past the end of your array, which is a Bad Thingtm

The example in the book is wrong too, which is why I said the proofreader should be fired.
Title: Re: 2 way serial arduino to vb6
Post by: backbone on Dec 21, 2012, 09:01 am
I changed to
Code: [Select]
for(int i=0; i < fieldIndex; i++)

Now I see what you tried to tell.
But same problem still.

Paco
Title: Re: 2 way serial arduino to vb6
Post by: AWOL on Dec 21, 2012, 09:43 am
So, post your code.
Title: Re: 2 way serial arduino to vb6
Post by: backbone on Dec 21, 2012, 09:51 am
AWOL,

Whole code too large to put it in the response code tags.
Have it zipped.

Thanks for looking in advance.
Curious where I made a mistake.

Paco
Title: Re: 2 way serial arduino to vb6
Post by: AWOL on Dec 21, 2012, 10:13 am
The usual comment in situations like this "It's a good thing you found this bug before the code got too large".

Code: [Select]
speedfinalValue = 0;  // set output to zero during boot up
 brakenormalValue = 0;  // set output to zero during boot up
 resetcalibMode = 0;
 lookupState = 0;

The compiler already did that initialisation for you.

Please remove commented-out code before posting.

Code: [Select]
pinMode(2,INPUT);
You gave all those pins nice names - why not use them?
Title: Re: 2 way serial arduino to vb6
Post by: backbone on Dec 21, 2012, 10:21 am
This project is a learning curve for me.
Along the way new options are dropping in and implemented.
One thing leads to an other and so the code grows and grows. It might be filthy coded and can be reduced in code lines but this is where it stands at the moment with my knowledge.

Paco
Title: Re: 2 way serial arduino to vb6
Post by: backbone on Dec 21, 2012, 10:39 am
The reason for commented code lines through out is that I still might use them later or they are SWITCHED off to prevent uncontrolled writing to EEPROM or are part of a base code that need to be used in a later project. This code is 100% test phase and I use one source file which is updated at certain stages and the file number raised. If I make a mistake (happens too often) I can revert back to a previous file. In the end all rubbish and commented code not used will be removed. But I near that stage now.

forgive me I do not understand

Code: [Select]
PinMode(2,INPUT);
You gave all those pins nice names - why not use them?


Title: Re: 2 way serial arduino to vb6
Post by: backbone on Dec 21, 2012, 11:06 am
One step further.

Found the cause for the EEPROM values to be restored again.
I connected the display again.
When I press the first time the VB button the data is updated as it should be.
When I click the second time the VB button the Arduino resets and the display is dimmed for a second!
So that is the reason why the EEPROM values are showing up again.
So this means the code for reading from PC to Arduino array is good.
Now remains the question why it can happen that the Arduino resets when we send the same data once again.

Paco
Title: Re: 2 way serial arduino to vb6
Post by: PaulS on Dec 21, 2012, 03:57 pm
Quote
Now remains the question why it can happen that the Arduino resets when we send the same data once again.

The new psychic we've hired has agreed to start just after the 12th of never. Stick around and wait for him, or post some code.

It seems likely, if the Arduino resets every time you send data, that you are opening the serial port, sending data, and closing the port. Bad idea.

Make the VB app open the port ONCE. Then, each time you need to send data, simply check that the port is open, rather than opening it. Close it when the program ends.
Title: Re: 2 way serial arduino to vb6
Post by: backbone on Dec 21, 2012, 05:04 pm
Paul,

This is the sequence. It is only when the data is send for the second time it reset.
It works fine when I send it only once.

1] I power up the Arduino and wait 5 seconds
2] I start the VB app which opens the serial communication automatically at comport 10 once via the USB port and wait 5 seconds
3] VB app has read the EEPROM data into the text boxes and when I move the trigger of the controller I see the trigger data live updated in the VB APP. So we have continously updating of the controller data to the VB APP.
4] I set the new settings in the VB APP and press the button send data to controller.
5] I wait 3 seconds and see that the new values are updated in the text boxes of the VB APP.
6] I move the trigger of the controller I see the trigger data live updated in the VB APP.
So even if we receive and send data once we still have connection.
7] Now I once again press the VB app button and send data to the controller.
8] Now the Arduino resets or hangs or what ever happens at that point.

It always reset at the 2nd time never at the first.

VB code for sending the data to the controller
Code: [Select]
Private Sub cmdSendDataToController_Click()
strOutput = cmbdeadbandX.Text & "," & cmbregBrake.Text & "," & cmbregbrakeState.Text & "," & cmbregHy & "," & cmbspeedBrake.Text & "," & cmbspeedbrakeState.Text & "," & cmbModelNumber.Text & ":"
MSComm1.Output = strOutput
End Sub


vb code that opens the port at formload of the VB app
Code: [Select]
Private Sub cmdComport_Click()
On Error Resume Next
With MSComm1
       If .PortOpen Then .PortOpen = False
          .CommPort = cmbComport.Text
          .Settings = "9600,n,8,1"
          .DTREnable = True
          .RTSEnable = True
          .RThreshold = 1
          .SThreshold = 0
          .PortOpen = True
         
 End With
 If MSComm1.PortOpen = False Then
 MsgBox "No USB device available or wrong Com Port number", vbCritical, "Please check!"
 End If

End Sub


Title: Re: 2 way serial arduino to vb6
Post by: backbone on Dec 22, 2012, 05:21 pm
PaulS, I waited for the Maya calendar to be true but it was not so tried to solve and debug the problem further the past hours.
For AWOL I undressed the large code and removed the LCD code but kept the I2C LCD hooked on the Arduino NANO.
So almost bare code to send and receive from VB app to arduino and vice versa.

1] I power up the Arduino and wait 5 seconds
2] I start the VB app which opens the serial communication automatically at comport 10 once via the USB port and wait 5 seconds
3] VB app has read one variable data with value "12" into the text boxes and when I move the trigger of the controller I see the trigger data live updated in the VB APP. So we have continously updating of the controller data to the VB APP.
4] I set the new setting "8" in the VB APP and press the button send data to controller.
5] I wait 3 seconds and see that the new value "8" is updated in the text box of the VB APP.
6] I move the trigger of the controller I see the trigger data live updated in the VB APP.
So even if we receive and send data once we still have connection.
7] Now I change the data to be send to "14" and once again press the VB app button and send data to the controller.
8] Now the value returned from the Arduino to the VB app text box is "0" where it should be "14".
9] I move the trigger of the controller I see the trigger data live updated in the VB APP. (so the reset problem is temporary switched off)

After restart of the Arduino the same sequence happens. Second click returs "0".
Why does the Arduino by the first click reads the value correctly and second returns 0?
It puzzels me still.

Paco





Code: [Select]
const int sensorspeedPin = A0;
byte sensorrawValue = 0;
byte deadbandxValue = 12;
byte i;

const int NUMBER_OF_FIELDS = 1; // how many comma separated fields we expect
int fieldIndex = 0;             // the current field being received
int values[NUMBER_OF_FIELDS];   // array holding values for all the fields

void setup()
{
  Serial.begin(9600);                       // Output to serial writing or BT
}

void loop()
{
 
    Serial.print("A,");
  Serial.println(sensorrawValue);

  Serial.print("K,");
  Serial.println(deadbandxValue);

  sensorrawValue = analogRead(sensorspeedPin); // read the raw value from the linear hall sensor

  if( Serial.available())
  {

    char ch = Serial.read();
    if(ch >= '0' && ch <= '9') // is this an ascii digit between 0 and 9?
    {
      // yes, accumulate the value
      values[fieldIndex] = (values[fieldIndex] * 10) + (ch - '0');
    }
    else if (ch == ',')  // comma is our separator, so move on to the next field
    {
      if(fieldIndex <= NUMBER_OF_FIELDS-1)
        fieldIndex++;   // increment field index
    }
    else
    {
      for(int i=0; i < fieldIndex; i++)
        deadbandxValue = (values[0]);
      {
        values[i] = 0; // set the values to zero, ready for the next message
      }
    }
  }
}
Title: Re: 2 way serial arduino to vb6
Post by: PaulS on Dec 22, 2012, 05:50 pm
Code: [Select]
      for(int i=0; i < fieldIndex; i++)
        deadbandxValue = (values[0]);
      {
        values[i] = 0; // set the values to zero, ready for the next message
     

fieldIndex is adjusted each time a comma is received. It starts at 0. The first time VB sends data, 7 values separated by 6 commas, fieldIndex will be incremented to 6. The last value is written into values[6]. No comma at the end of the 7th value, so fieldIndex is not incremented again.

The last value ends with a non-digit, so the code posted above is executed. In that code, the value from values[ 0 ] is copied into deadbandxValue 7 times. Why?

Then, a value is stored in the non-existent location values[7]. Why?

The next time VB sends some data, fieldIndex is 6, so data is stored in values[6]. The comma does not cause fieldIndex to be incremented, because it already contains the maximum value.

You REALLY need to understand what the { and } after the for loop are accomplishing when they are placed correctly (yours are not), what happens when they are not (as yours are not), and that variables are not magically reset just because you want them to be. YOU must make the effort to type the command.


Title: Re: 2 way serial arduino to vb6
Post by: backbone on Dec 22, 2012, 06:52 pm
Paul,

The current VB code only sends one value to debug and I see I forgot to add this vb code change, my humble appologies for that.
Code: [Select]
Private Sub cmdSendDataToController_Click()
strOutput = cmbdeadbandX.Text & "," & ":"
MSComm1.Output = strOutput
End Sub


The receiving code was adapted to receive also one value only.
Code: [Select]
const int NUMBER_OF_FIELDS = 1; // how many comma separated fields we expect

When this works I will add the other values at both side one by one.

Will look into it again with your given explanation.

Thanks, Paco
Title: Re: 2 way serial arduino to vb6
Post by: backbone on Dec 26, 2012, 09:22 am
Did cost me some time with debug and try and error.
Used the serialmonitor instead of VB app to find it.

Here is the code for 7 variables that works fine in my case.

Thanks for all assistance.

Paco

Code: [Select]
/*
* SerialReceiveMultipleFields sketch
* This code expects a message in the format: 12,255,1,121,16,89,74:
* This code requires a ":" character to indicate the end of the data
*/

const int NUMBER_OF_FIELDS = 7; // how many comma separated fields we expect
int fieldIndex = 0;                        // the current field being received
int values[NUMBER_OF_FIELDS];   // array holding values for all the fields
int data0= 0;
int data1= 0;
int data2= 0;
int data3= 0;
int data4= 0;
int data5= 0;
int data6= 0;
int i;

void setup()
{
  Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud
}

void loop()
{
  if( Serial.available())
  {
    char ch = Serial.read();
    if(ch >= '0' && ch <= '9') // is this an ascii digit between 0 and 9?
    {
      // yes, accumulate the value
      values[fieldIndex] = (values[fieldIndex] * 10) + (ch - '0');
    }
    else if (ch == ',')  // comma is our separator, so move on to the next field
    {
      if(fieldIndex < NUMBER_OF_FIELDS-1)
        fieldIndex++;   // increment field index
    }
    else
    {
      // any character not a digit or comma ends the acquisition of fields
      // in this example it's the : character sent
      Serial.print( fieldIndex +1);
      Serial.println(" fields received:");
      for(int i=0; i <= fieldIndex; i++)

      {
        data0 = (values[0]);
        //Serial.println (data0); //debug line
        data1 = (values[1]);
        //Serial.println (data1); //debug line
        data2 = (values[2]);
        //Serial.println (data2); //debug line
        data3 = (values[3]);
        //Serial.println (data3); //debug line
        data4 = (values[4]);
        //Serial.println (data4); //debug line
        data5 = (values[5]);
        //Serial.println (data5); //debug line
        data6 = (values[6]);
        //Serial.println (data6); //debug line
        values[0] = 0; // set the values to zero, ready for the next message
        values[1] = 0; // set the values to zero, ready for the next message
        values[2] = 0; // set the values to zero, ready for the next message
        values[3] = 0; // set the values to zero, ready for the next message
        values[4] = 0; // set the values to zero, ready for the next message
        values[5] = 0; // set the values to zero, ready for the next message
        values[6] = 0; // set the values to zero, ready for the next message
        fieldIndex = 0;  // ready to start over
      }
    }
  }
}
Title: Re: 2 way serial arduino to vb6
Post by: PaulS on Dec 26, 2012, 02:55 pm
Code: [Select]
        data0 = (values[0]);
        data1 = (values[1]);
        data2 = (values[2]);
        data3 = (values[3]);
        data4 = (values[4]);
        data5 = (values[5]);
        data6 = (values[6]);

None of the parentheses are necessary. The names on the right really don't mean anything.

Code: [Select]
        values[0] = 0; // set the values to zero, ready for the next message
        values[1] = 0; // set the values to zero, ready for the next message
        values[2] = 0; // set the values to zero, ready for the next message
        values[3] = 0; // set the values to zero, ready for the next message
        values[4] = 0; // set the values to zero, ready for the next message
        values[5] = 0; // set the values to zero, ready for the next message
        values[6] = 0; // set the values to zero, ready for the next message

The for loop was doing this part just fine.
Title: Re: 2 way serial arduino to vb6
Post by: backbone on Dec 26, 2012, 05:45 pm
unfortunenatly
Code: [Select]
values[i] = 0; // set the values to zero, ready for the next message
in the for loop did not worked.
Maybe it is against what it should be but what I opted for was the only way I could get it to work.
Other options are welcome anyway.

Paco
Title: Re: 2 way serial arduino to vb6
Post by: AWOL on Dec 26, 2012, 05:54 pm
Code: [Select]
values[0] = 0; // set the values to zero, ready for the next message
        values[1] = 0; // set the values to zero, ready for the next message
        values[2] = 0; // set the values to zero, ready for the next message
        values[3] = 0; // set the values to zero, ready for the next message
        values[4] = 0; // set the values to zero, ready for the next message
        values[5] = 0; // set the values to zero, ready for the next message
        values[6] = 0;

aka
Code: [Select]
for (byte i = 0; i < 7; ++i) {
  values [i] = 0;
}

or memset.
Title: Re: 2 way serial arduino to vb6
Post by: PaulS on Dec 26, 2012, 07:06 pm
Quote
in the for loop did not worked.

I'd need proof of that. What didn't work was assigning value[ i ] to another variable inside the loop. The assignment of value[ 0 ] to another variable worked, when i was 0, but the assignment of value[ 0 ] to that variable produced unexpected results, when i was 1, because by then value[ 0 ] has been set to 0.

The same problem occurred assigning value[ 1], when i was 2, because value[ 2 ] has been set to 0.

Try this:
Code: [Select]

for(int i=0; i<7; i++)
{
   Serial.print("values[");
   Serial.print(i);
   Serial.print("] = ");
   Serial.print(values[i]);
   Serial.println("]");
}

for(int i=0; i<7; i++)
{
   values[i] = 14;
}

for(int i=0; i<7; i++)
{
   Serial.print("values[");
   Serial.print(i);
   Serial.print("] = ");
   Serial.print(values[i]);
   Serial.println("]");
}


Show us the serial output.

Then, of course, you can change the 14 to 0, and try again. Then, obviously, you can get rid of the first and third for loop. But, try this first, just to prove that the for loops do indeed work, or not work.
Title: Re: 2 way serial arduino to vb6
Post by: backbone on Dec 26, 2012, 07:49 pm
Here the screenshot of the added code and the serial monitor result.
In serialmonitor I pasted 1,2,3,4,5,6,7:    one time and then once more.
Code: [Select]
/*
* SerialReceiveMultipleFields sketch
* This code expects a message in the format: 12,345,678,121,16,89,74:
* This code requires a : character to indicate the end of the data
*/

const int NUMBER_OF_FIELDS = 7; // how many comma separated fields we expect
int fieldIndex = 0;            // the current field being received
int values[NUMBER_OF_FIELDS];   // array holding values for all the fields
int data0= 0;
int data1= 0;
int data2= 0;
int data3= 0;
int data4= 0;
int data5= 0;
int data6= 0;
int i;

void setup()
{
  Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud
}

void loop()
{
  if( Serial.available())
  {
    char ch = Serial.read();
    if(ch >= '0' && ch <= '9') // is this an ascii digit between 0 and 9?
    {
      // yes, accumulate the value
      values[fieldIndex] = (values[fieldIndex] * 10) + (ch - '0');
    }
    else if (ch == ',')  // comma is our separator, so move on to the next field
    {
      if(fieldIndex < NUMBER_OF_FIELDS-1)
        fieldIndex++;   // increment field index
    }
    else
    {
      // any character not a digit or comma ends the acquisition of fields
      // in this example it's the : character sent
      Serial.print( fieldIndex +1);
      Serial.println(" fields received:");
     
      for(int i=0; i<7; i++)
      {
        Serial.print("values[");
        Serial.print(i);
        Serial.print("] = ");
        Serial.print(values[i]);
        Serial.println("]");
      }

      for(int i=0; i<7; i++)
      {
        values[i] = 14;
      }

      for(int i=0; i<7; i++)
      {
        Serial.print("values[");
        Serial.print(i);
        Serial.print("] = ");
        Serial.print(values[i]);
        Serial.println("]");
      }

    }
  }
}
Title: Re: 2 way serial arduino to vb6
Post by: PaulS on Dec 26, 2012, 08:26 pm
OK. So, think about this. Where in the values array does the program store values when the first set of values is entered. If you said positions 0 to 6, you'd be right.

Now, where does it store values when the second set arrives? If you said 0 to 6 again, you'd be wrong. It stores them in positions 7 to 13 of the 7 element array, because you do not set fieldIndex back to 0.