[C] Lecture de caractère sur port série
salut à tous;
je ne suis pas un bon développeur en C c'est pour cela je compte beaucoup sur votre aide.
Le problème(pb) consiste à récupérer des caractères envoyés par un microcontroleur sur un port série et l'enregistrer dans un fichier texte.
j'ai commencé par développer qui permet la lecture des caractères sur la console /dev/pts/2.
le programme que j'ai développé est le suivant :
Code:
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
|
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
/*
* 'open_port()' - Open serial port 1.
*
* Returns the file descriptor on success or -1 on error.
*/
int open_port(void)
{
int fd; /* File descriptor for the port */
fd = open("/dev/TTYS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
/*
* Could not open the port.
*/
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
fcntl(fd, F_SETFL, 0);
return (fd);
}
FILE *fichier;
struct capeur
{ int num;
char test_led;
char test_flash;
char test_cc1100;
}cap ;
int main(void)
{
char tab[200];
char Car ;
int Res;
char *pChaine;
printf("bonjour \n");
int s;
s=open_port();
fichier= fopen("fichier.txt", "w");
int i=0;
int l=0;
int suite=1;
while(1 && (suite==1) )
{
Res = read(s, &Car, 1);
printf("walid \n");
//printf("%d",Res);
if (Res > 0)
{
//pChaine[i] = Car;
//printf("%c",Car);
tab[i]=Car;
fprintf(fichier,"%c \n",Car);
i++;
}
else {
printf("erreur");
suite=0;
fclose(fichier);
break;}
}
int h=1;
int k=0;
fprintf(fichier,"%s",tab);
fclose(fichier);
close(s);
printf("%i \n ",i);
/*fichier= fopen("fichier.txt", "w");
while ((k+2)<=i)
{cap.num=h;
cap.test_led=pChaine[k];
cap.test_flash=pChaine[k+1];
cap.test_cc1100=pChaine[k+2];
h++;
k=k+3;
fprintf(fichier,"%d %c %c %c \n",cap.num,cap.test_flash,cap.test_led,cap.test_cc1100); }
fclose(fichier);*/
printf("le contenu de la chaine est \n");
return 0;
} |
le problème(pb) est le suivant:
si je tape des caractères directement sur /dev/pts/2 ils sont enregistrés dans le fichier.
mais si j'exécute un programme(pg) qui permet d'envoyer des caractères et je redirige le résultat sur /dev/pts/2 ==> :evilred: aucun caractère ne s'enregistre.
merci;