Port serie

maurejac Messages postés 2 Date d'inscription mercredi 28 juin 2006 Statut Membre Dernière intervention 4 décembre 2006 - 4 déc. 2006 à 17:51
maurejac Messages postés 2 Date d'inscription mercredi 28 juin 2006 Statut Membre Dernière intervention 4 décembre 2006 - 4 déc. 2006 à 17:53
bonjour,


je dois realiser un petit programme de communication avec une carte par
port serie, la plupart du temps il marche tres bien mais de temps en
temps il ratte des caracteres en reception: le nombre de caracteres
retournes par cbInQue est de 4 mais readfile en lit 0....

y'a t il quelque part une indication qui me permettrai de trouver ou sont passés ces caracteres?


merci d'avance

mathieu


voici le programme en question  je sais ca resemble un peu a rien
mais depuis le temps que je cherche d'ou vient le pb j'ai fait des
modifs partout et j'ai pas encore fait le menage.


dcb.fInX = FALSE;

            dcb.fOutX = FALSE;

            dcb.fOutxCtsFlow = FALSE;

            dcb.fOutxDsrFlow = FALSE;

            dcb.fDsrSensitivity = FALSE;

            dcb.fTXContinueOnXoff = TRUE;

            dcb.fRtsControl = RTS_CONTROL_ENABLE;

             dcb.fDtrControl = DTR_CONTROL_ENABLE;

            break;


e_ErrCom OuvreCom(char *strPort,long BaudRate,int BitsSize,int Parity,int StopBits)

{

g_ErrCom = e_ErrCom_None;


UINT m_nInputBufferSize=1050;

UINT m_nOutputBufferSize=1050;

DWORD m_EventMask;

COMMTIMEOUTS m_ComTimeouts;


// On ouvre le port série

g_hCom = CreateFile(strPort,GENERIC_READ |
GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_WRITE_THROUGH |
FILE_FLAG_NO_BUFFERING,NULL);

ClearCommBreak(g_hCom);


FillMemory( &g_DCB,0, sizeof(g_DCB));


GetCommState (g_hCom, &g_DCB);


// On paramètre le port série


g_DCB.fBinary=1;

g_DCB.fParity=0;

g_DCB.fOutxCtsFlow=0;

g_DCB.fOutxDsrFlow=0;

g_DCB.fDtrControl=0;

g_DCB.fDsrSensitivity=0;

g_DCB.fTXContinueOnXoff=1;

g_DCB.fOutX=0;

g_DCB.fInX=0;

g_DCB.fErrorChar=0;

g_DCB.fNull=0;

g_DCB.fRtsControl=1;

g_DCB.fAbortOnError=1;

g_DCB.fDummy2=0;

g_DCB.DCBlength = sizeof(DCB);

g_DCB.BaudRate=BaudRate;

g_DCB.ByteSize=BitsSize;

g_DCB.Parity=Parity;

g_DCB.StopBits=StopBits;

g_DCB.fDtrControl=DTR_CONTROL_DISABLE;


BuildCommDCB( TEXT("9600,n,8,1"), &g_DCB);

//Configuration de la liaison serie

SetCommState(g_hCom,&g_DCB);


if(g_hCom == INVALID_HANDLE_VALUE)

{

// Echec

g_ErrCom=e_ErrCom_Creation;


}

else

{


SetupComm( g_hCom, m_nInputBufferSize ,m_nOutputBufferSize);


::GetCommMask(g_hCom, &m_EventMask);

::SetCommMask(g_hCom, 0);


// GetCommState (g_hCom, &g_DCB);


// On vide les buffers

PurgeComm(g_hCom,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);


// GetCommState (g_hCom, &g_DCB);


GetCommTimeouts(g_hCom, &m_ComTimeouts);


m_ComTimeouts.ReadIntervalTimeout=1000;

m_ComTimeouts.ReadTotalTimeoutConstant=1000;

m_ComTimeouts.ReadTotalTimeoutMultiplier=1000;

m_ComTimeouts.WriteTotalTimeoutConstant=1000;

m_ComTimeouts.WriteTotalTimeoutMultiplier=1000;


SetCommTimeouts (g_hCom, &m_ComTimeouts);


}

return g_ErrCom;

}

1 réponse

