[英]Are serial port settings permanent in Linux?
我有兩個程序要從串行端口讀取,另一端連接了某些設備。 第一個程序是使用Qt框架編寫的,它使用QextSerialPort與串行通信。 第二個程序是用純C語言編寫的。
問題是這樣的:
在系統啟動后,純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);
是否缺少某些東西?
此致Marek
我在這里抓住稻草,但是在做任何其他事情之前,我建議將另一個終端連接到另一端,看看是否有任何事情發生。 您的問題可能是因為您沒有在C應用程序中設置流控制模式,請嘗試
options.c_cflag &= ~CRTSCTS;
如果仍然無法解決問題,請在此處查看已接受的答案; 過去,我已經使用過幾次代碼,而串行通訊從未遇到任何問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.