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
Private Sub cmdSendDataToController_Click()
strOutput = "10,2,3,4,5,6,11:"
MSComm1.Output = strOutput
End Sub
Arduino code
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.
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++)
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?
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.
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.
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.
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.
I tried all your options if I understand correctly what you both mean.
I changed from for(int i=0; i <= fieldIndex; i++)
to 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 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.