1 2 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
#define BUFFER 15
#define BAUDRATE B19200
/**
* Donnée à traiter
*/
typedef struct s_Config{
int fd;
char *ptr_data;
struct termios oldTerm;
struct termios newTerm;
}ts_Config;
/**
* Fonction d'initialisation
*/
ts_Config *f_Init_Create_new_Config( void ){
ts_Config *ptr = NULL;
ptr = (ts_Config*)calloc( 1, sizeof( ts_Config) );
if( (NULL) == ptr ){
fprintf( stderr, "Erreur allocation new config %s\n",
strerror(errno) );
return ( NULL );
}
ptr->fd = 0;
ptr->ptr_data = NULL;
ptr->ptr_data = (char*)calloc( BUFFER, sizeof(char) );
if( (NULL) == ptr->ptr_data ){
free( ptr );
ptr = NULL;
fprintf(stderr, "Erreur allocation data liaison\n %s\n",
strerror(errno) );
return( NULL );
}
return( ptr );
}
/**
* Configuration
*/
void f_ConfigSaveTerm( ts_Config *ptr_data ){
/* Sauvegarde des configuration */
tcgetattr( ptr_data->fd, &(ptr_data->oldTerm) ); /* Ancienne */
tcgetattr( ptr_data->fd, &(ptr_data->newTerm) ); /* Nouvelle */
/* Ex de config & option nouvelle */
ptr_data->newTerm.c_oflag = 0L;
ptr_data->newTerm.c_lflag = 0L;
ptr_data->newTerm.c_cc[VMIN] = 1;
ptr_data->newTerm.c_cc[VTIME] = 0;
ptr_data->newTerm.c_cflag |= CS8;
ptr_data->newTerm.c_cflag |= CREAD;
ptr_data->newTerm.c_cflag |= CLOCAL;
ptr_data->newTerm.c_lflag = IGNBRK;
ptr_data->newTerm.c_cflag &= ~(CSIZE|PARENB|CSTOPB);
ptr_data->newTerm.c_iflag &= ~(IXON|IXOFF|IXANY);
cfsetispeed( &(ptr_data->newTerm), BAUDRATE );
cfsetospeed( &(ptr_data->newTerm), BAUDRATE );
/* Application de la nouvelle configuration */
tcsetattr( ptr_data->fd, TCSANOW, &(ptr_data->newTerm) );
tcflush( ptr_data->fd, TCIFLUSH );
}
/**
* Destruction de l'ensemble des donnée
*/
void f_FreeConfig( ts_Config *ptr ){
ptr->fd = 0;
free( ptr->ptr_data );
ptr->ptr_data = NULL;
free( ptr );
ptr = NULL;
}
/**
* fonction Close Liaison &
*/
void f_ResetLiaison( ts_Config *ptr ){
tcsetattr( ptr->fd, TCSANOW, &(ptr->oldTerm) );
f_FreeConfig( ptr );
}
/**
* Fonction Principale
*/
int main( void ){
int i_read = 0;
char c_str = '\0';
ts_Config *ptr = NULL;
unsigned int i_Cmpt = 0;
/* Initialisation et configuration */
ptr = f_Init_Create_new_Config();
if( (NULL) == ptr )
exit( EXIT_FAILURE );
f_ConfigSaveTerm( ptr );
/* Ouverture de la liaison */
ptr->fd = open( "/dev/tty0", O_RDWR|O_NOCTTY );
if( (-1) == ptr->fd ){
f_ResetLiaison( ptr );
fprintf( stderr, "Erreur ouverture de la liaison %s\n",
strerror(errno) );
return( EXIT_FAILURE );
}
/* Ex lecture data */
do{
i_read = read( ptr->fd, &c_str, 1 );
if( (-1) == i_read ){
close( ptr->fd );
f_ResetLiaison( ptr );
fprintf(stderr, "Erreur de lecture data %s\n",
strerror(errno) );
exit( EXIT_FAILURE );
}
*( ptr->ptr_data + i_Cmpt) = c_str;
i_Cmpt += 1;
}while( BUFFER > i_Cmpt );
/* Affichage */
printf(">>\t%s\n", ptr->ptr_data );
memset( ptr->ptr_data, 0, BUFFER );
/* fermeture */
close( ptr->fd );
f_ResetLiaison( ptr );
return( EXIT_SUCCESS );
} |
Partager