Untitled

                Never    
C
       
/*
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main (int argc, char **argv)
{
  int aflag = 0;
  int bflag = 0;
  char *cvalue = NULL;
  int index;
  int c;

  opterr = 0;


  while ((c = getopt (argc, argv, "abc:")) != -1)
    switch (c)
      {
      case 'a':
        aflag = 1;
        break;
      case 'b':
        bflag = 1;
        break;
      case 'c':
        cvalue = optarg;
        break;
      case '?':
        if (optopt == 'c')
          fprintf (stderr, "Option -%c requires an argument.\n", optopt);
        else if (isprint (optopt))
          fprintf (stderr, "Unknown option `-%c'.\n", optopt);
        else
          fprintf (stderr,
                   "Unknown option character `\\x%x'.\n",
                   optopt);
        return 1;
      default:
        abort ();
      }


  printf ("aflag = %d, bflag = %d, cvalue = %s\n",
          aflag, bflag, cvalue);

  for (index = optind; index < argc; index++)
    printf ("Non-option argument %s\n", argv[index]);
  return 0;
}
 */

/*
 * lecVolet.c
 * This program requests data from a slave, without controlling the outputs of the same slave.
 * Usage: lecVolet [-d <device-name>] [-a <adr>] [-s <simulated return>]
 * <device-name> is the full path name of the serial port used, defaults to /dev/ttyUSB0.
 * <adr> is the address of the slave, defaults to 1.
 * <simulated return> is the value returned by a (simulated) slave
 * This program executes max 3 successive readings then exit.
 */

#include <ctype.h> //pour isdigit(), etc
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //pour getopt()
#include <string.h> //pour strdup(),strerror()
#include <fcntl.h> //pour open(), etc
#include <signal.h> //pour signal(), etc

#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <termios.h>


#define SERIALDEVICE "/dev/ttyUSB0"
#define BAUDRATE B9600
#define INITIALCOUNT	25
#define TEMPO	100000



int useserial = 1;
int serfd;
int verbose = 0;

struct termios oldtio, newtio;


void sig_handler(int signo) // RESTAURE OLD CONFIG A LA FIN DU PROGRAMME
{
	if(signo == SIGINT || signo == SIGTERM) // si = à une interruption ou signal de fin
	{
		if(useserial)
		{
			// set old config
			if (tcsetattr(serfd,TCSANOW,&oldtio) != 0)
			{
				fprintf(stderr,"tcsetattr Error : %s (%d)\n", strerror(errno),errno);
				exit(EXIT_FAILURE);
			}
			close(serfd);
		}
		if(verbose)
			printf("\nBye.\n");
		exit(EXIT_SUCCESS);
	}
}


void printhelp(char *binname) // Affiche
{
	printf("Reading of data from a slave, using a very simple protocol I call immobus, inspired from ElektorBus\n\n");
	printf("Usage : %s [OPTIONS]\n", binname);
	printf(" -d <file> 		full path to serial dev node (default %s)\n");
	printf(" -v                  	be verbose (multiple time to increase verbosity)\n");
	printf(" -h                  	show this help\n");
}
	


int main(int argc, char** argv)
{
	int retopt;
	int opt = 0;
	char *endptr;
	int addr =  1, simul = 0;
	
	int idx;
	int whatIsMyNumbers[4];

//	int ret, pret;

	char *filename = NULL;
	
	/* récepteur GPS */
	char gps_trame[255] = "";


	while ((retopt = getopt(argc, argv, "d:ht:vp:")) != -1) // gestion des arguments de l'executable
	{
		switch (retopt)
		{
			case 'd':
				if(strlen(optarg) + 4 > 64)
				{
					printf("Device name too long (max 60 char)\n");
					return(EXIT_FAILURE);
				}
				filename = strdup(optarg);
				opt++;
				break;
			case 'h':
				printhelp(argv[0]);
				return(EXIT_SUCCESS);
				opt++;
				break;
			case 'v':
				verbose++;
				opt++;
				break;
			default:
				printhelp(argv[0]);
				return(EXIT_FAILURE);
		}
	}


	/* serial stuff */
	if(useserial)
	{
		if(!filename)
			filename=strdup(SERIALDEVICE);/* string.h*/ //renvoie un pointeur sur une nouvelle chaîne de caractères qui est dupliquée depuis SERIALDEVICE

		if(verbose)
		{
			printf("Serial device filename: '%s' (%lu)\n", filename, strlen(filename));
		}

		if ((serfd = open(filename, O_RDWR | O_NOCTTY )) < 0)
		{
			fprintf(stderr,"Serial Open Error : %s (%d)\n", strerror(errno),errno);
			return(EXIT_FAILURE);
		}

		if(tcgetattr(serfd,&oldtio) != 0)
		{
			fprintf(stderr,"tcgetattr Error : %s (%d)\n", strerror(errno),errno);
			return(EXIT_FAILURE);
		}

		// control mode flags
		newtio.c_cflag = BAUDRATE | CSTOPB | CREAD | CS8 | CLOCAL;
		// input mode flags
		newtio.c_iflag = IGNBRK;
		// output mode flags
		newtio.c_oflag = 0;
		// local mode flags
		newtio.c_lflag = 0;

		// flush all data
		if(tcflush(serfd, TCIOFLUSH) != 0)
		{
			fprintf(stderr,"tcflush Error : %s (%d)\n", strerror(errno),errno);
			exit(EXIT_FAILURE);
		}

		// set new config
		if (tcsetattr(serfd,TCSANOW,&newtio) != 0)
		{
			fprintf(stderr,"tcsetattr Error : %s (%d)\n", strerror(errno),errno);
			exit(EXIT_FAILURE);
		}
	}


	if(signal(SIGINT, sig_handler) == SIG_ERR)
	{
		fprintf(stderr,"Can't catch SIGINT\n");
		return(EXIT_FAILURE);
	}


	if(signal(SIGTERM, sig_handler) == SIG_ERR)
	{
		fprintf(stderr,"Can't catch SIGINT\n");
		return(EXIT_FAILURE);
	}

	while (!done)
	{
		int n = read(serfd,gps_trame,sizeof(gps_trame)); // read up to 255 characters if ready to read
		
		if (gps_trame != NULL)
			printf("%s",gps_trame);
		else
			gps_trame[0] = '\0';
			
		usleep(1000000);
	}
    	usleep(TEMPO);
	fprintf(stdout,"Bye\n");
	
	
	if(useserial)
	{
		// set old config
		if (tcsetattr(serfd,TCSANOW,&oldtio) != 0)
		{
			fprintf(stderr,"tcsetattr Error : %s (%d)\n", strerror(errno),errno);
			exit(EXIT_FAILURE);
		}
		close(serfd);
	}

	return(EXIT_SUCCESS);
}

Raw Text