Exemplo n.º 1
0
int _modbus_rtu_select(modbus_t *ctx, fd_set *rfds, struct timeval *tv, int length_to_read)
{
    int s_rc;
#if defined(_WIN32)
    s_rc = win32_ser_select(&(((modbus_rtu_t*)ctx->backend_data)->w_ser), length_to_read, tv);
    if (s_rc == 0) {
        errno = ETIMEDOUT;
        return -1;
    }

    if (s_rc < 0) {
        _error_print(ctx, "select");
        if (ctx->error_recovery && (errno == EBADF)) {
            modbus_close(ctx);
            modbus_connect(ctx);
            errno = EBADF;
            return -1;
        } else {
            return -1;
        }
    }
#else
    while ((s_rc = select(ctx->s+1, rfds, NULL, NULL, tv)) == -1) {
        if (errno == EINTR) {
            if (ctx->debug) {
                fprintf(stderr, "A non blocked signal was caught\n");
            }
            /* Necessary after an error */
            FD_ZERO(rfds);
            FD_SET(ctx->s, rfds);
        } else {
            _error_print(ctx, "select");
            if (ctx->error_recovery && (errno == EBADF)) {
                modbus_close(ctx);
                modbus_connect(ctx);
                errno = EBADF;
                return -1;
            } else {
                return -1;
            }
        }
    }

    if (s_rc == 0) {
        /* Timeout */
        errno = ETIMEDOUT;
        _error_print(ctx, "select");
        return -1;
    }
#endif

    return s_rc;
}
Exemplo n.º 2
0
int8_t _dynamixel_rtu_select(dynamixel_t *ctx, fd_set *rfds,
											struct timeval *tv, uint8_t length_to_read) {
	int s_rc;
#if defined(_WIN32)
	s_rc = win32_ser_select(
		&(((dynamixel_rtu_t*)ctx->backend_data)->w_ser),
		length_to_read,
		tv
	);
	if (s_rc == 0) {
		errno = ETIMEDOUT;
		return -1;
	}

	if (s_rc < 0) {
		return -1;
	}
#else
	while ((s_rc = select(ctx->s+1, rfds, NULL, NULL, tv)) == -1) {
		if (errno == EINTR) {
			if (ctx->debug) {
				fprintf(stderr, "A non blocked signal was caught\n");
			}
			/* Necessary after an error */
			FD_ZERO(rfds);
			FD_SET(ctx->s, rfds);
		} else {
			return -1;
		}
	}

	if (s_rc == 0) {
		/* Timeout */
		errno = ETIMEDOUT;
		return -1;
	}
#endif

	return s_rc;
}
Exemplo n.º 3
0
int _modbus_rtu_select(modbus_t *ctx, fd_set *rfds, struct timeval *tv, int msg_length_computed, int msg_length)
{
    int s_rc;
#if defined(_WIN32)
    s_rc = win32_ser_select(&(((modbus_rtu_t*)ctx->backend_data)->w_ser), msg_length_computed, tv);
    if (s_rc == 0) {
        errno = ETIMEDOUT;
        return -1;
    }

    if (s_rc < 0) {
        _error_print(ctx, "select");
        if (ctx->error_recovery && (errno == EBADF)) {
            modbus_close(ctx);
            modbus_connect(ctx);
            errno = EBADF;
            return -1;
        } else {
            return -1;
        }
    }
#else
    while ((s_rc = select(ctx->s+1, rfds, NULL, NULL, tv)) == -1) {
        if (errno == EINTR) {
            if (ctx->debug) {
                fprintf(stderr, "A non blocked signal was caught\n");
            }
            /* Necessary after an error */
            FD_ZERO(rfds);
            FD_SET(ctx->s, rfds);
        } else {
            _error_print(ctx, "select");
            if (ctx->error_recovery && (errno == EBADF)) {
                modbus_close(ctx);
                modbus_connect(ctx);
                errno = EBADF;
                return -1;
            } else {
                return -1;
            }
        }
    }

    if (s_rc == 0) {
        /* Timeout */
        if (msg_length == (ctx->backend->header_length + 2 +
                           ctx->backend->checksum_length)) {
            /* Optimization allowed because exception response is
               the smallest trame in modbus protocol (3) so always
               raise a timeout error.
               Temporary error before exception analyze. */
            errno = EMBUNKEXC;
        } else {
            errno = ETIMEDOUT;
            _error_print(ctx, "select");
        }
        return -1;
    }
#endif

    return s_rc;
}