/************************************************************ ** PROGRAM: SerCom1 ** ** AUTHOR: Ian Johnson ** ** NOTES: serial port handling pinched from the linux ** ** serial programming howto ** ************************************************************/ #include #include #include #include #include #include #define BAUDRATE B9600 #define MODEMDEVICE "/dev/ttyS0" main() { int fd,c, res, mesc, i, wx, wy; struct termios oldtio,newtio; char buf[255], mesg[255], ch; WINDOW *top, *bottom; initscr(); /* init curses */ cbreak(); /* unbuffered */ nodelay(stdscr, TRUE); /* non blocking */ top = newwin(LINES/2, COLS, 0, 0); wprintw(top, "TOP:"); bottom = newwin(LINES/2 ,COLS, LINES/2, 0); wprintw(bottom, "BOTTOM:"); wrefresh(top); wrefresh(bottom); nodelay(top, TRUE); fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY ); if (fd <0) { perror(MODEMDEVICE); exit(-1); } tcgetattr(fd,&oldtio); /* save current port settings */ bzero(&newtio, sizeof(newtio)); newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD; newtio.c_iflag = IGNPAR; newtio.c_oflag = 0; /* set input mode (non-canonical, no echo,...) */ newtio.c_lflag = 0; newtio.c_cc[VTIME] = 0; newtio.c_cc[VMIN] = 0; tcflush(fd, TCIFLUSH); tcsetattr(fd,TCSANOW,&newtio); mesc = 1; while (1) /* loop forever */ { res = read(fd,buf,1); if (res) { waddch(bottom,buf[0]); wrefresh(bottom); } ch = wgetch(top); if (ch != ERR) /* character is available */ write(fd,&ch,1); } tcsetattr(fd,TCSANOW,&oldtio); endwin(); }