-1

Im trying to create an application that will count pulses from an Oscilloscope, my problem is this:

I need a loop to run constantly, until a user input is entered. However I dont want getch() to be called unless there is an input on the terminal ready to be read. How would I go about checking for a character or integer existing on the terminal?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115

4 Answers4

4

If you're coding for UNIX, you'll need to use either poll(2) or select(2).

rslemos
  • 2,454
  • 22
  • 32
2

Since you mention the non-standard getch you might also have kbhit which tests if there is input waiting. But don't slug your oscilloscope with that every loop: you can ease the flow by checking occasionally.

#include <conio.h>
#include <stdio.h>

#define POLLMASK    0xFFFF

int main(void){

    int poll = 0;
    int ch = 0;

    while(ch != 27) {

        // ... oscilloscope details

        if ((poll++ & POLLMASK) == 0 && _kbhit()) {
            ch = _getch();
            // ... do something with input
        }
    }
    return 0;
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
0

Use scanf() . It's standard, it already waits for a character in the buffer, and it's pretty much universal.

EDIT:

In the case of an unnamed OS, this is simply not to be done. Standard C has no way of reading raw data from a terminal and guaranteeing verbatim, unformatted results. It's entirely up to your terminal handler.

However, if your microcontroller has some sort of serial library, I would suggest doing something like this:

if characters in buffer is greater than 0, and the character just read is not a terminating character, report it. Otherwise, keep waiting for a character in the buffer.

Mason Watmough
  • 495
  • 6
  • 19
0

until a user input is entered. this sentence indicates the user will use the keyboard eventually, therefore, you can track keyboard callback event to track the user's input. There are several libraries that provide you with such keyboard events (e.g. GLUT, SDL)

CroCo
  • 5,531
  • 9
  • 56
  • 88