2012-09-27 82 views
3

我在查看是否可以從串口讀取波特率爲100的數據。根據termio.h,沒有規定將100設置爲波特率。我在Linux上工作。另一端的通信設備以100波特率發送數據,並且它是固定的。我想知道我的波特率是否設置爲110,是否能保證我收到的數據是正確的?或者有沒有解決方案?以100波特率處理串行端口

請指導。

+2

你確定對方到底是不是實際發送110個波特? 100波特基本上是聞所未聞的。 –

回答

6

你真的很幸運。 100波特足夠低,以至於您可以計算一個約數,該約數將通過16450兼容的典型串行端口(這幾乎是所有內容)和linux supports custom divisors以及spd_cust參數到setserial來完成(1,152)。

+0

感謝您的意見。有沒有在Linux的命令知道串行的時鐘速度?謝謝..我正在檢查這個 - http://www.connecttech.com/KnowledgeDatabase/kdb309.htm –

+1

幾乎每個有一個包含串口的PC都有一個[1.6532MHz時鐘的兼容16550A的端口](http ://www.lammertbies.nl/comm/info/serial-uart.html)。這爲你制定了正確的除數1,152。 –

+0

謝謝。我似乎成功定製到100波特。你能不能讓我知道我可以通過編程確認已設置了100?我使用了每次輸出15的cfgetospeed()(不管波特率如何設置)。 –

0

嗯...... 110bps在串口速度方面是獨一無二的,因爲它通常有兩個停止位(所有其他速度使用一個停止位),所以發送一個字符需要10位用於7位數據,或者11位用於8位數據。

如果通信協議被傳送每秒十個字符,有人無知的1950協議可能轉換釐泊通過假設只有一個停止位和8位數據到波特,他們會得出這樣的結論100波特是結果。

如果真實100波特的自定義設置不起作用,請嘗試設置標準110波特率。

related answer摘錄:

#include <errno.h> 
#include <termios.h> 
#include <unistd.h> 

int 
set_interface_attribs (int fd, int speed, int parity) 
{ 
     struct termios tty; 
     memset (&tty, 0, sizeof tty); 
     if (tcgetattr (fd, &tty) != 0) 
     { 
       error_message ("error %d from tcgetattr", errno); 
       return -1; 
     } 

     cfsetospeed (&tty, speed); 
     cfsetispeed (&tty, speed); 

     tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;  // 8-bit chars 
     if (speed == B110) 
      tty.c_cflag |= CSTOPB;  // 2 stop bits for 110 

     // disable IGNBRK for mismatched speed tests; otherwise receive break 
     // as \000 chars 
     tty.c_iflag &= ~IGNBRK;   // ignore break signal 
     tty.c_lflag = 0;    // no signaling chars, no echo, 
             // no canonical processing 
     tty.c_oflag = 0;    // no remapping, no delays 
     tty.c_cc[VMIN] = 0;   // read doesn't block 
     tty.c_cc[VTIME] = 5;   // 0.5 seconds read timeout 

     tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl 

     tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls, 
             // enable reading 
     tty.c_cflag &= ~(PARENB | PARODD);  // shut off parity 
     tty.c_cflag |= parity; 
     tty.c_cflag &= ~CSTOPB; 
     tty.c_cflag &= ~CRTSCTS; 

     if (tcsetattr (fd, TCSANOW, &tty) != 0) 
     { 
       error_message ("error %d from tcsetattr", errno); 
       return -1; 
     } 
     return 0; 
} 

void 
set_blocking (int fd, int should_block) 
{ 
     struct termios tty; 
     memset (&tty, 0, sizeof tty); 
     if (tcgetattr (fd, &tty) != 0) 
     { 
       error_message ("error %d from tggetattr", errno); 
       return; 
     } 

     tty.c_cc[VMIN] = should_block ? 1 : 0; 
     tty.c_cc[VTIME] = 5;   // 0.5 seconds read timeout 

     if (tcsetattr (fd, TCSANOW, &tty) != 0) 
       error_message ("error %d setting term attributes", errno); 
} 


... 
char *portname = "/dev/ttyUSB1" 
... 
int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); 
if (fd < 0) 
{ 
     error_message ("error %d opening %s: %s", errno, portname, strerror (errno)); 
     return; 
} 

set_interface_attribs (fd, B110, 0); // set speed to 115,200 bps, 8n2 (no parity)