Go Down

Topic: 2 way serial arduino to vb6 (Read 12000 times) previous topic - next topic

backbone

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
Never to old to learn and I learn every day

coocooworld

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
}

AWOL

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
}

PaulS

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.
The art of getting good answers lies in asking good questions.

AWOL

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++)

backbone

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==

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
Never to old to learn and I learn every day

AWOL

Someone should fire the proofreader.

PaulS

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.
The art of getting good answers lies in asking good questions.

billroy

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

backbone

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

Never to old to learn and I learn every day

PaulS

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
        }
The art of getting good answers lies in asking good questions.

AWOL

Code: [Select]
for(int i=0; i <= fieldIndex;
No, really, no.

PaulS

Yeah, yeah.

Code: [Select]
        for(int i=0; i < fieldIndex; i++)
        {
          values[i] = 0; // set the values to zero, ready for the next message
        }
The art of getting good answers lies in asking good questions.

backbone

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.
Never to old to learn and I learn every day

AWOL

Code: [Select]
for(int i=0; i <= fieldIndex
When I said "no", I really meant it.

Go Up