#include "elastro.h" /* #include #include #include #include */ #include speed_t baud_konstante[] = { B0,B50,B75,B110,B134,B150,B200,B300,B600,B1200, B1800,B2400,B4800,B9600,B19200,B38400,B57600, B115200,B230400,B460800 }; unsigned long baud_werte[] = { 0,50,75,110,134,150,200,300,600,1200, 1800,2400,4800,9600,19200,38400,57600, 115200,230400,460800 }; unsigned long baudrate_wert(speed_t baud) { int i; for(i=0; i<=19; i++) { if(baud == baud_konstante[i]) return(baud_werte[i]); } return 1; } int waitWriteFD( char * buffer, int fd, size_t num ) { int len; fd_set wfds,rfds; struct timeval tv; int retval; if( fd <= 0 )return(0); FD_ZERO(&wfds); FD_SET(fd,&wfds); tv.tv_sec = 1; tv.tv_usec = 0; retval = select(fd + 1,NULL,&wfds,NULL,&tv); FD_ZERO(&wfds); if(retval > 0){ len = write( fd, ( const void * )buffer, num ); if(len < 0 ){ return(0); } return(1); } return(0); } int waitReadFD( char *buffer, int fd, size_t num ) { int len; fd_set rfds; struct timeval tv; int retval; if( fd <= 0 )return(0); FD_ZERO(&rfds); FD_SET( fd ,&rfds); tv.tv_sec = 2; tv.tv_usec = 0; retval = select(fd + 1,&rfds,NULL,NULL,&tv); FD_ZERO(&rfds); if(retval > 0){ len = read( fd, ( void * )buffer, num ); if(len < 0 ){ return(0); } return(1); } return(0); } /* opens port port, ( blocked, sync ) sets baudrate baudval to port port. returns fd of open port, if new baudrate is set, and port opened successfully, otherwise 0, if an error*/ int setBaudRateOpenPort(char *port, char * baudval) { int fd; speed_t baud_input, baud_output; speed_t newbaud; struct termios terminal; char *p; if((fd =open( port , O_RDWR | O_NONBLOCK )) == -1){ /* if((fd =open( port , O_RDWR | O_SYNC )) == -1){ */ return(0); } if(isatty(fd) == 0){ close(fd); return(0); } if(tcgetattr(fd, &terminal) == -1) { close(fd); return(0); } if(( baud_input = baudrate_wert(cfgetispeed(&terminal))) == 1) { close(fd); return(0); } if(( baud_output = baudrate_wert(cfgetospeed(&terminal))) == 1) { close(fd); return(0); } p = baudval; newbaud = 1; if(strcmp(p,"0")==0){ newbaud = B0; } if(strcmp(p,"50")==0){ newbaud = B50; } if(strcmp(p,"75")==0){ newbaud = B75; } if(strcmp(p,"110")==0){ newbaud = B110; } if(strcmp(p,"134")==0){ newbaud = B134; } if(strcmp(p,"150")==0){ newbaud = B150; } if(strcmp(p,"200")==0){ newbaud = B200; } if(strcmp(p,"300")==0){ newbaud = B300; } if(strcmp(p,"600")==0){ newbaud = B600; } if(strcmp(p,"1200")==0){ newbaud = B1200; } if(strcmp(p,"1800")==0){ newbaud = B1800; } if(strcmp(p,"2400")==0){ newbaud = B2400; } if(strcmp(p,"4800")==0){ newbaud = B4800; } if(strcmp(p,"9600")==0){ newbaud = B9600; } if(strcmp(p,"19200")==0){ newbaud = B19200; } if(strcmp(p,"38400")==0){ newbaud = B38400; } if(strcmp(p,"57600")==0){ newbaud = B57600; } if(strcmp(p,"115200")==0){ newbaud = B115200; } if(strcmp(p,"230400")==0){ newbaud = B230400; } if(strcmp(p,"460800")==0){ newbaud = B460800; } if(newbaud == 1){ close(fd); return(0); } cfmakeraw(&terminal); /* terminal.c_iflag |= IGNBRK | IGNCR; terminal.c_oflag |= ONOCR | ONLRET; terminal.c_lflag |= FLUSHO; terminal.c_cflag |= CLOCAL | CREAD; */ terminal.c_lflag |= FLUSHO; terminal.c_cflag |= CLOCAL | CREAD; if((cfsetispeed(&terminal,newbaud)) == -1 ) { close(fd); return(0); } if((cfsetospeed(&terminal,newbaud)) == -1 ) { close(fd); return(0); } if(tcsetattr(fd,TCSANOW, &terminal) == -1 ) { close(fd); return(0); } if(tcgetattr(fd, &terminal) == -1 ) { close(fd); return(0); } if(( baud_input = baudrate_wert(cfgetispeed(&terminal))) == 1 ) { close(fd); return(0); } if(( baud_output = baudrate_wert(cfgetospeed(&terminal))) == 1 ) { close(fd); return(0); } tcflush( fd, TCIOFLUSH ); return(fd ); }