//
// SendSerialData()
//  Send the speed and red LEDs code to the Arduino.
//
//  If memoryOK is false, the red LEDs light.  cpuUsage determines the speed
//   of the pulsing, with 1.0 being maximum speed (100% CPU usage).
//
//  The high bit determines if we go red (1) or not (0) (basically, the opposite
//   of memoryOK).  The remainder indicate the flashing speed, with lower numbers
//   (0) being faster speeds and higher numbers being slower speeds.  This is
//   computed as (1.0 - cpuSpeed) * 127.
//
int SendSerialData (
	bool		 memoryOK,
	float		 cpuUsage )
{
	uint8_t		 c = CLAMP( (char)((1.0 - cpuUsage) * 127.0f), 0, 127 );

	if( !memoryOK )
		c |= 0x80;

	return serialport_writebyte( serialFD, c );
}
Esempio n. 2
0
int send_byte (uint8_t in_byte){
    int rc = -1;

    if( fd == -1 ){ 
        printf("serial port not opened");
        return -1;
    }
    rc = serialport_writebyte(fd, in_byte);
    if(rc==-1){
        printf("error writing");
        return -2;
    }

    return 0;
}
int main(int argc, char *argv[])
{
    int fd = 0;
    char serialport[256];
    int baudrate = B9600;  // default
    char buf[256];
    int rc,n;
    
    if (argc==1) {
        usage();
        exit(EXIT_SUCCESS);
    }
    
    /* parse options */
    int option_index = 0, opt;
    static struct option loptions[] = {
        {"help",       no_argument,       0, 'h'},
        {"port",       required_argument, 0, 'p'},
        {"baud",       required_argument, 0, 'b'},
        {"send",       required_argument, 0, 's'},
        {"receive",    no_argument,       0, 'r'},
        {"num",        required_argument, 0, 'n'},
        {"delay",      required_argument, 0, 'd'}
    };
    
    while(1) {
        opt = getopt_long (argc, argv, "hp:b:s:rn:d:",
                           loptions, &option_index);
        if (opt==-1) break;
        switch (opt) {
            case '0': break;
            case 'd':
                n = strtol(optarg,NULL,10);
                usleep(n * 1000 ); // sleep milliseconds
                break;
            case 'h':
                usage();
                break;
            case 'b':
                baudrate = strtol(optarg,NULL,10);
                break;
            case 'p':
                strcpy(serialport,optarg);
                fd = serialport_init(optarg, baudrate);
                if(fd==-1) return -1;
                break;
            case 'n':
                n = strtol(optarg, NULL, 10); // convert string to number
                rc = serialport_writebyte(fd, (uint8_t)n);
                if(rc==-1) return -1;
                break;
            case 's':
                strcpy(buf,optarg);
                rc = serialport_write(fd, buf);
                if(rc==-1) return -1;
                break;
            case 'r':
                serialport_read_until(fd, buf, '\n');
                printf("read: %s\n",buf);
                break;
        }
    }
    
    exit(EXIT_SUCCESS);
} // end main
void main(void)
{

    int fd, Error;
    int baud = 9600;
    char serialport[] = "/dev/ttyACM0";

    union uuFreq {
        char fBytes[4];
        float fFloat;
    } uFreq;
    uFreq.fFloat = 50.00;

    union uuTime {
        char tBytes[4];
        unsigned long tLong;
    } uTime;
    uTime.tLong = 5000;

    union uuChannel {
       char cBytes[2]; 
       unsigned int cInt;
    } uChannel;
    uChannel.cInt = 1;

    fd = serialport_init(serialport, baud);
    Error = serialport_flush(fd);


    // Loop through several times then send song over

    for (int i = 0; i < 71; i++)
    {
    if (i == 70) 
    {
        uFreq.fFloat = 0xFFFFFFFF;
        uTime.tLong = 0xFFFF;
        uChannel.cInt = 0xFF;
    }

    // Send Frequency - incremented by 20
    uFreq.fFloat = uFreq.fFloat + 20.0;
    Error = serialport_writebyte(fd, uFreq.fBytes[0]);
    tcdrain(fd);
    Error = serialport_writebyte(fd, uFreq.fBytes[1]);
    tcdrain(fd);
    Error = serialport_writebyte(fd, uFreq.fBytes[2]);
    tcdrain(fd);
    Error = serialport_writebyte(fd, uFreq.fBytes[3]);
    tcdrain(fd);

    // Send Time - incremented by 500 ms
    uTime.tLong = uTime.tLong + 500;
    Error = serialport_writebyte(fd, uTime.tBytes[0]);
    tcdrain(fd);
    Error = serialport_writebyte(fd, uTime.tBytes[1]);
    tcdrain(fd);
    Error = serialport_writebyte(fd, uTime.tBytes[2]);
    tcdrain(fd);
    Error = serialport_writebyte(fd, uTime.tBytes[3]);
    tcdrain(fd);

    // Send Channel
    Error = serialport_writebyte(fd, uChannel.cBytes[0]);
    tcdrain(fd);
    Error = serialport_writebyte(fd, uChannel.cBytes[1]);
    tcdrain(fd);

    // Send \0
    Error = serialport_writebyte(fd, '\0');
    
    usleep(50000);
    tcdrain(fd);
    }


    serialport_close(fd);


//return 0;
}
int main(int argc, char *argv[])
{
    int fd = 0;
    FILE * tmpfile = stdin;
    char serialport[256];
    int baudrate = B9600;  /* default */
    int rc;
	unsigned char press;
	bool cxn_established = false;

    if (argc == 1)
	{
        usage();
        return EXIT_SUCCESS;
    }

    /* parse options */
    int option_index = 0, opt;
    static struct option loptions[] = {
        {"help",       no_argument,       0, 'h'},
        {"port",       required_argument, 0, 'p'},
        {"baud",       required_argument, 0, 'b'},
        {"file",       required_argument, 0, 'f'}
    };

	while (1)
	{
		opt = getopt_long (argc, argv, "hp:b:f:",
						   loptions, &option_index);

		if (opt == -1) break;

		switch (opt)
		{
			case '0': break;
			case 'h':
				usage();
				break;
			case 'b':
				if (cxn_established)
				{
					fputs("Error: baudrate must come before port number", stderr);
					return EXIT_FAILURE;
				}
				baudrate = strtol(optarg,NULL,10);
				break;
			case 'p':
				strcpy(serialport,optarg);
				fd = serialport_init(optarg, baudrate);
				if (fd == -1) return -1;
				cxn_established = true;
				break;
            case 'f':
                tmpfile = fopen(optarg, "r");
                if (tmpfile == NULL)
                {
                    perror(argv[0]);
                    return EXIT_FAILURE;
                }
                break;
		}
	}

	if (!cxn_established)
	{
		fputs("Error: port number is a required argument", stderr);
		return EXIT_FAILURE;
	}

	if (tmpfile == stdin)
	{
		puts("Press space to exit. l/r to rotate, n to stop, f to fire.");
	}

	while ((press = getc(tmpfile)) != ' ')
	{
		switch (press)
		{
			case 'l':
				puts("Sent a 2");
                                rc = serialport_writebyte(fd, (uint8_t)2);
				break;
			case 'n':
				puts("Sent a 0");
				rc = serialport_writebyte(fd, (uint8_t)0);
				break;
			case 'r':
				puts("Sent a 1");
				rc = serialport_writebyte(fd, (uint8_t)1);
				break;
                        case 'f':
                                puts("Sent a 3 (not yet implemented)");
                                rc = serialport_writebyte(fd, (uint8_t)3);
                                break;
			case 10: /* newline */
				break;
			case 255: /* eof */
				break;
			default:
				printf("Unrecognized: %i\n", (int)press);
				break;
		}
	}

    return EXIT_SUCCESS;
} /* end main */
Esempio n. 6
0
int main(int argc, char *argv[])
{
    /*accelerometer related declarations*/
    const int buf_max = 2048;

    int fd = -1;
    char serialport[buf_max];
    int baudrate = 9600;  // default
    char quiet=0;
    char eolchar = ' ';
    int timeout = 5000;
    char buf[buf_max];
    int rc,n,p;
    float xDat[103],yDat[103],zDat[103];
    float sumX=0,sumY=0,sumZ=0;
    float sum1=0,sum2=0,sum3=0;
    float stdX=0,stdY=0,stdZ=0,avgX=0,avgY=0,avgZ=0,varX=0,varY=0,varZ=0,recal=0;
    int total=1;
    int calibrate=0;
    /*---------------------------------*/

//    char stdbuffer[10];
    /*socket related declarations*/    
    int sockfd, portno, n2;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    int sendMessage=1;
   /*----------------------------*/

    if (argc==1) {
        usage();
    }

    /* parse options */
    int option_index = 0, opt;
    static struct option loptions[] = {
        {"help",       no_argument,       0, 'h'},
        {"port",       required_argument, 0, 'p'},
        {"baud",       required_argument, 0, 'b'},
        {"send",       required_argument, 0, 's'},
        {"setup",      no_argument,       0, 'S'},
        {"receive",    no_argument,       0, 'r'},
        {"flush",      no_argument,       0, 'F'},
        {"num",        required_argument, 0, 'n'},
        {"delay",      required_argument, 0, 'd'},
        {"eolchar",    required_argument, 0, 'e'},
        {"timeout",    required_argument, 0, 't'},
        {"quiet",      no_argument,       0, 'q'},
        {NULL,         0,                 0, 0}
    };

    while(1) {
        opt = getopt_long (argc, argv, "hp:b:s:rSFn:d:qe:t:",
                           loptions, &option_index);
        if (opt==-1) break;
        switch (opt) {
        case '0': break;
        case 'q':
            quiet = 1;
            break;
        case 'e':
            eolchar = optarg[0];
            if(!quiet) printf("eolchar set to '%c'\n",eolchar);
            break;
        case 't':
            timeout = strtol(optarg,NULL,10);
            if( !quiet ) printf("timeout set to %d millisecs\n",timeout);
            break;
        case 'd':
            n = strtol(optarg,NULL,10);
            if( !quiet ) printf("sleep %d millisecs\n",n);
            usleep(n * 1000 ); // sleep milliseconds
            break;
        case 'h':
            usage();
            break;
        case 'b':
            baudrate = strtol(optarg,NULL,10);
            break;
        case 'p':
            if( fd!=-1 ) {
                serialport_close(fd);
                if(!quiet) printf("closed port %s\n",serialport);
            }
            strcpy(serialport,optarg);
            fd = serialport_init(optarg, baudrate);
            if( fd==-1 ) error("couldn't open port");
            if(!quiet) printf("opened port %s\n",serialport);
            serialport_flush(fd);
            break;
        case 'n':
            if( fd == -1 ) error("serial port not opened");
            n = strtol(optarg, NULL, 10); // convert string to number
            rc = serialport_writebyte(fd, (uint8_t)n);
            if(rc==-1) error("error writing");
            break;
        case 'S':
	     portno = 52002;//atoi(argv[2]);
             /* Create a socket point*/ 
             sockfd = socket(AF_INET, SOCK_STREAM, 0);
             if (sockfd < 0) 
             {
                perror("ERROR opening socket");
                exit(1);
             }
             server = gethostbyname("192.168.2.103");//gethostbyname(argv[1]);
             if (server == NULL) {
                fprintf(stderr,"ERROR, no such host\n");
                exit(0);
             }

             bzero((char *) &serv_addr, sizeof(serv_addr));
             serv_addr.sin_family = AF_INET;
             bcopy((char *)server->h_addr, 
                   (char *)&serv_addr.sin_addr.s_addr,
                         server->h_length);
             serv_addr.sin_port = htons(portno);

             /* Now connect to the server */
             if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) 
             {
                  perror("ERROR connecting");
                  exit(1);
              }
              while(1){
              int r;
              int a1, a2, a3;
	      char a4[sizeof(float)*3+3];
              memset (buf, 0, 256);
              r = serialport_read_until(fd, buf);
              //fprintf (stderr, "VAL: %s -->", buf);
              if (r >= 0)
              {
               sscanf (buf, "A1:%d\tA2:%d\tA3:%d", &a1, &a2, &a3);
              // fprintf (stderr, "(%d)(%d)(%d)\n", a1, a2, a3);
               if(calibrate==0){
                  sumX= sumX + a1; sumY= sumY + a2; sumZ= sumZ + a3;
                  xDat[total]=a1;yDat[total]=a2;zDat[total]=a3;
                  total++;
              if (total==100){ 
		  calibrate=1;
                  avgX = sumX/(float)total; avgY = sumY/(float)total; avgZ = sumZ/(float)total;
                  for(p = 1; p<total;p++){
                  sum1 = sum1 + pow((xDat[p]-avgX),2); sum2 = sum2 + pow((yDat[p]-avgY),2); sum3 = sum3 + pow((zDat[p]-avgZ),2);
                  }
		   varX= sum1/(float)total; varY=sum2/(float)total; varZ=sum3/(float)total;
                   stdX=sqrt(varX); stdY=sqrt(varY); stdZ=sqrt(varZ);
               }
	       }
               if(calibrate==1){snprintf(a4, sizeof a4*7, "%f %f %f %f %f %f %f %f %f", (float)a1, (float)a2,(float)a3,avgX,avgY,avgZ,stdX,stdY,stdZ);}
               while(sendMessage==1&&calibrate==1){
                /* Send message to the server*/ 
                //printf("here");
		n2 = write(sockfd,a4,strlen(a4));
                if (n2 < 0) 
                {
                        perror("ERROR writing to socket");
                        exit(1);
                }
                /* Now read server response */
                bzero(buffer,256);
                n2 = read(sockfd,buffer,255);
                    if (n2 < 0) 
                {
                        perror("ERROR reading from socket");
                        exit(1);
                }
//                printf("Received message: %s\n",buffer);
                 sendMessage=0;
                }
             sendMessage=1; 
	     }
              sendMessage=1;
              fflush (0);
              //sleep (5);
           }    
           break;
        case 's':
            if( fd == -1 ) error("serial port not opened");

            sprintf(buf, (opt=='S' ? "%s\n" : "%s"), optarg);

            if( !quiet ) printf("send string:%s\n", buf);
            rc = serialport_write(fd, buf);
            if(rc==-1) error("error writing");
            break;
        case 'r':
	  while (1)
	    {
	      int r;
	      int a1, a2, a3;

	      memset (buf, 0, 256);
	      r = serialport_read_until(fd, buf);
              fprintf (stderr, "VAL: %s -->", buf);
//              printf("%d",r);
	      if (r >= 0)
		{
		 sscanf (buf, "A1:%d\tA2:%d\tA3:%d", &a1, &a2, &a3);
		 fprintf (stderr, "(%d)(%d)(%d)\n", a1, a2, a3);
                 //fgets(stdbuffer,10,stdin);
	         //if(strcmp(stdbuffer,"getData")==0){
		 // printf("%d",a1);
                 // printf("%d",a2);
                 // printf("%d",a3);}
                //}
        /*       while(sendMessage==1){
    		/* Send message to the server 
    		n2 = write(sockfd,"measure",strlen("measure"));
    		if (n2 < 0) 
    		{
         		perror("ERROR writing to socket");
         		exit(1);
    		}
    		/* Now read server response 
    		bzero(buffer,256);
    		n2 = read(sockfd,buffer,255);
        	    if (n2 < 0) 
    		{
         		perror("ERROR reading from socket");
         		exit(1);
    		}
    		printf("Received message: %s\n",buffer);
                sendMessage=0;
   		}*/
              }
	      fflush (0);
	      usleep (10000);
	    }
            break;
        case 'F':
            if( fd == -1 ) error("serial port not opened");
            if( !quiet ) printf("flushing receive buffer\n");
            serialport_flush(fd);
            break;

        }
    }

    exit(EXIT_SUCCESS);
} // end main