| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 
 | #define PORT "/dev/ttyS0"
#define FALSE 0
#define TRUE 1
#define TIMEOUT 5            /* en ms */
 
void config_termios(int fd)
{
  struct termios termios_p;
  /* Lecture des parametres courants */
  tcgetattr(fd,&termios_p);
  /* On ignore les BREAK et les caracteres avec erreurs de parite */
  termios_p.c_iflag = IGNBRK | IGNPAR;
  /* Pas de mode de sortie particulier */
  termios_p.c_oflag = 0;
  /* Liaison a 9600 bps avec 8 bits de donnees et une parite paire */
  termios_p.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
  /* Mode non-canonique avec echo */ 
  termios_p.c_lflag = 0;
  /* accès bloquant jusqu'à ce que 1 caractère soit lu sans timeout */
  termios_p.c_cc[VMIN] = 1;
  termios_p.c_cc[VTIME] = 0;
  /* Sauvegarde des nouveaux parametres */
  tcsetattr(fd,TCSANOW,&termios_p);
}
 
void start_timeout(int fd)
{
  struct termios termios_p;  
  tcgetattr(fd,&termios_p); 
  termios_p.c_cc[VMIN] = 0;
  termios_p.c_cc[VTIME] = TIMEOUT; 
  tcsetattr(fd,TCSANOW,&termios_p);
}
 
void stop_timeout(int fd)
{
  struct termios termios_p;  
  tcgetattr(fd,&termios_p); 
  termios_p.c_cc[VMIN] = 1;
  termios_p.c_cc[VTIME] = 0; 
  tcsetattr(fd,TCSANOW,&termios_p);
}
 
int main () {  
  int fd,n,stop;
  int i,j,num_int;
  char recup_port[20],num_str[20];
  char c;
 
  /* Ouverture de la liaison série */
  fd = open(PORT, O_RDWR | O_NOCTTY);
  if (fd <0) {perror(PORT); exit(-1); }
 
  /* Configuration du termios */
  config_termios(fd);
 
  /* Boucle de lecture du port */
  while(1){  
    /* Vide le buffer */
    tcflush(fd, TCIFLUSH);
 
    /* Initialise le caractère et le compteur de caractère */
    c='\0';
    i=0;
    stop=FALSE;
 
    while((c!=0x0a) & (stop==FALSE)){
      /* Récupère un caractère */
      n=read(fd, &c, 1);
 
      if (i==0){
      /* Lance le timeout */
      start_timeout(fd);
      }
 
      if (n>0){                        // si timeout non dépassé
        printf("%c\n",c);        
        /* Concatène le caractère dans le string recup_port */
        recup_port[i]=c;
        i++;
      }
      else{                            // si timeout dépassé -> ereur
        /* Stop le timeout */
        stop_timeout(fd);
        printf("Erreur de lecture carte : TimeOut dépassé\n");
        /* Réinitialise les variables pour faire une nouvelle boucle */
        stop=TRUE;
        c=0x0a;      
      }
    }
 
    /* Stop le timeout */
    stop_timeout(fd);
 
    if (!stop){                        // si pas d'erreur
      ...    /* traitement des données lues */
    }
 
    /* Tempo : évite d'endormir le programme de lecture sur la même carte */
    sleep(1);
 
  }
 
/* Fermeture de la liaion serie */
close(fd);   
 
return 0;
} | 
Partager