6 years, 7 months ago.  This question has been closed. Reason: Duplicate question

Receive the string from c ++ to mbed, then work the string.

After the first run, the program stops responding. This happens after I call the 'void control ()' function, why?

#include "mbed.h"
Serial pc(USBTX, USBRX); // tx, rx
char stringa[200];
int seq_ph[200];  //vettore in cui salvo gli indici delle immagini dei fonemi trovati nella stringa 
struct  phoneme{                //struct per contenere vari fonemi (matrice) e l'indice dell'immagine associata
        char poss_ph[25][4];
        int lips;
    };
phoneme vect_ph[20];  //vettore di struct, serve per contenere i vari gruppi di fonemi di una lingua

//Questa funzione riempie il vettore di struct vect_ph[] con tutti i fonemi e le immagini associate della lingua italiana
void italian_ph()
{   
    for (int i=0; i<20; i++)   //inizializzo il vettore
    {
        for (int j=0; j<25; j++)
            for (int k=0; k<4; k++)
            {
                vect_ph[i].poss_ph[j][k]='\0';    //carico tutti caratteri nulli nelle matrici in cui salverò i fonemi
            }
        vect_ph[i].lips=0;    //pongo uguali a 0 tutti gli indici delle immagini
    }

    //Fonema 1                         
    vect_ph[0].poss_ph[0][0]='a';    //carico tutti i fonemi della lingua
    vect_ph[0].lips=1;
    
    //Fonema 2
    vect_ph[1].poss_ph[0][0]='o';
    vect_ph[1].poss_ph[1][0]='u';
    vect_ph[1].poss_ph[2][0]='h';
    vect_ph[1].poss_ph[3][0]='q';
    vect_ph[1].poss_ph[3][1]='u';   
    vect_ph[1].lips=2;
    
    //Fonema 3
    vect_ph[2].poss_ph[0][0]='e';
    vect_ph[2].poss_ph[1][0]='i';
    vect_ph[2].lips=3;
    
    //Fonema 4
    vect_ph[3].poss_ph[0][0]='c';
    vect_ph[3].poss_ph[1][0]='d';
    vect_ph[3].poss_ph[2][0]='g';
    vect_ph[3].poss_ph[3][0]='n';
    vect_ph[3].poss_ph[4][0]='s';
    vect_ph[3].poss_ph[5][0]='t';
    vect_ph[3].poss_ph[6][0]='z';
    vect_ph[3].poss_ph[7][0]='g';
    vect_ph[3].poss_ph[7][1]='n';
    vect_ph[3].poss_ph[8][0]='f';
    vect_ph[3].poss_ph[9][0]='v';
    vect_ph[3].poss_ph[10][0]='s';
    vect_ph[3].poss_ph[10][1]='c';
    vect_ph[3].poss_ph[11][0]='c';
    vect_ph[3].poss_ph[11][1]='h';
    vect_ph[3].poss_ph[12][0]='s';
    vect_ph[3].poss_ph[12][1]='t';
    vect_ph[3].lips=4;
    
    //Fonema 5
    vect_ph[4].poss_ph[0][0]='l';
    vect_ph[4].poss_ph[1][0]='r';
    vect_ph[4].lips=5;
    
    //Fonema 6
    vect_ph[5].poss_ph[0][0]='m';
    vect_ph[5].poss_ph[1][0]='b';
    vect_ph[5].poss_ph[2][0]='p';
    vect_ph[5].lips=6;
    
    
    //Fonema 7
    vect_ph[6].poss_ph[0][0]='-';
    vect_ph[6].poss_ph[1][0]='.';
    vect_ph[6].lips=7;
    
    return;
}

