1

我编写了一个可以在前台或后台执行的程序。该程序使用串行端口进行通信。如果我在前台启动它(即它不分叉),一切运行正常。如果我从命令行将它作为守护进程启动,那么一切都运行良好。

仅在引导期间使用启动脚本在后台将其作为守护程序启动始终无法设置正确的波特率。它似乎以某种方式默认为 115200 bps,而不是我配置的:9600 bps。我使用标准的“termios”结构和相关函数来配置串口。串行端口是内核映像的一部分,而不是可加载模块。以下是相关代码:

struct termios TermIO;
speed_t locBaudrate;

...

// retrieve current settings
if (tcgetattr(nFile, &TermIO)==-1) {
    // failed to get attributes
    return false;
}


// initialize anything to 0
memset(&TermIO, 0, sizeof(TermIO));

// set the baudrate
locBaudrate= B9600;
if (cfsetispeed(&TermIO, locBaudrate)==-1) {
    // failed to set the input baudrate
    return false;
}
if (cfsetospeed(&TermIO, locBaudrate)==-1) {
    // failed to set the output baudrate
    return false;
}

... // more configuraton like stop, bitlen, etc

// set the new configuration immediately i.e. do not wait for Rx or Tx to finish
if (tcsetattr(nFile, TCSANOW, &TermIO)==-1) {
    // failed to set parameters
    return false;
}

绝对清楚:我可以从命令行将程序作为守护程序运行(即它将分叉),然后一切正常。只有在启动过程中使用 /etc/init.d 中的脚本启动程序时,波特率的配置才会失败。程序本身在后台运行良好。

任何想法这里可能有什么问题?

4

0 回答 0