[英]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.