Précédent   Forum du club des développeurs et IT Pro > Général Développement > Programmation système > Linux
Linux Forum d'entraide sur la programmation Linux : shell, système, ...
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 20/11/2012, 14h38   #1
ABouras
Invité régulier
 
Homme
Étudiant
Inscription : juin 2012
Messages : 46
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : juin 2012
Messages : 46
Points : 9
Points : 9
Par défaut Lecture /dev/ttyUSB0 en C

Bonjour!

Je voudrais lire des trames de données envoyées par un GPS par protocole XBee. La clé USB XStick reçoit les données suivantes :
Code :
1
2
3
CHARS : 15931    SENTENCES = 0    CHECKSUM : 58
Heading : 55    Tilt: -46    Roll:2
CHARS : .....
et ainsi de suite ... J'arrive à les lire en tapant dans le terminal la commande :
Moi j'aimerais pouvoir afficher ces corrdonnées de la même manière, mais avec un programme écrit en C. Voici ce que j'ai fais :
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
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "serial_port.h"
 
void read_Serial_Port(const char* DEVICE_PORT)
{
	int file;
	struct termios options;
	char message[100];
	unsigned int nCountMax = 60;
	bool b;
 
	file = open(DEVICE_PORT, O_RDONLY | O_NOCTTY | O_NDELAY);
 
	if(file == -1){perror("Unable to open the serial port\n");}
	printf("Serial port open successful\n");
 
	tcgetattr(file, &options); 			
	cfsetispeed(&options, B9600); 					
	cfsetospeed(&options, B9600); 					
	options.c_cflag |= (CLOCAL | CREAD); 			  
	options.c_cflag |= PARENB; 						//No parity					
	options.c_cflag |= PARODD; 						
	options.c_cflag &= ~CSTOPB; 					
	options.c_cflag &= ~CSIZE; 						
	options.c_cflag |= CS8; 						//8 bits					
	options.c_iflag |= (INPCK | ISTRIP); 			
	tcsetattr(file, TCSANOW, &options); 	     
	fcntl(file, F_SETFL, FNDELAY);			
 
	printf("Reading serial port ...\n\n"); 
	b = readMessage(file, message, nCountMax);
	if (b == 0){printf("Error while reading serial port\n");}
	else printf("Serial port read successful\n");
	close(file);
	printf("Serial port closed\n");
};
 
bool readMessage(int file, unsigned int nCountMax)
{
    int i;
    size_t nbytes;
	ssize_t bytes_read;
 
    if (file != -1)
    {
	    i = 0;  
	    char message[100];
	    char data[100];
		while (i<nCountMax && data != ".")
		{
		    if (read(file, data, 1) == -1)
		    {
			    printf("reception error\n");
			    printf("code errno = %d\n", errno);
			    return false;
		    }
		    else
		    {   
				nbytes = sizeof(data);
				bytes_read = read(file, data, nbytes);
			    message[i] = *data;
			    printf("%c", message[i]);
			    i++;
		    }
		}
	    message[i] = 0;
	    return true;
    }
};
Mais ça ne fonctionne pas, il me sors "reception error" correspondant au cas où
Code :
read(file,&data,1) = -1
. Et le code errno est 11, c'est à dire "try again" ...

Pouvez-vous m'aider svp ?

Merci d'avance !
ABouras est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 20/11/2012, 17h23   #2
ABouras
Invité régulier
 
Homme
Étudiant
Inscription : juin 2012
Messages : 46
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : juin 2012
Messages : 46
Points : 9
Points : 9
Mon programme 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
bool readMessage(int file, unsigned int nCountMax)
{
    int i;
    size_t nbytes;
	ssize_t bytes_read;
 
    if (file != -1)
    {
	    i = 0;  
	    char message[100];
	    char data[100];
		while (i<nCountMax && data != ".")
		{
		    if (read(file, data, 1) == -1)
		    {
			    printf("reception error\n");
			    printf("code errno = %d\n", errno);
			    return false;
		    }
		    else
		    {   
				nbytes = sizeof(data);
				bytes_read = read(file, data, nbytes);
			    message[i] = *data;
			    printf("%c", message[i]);
			    i++;
		    }
		}
	    message[i] = 0;
	    return true;
    }
};
Cette fois-ci, plus d'erreurs, mais les caractères affichés sont étranges :
Code :
1
2
3
4
$$$$QUC
U$C
$$$$JQMJ'	J$Cz(HSQ'Q'y
UKUNiQUMJ
les dollars $$$$ sont représentés par des carrés contenant 4 chiffres, bref .... je rappel que je voudrais avoir :
Code :
1
2
3
CHARS : 15931    SENTENCES = 0    CHECKSUM : 58
Heading : 55    Tilt: -46    Roll:2
CHARS : .....
J'ai essayé en mettant %c, %d, %x dans le printf, mais évidemment aucun ne fonctionne correctement ...

Merci !
ABouras est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 21/11/2012, 10h45   #3
ABouras
Invité régulier
 