maurejac Messages postés 2 Date d'inscription mercredi 28 juin 2006 Statut Membre Dernière intervention 4 décembre 2006
4 déc. 2006 à 17:53
erreur dans le message precedent, que je sais pas comment modifier: le code source n'est pas le bon, celui ci est le bon:


 // FICHIERS D'INCLUSION   

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

 #include <windows.h>

 #include <winbase.h>

 #include <stdio.h>

 #include <conio.h>

 #include <string.h>

     

 // CONSTANTES   

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

 

 

 struct fichier

   {

   int
etat_r;                 
//etat de la reception

   int
ndccarte;              
//nbre de commandes dans la carte

   int
ndligne;                
//derniere ligne envoyée

   FILE
*ptr_f;                
//pointeur vers le fichier de commandes

   int
nlv;                     
//nombre de lectures vides successives (pas de caracteres a lire)

   char
*ligne[10][30];         
//tableau des 10 dernieres lignes envoyées

   int fin_fichier;

  

   };

 

 // Définition du code d'erreurs   

 typedef enum   

 {   

     e_ErrCom_None,        // Pas d'erreur   

     e_ErrCom_Creation,    // Erreur lors de la création du flux   

    
e_ErrCom_Utilise,        // Le port
com est déjà utilisé   

     e_ErrCom_Inexistant,    // Le port com n'existe pas   

     e_ErrCom_Timeout,    // Timeout lors d'une émission-réception   

    
e_ErrCom_Emission,        // Erreur
lors de l'émission   

    
e_ErrCom_Reception,        // Erreur
lors de la réception   

     e_ErrCom_Definition_Trame,    // Erreur de définition de la trame   

     e_ErrCom_Nack,    // Demande non prise en coompte   

     e_ErrCom_Checksum        // Erreur de checksum   

 } e_ErrCom;   

     

 // Nom du port série   

 #define PORT1        "COM1"   

 #define PORT2        "COM2"   

     

 // Définition des vitesses de communication   

 #define V1200        1200   

 #define V2400        2400   

 #define V4800        4800   

 #define V9600        9600   

     

 // Définition du nombre de bits   

 #define BITS_7    7   

 #define BITS_8     8   

     

 // Définition du nombre de bits de stop   

 #define BIT_DE_STOP_1    ONESTOPBIT     

 #define BIT_DE_STOP_2    TWOSTOPBIT     

     

 // Définition de la parité   

 #define PAS_DE_PARITE    NOPARITY    

 #define PARITE_IMPAIRE    ODDPARITY   

 #define PARITE_PAIRE    EVENPARITY     

     

 // Codes de retour génériques   

 #define OK 1   

 #define KO 0   

     

 // Longueur max réservée pour une trame   

 #define LG_TRAME    100   

     

 // PROTOTYPES   

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

 e_ErrCom OuvreCom(char *strPort,long BaudRate,int BitsSize,int Parity,int StopBits);   

 e_ErrCom EmissionCom(const char *lpBuf);   

 e_ErrCom ReceptionCom(unsigned char *lpBuf, unsigned int nCountMax, unsigned int *pCountRead);   

 void FermeCom();   

 int    envoi_commande(struct fichier *commande, FILE *P_s);

 int reception_commande(struct fichier *commande, FILE *P_s);

 int arret_urgence(struct fichier *commande, FILE *P_s);

 // VARIABLES GLOBALES   

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

 DCB g_DCB; // structure dcb du port   

 e_ErrCom g_ErrCom= e_ErrCom_None;    // Variable des erreurs de com   

 HANDLE g_hCom =
0;            //
handle de la com   

     

     

