2012-10-16 18 views
5

मुझे एक सीरियल (आरएस 232) पोर्ट के लिए बॉड दर सेट करने वाला एक सरल प्रोग्राम मिला है। मैं cfsetospeed() और cfsetispeed() फ़ंक्शंस का उपयोग करके स्वतंत्र रूप से इनपुट और आउटपुट दरों को सेट कर रहा हूं। man page के अनुसार, इस प्रदान की मैं इन कार्यों और एक उचित निरंतर का उपयोग संभव हो जाना चाहिए:इनपुट और आउटपुट बॉड दरें हमेशा समान क्यों होती हैं?

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) का उपयोग करने पर चलाया जाएगा। भले ही, RX और TX प्रति UART के लिए संदर्भ पुस्तिका के रूप में अलग-अलग दरों के लिए सक्षम होना चाहिए: अब मैं @ तथ्य के रूप में TJD के जवाब को स्वीकार करेंगे के लिए

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

मेरे द्वारा निपटाए गए सभी चिप्स में, सीरियल पोर्ट हार्डवेयर में वास्तव में केवल एक ही बॉड दर जनरेटर होता है, और इसलिए अलग-अलग टीएक्स और आरएक्स बॉड दरों को संभालने का कोई संभावित तरीका नहीं है। – TJD

+0

@ टीजेडी - अजीब अगर ऐसा है कि टीएक्स/आरएक्स के लिए दो अलग-अलग दरों को सेट करने की कोशिश में कोई त्रुटि नहीं है। _a_ दर लिया, लेकिन आदेश (दो अलग-अलग दरों को स्थापित करना) तकनीकी रूप से विफल रहा। – Mike

+0

लेकिन वास्तव में कोई बिंदु नहीं है जहां कोई त्रुटि होती है। प्रत्येक बार जब आप दर निर्धारित करने के लिए कॉल करते हैं, तो यह सफलतापूर्वक करता है (लेकिन यह केवल दोनों पर लागू होता है)। तो आप जो भी दर तय करते हैं उसके साथ आपको छोड़ दिया जाता है। – TJD

उत्तर

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.

: इस तथ्य के रूप में है कि मैं किसी भी त्रुटि नहीं दिखाई दे रही है, क्योंकि tcsetattr() का अनुरोध किया आपरेशन के कम से कम एक this page राज्यों के रूप में सफल था है

तो अभी के लिए, मैं अनुमान लगा रहा हूं कि हार्डवेयर का समर्थन करने की क्षमता नहीं है, लेकिन मुझे सेट फ़ंक्शन से सफल वापसी मिल रही है क्योंकि यह अनुरोध की गई दो चीज़ों में से एक सेट करता है।

+0

@TJD उत्तर निश्चित रूप से सही है। मैंने कभी भी यूआरएटी हार्डवेयर कार्यान्वयन को कभी नहीं देखा है जिसमें अलग-अलग टीएक्स/आरएक्स बॉड दरें हैं। यदि आपको कुछ अस्पष्ट कारणों के लिए इस कार्यक्षमता की आवश्यकता है, तो मैं दो यूएसबी <> सीरियल एडेप्टर और एक विशेष केबल का सुझाव देता हूं जो एक एडाप्टर से टीएक्स लाइन और दूसरे से आरएक्स लाइन लेता है। टीएक्स एक बंदरगाह और दूसरे से आरएक्स। –

संबंधित मुद्दे