Advertisement
Guest User

Wookie

a guest
Mar 2nd, 2009
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.97 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <termios.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. #define BAUDRATE B9600
  11. #define DEVICE "/dev/ttyUSB0"
  12. #define _POSIX_SOURCE 1
  13.  
  14. int main(int argc, char **argv)
  15. {
  16.     int fd, i, res;
  17.     struct termios oldtio,newtio;
  18.     char buf[255];
  19.     char cmd;
  20.  
  21.     fd = open(DEVICE, O_RDWR | O_NOCTTY);
  22.     if(fd < 0)
  23.     {
  24.         perror(DEVICE);
  25.         exit(-1);
  26.     }
  27.     printf("deviced opened\n");
  28.    
  29.     // get old settings
  30.     if(tcgetattr(fd, &oldtio) < 0)
  31.     {
  32.         printf("error getting old serial settings\n");
  33.         exit(-1);
  34.     }
  35.     printf("got old settings\n");
  36.    
  37.     // set up new settings
  38.     bzero(&newtio, sizeof(newtio));
  39.     newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
  40.     newtio.c_iflag = IGNPAR;
  41.     newtio.c_oflag = 0;
  42.     newtio.c_lflag = 0;
  43.     newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
  44.     newtio.c_cc[VMIN]     = 1;   /* blocking read until 5 chars received */
  45.    
  46.     if(tcflush(fd, TCIOFLUSH) < 0)
  47.     {
  48.         printf("failed to flush serial device\n");
  49.         exit(-1);
  50.     }
  51.     printf("device flushed\n");
  52.  
  53.     // set new settings
  54.     if(tcsetattr(fd, TCSANOW, &newtio) < 0)
  55.     {
  56.         printf("failed to set new settings\n");
  57.     }
  58.     printf("new settings\n");
  59.    
  60.     // write the nesinfo command
  61.     cmd = 0xA1;
  62.     res = write(fd, &cmd, 1);
  63.     if(tcdrain(fd) < 0)
  64.     {
  65.         printf("failed to send data\n");
  66.     }
  67.     printf("wrote %d bytes\n", res);
  68.    
  69.     // read the nesinfo command results
  70.     bzero(buf, 255);
  71.     for(i = 0; i < 255; i++)
  72.     {
  73.         printf("%d\n", i);
  74.         res = read(fd, &buf[i], 1);
  75.         if(res <= 0)
  76.         {
  77.             buf[i] = 0;
  78.             break;
  79.         }
  80.     }
  81.     printf("%s\n", buf);
  82.    
  83.     // restore old settings
  84.     tcsetattr(fd, TCSANOW, &oldtio);
  85.  
  86.     return EXIT_SUCCESS;
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement