2014-01-30 23 views
2

我有兩個程序從串口讀取,有些設備連接在另一端。 第一個程序是使用Qt框架編寫的,它使用QextSerialPort與串行通信。第二套方案是用純C.串口設置在Linux中是永久的嗎?

問題是這樣的:

系統啓動後立刻純C程序都有從串行讀取數據的問題,我知道,它發送的數據正確,因爲設備的數據作出反應,儘管pselect(即監視serial_fd)永遠不會返回serial_fd從設備讀取數據。

當我啓動第二個程序(用Qt編寫)它正在發送和從設備接收數據,沒問題。更多的是,在啓動Qt程序,然後是純C程序後,純C突然無誤地工作,直到我再次重啓系統。 所以它看起來像在Qt中編寫的程序在初始化期間永久性地改變了串口的一些設置,這是可能的嗎?

下面是初始化串口的Qt程序的代碼片段:

if (rs232->open(QIODevice::ReadWrite)) { 
    rs232->setBaudRate(BAUD38400); 
    rs232->setFlowControl(FLOW_OFF); 
    rs232->setParity(PAR_NONE); 
    rs232->setDataBits(DATA_8); 
    rs232->setStopBits(STOP_1); 
    connect(rs232, SIGNAL(readyRead()), this, SLOT(onReadyRead())); 
} else { 
    qDebug() << "Rs232::rs232Connect OPEN PORT FAILURE"; 
    exit(1); 
} 

這是純C程序:

fd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY); 

if (fd == -1) { 
/* 
* Could not open the port. 
*/ 
    error_exit(ERROR,"open_port: Unable to open /dev/ttyAMA0"); 
} 
else 
    fcntl(fd, F_SETFL, 0); 

/* 
* Get the current options for the port... 
*/ 

tcgetattr(fd, &options); 

/* 
* Set the baud rates to 19200... 
*/ 

cfsetispeed(&options, B38400); 
cfsetospeed(&options, B38400); 

/* 
* Enable the receiver and set local mode... 
*/ 

options.c_cflag |= (CLOCAL | CREAD); 

options.c_cflag &= ~PARENB; 
options.c_cflag &= ~CSTOPB; 
options.c_cflag &= ~CSIZE; 
options.c_cflag |= CS8; 

/* 
* Set the new options for the port... 
*/ 

tcsetattr(fd, TCSANOW, &options); 

有缺什麼還是什麼?

問候 馬立克

回答

0

我在抓救命稻草這裏,但其他人做任何事情之前,我建議連接另一端的對方,看看什麼是發生在所有。你的問題威力是你不是在C應用程序設置流量控制模式的事實,試圖

options.c_cflag &= ~CRTSCTS; 

如果還是不行,看看接受的答案here;我過去曾多次使用該代碼,並且從來沒有遇到任何串行通信問題。