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(" -t <second>            time of simulation \n");
	printf(" -p <position>		position format x,N,y,E \n");
	printf(" -h                  	show this help\n");
}



/* GPS : Génération string temps */
char * gen_gps_time(int sec) // retourne string avec format du temps
{
	char time[8] = "";
	int i, j;
	int hour_d, minute_d, second_d;
	char hour_s[3], minute_s[3], second_s[3];
	
	second_d = sec%60;
	minute_d = (sec/60)%60;
	hour_d = sec/3600;
	
	/* conversion en string */
	if (hour_d < 10)
		{hour_s[0] = '0';   hour_s[1] = hour_d + 0x30;   hour_s[2] = '\0';}
	else
		sprintf(hour_s, "%d", hour_d);
	if (minute_d < 10)
		{minute_s[0] = '0';   minute_s[1] = minute_d + 0x30;   minute_s[2] = '\0';}
	else
		sprintf(minute_s, "%d", minute_d);
	if (second_d < 10)
		{second_s[0] = '0';  second_s[1] = second_d + 0x30;   second_s[2] = '\0';}
	else
		{sprintf(second_s, "%d", second_d);}
	
	/* CONCATENATION */
	strcat(time, hour_s);
	strcat(time, minute_s);
	strcat(time, second_s);
	
	time[6] = ',';
	time[7] = '\0';
	char *addressTimeArray = time;
	return addressTimeArray;
}



char * gen_gps_checksum(char * trame) // retourne string avec format du temps
{
	int i;
	int checksum_d = 0;
	char checksum_s[3]; // init checksum string vide
	for(i=0; i<strlen(trame)-1; i++)
	{
		checksum_d ^= trame[i];
	}
	sprintf(checksum_s, "%x\0", checksum_d);
	
	char *checksum = checksum_s;
	return checksum;
}
	


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;
	
	/* gps */
	int second  = 28800; // 8h du matin par défaut
	int checksum = 0;
	int done = 0;
	char gps_trame[255];
	char gps_position[64] = "4843.9031,N,00215.2230,E";


	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;
			case 't': // time en s
				second = atoi(optarg);
				break;
			case 'p': // position en p
				strcpy(gps_position,optarg);
				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)
	{
		gps_trame[0] = '\0'; // on efface l'ancienne trame
	
		char gps_type[] = "$GPGGA,";
		char *gps_time = gen_gps_time(second);
		char other[] = ",1,04,2.5,130.6,M,47.3,M,,0000*";
		
		strcat(gps_trame, gps_type);
		strcat(gps_trame, gps_time);
		strcat(gps_trame, gps_position);
		strcat(gps_trame, other);
		
		char *checksum = gen_gps_checksum(gps_trame);
		
		strcat(gps_trame, checksum);
		strcat(gps_trame, "\n\0");
		
		sleep(1); second++;
		
		fprintf(stdout,"%s",gps_trame);
		
		write(serfd,gps_trame,strlen(gps_trame));
	}
    	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