//Questa funzione a partire dalla stringa di ingresso genera un vettore con tutti gli indici delle immagini associate ai fonemi trovati
void control ()
{
    int z=0;
    bool ok3=false, ok2=false, ok1=false;
    char temp[4];  //uso una stringa temporanea in cui salvo i caratteri prelevati dalla stringa in ingresso da confrontare con i fonemi della lingua scelta
    for (int q=0; q<4; q++)
        temp[q]='\0';
    int index_seq=0;
    
    while (stringa[z]!='\0') //fino a che non trovo il carattere di terminazione della stringa
    {   
        if (ok3==false && z<197) //controllo sillabe da 3
        {
            for (int j=0; j<3; j++)
            {
                temp[j]=stringa[z+j];
            }
            for (int k=0; k<20 && ok3==false; k++)
            {
                for (int h=0; h<25 && ok3==false; h++)
                {
                    if (temp[0]==vect_ph[k].poss_ph[h][0])
                        if (temp[1]==vect_ph[k].poss_ph[h][1])
                            if (temp[2]==vect_ph[k].poss_ph[h][2])
                            {
                                ok3=true;                              //se i 3 caratteri prelevati corrispondono ad un fonema
                                seq_ph[index_seq]=vect_ph[k].lips;     //allora salvo l'indice dell'immagine associata al fonema nel vettore
                                pc.printf("%d",(seq_ph[index_seq]));
                                index_seq++;                           //mi sposto di una posizione avanti nel vettore
                                z=z+3;                                 //mi sposto di 3 posizioni avanti nella stringa di ingresso
                              
                            }
                }
            }
        }
        if (ok3==false && ok2==false && z<198) //controllo sillabe da 2 se non c'è corrispondenza con quelle da 3
        {
            for (int j=0; j<2; j++)
            {
                temp[j]=stringa[z+j];
            }
            for (int k=0; k<20 && ok2==false; k++)
            {
                for (int h=0; h<25 && ok2==false; h++)
                {
                    if (temp[0]==vect_ph[k].poss_ph[h][0])
                        if (temp[1]==vect_ph[k].poss_ph[h][1])
                        {
                            ok2=true;                               //se i 2 caratteri prelevati corrispondono ad un fonema
                            seq_ph[index_seq]=vect_ph[k].lips;      //allora salvo l'indice dell'immagine associata al fonema nel vettore
                            pc.printf("%d",(seq_ph[index_seq]));
                            index_seq++;                            //mi sposto di una posizione avanti nel vettore
                            z=z+2;   
                                                          //mi sposto di 2 posizioni avanti nella stringa di ingresso
                        }
                }
            }
        }     
        if (ok3==false && ok2==false && ok1==false && z<199) //controllo sillabe da 1
        {   
            temp[0]=stringa[z];
            for (int k=0; k<20 && ok1==false; k++)
            {
                for (int h=0; h<25 && ok1==false; h++)
                {
                    if (temp[0]==vect_ph[k].poss_ph[h][0])
                    {
                        ok1=true;                                       //se il carattere prelevato corrisponde ad un fonema
                        seq_ph[index_seq]=vect_ph[k].lips;              //allora salvo l'indice dell'immagine associata al fonema nel vettore
                        pc.printf("%d",(seq_ph[index_seq]));
                        index_seq++;                                    //mi sposto di una posizione avanti nel vettore
                        z=z+1;  
                                                              //mi sposto di una posizione avanti nella stringa di ingresso
                    }
                }
            }
            
        }
        ok3=false;
        ok2=false;
        ok1=false;      
    }
    return;
}

void insert() {
    int i=0;
        char ch=0;
do
{ 
   if (pc.readable())      // if there is an character to read from the device
   {
       ch = pc.getc();   // read it
      if (i<200)               // just to avoid buffer overflow
         stringa[i++]=ch;  // put it into the value array and increment the index         
  }
} while (ch!='\n');    // loop until the '\n' character
stringa[i]=0; 
pc.printf(stringa);


}
int main() {
  pc.baud(115200);     
  italian_ph();
  while(1){
     insert();
     control();
    }
}

Hi Thomas, can you please provide us with a code snippet or error output so we can more easily assist you?

- Jenny, team Mbed

posted by Jenny Plunkett 23 Aug 2017

I would, but mark it as spam

I modified the first post and I entered the code, but it says this: "This question is awaiting moderation"

posted by Thomas Villacci 24 Aug 2017

Post the code here. Use

<<code>>
your code
<</code>>

to preserve the formatting.

posted by Andy A 24 Aug 2017