Helo,我一直在尝试使用 tcgetattr 检索串行端口的配置,并且我能够在第一次获得正确的值。当我更改相应参数的值时,我得到的返回值是以前返回的重复。如果有人知道请帮忙。当串行端口连接处于活动状态并正常工作时,我尝试了这个。
#include<termios.h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
char *baudQuery(speed_t speed){
static char SPEED[20];
switch (speed){
case B4800:
strcpy(SPEED, "B4800");
return SPEED;
break;
case B9600:
strcpy(SPEED, "B9600");
return SPEED;
break;
case B19200:
strcpy(SPEED, "B19200");
return SPEED;
break;
case B38400:
strcpy(SPEED, "B38400");
return SPEED;
break;
case B57600:
strcpy(SPEED, "B57600");
return SPEED;
break;
case B115200:
strcpy(SPEED, "B115200");
return SPEED;
break;
default :
strcpy(SPEED, "ERROR");
return SPEED;
}
}
main() {
int fd;
struct termios term;
speed_t speed;
term.c_cflag = B38400 | ~CSIZE | CS5 | PARENB | (~PARODD);
term.c_lflag = ~(ICANON | ECHO | ECHOE);
if((fd = open("/dev/ttyS1",O_RDWR)) <0)
printf("Failed to open file\n");
tcsetattr(fd,TCSANOW,&term);
if (tcgetattr(fd, &term) != 0)
perror("tcgetatt() error");
else {
speed = cfgetospeed(&term);
printf("cfgetispeed() says the speed of stdin is %s\n",
baudQuery(speed));
tcgetattr(fd, &term);
switch(term.c_cflag & CSIZE){
case CS5:
puts("Data Bits = 5");
break;
case CS6:
puts("Data Bits = 6");
break;
case CS7:
puts("Data Bits = 7");
break;
case CS8:
puts("Data Bits = 8");
break;
}
tcgetattr(fd, &term);
if(term.c_cflag & (~PARENB))
puts("No parity is used");
if (term.c_cflag & (PARENB & (~PARODD)) )
puts("Even parity is used");
if (term.c_cflag & (PARODD & PARENB))
puts("Odd parity is used");
//Get the mode (Only part working correct!)
if(term.c_lflag & ICANON)
puts("Mode is canonical");
else
puts("Mode is non canonical");
}
close(fd);
}