#include #include #include #include #include #include #include #include #include #define _POSIX_SOURCE 1 int open_sport( char *ttyname ); int init_sport( int fd ); /* ------------------------------------------------------------------- serial port control program for LIPS initialization subroutines 'set_sport.c' Ver 1.00 2000.11.15 Akitaya, H see also "Serial Programming Guide for POSIX Operating Systems 5th edition" Michael R. Sweet 1999 http://www.easysw.com/~mike/serial/ ------------------------------------------------------------------- */ /*------------------------- * open_sport( char *ttyname ) * open serial port *------------------------- */ int open_sport( char *ttyname ) { int fd; printf( "opening serial port %s\n", ttyname ); /* fd = open( ttyname, O_RDWR | O_NOCTTY | O_NDELAY );*/ fd = open( ttyname, O_RDWR ); /* supress control singanls & non-blocking term. I/O */ if ( fd == -1){ perror( "open_sport: Unable to open serial port - " ); } printf( "succeeded !!\n"); /* else{ fcntl( fd, F_SETFL, O_NDELAY ); }*/ return( fd ); } /*------------------------- * init_sport( int fd ) * initialize serial port *------------------------- */ int init_sport( int fd ) { struct termios options; tcgetattr( fd, &options ); /* control options (c_cflag) & input baud (c_ispeed) */ options.c_cflag |= B1200 ; /* set baud rates */ cfsetispeed( &options, B1200 ); cfsetospeed( &options, B1200 ); /* set character size */ options.c_cflag &= ~CSIZE ; options.c_cflag |= CS7 ; /* character size 7bits */ /* set parity checking */ options.c_cflag |= PARENB ; options.c_cflag |= PARODD ; /* parity odd */ /* set stop bit */ options.c_cflag &= ~CSTOPB ; /* stop bit 1 */ /* enable the reciever and set local mode */ options.c_cflag |= ( CLOCAL | CREAD ); /* local options (c_lflag) */ /* canoncal input */ options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* options.c_lflag |= (ICANON | ECHO | ECHOE );*/ /* input options (c_iflag) */ /* enable input parity check */ options.c_iflag |= ( INPCK | ISTRIP ); /* options.c_iflag |= ( IGNPAR ) ;*/ /* enable software flow control */ options.c_iflag |= ( IXON | IXOFF ); /* options.c_iflag &= ~( IXON | IXOFF | IXANY ); */ /* ignore CR (for Lakeshore model 330) */ /* options.c_iflag |= IGNCR ;*/ /* output options (c_oflag) */ /* not raw or raw output */ options.c_oflag |= OPOST; /* not raw */ /* options.c_oflag &= ~OPOST; */ /* raw */ /* Map NL to CR-NL(for Lakeshore model 330) */ options.c_oflag |= ONLCR ; /* control options (c_cc) */ /* set read timeouts () available when noncanonical mode*/ /*options.c_cc[4] = VEOF; options.c_cc[5] = VEOL;*/ options.c_cc[VMIN] = 0; options.c_cc[VTIME] = 1; /* 0.1 sec unit*/ /* set the new options for the port */ tcsetattr( fd, TCSAFLUSH, &options ); return( fd ); }