6 years, 8 months ago.

Receive the string from c ++ to mbed

The program receives a string from c ++ and then analyzes it by finding the corresponding language phonemes: Italian. Each given phoneme corresponds to a number (index) that in turn corresponds to an associated image (which I can not see in the program because the problem is another), these numbers (indexes) are inserted into a vector "seq_ph ". If I only use the "insert ()" and "control ()" function, the program is ok.

If between two successive phonemes I want to match two, one, or zero interim images and therefore two, one, or zero numbers (indices), the program does it once by placing these numbers (indices) in the "vett_fin".

If I want to repeat the operation according to the receiving string, the "vett_fin" remains the same and in addition I only read the first 16 characters and not all the ones I send.

Example receive string;

I will send the 'ciao' string from c ++:

//Teraterm
ciao
4312 
Inserisci valore dell'intervallo temporale t: 0.2300
Ricevuto 0.23000
43212 Inserisci valore dell'intervallo temporale t:
//Teraterm


If you want to repeat the operation with another string : "thomas-come-stai-tutto-bene"

//Teraterm
thomas-come-stai- 
Inserisci valore dell'intervallo temporale t: 0.2800
Ricevuto 0.28000
43212 Inserisci valore dell'intervallo temporale t:
//Teraterm

#include "mbed.h"

Serial pc(USBTX, USBRX); // tx, rx   

char stringa[200];

int seq_ph[200];  
struct  phoneme{               
        char poss_ph[25][4];
        int lips;
    };
phoneme vect_ph[20];  

struct trsz2{           //struct in cui si possono salvare gli indici di 2 immagini
        int indx [2];   
    };
    
trsz2 mat_trsz2[7][7];  //matrice in cui salvo gli indici delle immagini che uso per realizzare l'algoritmo con 2 immagini intermedie

    
int mat_trsz1[7][7];   //matrice in cui salvo gli indici delle immagini che uso per realizzare l'algoritmo con 1 immagine intermedia

int image_index; //indice che uso per scorrere le immagini nella visualizzazione

int vett_fin[600];  //vettore finale in cui salvo tutti gli indici delle immagini, anche quelle intermedie

float t=0.08;  //intervallo tra la visualizzazione dell'immagine di un fonema e l'immagine del fonema successivo
float durata=0.08; //durata della visualizzazione della singola immagine

void italian_ph()
{   
    for (int i=0; i<20; i++)   
   {
       for (int j=0; j<25; j++)
            for (int k=0; k<4; k++)
            {
                vect_ph[i].poss_ph[j][k]='\0';    
            }
        vect_ph[i].lips=0;  
    }                        
    vect_ph[0].poss_ph[0][0]='a';    
    vect_ph[0].lips=1;
       
    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;
       
    vect_ph[2].poss_ph[0][0]='e';
    vect_ph[2].poss_ph[1][0]='i';
    vect_ph[2].lips=3;
        
    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;
        
    vect_ph[4].poss_ph[0][0]='l';
    vect_ph[4].poss_ph[1][0]='r';
    vect_ph[4].lips=5;
        
    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;
        
    vect_ph[6].poss_ph[0][0]='-';
    vect_ph[6].poss_ph[1][0]='.';
    vect_ph[6].lips=7;
    
    return;
}

//Questa funzione riempie le matrici in cui salvo gli indici delle immagini intermedie
void riemp_mat()
{   
    for (int i=0; i<7; i++)
        for (int j=0; j<7; j++){
            mat_trsz1[i][j]=0;      //inizializzo la matrice con gli indici per l'algoritmo con 1 immagine intermedia
            for (int z=0; z<2; z++)
                mat_trsz2[i][j].indx[z]=0;   //inizializzo la matrice con gli indici per l'algoritmo con 2 immagini intermedie
            }   
    
    
    int increm1=0, increm2=0;
    int start_riga=0,start_col=1;       
    for (int i=start_riga; i<6; i++)    //riempio solo la parte delle matrici sopra la diagonale, tanto sono simmetriche
    {                                   
        for (int j=start_col; j<7; j++){
            mat_trsz1[i][j]=8+increm1;  //parto dall'indice 8 perchè le immagini da 1 a 7 corrispondono alle 7 immagini associate ai 7 gruppi di fonemi      
            increm1++;                  
            for (int z=0; z<2; z++)
            {
                mat_trsz2[i][j].indx[z]=8+increm2;
                increm2++;
            }
        }
        start_col++;
    }
    
    return;
}


