Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 分叉后串行端口配置不工作_C++_Linux_Configuration_Serial Port_Termios - Fatal编程技术网

C++ 分叉后串行端口配置不工作

C++ 分叉后串行端口配置不工作,c++,linux,configuration,serial-port,termios,C++,Linux,Configuration,Serial Port,Termios,我已经写了一个程序,可以在前台或后台执行。该程序使用串行端口进行通信。如果我在前台启动它(即它不分叉),一切都会正常运行。如果我从命令行以守护进程的形式启动它,那么一切都可以正常运行 在引导期间,仅在后台使用启动脚本作为守护进程启动它,始终无法设置正确的波特率。它似乎默认为115200 bps,而不是我配置的9600 bps。 我使用标准的“termios”结构和相关功能来配置串行端口。串行端口是内核映像的一部分,不是可加载的模块。以下是相关代码: struct termios TermIO;

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

在引导期间,仅在后台使用启动脚本作为守护进程启动它,始终无法设置正确的波特率。它似乎默认为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中的脚本启动时,波特率的配置才会失败。程序本身在后台运行良好

你知道这里有什么问题吗