我有一个简单的程序设置串行(RS232)端口的波特率。我正在使用cfsetospeed()
和cfsetispeed()
函数单独设置输入和输出速率。根据{{3}},如果我使用这些函数和适当的常量,这应该是可能的:
cfsetispeed() sets the input baud rate stored in the termios structure to speed, which must be specified as one of the Bnnn constants listed above for cfsetospeed(). If the input baud rate is set to zero, the input baud rate will be equal to the output baud rate.
cfsetospeed() sets the output baud rate stored in the termios structure pointed to by termios_p to speed, which must be one of these constants:
...
B600
...
B19200
我的问题在于,我设置的第二个(输入或输出)似乎是两者的最终值。我正试图设置两个独立的速度。
CODE:
int main() {
int fd, ret;
char buf[100] = {0};
char buf2[100] = {0};
struct termios options;
// Open the serial-USB device driver
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd < 0){
perror("open_port: Unable to open port - ");
return 1;
}
tcgetattr(fd, &options); //Get the current settings
cfsetospeed(&options, B9600); //Set input speed as 9600 Baud Rate
cfsetispeed(&options, B19200); //Set output speed as 19200 Baud Rate
ret= tcsetattr(fd, TCSANOW, &options); //Get the return to make sure it worked
sleep(3); // Just for kicks, let it "settle"
tcgetattr(fd, &options); //Read back the values
getBRate(buf, cfgetispeed(&options));
getBRate(buf2, cfgetospeed(&options));
printf("return code was: %d, ispeed %s, ospeed %s\n", ret, buf, buf2);
//Clean up
memset(buf, '0', 100);
memset(buf2, '0', 100);
close(fd);
return 0;
}
我的getBRate()
函数只是一个简单(丑陋)的开关,用于返回波特率的字符串版本:
void getBRate(char rate[], speed_t brate)
{
switch(brate) {
case B0: strcpy(rate,"none"); break;
case B50: strcpy(rate,"50 Baud");break;
case B110: strcpy(rate,"110 Baud");break;
case B134: strcpy(rate,"134 Baud");break;
case B150: strcpy(rate,"150 Baud");break;
case B200: strcpy(rate,"200 Baud");break;
case B300: strcpy(rate,"300 Baud");break;
case B600: strcpy(rate,"600 Baud");break;
case B1200: strcpy(rate,"1200 Baud");break;
case B1800: strcpy(rate,"1800 Baud");break;
case B2400: strcpy(rate,"2400 Baud");break;
case B4800: strcpy(rate,"4800 Baud");break;
case B9600: strcpy(rate,"9600 Baud");break;
case B19200: strcpy(rate,"19200 Baud");break;
default: strcpy(rate, "No valid baud found\n");break;
}
return;
}
此处的输出将为:
return code was: 0, ispeed 19200 Baud, ospeed 19200 Baud
如果我像这样反转两个“设置”行:
cfsetispeed(&options, B19200); //Set output speed as 19200 Baud Rate
cfsetospeed(&options, B9600); //Set input speed as 9600 Baud Rate
我的输出将更改为:
return code was: 0, ispeed 9600 Baud, ospeed 9600 Baud
有什么想法吗?
修改
自提出问题以来,此代码将使用Coldfire 528X(5280或5282)在电路板上运行。无论如何,根据UART的参考手册,RX和TX应该能够具有不同的速率:
23.3.4 UART Clock Select Registers (UCSRn)
The UCSRs select an external clock on the DTIN input (divided by 1 or 16) or a prescaled internal bus clock as the clocking source for the transmitter and receiver. See Section 23.4.1, “Transmitter/Receiver Clock Source.” The transmitter and receiver can use different clock sources.
答案 0 :(得分:0)
现在我接受@ TJD的答案为In all the chips I have dealt with, the serial port hardware actually only has a single baud rate generator, and therefore has no possible way to handle different Tx and Rx baud rates.
关于我没有看到任何错误的事实,这是因为tcsetattr()
的至少一个请求的操作确实成功为this page个状态:
The tcsetattr() function returns successfully if it was able to perform any of the requested actions, even if some of the requested actions could not be performed.
所以现在,我猜测硬件没有能力支持这个,但是我从set函数中成功返回,因为它设置了我要求的两件事之一。