#include #include #include #include enum{ERROR=-1,SUCCESS}; typedef unsigned long Ulong; typedef struct termios TTY; speed_t baud_konstante[] = { B0,B50,B75,B110,B134,B150,B200,B300,B600,B1200, B1800,B2400,B4800,B9600,B19200,B38400,B57600, B115200,B230400,B460800 }; Ulong baud_werte[] = { 0,50,75,110,134,150,200,300,600,1200, 1800,2400,4800,9600,19200,38400,57600, 115200,230400,460800 }; Ulong baudrate_wert(speed_t baud) { int i; for(i=0; i<=19; i++) { if(baud == baud_konstante[i]) return(baud_werte[i]); } return ERROR; } int serialopen(char *device, char *baud, int nonblock ) { int fd; speed_t baud_input, baud_output; speed_t newbaud; TTY terminal; char *p; /* printf("Call: setbaud devicename baudrate!!!\n"); printf("Valid baudrates are: 0,50,75,110,134,150,200,\n"); printf("300,600,1200,1800,2400,4800,9600,19200,38400,57600,115200,\n"); printf("230400,460800\n\n"); */ if( nonblock ) { if((fd =open( device , O_RDWR | O_NONBLOCK)) == ERROR){ printf("Cannot open device %s !!", device); return( 0 ); } } else { if((fd =open( device , O_RDWR )) == ERROR){ printf("Cannot open device %s !!", device); return( 0 ); } } if(isatty(fd) == 0){ printf("%s is not a tty......",device); close( fd ); return( 0 ); } if(tcgetattr(fd, &terminal) == ERROR){ printf("Error for tcgetattr......\n"); close( fd ); return( 0 ); } if(( baud_input = baudrate_wert(cfgetispeed(&terminal))) == ERROR){ printf("Error for baud_input........\n"); close( fd ); return(0); } if(( baud_output = baudrate_wert(cfgetospeed(&terminal))) == ERROR){ printf("Error for baud_output........\n"); close( fd ); return(0); } printf("Current input-baudrate : %lu\n",baud_input); printf("Current output-baudrate : %lu\n",baud_output); p = baud; 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){ printf("Invalid baudrate given as argument !!!\n"); close( fd ); return( 0 ); } printf("Changing input-, and output-baudrate to : %lu\n",newbaud); if((cfsetispeed(&terminal,newbaud)) == ERROR){ printf("Error when setting input-baudrate........\n"); close( fd ); return(0); } if((cfsetospeed(&terminal,newbaud)) == ERROR){ printf("Error when setting output-baudrate........\n"); close( fd ); return(0); } if(tcsetattr(fd,TCSAFLUSH, &terminal) == ERROR){ printf("Error for tcsetattr......\n"); close( fd ); return(0); } if(tcgetattr(fd, &terminal) == ERROR){ printf("Error for tcgetattr......\n"); close(fd); return(0); } if(( baud_input = baudrate_wert(cfgetispeed(&terminal))) == ERROR){ printf("Error for baud_input........\n"); close(fd); return(0); } if(( baud_output = baudrate_wert(cfgetospeed(&terminal))) == ERROR){ printf("Error for baud_output........\n"); close(fd); return(0); } printf("Input-Baudrate-new : %lu\n",baud_input); printf("Output-Baudrate-new : %lu\n\n",baud_output); return( fd ); }