Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在c+中使用select+;在协议中执行超时以通过串行端口传输文件_C++_C_Serial Port - Fatal编程技术网

C++ 在c+中使用select+;在协议中执行超时以通过串行端口传输文件

C++ 在c+中使用select+;在协议中执行超时以通过串行端口传输文件,c++,c,serial-port,C++,C,Serial Port,我有一个功能,可以用特定的协议将数据写入串行端口。当函数写入一帧时,它将等待接收器的一个应答。如果没有收到应答,则必须在3次超时期间重新发送数据,并且在3次超时结束时没有成功,请关闭通信 我有这个功能: int serial_write(int fd, unsigned char* send, size_t send_size) { ...... int received_counter = 0; while (!RECEIVED) {

我有一个功能,可以用特定的协议将数据写入串行端口。当函数写入一帧时,它将等待接收器的一个应答。如果没有收到应答,则必须在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;
            }

        }

......

}

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

自动重新发送数据需要什么?变量(?)I是什么?您应该检查
write
调用是否有错误。变量I是一个无符号字符的向量,其中有一个我想要发送的向量。当我没有收到目标串行端口的应答时,我会重新发送数据。我所说的检查
write
调用是否存在错误的意思是,您应该检查
write
返回的内容。如果是
-1
,则
写入
失败,您应该检查是否出错。您还可以使用获取更好的错误消息,您可以打印…或者使用
perror
strerror