Пример #1
0
/* Handle suspension reasonably ... */
static void sig_suspend(int sig) {
  if (!interactive) {
    reset_stdin();    /* Reset the standard input channel */
    raise(SIGSTOP);             /* Actually suspend */
    setup_stdin();              /* Put it back */
  } else
    raise(SIGSTOP);             /* Actually suspend */
}
Пример #2
0
/*
 * datapath_task
 */
void datapath_task(const char* portname, const char* logfile) {

    FILE*               log;
    int                 check; 

    int                 fd, closed; 

    int                 write_serial = 0; 
    int                 bytes_serial = 0; 
    int                 value_serial = 0; 

    int                 bytes_stdin = 0; 
    int                 value_stdin = 0; 

    struct termios      orig_tios;
    struct termios      orig_stdin_tios;

    log = fopen(logfile, "w");
    if(log == NULL) {
        printf("logfile: %s open in write mode failure\n", logfile);
    }

    printf("Starting datapath test.\n\n");
    printf("Opening serial port.\n");

    fd = init_port_raw(portname, 115200, &orig_tios) ;
    if(fd < 0) {
        fprintf(stderr, "Failed to open %s\n", portname);
        exit(EXIT_FAILURE);
    }

    // read value from stdin
    if(init_stdin(&orig_stdin_tios) != 0) {
        fprintf(stderr, "datapath_task: Failed to init stdin.\n");
        exit(EXIT_FAILURE);
    };


    check = flush_port(fd);
    if (check != 0 ) {
        fprintf(stderr,"flush port failed.\n");
        exit(EXIT_FAILURE);
    }
    while(1) {
        bytes_stdin = read(0, &value_stdin, 1);
        if(bytes_stdin < 0) {
        //    fprintf(stderr, "datapath_task: stdin read error. %i\n", bytes_stdin);
        } else if (bytes_stdin > 0) {
            if(value_stdin == 'f') {
                check = flush_port(fd);
                if (check != 0 ) {
                    fprintf(stderr,"flush port failed.\n");
                    exit(EXIT_FAILURE);
                }
                printf("\nFlush buffer.\n");
                continue;
            } else {
                printf("\nOptions: (s)-stop, (r)-reset, (g)-go, (f)-flush host buffer, (q)-quit\n");
                printf("\nYou typed: %c\n", value_stdin);
                write_serial = write(fd, &value_stdin, 1);
                if(write_serial != 1) {
                    fprintf(stderr, "Write serial %i\n", write_serial);
                }
            }
        } else {}

        if(value_stdin == 'q') {
            printf("You typed quit.\n");
            break;
        }

//        printf("Read from serial...\n");
        bytes_serial = read(fd, &value_serial, 4);
        if(bytes_serial < 0) {
////            fprintf(stderr, "datapath_task: serial read error. %d\n", bytes_serial);
        } else if (bytes_serial > 0) {
            //printf("\nReceived: %u bytes: 0x%x\n", bytes_serial, value_serial);
            printf("%x\n", value_serial);
            fprintf(log, "%x %u\n", value_serial, value_serial);
        } else {}
    }

    printf("Closing serial port.\n");
    closed = close_port(fd, &orig_tios);
    if(closed != 0) {
        fprintf(stderr, "Unable to close fd: %i\n", fd);
        exit(EXIT_FAILURE);
    }

    fclose(log);

    printf("Reset stdin.\n");
    reset_stdin(&orig_stdin_tios);

}