Menu

Non-blocking serial gets...

Help
2002-05-08
2002-05-14
  • Michael Richards

    I'm using the serial part of the library through a ttystream. I can't seem to find a way to get a character or determine if there is one waiting without my program blocking.

    Here is a quick program to illustrate the problem:
    #include "cc++/serial.h"
    #include <iostream.h>

    using namespace ost;

    main() {
      ttystream modem("/dev/cuaa0");
      if (!modem) {
        cerr << "Unable to open modem\n";
        return 0;
      }

      modem.setSpeed(38400);
      modem.setCharBits(8);
      modem.setParity(Serial::parityNone);
      modem.setStopBits(1);
      modem.setFlowControl(Serial::flowHard);
      modem.toggleDTR(500);

      modem.interactive(true);
      modem << "ATZ\r";

      while (1) {
        cout << "Going to wait\n";
        char c=modem.get();
        cout << "Got a (" << c << ")\n";
      }
    }

    When run it outputs:
    ./test
    Going to wait
    Got a (A)
    Going to wait
    Got a (T)
    Going to wait
    Got a (Z)
    Going to wait
    )ot a (
    Going to wait
    )ot a (
    Going to wait
    Got a (
    )
    Going to wait
    Got a (O)
    Going to wait
    Got a (K)
    Going to wait
    )ot a (
    Going to wait
    Got a (
    )
    Going to wait

    This tells me that it's blocking on the get() call. How can I tell if there are chars waiting?

    -Michael

     
    • Buck

      Buck - 2002-05-14

      shouldn't

              while (modem.isPending(pendingInput, suitableTimeout)) {
                      cout << "not waiting for nobody!" << endl;
                      char c;
                      modem.get(c);
                      cout << "got a (" << c << ")" << endl;
              }

      do the trick?

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.