0
    configuration of my serial port is:

        timer_t tid = 0;
            struct itimerspec it;

            fd = open("/dev/ttyUSB1", O_RDWR | O_NOCTTY | O_NDELAY);
            if (fd == -1) {
                perror("open_port: Unable to open /dev/ttyS0\n");
                exit(1);
            }

            satimer.sa_handler = signal_handler_TIMER;
            satimer.sa_flags = 0;
            satimer.sa_restorer = NULL;
            sigaction(SIGALRM, &satimer, NULL);

            it.it_value.tv_sec = 0;
            it.it_value.tv_nsec = 10000000;
            it.it_interval.tv_sec = 0;
            it.it_interval.tv_nsec = 10000000;
            if (timer_create(CLOCK_REALTIME, NULL, &tid) == -1)
                fprintf(stderr, "error in timer_create \n");
            // printf("timer ID is 0x%lx\n", (long) tid);
            if (timer_settime(tid, 0, &it, NULL) == -1)
                fprintf(stderr, "error in settime \n");

            fcntl(fd, F_SETFL, 0);
            fcntl(fd, F_SETOWN, getpid());
            fcntl(fd, F_SETFL, O_SYNC); /**<<<<<<------This line made it work.**/

            tcgetattr(fd, &termAttr);
            //baudRate = B115200;          /* Not needed */
            cfsetispeed(&termAttr, B9600);
            cfsetospeed(&termAttr, B9600);
            termAttr.c_cflag |= PARENB;
            termAttr.c_cflag &= ~PARODD;
            termAttr.c_cflag &= ~CSTOPB;
            termAttr.c_cflag &= ~CSIZE;
            termAttr.c_cflag |= CS8;
            termAttr.c_cflag |= (CLOCAL | CREAD);
            termAttr.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
            termAttr.c_iflag &= ~(IXON | IXOFF | IXANY);
            termAttr.c_oflag &= ~OPOST;
            termAttr.c_cc[VMIN] = 5;
            termAttr.c_cc[VTIME] = 5;
            tcsetattr(fd, TCSANOW, &termAttr);
    ...................
    void PowerUp(void){
        unsigned char *p_commands,ima,komanda = STATUS_REQUEST;
        p_commands = &comandi[0];
        unsigned char *p_tx_buffer_;
        for (;;) {
                if ((milisekundi == 10) || (milisekundi == 30) || (milisekundi == 50)
                        || (milisekundi == 70) || (milisekundi == 90)) {
                    makeTXpaket(0x00);
                    makeTXpaket(komanda);
                    p_tx_buffer_ = &tx_buffer[1];
                    nbytes = write(fd, tx_buffer, *p_tx_buffer_);
                                printf("%d"errno);
                    if (nbytes != sizeof(tx_buffer)) {

                    }
                    sleep(0.2);
                    bytes = read(fd, rx_buffer, sizeof(rx_buffer));
                                printf("%d"errno);
                                if (bytes != sizeof(rx_buffer)) {
                                }
                                printf("%X\n", rx_buffer);
                            }
.....................

after n times write and read from serial serial comunication stop and errno =4 how to solve this ?why serial comunication stop?my timer interupted write() function and this is reason for this?thanks for your help!

4

1 回答 1

0

EINTR意味着您的程序在write能够写入数据之前捕获了一个信号。

由于您的程序中有一个基于信号的计时器,因此可能是该信号中断了呼叫。

你可能想SA_RESTART在你的satimer.sa_flags. 在手册页中阅读有关信号和SA_RESTART标志的更多信息。signal(7)

于 2013-07-07T14:58:14.650 回答