#include #include #include #include #include #include #include #include #include int open_port(void) { int fd; /* File descriptor for the port */ fd = open("/dev/com4", O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { /* * Could not open the port. */ perror("open_port: Unable to open /dev/ttyS0 - "); } else { fcntl(fd, F_SETFL, 0); } return (fd); } int setup_port(int fd) { struct termios options; /* get the current options */ tcgetattr(fd, &options); cfsetispeed(&options, B57600); cfsetospeed(&options, B57600); /* set raw input, 1 second timeout */ options.c_cflag |= (CLOCAL | CREAD); options.c_oflag &= ~OPOST; options.c_cc[VMIN] = 0; options.c_cc[VTIME] = 10; options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; options.c_cflag &= ~CRTSCTS; options.c_cflag &= ~(IXON | IXOFF | IXANY); /* no flow control */ options.c_cflag &= ~(IXON | IXOFF | IXANY); /* no flow control */ options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // raw processing options.c_oflag &= ~OPOST; /* No output processing */ options.c_oflag &= ~ONLCR; /* Don't convert linefeeds */ options.c_cflag |= (CLOCAL | CREAD); /* Enable receiver, set local */ options.c_cflag |= (IXON | IXOFF); /* Software flow control */ options.c_lflag = 0; /* no local flags */ options.c_cflag |= HUPCL; /* Drop DTR on close */ /* Setup non blocking, return on 1 character */ options.c_cc[VMIN] = 0; options.c_cc[VTIME] = 1; /* Clear the line */ tcflush(fd,TCIFLUSH); if (tcsetattr(fd,TCSANOW,&options) != 0) { perror("coudn't setup serial port..."); return -1; } else { return 0; } } int open_keyboard() { int fd; if (!isatty(fileno(stdout))) { fprintf(stderr,"You are not a terminal, OK.\n"); } fd = open("/dev/tty", O_RDONLY | O_NONBLOCK); if(!fd) { fprintf(stderr, "Unable to open /dev/tty\n"); exit(1); } return fd; } int setup_keyboard(int kb) { FILE *input; int selected; struct termios initial_settings; struct termios new_settings; tcgetattr(kb,&initial_settings); new_settings = initial_settings; new_settings.c_lflag &= ~ICANON; new_settings.c_lflag &= ~ECHO; new_settings.c_cc[VMIN] = 1; new_settings.c_cc[VTIME] = 0; new_settings.c_lflag &= ~ISIG; if(tcsetattr(fileno(input), TCSANOW, &new_settings) != 0) { fprintf(stderr,"could not set attributes\n"); } // selected = fgetc(input); // tcsetattr(fileno(input),TCSANOW,&initial_settings); // fclose(input); // return selected; } int process_fd(int keyboard, int serialport) { ssize_t n; const int MAX_READ = 1024; char ch; fcntl(keyboard, F_SETFL, FNDELAY); n = read(keyboard, &ch, 1); if (n > 0) { if (ch == 0x03) { close(keyboard); close(serialport); exit(0); } else { n = write(serialport, &ch, 1); if (n < 0 ) { perror("write() to serial port failed!\n"); return -1; } } } else if (n < 0) { perror("read() frp, keyboard failed!\n"); return -1; } return 0; } int process_socket(int keyboard, int serialport) { ssize_t n; const int MAX_READ = 1024; char buf[MAX_READ]; n = read(serialport, buf, MAX_READ); if (n > 0) { n = write(STDOUT_FILENO, buf, n); if (n < 0 ) { perror("write() failed!\n"); return -1; } } else if (n < 0) { perror("read() failed!\n"); return -1; } return 0; } int main(int argc, char *argv[]) { int n; int sock; int fd; int max_fd; fd_set input; struct timeval timeout; char ch; int bytes_written; FILE* infile; fd = open_keyboard(); setup_keyboard(fd); sock = open_port(); if (setup_port(sock) == -1) { exit(-1); } // if a program was supplied, then download the program to the // board: if (argc == 2) { printf("Downloading application %s to the board:\n", argv[1]); infile = fopen(argv[1], "r"); if (!infile) { fprintf(stderr, "couldn't open file %s\n", argv[1]); } bytes_written = 0; while (fread(&ch, 1, 1, infile)) { /* read one char at a time */ ch = write(sock, &ch, 1); if (ch == 0) { fprintf(stderr, "couldn't write char to serial port"); break; } ++bytes_written; } printf("wrote %d bytes to serial port\n", bytes_written); } /* Initialize the input set */ FD_ZERO(&input); FD_SET(fd, &input); FD_SET(sock, &input); max_fd = (sock > fd ? sock : fd) + 1; /* Initialize the timeout structure */ timeout.tv_sec = 10; timeout.tv_usec = 0; /* Do the select */ for (;;) { n = select(max_fd, &input, NULL, NULL, &timeout); /* See if there was an error */ if (n < 0) { perror("select failed"); } else { /* We have input */ if (FD_ISSET(fd, &input)) process_fd(fd, sock); if (FD_ISSET(sock, &input)) process_socket(fd, sock); } FD_ZERO(&input); FD_SET(fd, &input); FD_SET(sock, &input); } close(fd); close(sock); return; }