Go Down

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

backbone

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

AWOL

#16
Dec 21, 2012, 08:53 am Last Edit: Dec 21, 2012, 08:56 am by AWOL Reason: 1
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.

backbone

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

AWOL


backbone

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

AWOL

#20
Dec 21, 2012, 10:13 am Last Edit: Dec 21, 2012, 10:17 am by AWOL Reason: 1
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?

backbone

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

backbone

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?


Never to old to learn and I learn every day

backbone

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

PaulS

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

backbone

#25
Dec 21, 2012, 05:04 pm Last Edit: Dec 21, 2012, 08:41 pm by backbone Reason: 1
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


Never to old to learn and I learn every day

backbone

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

PaulS

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.


The art of getting good answers lies in asking good questions.

backbone

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

backbone

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

Go Up