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
| int main()
{
/* Connexion */
int fd = 0; /* Fichier de déscription du port */
struct termios options; /* Structure contenant les options du port */
cout << endl << "Programme d'acquisition des donnees accelerometre!" << endl;
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY); /* Ouverture du port com ttyUSB0 */
if (fd == -1 )
{
fprintf(stderr,"erreur : %s\n",strerror(errno)); /* Impossible d'ouvrir le port */
}
else
{
cout << "Le port est ouvert, le fichier de description est : " << fd << endl; /* Ouverture du port ok */
}
cfsetispeed(&options, B19200); /* Vitesse de communication */
cfsetospeed(&options, B19200); /* Vitesse de communication */
options.c_cflag |= CS8; /* 8 bits de donnees */
options.c_cflag |= CSTOPB; /* 2 bits de stop */
options.c_cc[VTIME] = 3; /* inter-character timer unused */
options.c_cc[VMIN] = 0;
options.c_lflag = 0; /* set input mode (non-canonical, no echo,...) */
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &options); /* Application de la nouvelle configuration */
/* Configuration */
char trame_config[5];
trame_config[0] = 0x55; // Commande (Lire/Ecrire pour un module avec une adresse sur un octet).
trame_config[1] = 0x3A; // Indique une écriture dans les registre du module accéléromètre
trame_config[2] = 0x31; // Adresse du registre DATA_FORMAT
trame_config[3] = 0x01; // Nombre de octets à écrire
trame_config[4] = 0x0B; // Valeur à écrire
char trame_lecture[4];
trame_lecture[0] = 0x55; // Commande (Lire/Ecrire pour un module avec une adresse sur un octet).
trame_lecture[1] = 0x3B; // Indique une écriture dans les registre du module accéléromètre
trame_lecture[2] = 0x32;
trame_lecture[3] = 0x01;
char retour;
int nombre_carac_write;
nombre_carac_write = write(fd, trame_config, 5);
printf("nombre_carac_write = %d\n", nombre_carac_write);
nombre_carac_write = write(fd, trame_lecture, 4);
printf("nombre_carac_write = %d\n", nombre_carac_write);
int nombre_carac_read;
nombre_carac_read = read(fd, &retour, 1);
printf("nombre_carac_read = %d\n", nombre_carac_read);
printf("reponse : %x\n", retour);
return 0;
} |
Partager