簡體   English   中英

為什么輸入和輸出波特率始終相同?

[英]Why are the input and output baud rates always the same?

我有一個簡單的程序設置串行(RS232)端口的波特率。 我使用cfsetospeed()cfsetispeed()函數獨立設置輸入和輸出速率。 根據手冊頁 ,如果我使用這些函數和一個適當的常量,這應該是可能的:

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

我的問題在於,我設置的第二個(輸入或輸出)似乎是兩者的最終值。 我正試圖設置兩個獨立的速度。

碼:

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.

現在我接受@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()操作確實成功,因為此頁面指出:

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函數中成功返回,因為它設置了我請求的兩件事之一。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM