Problem with fopen, linux, and RS-232, possibly

Discussion in 'C Programming' started by Gregory Graham, Dec 8, 2004.

  1. I have a temperature sensor device we use to shutdown our servers. It
    relys on fopen() blocking when the serial port device isn't ready.
    Unfortunately that's not what's happening. The fopen() returns
    immediately without an error. What have I got wrong?

    Basically when it gets too hot the device connects the 8 and 6 lines
    to line 20. That should, as I understand it, tell the fopen() that
    the file is available and cause the rest of the program to execute.

    The code is below. I compile it with "gcc -o minmon minmon.c" and
    invoke it as "minmon /dev/tyyS0" on SuSE9 with gcc 2.95.3

    #include <stdio.h>
    #include <signal.h>

    int await_event(char* port)
    {
    FILE *file;
    int fd;

    if ((file = fopen(port, "r")) != NULL) /*blocks*/
    fclose(file); /*no longer need port*/
    else
    {
    fprintf(stderr, "ERROR opening port %s", port);
    return -1;
    }
    return 0;
    }

    main (int argc, char* argv[])
    {
    int pid, getuid();
    char* port, *script;

    if (argc != 2 )
    {
    fprintf(stderr, "Usage:");
    fprintf(stderr, "\t%s device\n", argv[0]);
    exit(3);
    }
    else /*run in diagnostic mode*/
    {
    port = argv[1];
    printf("watching on port: %s\n", port);
    if (await_event(port) == 0) /*blocks*/
    {
    printf("%s: diagnostic power out\n", argv[0]);
    /*insert shutdown code here*/
    }
    else
    printf("%s: ERROR while waiting for event\n",
    argv[0]);
    exit(0);
    }
    }
     
    Gregory Graham, Dec 8, 2004
    #1
    1. Advertising

  2. Gregory Graham

    Guest

    Hi Gregory,

    Are those pins you specified the DCD signal?
    I am not sure how fopen() is calling open(), but you want to make sure
    open() is called WITHOUT the O_NDELAY flag.
    Also, you may want the O_NOCTTY flag to be present.

    Try writing the program using open() instead, and specify the exact
    flags.
    You can also pass fcntl()s to the device.

    If this is not DCD signal, and rather some obscure pins than the driver
    may be unaware of its "readiness", then any open() call may not block.
    Whether the device is actually capable of sending data or not. I
    haven't checked the serial pinout to verify.

    Here is some reading that may be of assistance.
    http://www.easysw.com/~mike/serial/serial.html#2_5
    Hope this helps.

    Art Perry
    Linux Systems/Software Engineer
     
    , Dec 9, 2004
    #2
    1. Advertising

  3. Gregory Graham

    Arthur Perry Guest

    On Wed, 8 Dec 2004, Gregory Graham wrote:

    Hi Gregory,

    Are those pins you specified the DCD signal?
    I am not sure how fopen() is calling open() (I didn't bother checking),
    but you want to make sure that open() is called WITHOUT the O_NDELAY flag.
    Also, you may want the O_NOCTTY flag to be present.

    Try writing the program using open() instead, and specify the exact flags.
    You can also pass fcntl()s to the device.

    If these pins are not DCD signal, and rather some obscure pins then the
    driver may be unaware of its "readiness". Then any open() call may not
    block, whether the device is actually capable of sending data or not. I
    haven't checked the serial pinout to verify.

    Here is some reading that may be of assistance.
    http://www.easysw.com/~mike/serial/serial.html#2_5

    Hope this helps.

    Art Perry
    Linux Systems/Software Engineer




    > I have a temperature sensor device we use to shutdown our servers. It
    > relys on fopen() blocking when the serial port device isn't ready.
    > Unfortunately that's not what's happening. The fopen() returns
    > immediately without an error. What have I got wrong?
    >
    > Basically when it gets too hot the device connects the 8 and 6 lines
    > to line 20. That should, as I understand it, tell the fopen() that
    > the file is available and cause the rest of the program to execute.
    >
    > The code is below. I compile it with "gcc -o minmon minmon.c" and
    > invoke it as "minmon /dev/tyyS0" on SuSE9 with gcc 2.95.3
    >
    > #include <stdio.h>
    > #include <signal.h>
    >
    > int await_event(char* port)
    > {
    > FILE *file;
    > int fd;
    >
    > if ((file = fopen(port, "r")) != NULL) /*blocks*/
    > fclose(file); /*no longer need port*/
    > else
    > {
    > fprintf(stderr, "ERROR opening port %s", port);
    > return -1;
    > }
    > return 0;
    > }
    >
    > main (int argc, char* argv[])
    > {
    > int pid, getuid();
    > char* port, *script;
    >
    > if (argc != 2 )
    > {
    > fprintf(stderr, "Usage:");
    > fprintf(stderr, "\t%s device\n", argv[0]);
    > exit(3);
    > }
    > else /*run in diagnostic mode*/
    > {
    > port = argv[1];
    > printf("watching on port: %s\n", port);
    > if (await_event(port) == 0) /*blocks*/
    > {
    > printf("%s: diagnostic power out\n", argv[0]);
    > /*insert shutdown code here*/
    > }
    > else
    > printf("%s: ERROR while waiting for event\n",
    > argv[0]);
    > exit(0);
    > }
    > }
    >
     
    Arthur Perry, Dec 9, 2004
    #3
  4. >I have a temperature sensor device we use to shutdown our servers. It
    >relys on fopen() blocking when the serial port device isn't ready.
    >Unfortunately that's not what's happening. The fopen() returns
    >immediately without an error. What have I got wrong?


    Sometimes serial ports have several associated devices.
    Some of them wait for carrier detect ("dial-in") and some
    of them don't ("dial-out").

    >Basically when it gets too hot the device connects the 8 and 6 lines
    >to line 20.


    DTR -> DSR and DTR -> DCD . This is typical of a quick-and-dirty
    way for a UPS to signal a power failure when batteries are
    near to exhausted.


    >That should, as I understand it, tell the fopen() that
    >the file is available and cause the rest of the program to execute.


    Gordon L. Burditt
     
    Gordon Burditt, Dec 9, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nonee
    Replies:
    2
    Views:
    2,657
    Neredbojias
    Oct 25, 2005
  2. wilson
    Replies:
    3
    Views:
    953
    wilson
    Aug 10, 2004
  3. Michel Rouzic
    Replies:
    4
    Views:
    1,836
    Michel Rouzic
    Apr 28, 2008
  4. jasperng
    Replies:
    0
    Views:
    1,741
    jasperng
    Nov 2, 2008
  5. Li Han
    Replies:
    2
    Views:
    508
    bobicanprogram
    Feb 9, 2009
Loading...

Share This Page