IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Linux Discussion :

Programmation série C pour dialoguer avec GPS


Sujet :

Linux

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    64
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 64
    Points : 43
    Points
    43
    Par défaut Programmation série C pour dialoguer avec GPS
    Bonjour,

    Je suis en train de faire un petit programme pour récupérer les lignes NMEA de mon GPS. J'arrive déjà à accéder à ces informations là facilement
    par le biais de minicom, ckermit ou simplement en tapant ces deux commandes :
    `stty /dev/ttyUSB0 4800 cs8 && cat /dev/ttyUSB0`
    Vous l'avez compris (ttyUSB0) je récupère les informations par le biais d'un bridge USB-UART (le bridge cp210x).

    Si je comptais exploiter les lignes NMEA avec un shell script ou un script python cela me conviendrait tout à fait mais je souhaite en fait traiter les lignes NMEA de mon GPS par l'intermédiaire d'un programme codé en C.

    J'ai lu diverses documentations sur la programmation série :http://www.easysw.com/~mike/serial/serial.html
    et
    http://www.faqs.org/docs/Linux-HOWTO...ing-HOWTO.html

    Malgré plusieurs tentative je n'obtiens aucune donnée de la part de mon GPS
    avec mon programme ...

    Est-ce que quelqu'un d'entre vous a déjà eu ce genre de problème ?

    voici mes sources :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #include <stdio.h>
    #include <termios.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <error.h>
    #include <stdlib.h>
     
    int main(void)
    {
    	int fd, result;
    	char buffer[255];
    	struct termios options, storedOptions;
     
    	if((fd = open("/dev/ttyUSB1", O_RDWR | O_NOCTTY) == -1))
    	{
    		perror("ERROR (device not opened) ");
    		exit(EXIT_FAILURE);
    	}
    	else
    	{
    		if(fcntl(fd, F_SETFL, 0) == -1)
    		{
    			perror("ERROR (device problem) ");
    			exit(EXIT_FAILURE);
    		}
     
    		if(tcgetattr(fd, &options) == -1)
    		{	
    			perror("ERROR (port configuration problem) ");
    			exit(EXIT_FAILURE);
    		}
     
    		storedOptions = options;
     
    		// baudrate 
    		if(cfsetispeed(&options, B4800) == -1 || cfsetospeed(&options, B4800) == -1)
    		{
    			perror("ERROR (baudrate setting) ");
    			exit(EXIT_FAILURE);	
    		}
     
    		// enable receiver 
    		options.c_cflag |= CREAD | CLOCAL;
     
    		// line oriented mode 
    		// (notice : be careful to desactivate ECHO before sending something to the devive)
    		options.c_lflag |= (ICANON | ECHO | ECHOE);
    		options.c_oflag |= (OPOST | NL1 | CR2);
     
    		options.c_cflag &= ~PARENB;
    		options.c_cflag &= ~CSTOPB;
    		options.c_cflag &= ~CSIZE;
    		options.c_cflag |= CS8;
     
    		options.c_iflag = IGNPAR;
     
    		options.c_cc[VMIN] = 1;  
     
    		if(tcsetattr(fd, TCSANOW, &options) == -1)
    		{
    			perror("ERROR (port configuration setting) ");
    			exit(EXIT_FAILURE);	
    		}
     
    	        int nodataCnt = 0;
     
    		while(nodataCnt < 200)
    		{
    			result = read(fd, buffer, 255);
    			perror("ERROR? "); // keep returning "Success"
     
    			// read fail
    			if(result == -1)
    			{
    				perror("ERROR (read process) ");
    				exit(EXIT_FAILURE);	
    			}
     
    			// no data
    			if(!result)
    			{
    				printf("no data\n");
    				nodataCnt++;	
    			}
     
    			// data received
    			if(result > 0)
    			{
    				buffer[result] = 0;
    				printf("string : %s number : %d\n", buffer, result);
    			}
    		}
     
    		tcsetattr(fd, TCSANOW, &storedOptions);
     
    		close(fd);
    	}
     
    	return EXIT_SUCCESS;
    }
    merci
    -------------------

    @(zmodai)+

  2. #2
    Responsable 2D/3D/Jeux


    Avatar de LittleWhite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2008
    Messages
    26 858
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 26 858
    Points : 218 577
    Points
    218 577
    Billets dans le blog
    120
    Par défaut
    Bonjour,

    A quel endroit s'arrête votre programme ?
    Vous devez peut être lancé une commande avant de recevoir des données ?
    Vous souhaitez participer à la rubrique 2D/3D/Jeux ? Contactez-moi

    Ma page sur DVP
    Mon Portfolio

    Qui connaît l'erreur, connaît la solution.

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    64
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 64
    Points : 43
    Points
    43
    Par défaut
    Je viens de me rendre compte que j'ai fait une erreure de débutant ...
    En effet dans mon code on ne peut pas réutiliser la variable fd dans le bloque else .... (merci strace pour m'avoir fait remarquer cela)

    Voici la version corrigé de mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
     
    #include <stdio.h>
    #include <termios.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <error.h>
    #include <errno.h>
    #include <stdlib.h>
    #include <string.h>
     
    int main(void)
    {
    	int fd, result;
    	char buffer[255];
    	struct termios options, storedOptions;
     
    	fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
     
    	if(!fd)
    	{
    		perror("ERROR (device not opened) ");
    		exit(EXIT_FAILURE);
    	}
     
    	if(fcntl(fd, F_SETFL, FNDELAY) == -1)
    	{
    		perror("ERROR (device problem) ");
    		exit(EXIT_FAILURE);
    	}
     
    	if(tcgetattr(fd, &options) == -1)
    	{	
    		perror("ERROR (port configuration problem) ");
    		exit(EXIT_FAILURE);
    	}
     
    	bzero(&options, sizeof(options));
     
    	// baudrate 
    	if(cfsetispeed(&options, B4800) == -1 || cfsetospeed(&options, B4800) == -1)
    	{
    		perror("ERROR (baudrate setting) ");
    		exit(EXIT_FAILURE);	
    	}
     
    	// line oriented mode 
    	// (notice : be careful to desactivate ECHO before sending something to the devive)
    	options.c_lflag &= ~ECHONL;
    	options.c_lflag &= ~NOFLSH;
    	options.c_lflag &= ~ECHOPRT;
    	options.c_lflag &= ~TOSTOP;
    	options.c_lflag &= ~XCASE;
     
    	options.c_lflag |= ICANON;
    	options.c_lflag |= ISIG;
    	options.c_lflag |= IEXTEN;
    	options.c_lflag |= ECHO;
    	options.c_lflag |= ECHOE;
    	options.c_lflag |= ECHOK;
    	options.c_lflag |= ECHOKE;
    	options.c_lflag |= ECHOCTL;
     
    	options.c_cflag &= ~PARENB;
    	options.c_cflag &= ~PARODD;
    	options.c_cflag &= ~CSTOPB;
    	options.c_cflag &= ~CRTSCTS;	
     
    	options.c_cflag |= CS8;
    	options.c_cflag |= HUPCL;
    	options.c_cflag |= CREAD;
    	options.c_cflag |= CLOCAL;
     
    	options.c_iflag &= ~IGNBRK;
    	options.c_iflag &= ~BRKINT;
    	options.c_iflag &= ~IGNPAR;
    	options.c_iflag &= ~PARMRK;
    	options.c_iflag &= ~INPCK;
    	options.c_iflag &= ~ISTRIP;
    	options.c_iflag &= ~INLCR;
    	options.c_iflag &= ~IGNCR;
    	options.c_iflag &= ~IXOFF;
    	options.c_iflag &= ~IUCLC;
    	options.c_iflag &= ~IXANY;
    	options.c_iflag &= ~IMAXBEL;
    	options.c_iflag &= ~IUTF8;
     
    	options.c_iflag |= ICRNL;
    	options.c_iflag |= IXON;
     
    	options.c_oflag &= ~OLCUC;
    	options.c_oflag &= ~OCRNL;
    	options.c_oflag &= ~ONOCR;
    	options.c_oflag &= ~ONLRET;
    	options.c_oflag &= ~OFILL;
    	options.c_oflag &= ~OFDEL;
     
    	options.c_oflag |= OPOST;
    	options.c_oflag |= ONLCR;
    	options.c_oflag |= NL0;
    	options.c_oflag |= CR0;
    	options.c_oflag |= TAB0;
    	options.c_oflag |= BS0;
    	options.c_oflag |= VT0;
    	options.c_oflag |= FF0;
     
    	/* character setting */
    	options.c_cc[VINTR]= 3;  // "^C"
    	options.c_cc[VQUIT]= 28; // "^\"
    	options.c_cc[VERASE] = 127; // "^?"
    	options.c_cc[VKILL]= 21; // ^U
    	options.c_cc[VEOF]= 4; // ^D
    	options.c_cc[VSTART]= 17; // ^Q
    	options.c_cc[VSTOP]=  19; // ^S
    	options.c_cc[VSUSP]= 26; // ^Z
    	options.c_cc[VREPRINT]= 18; // ^R
    	options.c_cc[VWERASE]= 23; // ^W
    	options.c_cc[VLNEXT]= 22; // ^V
    	options.c_cc[VMIN]=1;
    	options.c_cc[VTIME]=0;
     
    	if(tcsetattr(fd, TCSANOW, &options) == -1)
    	{
    		perror("ERROR (port configuration setting) ");
    		exit(EXIT_FAILURE);	
    	}
     
    	int nodataCnt = 0;
     
    	while(nodataCnt < 200)
    	{
    		result = read(fd, buffer, sizeof(buffer) - 1);
     
    		// read fail
    		if(result == -1)
    		{
    			perror("ERROR (read process) ");
    			exit(EXIT_FAILURE);	
    		}
     
    		// no data
    		if(!result)
    		{
    			printf("no data\n");
    			nodataCnt++;	
    		}
     
    		// data received
    		if(result > 0)
    		{
    			buffer[result] = 0;
    			printf("string : %s number : %d\n", buffer, result);
    		}
     
    	}
     
    	tcsetattr(fd, TCSANOW, &storedOptions);
     
    	close(fd);
     
    	return EXIT_SUCCESS;
    }
    Merci quand même !
    -------------------

    @(zmodai)+

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 5
    Dernier message: 25/09/2009, 16h16
  2. Programmation carte SCM2116 pour communication avec XBT
    Par wulfen80 dans le forum Automation
    Réponses: 10
    Dernier message: 12/05/2009, 14h10
  3. Problème pour dialoguer avec port parallèle
    Par jejerome dans le forum C++
    Réponses: 8
    Dernier message: 16/05/2005, 11h13
  4. recherche composant pour dialoguer avec modem
    Par newbie qui galere dans le forum Bases de données
    Réponses: 1
    Dernier message: 15/10/2004, 23h20

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo