I am developing a project in which I need Arduino communicate with an application developed in VB.NET and C #.
First I need to detect in which USB the Arduino is connected, so my application to get all the ports installed on the computer and tries to communicate with the Arduino using a protocol that I created. After communication is established the application sends several sequences of strings to the Arduino, and each sequence correspond to a different action that the Arduino must execute.
Below is a small demonstration of what I need, this is just a schematic for me to learn to do the communication. The problem is that when I send the sequence of strings to Arduino it does not recognize the sequence that I sent. Can someone help me?
int x1Pin=6, x2Pin=7; //Pins used to light the LEDs
int estado[10]; //Array that stores each character in the string to be sent by my application
int FUNCAO;//Determines which LED should be lit
void setup()
{
Serial.begin(9600);
pinMode(x1Pin,OUTPUT), pinMode(x2Pin,OUTPUT);
}
void loop()
{
if(Serial.available()>0)
{
for(int i=0; i<=10; i++){
estado[i]=Serial.read(); //Gets the string sent
Serial.print(estado[i]);
}
}
///////////////////////////////////////////////////////
//Begin of the protocol to define which USB port the Arduino is connected
if(estado[0]==68) //The application sends the letter D
{
Serial.print("E"); //The Arduino sends the letter E to confirm that the USB port was found
delay(200);
}
///////////////////////////////////////////////////////
if(estado[0]==65) //The application sends the letter A for the Arduino, and waits until the Arduino send the letter B
{
Serial.print("B"); //The Arduino responds by sending the letter B, stating that the application can send a sequence of strings
delay(200);
}
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
//Sequence of strings that will be sent by the application: G49T02M6
// G 4 9 T 0 2 M 0 6
if((estado[0]==71)&(estado[1]==52)&(estado[2]==57)&(estado[3]==84)&(estado[4]==48)&(estado[5]==50)&(estado[6]==77)&(estado[7]==48)&(estado[8]==54))
{
FUNCAO=1;//Sets the LED pin 6 will be lit
}
///////////////////////////////////////////////////////
//New sequence of strings that are sent by the application: S6000MO3
// S 6 0 0 0 M 0 3
if((estado[0]==83)&(estado[1]==54)&(estado[2]==48)&(estado[3]==48)&(estado[4]==48)&(estado[5]==77)&(estado[6]==48)&(estado[7]==51))
{
FUNCAO=2;//Sets the LED pin 7 will be lit
}
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
switch (FUNCAO)
{
///////////////////////////////////////////////////////////
// G49T02M06
case 1:
digitalWrite(x1Pin,1);
delay(1000);
digitalWrite(x1Pin,0);
for(int j=0; j<=10; j++)
{
estado[j]=0;
}
FUNCAO=0;
Serial.print("C");//Send the letter C to the application stating that the sequence of strings has been received and the next sequence can be sent
break;
///////////////////////////////////////////////////////////
// S6000M03
case 2:
digitalWrite(x2Pin,1);
delay(1000);
digitalWrite(x2Pin,0);
for(int j=0; j<=10; j++)
{
estado[j]=0;
}
FUNCAO=0;
Serial.print("C");
break;
}
}