2012-10-16 21 views
5

Seri (RS232) bağlantı noktası için baud hızını ayarlayan basit bir programım var. Giriş ve çıkış oranlarını cfsetospeed() ve cfsetispeed() işlevlerini kullanarak bağımsız olarak ayarlıyorum. man page gereğince, bu şimdiye Bu işlevleri ve uygun sabit kullanın sağlanan mümkün olmalıdır:Giriş ve çıkış baud hızı neden her zaman aynıdır?

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

burada Sorunum ikinci set olursa olsun (giriş veya çıkış) gibi görünüyor olmasıdır için son değer. İki bağımsız hız ayarlamaya çalışıyorum.

KODU:

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; 
} 

Benim getBRate() fonksiyonu sadece (çirkin) baud hızının bir dize sürümünü dönmek anahtarı basit olan:

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; 
} 

burada çıkış olacaktır:

return code was: 0, ispeed 19200 Baud, ospeed 19200 Baud 

İki " et" şöyle hatları:

cfsetispeed(&options, B19200); //Set output speed as 19200 Baud Rate 
cfsetospeed(&options, B9600); //Set input speed as 9600 Baud Rate 

My çıktı değişecektir:

return code was: 0, ispeed 9600 Baud, ospeed 9600 Baud 

Herhangi fikirler?

DÜZENLEME:
soru geldi beri, bu kod, bir Coldfire 528X (5280 veya 5282 ya) kullanılarak bir gemide işletilecek. Ne olursa olsun, RX ve TX UART için referans kılavuzuna göre farklı fiyatları vardır mümkün olmalıdır: şimdi gerçek olarak TJD cevabı @ kabul edeceğiz İçin

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.

+6

Üzerinde durduğum tüm çiplerde, seri bağlantı noktası donanımı aslında sadece bir baud hızı üretecine sahiptir ve bu nedenle farklı Tx ve Rx baud oranlarını kullanmanın bir yolu yoktur. – TJD

+0

@TJD - Bu, Tx/Rx için iki farklı hız ayarlamaya çalışırken hiçbir hata olmaması durumunda garip. _a_ oranı aldı, ancak (iki farklı hızın ayarlanması) komutu teknik olarak başarısız oldu. – Mike

+0

ancak tam olarak bir hatanın meydana geldiği nokta yoktur. Oranı ayarlamak için her aradığınızda, başarılı bir şekilde çalışır (ancak bunu her ikisine de uygular). Yani en son ayarladığınız oranla kaldı. – TJD

cevap

0

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.

Herhangi bir hata görmediğim gerçeğine gelince, tcsetattr()'un istenen işlemlerinden en az birinin this page durumları başarılı olduğu için bu hata:

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.

Şimdilik, donanımın bunu destekleme kapasitesine sahip olmadığını tahmin ediyorum, ancak istediğim iki şeyden birini ayarladığından set işlevinden başarılı bir dönüş elde ediyorum.

+0

@TJD yanıtı kesinlikle doğrudur. Ayrıca, farklı tx/rx baud oranlarına sahip herhangi bir UART donanım uygulaması görmedim. Bazı işlevsiz nedenlerden dolayı bu işlevselliğe ihtiyacınız varsa, iki adet USB <> seri adaptör ve tx hattını bir adaptörden ve rx satırından diğerinden alan özel bir kablo öneririm. Bir bağlantı noktasından Tx ve diğerinden rx. –

İlgili konular