Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define BAUDRATE B9600
- #define DEVICE "/dev/ttyUSB0"
- #define _POSIX_SOURCE 1
- int main(int argc, char **argv)
- {
- int fd, i, res;
- struct termios oldtio,newtio;
- char buf[255];
- char cmd;
- fd = open(DEVICE, O_RDWR | O_NOCTTY);
- if(fd < 0)
- {
- perror(DEVICE);
- exit(-1);
- }
- printf("deviced opened\n");
- // get old settings
- if(tcgetattr(fd, &oldtio) < 0)
- {
- printf("error getting old serial settings\n");
- exit(-1);
- }
- printf("got old settings\n");
- // set up new settings
- bzero(&newtio, sizeof(newtio));
- newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
- newtio.c_iflag = IGNPAR;
- newtio.c_oflag = 0;
- newtio.c_lflag = 0;
- newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
- newtio.c_cc[VMIN] = 1; /* blocking read until 5 chars received */
- if(tcflush(fd, TCIOFLUSH) < 0)
- {
- printf("failed to flush serial device\n");
- exit(-1);
- }
- printf("device flushed\n");
- // set new settings
- if(tcsetattr(fd, TCSANOW, &newtio) < 0)
- {
- printf("failed to set new settings\n");
- }
- printf("new settings\n");
- // write the nesinfo command
- cmd = 0xA1;
- res = write(fd, &cmd, 1);
- if(tcdrain(fd) < 0)
- {
- printf("failed to send data\n");
- }
- printf("wrote %d bytes\n", res);
- // read the nesinfo command results
- bzero(buf, 255);
- for(i = 0; i < 255; i++)
- {
- printf("%d\n", i);
- res = read(fd, &buf[i], 1);
- if(res <= 0)
- {
- buf[i] = 0;
- break;
- }
- }
- printf("%s\n", buf);
- // restore old settings
- tcsetattr(fd, TCSANOW, &oldtio);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement