1

I have written a program that can execute in foreground or background. The program uses a serial port to communicate. If I start it in foreground (i.e. it does not fork) everything runs fine. If I start it as daemon from the command line everything runs fine too.

Only starting it in the background as daemon with a start-up script during boot always fails to set the correct baud rate. It seems to somehow default to 115200 bps instead of what I configured: 9600 bps. I use the standard 'termios' structure and related functions for the configuration of the serial port. The serial port is part of the kernel image, not a loadable module. Here is the relevant code:

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;
}

To be absolutely clear: I can run the program as daemon (i.e. it will fork) from the command line and then things are working fine. It is just when the program is started with a script in /etc/init.d during start-up that the configuration of the baud-rate fails. The program itself runs fine in the background.

Any ideas what could be wrong here?

felix
  • 61
  • 7

0 Answers0