Homme
Étudiant
Inscription : juin 2012
Messages : 46
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : juin 2012
Messages : 46
Points : 9
Points : 9
Bonjour, j'ai légèrement modifié mon code :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
bool readMessage(int file, unsigned int nCountMax) { int i; size_t nbytes; ssize_t bytes_read;
 
    if (file != -1) 
    { 
     i = 0;   
     char message[100] = {0}; 
     char data[1] = ""; 
     nbytes = sizeof(data); 
 
  while (i<nCountMax) 
  { 
   bytes_read = read(file, data, 1); 
      if (bytes_read >0) 
      {    
       message[i] = *data; 
       printf("%c", message[i]); 
       i++; 
      } 
  } 
     message[i] = 0; 
     return true; 
    } 
}
J'obtiens sur le prompteur ceci :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
Start read_Serial_Port function ...
Serial port open successful !
Reading serial port ...
 
CJ*$    CzQC*H
Q$U$u[-CJQ$U$[CJ
CJQ$U$0
 
S
SCJMQ$U$
Serial port read successful
Serial port closed
Mais les caractères sont incompressibles ... J'ai essayé de modifier la vitesse en Baud mais j'ai toujours ces caréctères !

Autre chose, je remarque qu'en faisant cat /dev/ttyUSB0, j'obtiens les même caractères bizarres, mais si je fais un screen /dev/ttyUSB0, j'obtiens les bonnes données ... Comment faire pour afficher correctement mes données comme avec la commande screen au lieu de cat ?
ABouras est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 21/11/2012, 11h15   #4
ABouras
Invité régulier
 
Homme
Étudiant
Inscription : juin 2012
Messages : 46
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : juin 2012
Messages : 46
Points : 9
Points : 9
C'est bon ça marche !!!!!!!!!!!!!!!!!! merci beaucoup !!!!!!!!!

Dernière chose, j'aimerais recevoir les données :

Code :
1
2
3
CHARS : 15931    SENTENCES = 0    CHECKSUM : 58 
Heading : 55    Tilt: -46    Roll:2 
CHARS : .....

de manière régulière, bloc par bloc. Là je reçois genre une seule fois, 60 caractères (j'ai mis nCountMax = 60 dans la fonction pour essayer) :

Code :
1
2
: -47 Roll: 1 
CHARS : 3CHARS : 3809  SENTENCES : 0  CHECKSU


puis si je réexecute :

t: -46 Roll: 1
CHARS : CHECKSUM : 53
Heading: 53 Tilt: -46



Bref ... comment faire pour recevoir les données régulièrement ? Il faudrait que j'affiche les bloc en commençant par CHARS, puis que le bloc s'arrête sur le nombre de Roll, ensuite il recommence sur CHARS, ainsi de suite ...

Ensuite j'essayerai d'écrire ces données dans un fichier texte proprement de cette manière :

CHARS : 15931 SENTENCES = 0 CHECKSUM : 58
Heading : 55 Tilt: -46 Roll:2
CHARS : 15931 SENTENCES = 1 CHECKSUM : 59
Heading : 55 Tilt: -46 Roll:1
CHARS : 15931 SENTENCES = 0 CHECKSUM : 58
Heading : 54 Tilt: -45 Roll:1
...

Merci pour vos prochaines réponses !
ABouras est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 21/11/2012, 13h06   #5
ABouras
Invité régulier
 
Homme
Étudiant
Inscription : juin 2012
Messages : 46
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : juin 2012
Messages : 46
Points : 9
Points : 9
Merci pour vos réponses, c'est bon ça fonctionne.
Voici mon code fonctionnel pour ceux qui en auraient besoin :
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
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "serial_port.h"
 
void read_Serial_Port(const char* DEVICE_PORT)
{
	int file;
	struct termios options;
	unsigned int nCountMax = 60;
	bool b;
 
	file = open(DEVICE_PORT, O_RDONLY | O_NOCTTY | O_NDELAY);
 
	if(file == -1){perror("Unable to open the serial port\n");}
	printf("Serial port open successful !\n");
 
	tcgetattr(file, &options); 			
	cfsetispeed(&options, B9600); 					
	cfsetospeed(&options, B9600); 					
	options.c_cflag |= (CLOCAL | CREAD); 			  
	options.c_cflag |= PARENB; 					
	options.c_cflag |= PARODD; 						 					
	options.c_cflag &= ~CSIZE; 						
	options.c_cflag |= CS8;			
 
	printf("Reading serial port ...\n\n"); 
	b = readMessage(file, nCountMax);
 
	if (b == 0){printf("Error while reading serial port\n");}
	else printf("\nSerial port read successful\n");
 
	close(file);
	printf("Serial port closed\n");
};
 
bool readMessage(int file, unsigned int nCountMax)
{
    int i,j;
	ssize_t bytes_read;
 
    if (file != -1)
    {
	    i = 0;
	    char data[1] = "";
 
		while (i<nCountMax)
		{
			bytes_read = read(file, data, sizeof(data));
			if (bytes_read > 0)
			{
			   	printf("%c", data[0]);
			   	i += bytes_read;
			}
		}
	    return true;
    }
};
Je pense pouvoir me débrouiller pour la suite ... encore merci !
ABouras est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Cette discussion est résolue.
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 15h09.


 
 
 
 
Partenaires

Hébergement Web