2

我有一个使用某种协议将数据写入串行端口的功能。当函数写入一帧时,它等待接收者的一个答复。如果没有收到应答,它必须在 3 次超时期间重新发送数据,并且在 3 次超时结束时没有成功,关闭通信...

我有这个功能:

int serial_write(int fd, unsigned char* send, size_t send_size) { 

......


        int received_counter = 0;
        while (!RECEIVED) {
            Timeout.tv_usec = 0;  // milliseconds
            Timeout.tv_sec  = timeout;  // seconds
            FD_SET(fd, &readfs);
            //set testing for source 1

            res = select(fd + 1, &readfs, NULL, NULL, &Timeout);
            //timeout occurred.

            if (received_counter == 3) {
                printf(
                        "Connection maybe turned off! Number of resends exceeded!\n");
                exit(-1);
            }

            if (res == 0) {
                printf("Timeout occured\n");
                write(fd, (&I[0]), I.size());
                numTimeOuts++;
                received_counter++;

            } else {
                RECEIVED = true;
                break;
            }

        }

......

}

我已验证此函数在超时时不会重新发送数据。为什么?

4

0 回答 0