int main (int argc, char *argv[])   

 {   

         

unsigned char TrameRecue[100];

unsigned int NbMaxCar=100;

unsigned int NbCarRecus;


 

 

 struct fichier *commandes=malloc(sizeof( struct fichier));

  int i=0,n, c=0,j,choix, ncr=0, fin=1;

 FILE *P_sortie;

      FILE *P_s;

  int *Nau;

 char schoix[5]="     ";


 commandes->ndccarte=0;

 commandes->ndligne=0;

 commandes->etat_r=0;

 commandes->fin_fichier=0; 

 P_s=fopen("envoi.txt","w+");                             
//ouverture des fichiers


 P_sortie=fopen("recu.txt","w");

     if (commandes->ptr_f=fopen(argv[1],"r"))

    {


        //printf ("fichier %s ouvert!\n",argv[1]);

  }

    else

        {

        printf ("fichier %s pas ouvert!\n",argv[1]);

        system("pause");

        return -1;

        }


     //Exemple de configuration du port serie :   

     g_ErrCom=OuvreCom(PORT1,V9600,BITS_8,PAS_DE_PARITE,BIT_DE_STOP_1);   

     if(g_ErrCom!=0){

                    
printf("erreur d'ouverture du port \n");

                    
system("pause");

                    
return 0;

                     }

     // configuration du fin de course x comme arret d'urgence

     EmissionCom("PP 02\r");

     EmissionCom("PM 02\r");

     g_ErrCom=ReceptionCom(TrameRecue,NbMaxCar,&NbCarRecus);

    

     //envoi des 5 premieres commandes

    

     for(i=0;i<5;i++)

                      
if(envoi_commande(commandes,P_sortie)==0) i=10;

                      


              

     //boucle principale

                                    


     while(1){

                                        


                
//printf("%i commandes dans la carte\n",commandes->ndccarte );

                
ncr=reception_reponse(commandes,P_sortie);

                 //printf("ncr=%d\n",ncr);

                
if(ncr==-1) { //gestion de l'arret d'urgence


                            
if(arret_urgence(commandes,P_sortie)==-1){

                                                                      
printf("fin apres arret d'urgence\n");

                                                                      
system("pause");

                                                                      
return 1;

                                                                      
}

                            
}

                
if(commandes->fin_fichier==0){           


                    
for(j=0;j< ncr;j++){

                           
n=envoi_commande(commandes,P_sortie);

                           
if(n==0) {

                                    
printf("fin du fichier\n");

                                    
fprintf(P_sortie, "fin du fichier\n");

                                    
commandes->fin_fichier=1;

                                    
system("pause");

                                    
//return 1;

                                    
}// fin if

                           
}//fin for

                           


                }//fin if

                else{

                    
fprintf(P_sortie, "fichier fini, on attend la fin de l'execution");

                     
if(commandes->ndccarte==0) return 1;

                     
}//fin else          


                 }

   

     //g_ErrCom=ReceptionCom(TrameRecue,NbMaxCar,NbCarRecus);

     printf("fini\n"   );


     system("pause");

    

     return 1;   

     

 }   

 

 

  /* fonction de gestion de l'arret d'urgence

  1er parametre: structure du fichier de commandes

  2eme parametre: pointeur vers un fichier pour les log

  retourne 1 si le programme a repris, -1 si on doit arreter

  */   

 

 int arret_urgence(struct fichier *commande, FILE *P_s){

 char schoix[10]="0000000000";

     char buffer[20]="00000000000000000000",
com[40]="0000000000000000000000000000000000000000", c=32,
envoi[30]="000000000000000000000000000000";


 int choix=0,n=0,l=0,i=0, renvoi;

 printf("arret d'urgence active avec %d commandes dans la carte\n\n", commande->ndccarte);

 printf("1. retour a l'origine\n");

 printf("2. reprise du fichier à la ligne %d\n", commande->ndligne-commande->ndccarte+1);

 printf("3. arret sur place, création d'un fichier avec les commandes restantes");

 

 while(n==0){

              scanf("%s",schoix);

              choix=atoi(schoix);

              printf("choix=%d",choix);

              n=1;

              switch(choix){

                           
case 1 :

                                
printf("retour a l'origine\n");

                                
EmissionCom("M0 X0 Y0");

                                
system("pause");

                                


                                
return -1;

                           
case 2 :

                                
printf("reprise du fichier \n\n!! marche pas encore !!\n\n");

                                
/*renvoi=commande->ndccarte;

                                
for(i=renvoi;i!=0;i--){

                                                                   


                                                         
sprintf(envoi,"%s\rPI\r",commande->ligne[i-1]); 

                                                           
if(EmissionCom(envoi)==0){

                                                           
fprintf(P_s,"ligne envoyée correctement: |%s|\n",
commande->ligne[0]);

                                                        
fprintf(P_s,"\n");

                                                       
for(i=0;i<10;i++)

                                                       
fprintf(P_s,"|%s",commande->ligne[i]);

                                                       
fprintf(P_s,"\n");

                                                       
fprintf(P_s,"\n");

                                                                 


                                                           
//printf("ligne envoyée correctement: %s \n", commande->ligne[0]);

                                                           
commande->ndccarte++;

                                                           
commande->ndligne++;}

                                                     
}

                                
system("pause");

                                
*/break;

                           
case 3 :

                                
printf("arret, ecriture du fichier");

                                
fprintf(P_s,"-------------commandes restantes---------------\n");

                                
for(i=commande->ndccarte;i!=0;i--){

                                                     
fprintf(P_s,"%s\n",commande->ligne[i-1]);

                                                     
}

               

                                
while(l==0){

                                      
fscanf(commande->ptr_f,"%s", buffer);

                                       
sprintf(com,"%s", buffer);

                                       
c=fgetc(commande->ptr_f);

 

                                    
while(c==' '){

                                              
fscanf(commande->ptr_f,"%s", buffer);

                                              
printf("%s\t", buffer);

                                              
sprintf(com,"%s %s",com, buffer);

                                              
c=fgetc(commande->ptr_f);

                               


                               


                                                  


                                                    
}// fin du while 2

                               
fprintf(P_s,"%s\n",com);

                               
l=feof(commande->ptr_f);             


                               
//printf("ecriture de la commande %s, l=%d\n",com,l);


         

                               


                               
}// fin du while 1

                               
printf("arret d'urgence fin de la fonction");

                               
system("pause");

                               
return -1;

                          
default:

                                  
printf("choix non valide\n");

                                  
n=0;

                                       


            }// fin du switch

            }//fin du while 0

                               


                                                    


     

               

                                                


             

              printf("choix non valide");

 printf("n=%d",n);

 return 1;

}

 

 

 /*

 fonction de reception des reponses sur le port serie

  1er parametre: structure du fichier de commandes

  2eme parametre: pointeur vers un fichier pour les log

  retourne le nombre de validations de commandes recues, -1 si on a un arret d'urgence

  */


 

 int reception_reponse(struct fichier *commande, FILE *P_s){

    

 COMSTAT Stat;   

 DWORD Errors;   

 unsigned char buffer[50]="00000000000000000000000000000000000000000000000000";   

 unsigned int NbCarRecus;

 unsigned int nCarALire=0;   

 unsigned long NCarLus=0;

 e_ErrCom etat_com=0;

 

 int car;   

 

 int i=0,nc=0;


   

     if(g_hCom!=NULL)   

     {   

        

         sleep(300);   

     

         //Pour connaitre le nombre d'octets dans le buffer d'entrée   

         ClearCommError(g_hCom,&Errors,&Stat);   

         nCarALire=Stat.cbInQue;

         fprintf(P_s,"%d caracteres a lire\n",nCarALire);

       // system("pause");

    commande->nlv++;

    if(commande->nlv>100)      EmissionCom("PI\r");


         //On effectue la lecture si il y a des caractères présents   

         if( nCarALire>0){

    fprintf(P_s,"debut:\nnombre de commandes recues=
%d\n, etat reception=%d,\n nombre de commandes dans la
carte=%d\n\n",nc,commande->etat_r,commande->ndccarte);

          

           etat_com=ReceptionCom(buffer,nCarALire,&NCarLus);

           if(NCarLus!=nCarALire){

                                 
printf("pb de lecture!\nerreur=%d\n",etat_com);

                                 
fprintf(P_s,"-------------------pb de
lecture!-------------------\n%d\n",etat_com);

                                 
system("pause");

                                 
}

           //ReadFile(g_hCom,buffer,nCarALire,&NCarLus,NULL);

           printf("%d caracteres recus:",NCarLus);

           fprintf(P_s,"%d caracteres recus:",NCarLus);

          

           for (i=0;i<NCarLus;i++){

               printf("%c",buffer[i]);

               fprintf(P_s,"%c",buffer[i]);

               }

           printf("\n");

           fprintf(P_s,"\n");   

           for (i=0;i<NCarLus;i++){

              

               car=(int)buffer[i];

               switch(car){

              

               case 62:

                   
if(commande->etat_r==0) commande->etat_r=1;

                   
if(commande->etat_r==6){

                                           
commande->etat_r=0;

                                           
nc++;

                                           
commande->ndccarte--;

                                           
}

                    break;

               case 55:

                   
if(commande->etat_r==1 ||
commande->etat_r==2)commande->etat_r++;

                    break;

               case 70:

                   
if(commande->etat_r==3)

                                          
{

                                          
commande->etat_r++;

                                          
break;

                                          
}

                   
if(commande->etat_r==4){                 
//arret d'urgence detecté

                                            


                                            
return -1;}

                    break;

               case 68:

                   
if(commande->etat_r==4)commande->etat_r++;

                    break;

               case 0:

                   
if(commande->etat_r==5)commande->etat_r++;

                    break;

                   
/*if(commande->etat_r==6)commande->etat_r++;

                    nc++;

                    break;*/

                 

                   
} // fin du switch

              
//printf("caractere %d sur %d %c> reception=
%d\n",i,NCarLus,buffer[i],commande->etat_r);


                     
}// fin du for

    printf("fin:\nnombre de commandes recues= %d\n etat
reception=%d,\n nombre de commandes dans la
carte=%d\n",nc,commande->etat_r,commande->ndccarte);

    fprintf(P_s,"fin:nombre de commandes recues=
%d\netat reception=%d,\n nombre de commandes dans la carte=%d,\n
nlv=%d\n\n\n\n",nc,commande->etat_r,commande->ndccarte,commande->nlv);

    commande->nlv=0;

    //system("pause");


              
}          //fin du if

     

 }  //fin du if 2 

     return nc    ;

}

    

 

 

 

 


int    envoi_commande(struct fichier *commande, FILE *P_s){

   

    char
buffer[20]="                   
", c=32,
envoi[30]="                             
";

    int i=0;


for(i=commande->ndccarte;i>0;i--){

                                
// printf("\ni=%d, %d=>%d, copie de %s dans
%s\n",i,i-1,i,commande->ligne[i-1],commande->ligne[i]);

                                
// system("pause");

                
strcpy(commande->ligne[i],commande->ligne[i-1]);

                 }


    fscanf(commande->ptr_f,"%s", buffer);

    sprintf(commande->ligne[0],"%s", buffer);

    c=fgetc(commande->ptr_f);

              

    while(c==' '){

               fscanf(commande->ptr_f,"%s", buffer);

              
sprintf(commande->ligne[0],"%s %s",commande->ligne[0], buffer);

               c=fgetc(commande->ptr_f);


                     i++;

                     }

    sprintf(envoi,"%s\rPI\r",commande->ligne[0]); 

    if(EmissionCom(envoi)==0){

    fprintf(P_s,"ligne envoyée correctement: |%s|\n", commande->ligne[0]);

 fprintf(P_s,"\n");

//for(i=0;i<10;i++)

         //fprintf(P_s,"|%s",commande->ligne[i]);

//fprintf(P_s,"\n");

//fprintf(P_s,"\n");

         

    //printf("ligne envoyée correctement: %s \n", commande->ligne[0]);

    commande->ndccarte++;

    commande->ndligne++;

}


else

system("pause");

    //printf("%d commandes dans la carte\n",commande->ndccarte );

     if(feof(commande->ptr_f)!=0) return
0;             


  

    return 1;

}


     

     

     

     

     

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

 // FONCTION    : OuvreCom   

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

 // DESCRIPTION     :   

 // Initialise et ouvre un port série   

 //   

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

 // PARAMETRES     :   

 //        -
strPort        Nom du port "COM1",
"COM2"   

 //        -
BaudRate       
Vitesse   

 //        -
BitsSize        Taille de
l'info   

 //        - Parity        Parité   

 //        -
StopBits        Nombre de bits de
stop   

 //   

 // RETOUR     :Code d'erreur   

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

 e_ErrCom OuvreCom(char *strPort,long BaudRate,int BitsSize,int Parity,int StopBits)   

 {   

     g_ErrCom = e_ErrCom_None;   

     

     // On ouvre le port série   

     g_hCom = CreateFile(strPort,GENERIC_READ |
GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_WRITE_THROUGH |
FILE_FLAG_NO_BUFFERING ,NULL);   

     

     if(g_hCom == INVALID_HANDLE_VALUE)   

     {   

         // Echec   

         g_ErrCom=e_ErrCom_Creation;   

     }   

     else   

     {   

         // On vide les buffers   

        
PurgeComm(g_hCom,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);       
   

     

         // On paramètre le port série   

         g_DCB.DCBlength = sizeof(DCB);

         //Configuration actuelle   

         GetCommState(g_hCom, &g_DCB);   

         //Modification du DCB   

         g_DCB.fOutX = FALSE;

         g_DCB.fOutxCtsFlow = FALSE;

         g_DCB.fOutxDsrFlow = FALSE;

         g_DCB.fDsrSensitivity = FALSE;

         g_DCB.fTXContinueOnXoff = TRUE;

         g_DCB.fRtsControl = RTS_CONTROL_ENABLE;

         g_DCB.fDtrControl = DTR_CONTROL_ENABLE;

         g_DCB.BaudRate=BaudRate;

         g_DCB.ByteSize=BitsSize;

         g_DCB.Parity=Parity;

         g_DCB.StopBits=StopBits;

         g_DCB.EvtChar='>';

         g_DCB.fBinary=TRUE;

         //Configuration de la liaison serie   

         SetCommState(g_hCom,&g_DCB);   

     }

   

     return g_ErrCom;   

 }   

     


     

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

 // FONCTION    : EmissionCom   

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

 // DESCRIPTION    :   

 // Emission d'octets sur la liaison série   

 //   

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

 // PARAMETRES    :   

 //        -lpBuf Pointeur sur les octets a emettre   

 //        -nCount Nombre d'octet a emettre   

 //   

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

 // RETOUR    :Code d'erreur   

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

 e_ErrCom EmissionCom(const char* lpBuf)   

 {       

     

     DWORD NumBytes=0;   

      unsigned int nCount;

      nCount=strlen(lpBuf);

     

     if(g_hCom!=NULL)   

     {   

         // On pari sur pas d'erreur   

         g_ErrCom=e_ErrCom_None;   

     

         //Emission du buffer   

         if(WriteFile(g_hCom,lpBuf,nCount,&NumBytes,NULL)==0)   

         {   

             g_ErrCom=e_ErrCom_Emission;   

         }   

     }   

     else   

         //Le port n'a pas été ouvert   

         g_ErrCom=e_ErrCom_Creation;   

      if(g_ErrCom==e_ErrCom_Creation){

                                   


                                        
printf("problème lors de l'envoi de la chaine %s",*lpBuf);

                                        
system("pause");

                                        
}                              


     return g_ErrCom;   

     

 }   

     

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

 // FONCTION    : ReceptionCom   

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

 // DESCRIPTION    :   

 // Reception de caractères sur la liaison série   

 //   

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

 // PARAMETRES    :   

 //        -lpBuf Pointeur sur le buffer de caractère a lire   

 //        -nCountMax Nombre maxi de caractère a lire   

 //        -pCountRead Pointeur sur le nombre de caractères lus   

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

 // RETOUR    :Code d'erreur   

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

 e_ErrCom ReceptionCom(unsigned char *lpBuf,unsigned int nCountMax, unsigned int* pCountRead)   

 {   

 COMSTAT Stat;   

 DWORD Errors;   

 LPDWORD event;

 unsigned int nCarALire=0,e=0;   

 unsigned long NCarLus=0;   

         //Sleep(500);   


   

     if(g_hCom!=NULL)   

     {   

         //on parie sur pas d'erreur   

         g_ErrCom=e_ErrCom_None;   

     

         //Pour éviter de gérer un time out   

         Sleep(200);   

     

         //Pour connaitre le nombre d'octets dans le buffer d'entrée   

         ClearCommError(g_hCom,&Errors,&Stat);   


         nCarALire=Stat.cbInQue;

         printf("nbre de car a lires=%d",nCarALire);


          Sleep(200);

   

         //On effectue la lecture si il y a des caractères présents   

         if( (nCarALire>0)&&(nCarALire<=nCountMax) )   

         {   

            
if(ReadFile(g_hCom,lpBuf,nCarALire,&NCarLus,NULL)==0)   


            
{           

                
g_ErrCom=e_ErrCom_Reception;   

             }   

     

         }   

         *pCountRead=NCarLus;   

     }   

     else   

         //Le port n a pas été ouvert   

         g_ErrCom=e_ErrCom_Creation;   

     
        
PurgeComm(g_hCom,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);       
   

            lpBuf[NCarLus]='\0';

     //Compte rendu de l'exécution   

     printf("reception  %d, %d caracteres recus: %s\n",g_ErrCom,NCarLus, lpBuf );

   //  system("pause");

     return g_ErrCom;   

     

 }   

         

     

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

 // FONCTION    : FermeCom   

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

 // DESCRIPTION    :   

 // Ferme le port série préalablement ouvert avec OuvreCom   

 //   

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

 // PARAMETRES    :   

 //        Néant   

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

 // RETOUR    :Néant   

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

 void FermeCom()   

 {   

     if(g_hCom!=NULL)   

     {   

         CloseHandle(g_hCom);   

     }   

 }   
0
Rejoignez-nous