I want to monitor a interactive C program (say program1), which is running on one terminal window. and takes input as number (0-9)
As in monitoring I expect : when I will provide input to program1(running on separate terminal) my observer should detect that the key is pressed (from separate terminal).
To achieve that I have to write a C program (observer_prog.c) which will execute on separate terminal waiting for the activities from program1.
I implemented the suggestions from below links for GCC compiler kbhit() function. But it is not able to detect the key strokes from different terminal executions.
kbhit() implementation for gcc
Is there anyway I can improve this existing module, OR any other workaround for this ?
Observer Program
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <termios.h>
#include <stdlib.h>
static struct termios g_old_kbd_mode;
static int kbhit(void){
struct timeval timeout;
fd_set read_handles;
int status;
// check stdin (fd 0) for activity
FD_ZERO(&read_handles);
FD_SET(0, &read_handles);
timeout.tv_sec = timeout.tv_usec = 0;
status = select(0 + 1, &read_handles, NULL, NULL, &timeout);
return status;
}
static void old_attr(void){
tcsetattr(0, TCSANOW, &g_old_kbd_mode);
}
void press(){
printf("Pressed !!!\n");
}
// main function
int main( void ){
char ch;
static char init;
struct termios new_kbd_mode;
if(init)
return 0;
// put keyboard (stdin, actually) in raw, unbuffered mode
tcgetattr(0, &g_old_kbd_mode);
memcpy(&new_kbd_mode, &g_old_kbd_mode, sizeof(struct termios));
new_kbd_mode.c_lflag &= ~(ICANON | ECHO);
new_kbd_mode.c_cc[VTIME] = 0;
new_kbd_mode.c_cc[VMIN] = 1;
tcsetattr(0, TCSANOW, &new_kbd_mode);
atexit(old_attr);
while (!kbhit()){
if(kbhit()){
press();
exit(1);
}
}
return 0;
}
hi I am not sure but you can try changing STDIN to /dev/ttyUSB*(or applicable device node.) as we are reading it from USART. Let us know if it works or not.