void control ()
{
    int z=0;
    bool ok3=false, ok2=false, ok1=false;
    char temp[4];
    for (int q=0; q<4; q++)
         temp[q]='\0';
    int index_seq=0;
    
    while (stringa[z]!=0) 
    {   
        if (ok3==false && z<197)
        {
            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;                              
                                seq_ph[index_seq]=vect_ph[k].lips;     
                                pc.printf("%d",(seq_ph[index_seq]));
                                index_seq++;                           
                                z=z+3;                                
                              
                            }
                }
            }
        }
        if (ok3==false && ok2==false && z<198) 
        {
            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;                               
                            seq_ph[index_seq]=vect_ph[k].lips;      
                            pc.printf("%d",(seq_ph[index_seq]));
                            index_seq++;                           
                            z=z+2;   
                                                         
                        }
                }
            }
        }     
        if (ok3==false && ok2==false && ok1==false && z<199) 
        {   
            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;                                       
                        seq_ph[index_seq]=vect_ph[k].lips;             
                        pc.printf("%d",(seq_ph[index_seq]));
                        index_seq++;                                    
                        z=z+1;  
                                                              
                    }
                }
            }
            
        }
        
        if (ok3==false && ok2==false && ok1==false)  // new
        z++;                                            // new
        
        ok3=false;
        ok2=false;
        ok1=false;      
    }
    
    return;
}


//Questa funzione mi permette di inserire l'intervallo di tempo che deve intercorrere tra la visualizzazione delle immagini associate a due fonemi consecutivi
void insert_t()
{   
    pc.printf("Inserisci valore dell'intervallo temporale t: ");
    pc.scanf("%f",&t);
    pc.printf("\nRicevuto: %f \n",t);
    
    return;  
}   

//Questa funzione crea un vettore in cui sono presenti anche gli indici delle immagini intermedie, a partire dal vettore degli indici delle immagini fondamentali
void control_vett_fin()
{   
    
    for (int i=0; i<600; i++)  //inizializzo il vettore
        vett_fin[i]=0;
     
    int position=0;
    int pos_fin=0;
    
    //a secondo dell'intervallo t, uso due, una o nessuna immagine intermedia
    if (t>=0.36)   //in questo caso ne uso 2
    {
        while (seq_ph[position]!=0 && position<198) //fino a che il vettore degli indici dei fonemi non termina oppure sforo la sua dimensione
        {   
            vett_fin[pos_fin]=seq_ph[position];   //inizialmente carico il primo indice trovato nel nuovo vettore
            pc.printf("%d",(vett_fin[pos_fin]));
            pos_fin++;                            //e mi sposto di una posizione in questo vettore
            int precedente=seq_ph[position];      //salvo l'indice del primo fonema
            position++;                           //e mi sposto di una posizione nel vettore degli indici fondamentali
            int successivo=seq_ph[position];      //salvo l'indice del secondo fonema
        
            if (successivo==0)                    //se non c'è un fonema successivo allora carico la sequenza di immagini per chiudere la bocca
                for (int i=0; i<2; i++) {
                
                    vett_fin[pos_fin+i]=mat_trsz2[precedente-1][6].indx[i];  //la sequenza di immagini per chiudere la bocca è sempre all'ultima colonna delle matrice
                    pc.printf("%d",(vett_fin[pos_fin+i]));
                }
            else                                                             //N.B. C'è "precedente-1" perchè gli indici delle immagini salvate nella SD parte da 0 e non da 1 come stiamo suppponendo in questo programma 
            {
                if (precedente==successivo)
                {
                    for (int i=0; i<2; i++) {
                    
                        vett_fin[pos_fin+i]=precedente;  //in questo caso ricopio due volte lo stesso indice
                        pc.printf("%d",(vett_fin[pos_fin+i]));
           }
                }
                else if (precedente<successivo)
                {
                    for (int i=0; i<2; i++) {
                    
                        vett_fin[pos_fin+i]=mat_trsz2[precedente-1][successivo-1].indx[i];   //sto sopra la diagonale e quindi prendo gli indici che trovo nella locazione indicata da precedente e successivo
                pc.printf("%d",(vett_fin[pos_fin+i]));
            }
                }
                else if (precedente>successivo)
                {
                    for (int i=0; i<2; i++) {
                    
                        vett_fin[pos_fin+i]=mat_trsz2[successivo-1][precedente-1].indx[1-i];  //starei sotto la diagonale, quindi basta invertire indici di riga e colonna per spostarsi sopra la diagonale e poi prendo gli indici in ordine inverso
                        pc.printf("%d",(vett_fin[pos_fin+i]));      
                   }
                }
                pos_fin=pos_fin+2;    //mi sposto di 2 posizioni nel vettore finale
            }  
        }
        durata=t/3;    //in questo caso ho il tempo per visualizzare 3 immagini
    }
    else 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)
        {
            vett_fin[pos_fin]=seq_ph[position];
            pc.printf("%d",(vett_fin[pos_fin]));           
            pos_fin++;
            position++;
           
        }
        
         durata=t;
    
    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]='\x0'; 

