VB6 C++ sending bits to Atmel328

hey!

I have successfully sent bits froma VB6 console application to the Arduino Atmel328 board, but I have to compile my processing code and upload to the Atmel328 board every time or else it does not work.

Does anybody know why?
Ive been told I should look into detection API's in VB6...
Can anybody help me?
-thanks

Whoa. Back up. Start over.

You do not compile Processing code to upload to the Arduino.

but I have to compile my processing code and upload to the Atmel328 board every time or else it does not work

It? What exactly is this pronoun referring to? The Arduino? The code on the Arduino? Some processing application? Your VB application? Your C++ application? How do you know "it" does not work?

Ive been told I should look into detection API's in VB6...

What are you trying to detect? Life on Mars? Water on the moon? Intelligent life in the White House?

Not meaning to be overly rude, but I did say that my CODE works fine.

So the 'it' refers to the problems I am having with the board connecting to the app. At present it requires the wiring script to be uploaded every time I connect the Atmel328 to my laptop via USB.
After the upload I can interact with the script countless times.

I am unsure how to enable a link between the win32 app and the Atmel board for comercial use (ie, without compiling and uploading code every time).

As for its use - that shouldnt matter as the code works fine on the board.

Since the Arduino stores the uploaded code in read-only memory, there is no reason why you should have to upload the code again to get the Arduino to execute the code.

I've never heard of anyone else needing to upload code every time. At least not to a standard Arduino with the default bootloader and factory fuse settings.

Have you changed the bootloader or fuse settings?

Can you post the Arduino code? The VB code?

I can.

Here is the console app code in question:

# include <windows.h>
# include <iostream.h>

char   Adam[6]               =    "Elson";
char   CommPorts[10][5]     =   {"COM1", "COM2", "COM3",
                  "COM4", "COM5", "COM6",
                  "COM7", "COM8", "COM9", "\0" };

// -----------------------------------------------------

int main()
{
    HANDLE  hSerial;
   hSerial  =  CreateFile( CommPorts[3], GENERIC_WRITE,
                          0, 0, OPEN_EXISTING,
               FILE_ATTRIBUTE_NORMAL, 0 );

    if( hSerial == INVALID_HANDLE_VALUE )
    {
        if( GetLastError() == ERROR_FILE_NOT_FOUND )
        {
            cout<< "\n\n\t BOLL----!";
        }
        else
        {
            cout<< "\n\n\t SOME OTHER BOLL----!";
        }
    }
    else
    {}

// -----------------------------------------------------

    DCB   dcbSerialParams  = { 0 };

    if( ! GetCommState( hSerial, &dcbSerialParams ))
    {
        cout << "\n\n\t ERROR GETTING STATE.";
    }
    else
    {}

    dcbSerialParams.BaudRate   =   CBR_9600;
    dcbSerialParams.ByteSize    =          8;
    dcbSerialParams.StopBits     = ONESTOPBIT;
    dcbSerialParams.Parity        =   NOPARITY;

    if( !SetCommState( hSerial, &dcbSerialParams ))
    {
        cout<< "\n\n\t ERROR SETING SERIAL PORT STATE!";
    }
    else
    {}

// -----------------------------------------------------

    COMMTIMEOUTS   timeouts = { 0 };
    timeouts.WriteTotalTimeoutConstant = 200;

    if( !SetCommTimeouts( hSerial, &timeouts ))
    {
        cout << "\n\n ERROR OCCURED WITH TIMEOUTS";
    }
    else
    {}

// -----------------------------------------------------

    DWORD  dwBytesRead   =  0;

    if( !WriteFile( hSerial, Adam, 8, &dwBytesRead, NULL ))
    {
        cout << "\n\n AN ERROR OCCUERED WHEN WRITTING = A.";
    }
    else
    {
        cout << "\n\t OK, WRITTEN " << Adam << " ";
    }

    cout << "\n\n\t " << dwBytesRead <<"\n\n";
    CloseHandle( hSerial );

    return 0;
};

... and here is the processing code:

# define  LED  8
# define  TWO  9

char  A[6]   =  "Adam";
int   x      =   0;

void setup( )
{
  pinMode( LED, OUTPUT );
  Serial.begin( 9600 );
}

void loop( )
{ 
  // READ data only when BUFFER has received data:
  if( Serial.available() > 0 )
  {
    // WRITE data to string, 'A', for comparison using a FOR-LOOP:
    for( x=0; x <= 6; x++ )
    {
      A[x] = Serial.read();
    }
    // FLUSH the Atmel328 serial BUFFER:
    Serial.flush();
  }
  else
  {
    // Compare string to "Elson":
    if( A[0] == 'E' && A[1] == 'l' && A[2] == 's' && A[3] == 'o' && A[4] == 'n' )
    {
      // Blink GREEN LED:
      for( int t=100; t>0; t-- )
      {
        delay(100);
        digitalWrite( LED, HIGH );
        delay(100);
        digitalWrite( LED, LOW );  
      }
      // covert string A back to its original content:
      A[0]  =  'A';     A[1]  =  'd';     A[2]  =  'a';     A[3]  =  'm';     A[4]  =  ' ';
    }
    // Compare string to "Adam":
    else if( A[0] == 'A' && A[1] == 'd' && A[2] == 'a' && A[3] == 'm' )
    {
      // Blink RED LED(s) with BUZZER
        delay(100);
        digitalWrite( TWO, HIGH );
        delay(100);
        digitalWrite( TWO, LOW );  
    }
    else
    {}
    //END IF
  }
  // END IF
}
// END LOOP

I was able to run your Arduino code and VB application. When I ran the VB app, the arduino saw the serial data, and read it.

I unplugged the Arduino, and plugged it back in. It resumed running the sketch (modified to blink the on-board LED so that I could see it was running).

I ran the VB app again, and the Arduino was happy to read serial data. After blinking the 2nd LED, it resumed blinking the 1st LED.

There are some issues that should be addressed, though.

The Arduino sketch begins running as soon as it is powered up. When at least one byte arrives on the serial port, you read 6 characters, whether there are 6 to read, or not. If you are going to read 6 characters, you should wait to read any until there are 6 to read.

The character array is not NULL terminated, so it is not a string. If it were, this:

if( A[0] == 'E' && A[1] == 'l' && A[2] == 's' && A[3] == 'o' && A[4] == 'n' )

could be replaced by this:

if(strcmp(A, "Elson") == 0)

To NULL terminate the array, change this:

A[x] = Serial.read();

to this:

A[x] = Serial.read();
A[x+1] = '\0';

This:

      // covert string A back to its original content:
      A[0]  =  'A';     A[1]  =  'd';     A[2]  =  'a';     A[3]  =  'm';     A[4]  =  ' ';

can be shortened to:

A[0] = '\0';
strcat(A, "Adam");

Try adding this:

for(byte b=0; b<10; b++)
{
   digitalWrite(13, HIGH);
   delay(250);
   digitalWrite(13, LOW);
   delay(250);
}

to the beginning of loop, to see that the sketch actually does start running as soon as the Arduino is plugged in.

Cheers PaulS;

So the problem isnt the board not being able to read the app with every connection... The problem is the board itself?

I guess I must have upset the bootloader then
(TBH, I had a mishap with a motor awhile back!).

The code itself was rushed, I just needed code that worked to form a bed-rock of the operation - I will fix up my code now.

Thanks again!