6 years, 8 months ago.

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();
    }
}

1 Answer

6 years, 8 months ago.

If the user input doesn't match any of the defined strings then the value of z never increases and you get stuck in an infinite loop.

At line 160 try adding:

        }

        if (ok3==false && ok2==false && ok1==false)  // new
          z++;                                            // new

        ok3=false;
        ok2=false;
        ok1=false;      
    }

This way if you didn't get a match you move on to the next character.

Accepted Answer

Thanks endless, now it does not stop running.

posted by Thomas Villacci 24 Aug 2017

I'm still writing the program, I came to this point where I put the insert_t () function in the while (1). After I run it once, I return to ask for the parameter t, why?

void control_vett_fin()
{   
    
    for (int i=0; i<600; i++)  
        vett_fin[i]=0;
   
    int position=0;
    int pos_fin=0;

    if (t>=0.24 && t<0.36)
    {
        while (seq_ph[position]!=0 && position<198)
        {   
            vett_fin[pos_fin]=seq_ph[position];
            pc.printf("%d",(vett_fin[pos_fin]));
            pos_fin++;
            int precedente=seq_ph[position];
            position++;
            
            int successivo=seq_ph[position];
            if (successivo==0)
                 vett_fin[pos_fin]=mat_trsz1[precedente-1][6];
                   pc.printf("%d",(vett_fin[pos_fin]));
                }
            else
            {
                if (precedente==successivo)
                {
                    vett_fin[pos_fin]=precedente;
                    pc.printf("%d",(vett_fin[pos_fin]));
                }
                else if (precedente<successivo)
                {
                    vett_fin[pos_fin]=mat_trsz1[precedente-1][successivo-1];
                     pc.printf("%d",(vett_fin[pos_fin]));
                }
                else if (precedente>successivo)
                {
                    vett_fin[pos_fin]=mat_trsz1[successivo-1][precedente-1]; 
                    pc.printf("%d",(vett_fin[pos_fin]));
                }
                pos_fin=pos_fin++;
            }  
            
        }
        durata=t/2;
    
    }
    else if (t<0.24)
    {
        while (seq_ph[position]!=0 && position<198)
        {
            //cout<<seq_ph[position];
            vett_fin[pos_fin]=seq_ph[position];
            pc.printf("%d",(vett_fin[pos_fin]));           
            pos_fin++;
            position++;
           
        }
        
         durata=t;
     
    }
    return;
}

void insert_t()
{
    pc.printf("Inserisci valore dell'intervallo temporale t: ");
    pc.scanf("%f",&t);
    pc.printf("\nRicevuto: %f \n",t);
    return;
}

int main() {

  pc.baud(115200);         
  italian_ph();
  riemp_mat(); 
  
  
  while(1){
     
    insert();
    control();
    insert_t();
    control_vett_fin();
    
}
}
posted by Thomas Villacci 24 Aug 2017

I'm not sure if I completely understand what you are asking but I'll make a guess...

Is your terminal program set to send /r/n when you hit enter?

insert_t() is using scanf which may use the /r to indicate the end of the text leaving a /n in the serial port receive buffer.

insert() will then read the input until it sees a /n and exit. Since /n is the first character in the buffer it will set stringa to "/n/0"

Since insert() doesn't print anything and control() won't print anything if you enter an empty string it will look like insert_t() function is running twice in a row.

Change insert() to print something to the terminal and make it ask again if it receives an empty input.

posted by Andy A 24 Aug 2017