pc.printf(stringa);

   }

int main() {

  pc.baud(115200); 
  
  italian_ph();
  riemp_mat(); 
  
  while(1){    
    insert();
    control();
    insert_t();
    control_vett_fin();    
}
}

Program to send the string from c ++

#include "string.h"

#include "windows.h"

#include "iostream"
#include "tchar.h"
#include "stdint.h"


using namespace std;

HANDLE SerialPort;

char stringa[200]; //stringa di ingresso
int count=0;


void Insert()
{     
        printf("\nInserire stringa: ");
        scanf("%s", stringa);
        printf("\nStringa inserita con successo\n");

}

int Open () {
	
	DCB dcb;
    BOOL fSuccess;
    TCHAR *pcCommPort=TEXT((TCHAR*)"COM4"); 
     
    SerialPort=CreateFile(pcCommPort,GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

    if (SerialPort == INVALID_HANDLE_VALUE){
	printf("CreateFile failed with error %d.\n", GetLastError());
	//return (1);
}

   SecureZeroMemory(&dcb, sizeof(DCB));
   dcb.DCBlength = sizeof(DCB);
   
   fSuccess = GetCommState(SerialPort, &dcb);

   if (!fSuccess) 
   {
      printf ("GetCommState failed with error %d.\n", GetLastError());
     // return(2);
   }
   
   //  115200 bps, 8 data bits, no parity, and 1 stop bit.
   dcb.BaudRate = CBR_115200;     //  baud rate
   dcb.ByteSize = 8;             //  data size, xmit and rcv
   dcb.Parity   = NOPARITY;      //  parity bit
   dcb.StopBits = ONESTOPBIT;    //  stop bit
   
   fSuccess = SetCommState(SerialPort, &dcb);

   if (!fSuccess) 
   {
      printf ("SetCommState failed with error %d.\n", GetLastError());
     // return(3);
   }
   
   fSuccess = GetCommState(SerialPort, &dcb);

   if (!fSuccess) 
   {
      
      printf ("GetCommState failed with error %d.\n", GetLastError());
      //return(2);
   }
   
    printf (TEXT("Serial port %s successfully reconfigured.\n"),pcCommPort);
    //return(0);
}
 
void Write() {
	
	BOOL write;
	DWORD written=0;
	count=strlen(stringa);
	write=WriteFile(SerialPort, stringa, count, &written, NULL);
	if(write){
		
		cout<<"\n\n"<<written;
	}
	
}    


int main() {
	
	Open();
	Insert();
	Write();
	
}
posted by Thomas Villacci 25 Aug 2017

1 Answer

6 years, 8 months ago.

When you recieve the string you insert a '\x0' Charaktar at the end of the string(line 354) . But in the control routine you search for a 0 to terminate the string(line 131). So my best guess is youre routine is running through memory forever. Hope that is the problem, have a nice day.