Пример #1
0
static int _serial_write(struct serial_device_t *serial,
                         const void *buf, size_t count,
                         int nonblocking, unsigned int timeout_ms)
{
  if (!serial) {
    log_error("serial_write: invalid serial port device.");
    return -DEVICE_CONN_ERROR;
  }
  if (!serial->data) {
    log_error("serial_write: cannot use unopened serial port %s.", serial->port);
    return -DEVICE_CONN_ERROR;
  }
  int ret;
  if (nonblocking) {
    ret = sp_nonblocking_write(serial->data, buf, count);
  } else {
    ret = sp_blocking_write(serial->data, buf, count, timeout_ms);
  }
  char *error;
  switch (ret) {
  case SP_ERR_ARG:
    log_error("serial_write: invalid serial port parameters.");
    return -DEVICE_CONF_ERROR;
  case SP_ERR_FAIL:
    error = sp_last_error_message();
    log_error("serial_write: write error (%d): %s.", sp_last_error_code(), error);
    sp_free_error_message(error);
    return -DEVICE_CONN_ERROR;
  }
  return ret;
}
Пример #2
0
/**
 * Write a number of bytes to the specified serial port.
 *
 * @param serial Previously initialized serial port structure.
 * @param buf Buffer containing the bytes to write.
 * @param count Number of bytes to write.
 *
 * @return The number of bytes written, or a negative error code upon failure.
 */
SR_PRIV int serial_write(struct sr_serial_dev_inst *serial,
		const void *buf, size_t count)
{
	ssize_t ret;
	char *error;

	if (!serial) {
		sr_dbg("Invalid serial port.");
		return SR_ERR;
	}

	if (serial->fd == -1) {
		sr_dbg("Cannot use unopened serial port %s (fd %d).",
				serial->port, serial->fd);
		return SR_ERR;
	}

	if (serial->nonblocking)
		ret = sp_nonblocking_write(serial->data, buf, count);
	else
		ret = sp_blocking_write(serial->data, buf, count, 0);

	switch (ret) {
	case SP_ERR_ARG:
		sr_err("Attempted serial port write with invalid arguments.");
		return SR_ERR_ARG;
	case SP_ERR_FAIL:
		error = sp_last_error_message();
		sr_err("Write error: %s.", error);
		sp_free_error_message(error);
		return SR_ERR;
	}

	sr_spew("Wrote %d/%d bytes (fd %d).", ret, count, serial->fd);

	return ret;
}
Пример #3
0
static int _serial_write(struct sr_serial_dev_inst *serial,
		const void *buf, size_t count, int nonblocking, unsigned int timeout_ms)
{
	ssize_t ret;
	char *error;

	if (!serial) {
		sr_dbg("Invalid serial port.");
		return SR_ERR;
	}

	if (!serial->data) {
		sr_dbg("Cannot use unopened serial port %s.", serial->port);
		return SR_ERR;
	}

	if (nonblocking)
		ret = sp_nonblocking_write(serial->data, buf, count);
	else
		ret = sp_blocking_write(serial->data, buf, count, timeout_ms);

	switch (ret) {
	case SP_ERR_ARG:
		sr_err("Attempted serial port write with invalid arguments.");
		return SR_ERR_ARG;
	case SP_ERR_FAIL:
		error = sp_last_error_message();
		sr_err("Write error (%d): %s.", sp_last_error_code(), error);
		sp_free_error_message(error);
		return SR_ERR;
	}

	sr_spew("Wrote %zd/%zu bytes.", ret, count);

	return ret;
}
Пример #4
0
int serial_send(struct sp_port *my_port, const void *command, size_t write_bytes_count, void *response) {

  int ret, read_count=6; 

  if (DO_BLOCKING_READ_WRITE) {
    unsigned int timeout = READ_WRITE_TIMEOUT_SEC*1000;
    
    ret = sp_blocking_write(my_port, command, write_bytes_count, timeout);
    if (ret <= 0) {
      printf("Failed to write serial bytes.\n");
      return 0;
    }
    ret = sp_blocking_read(my_port, response, (size_t)read_count, timeout);
 
  } else {

    int fd;
    ret = sp_get_port_handle(my_port,&fd);
    if (ret != SP_OK) {
      printf("Failed to get port handle for nonblocking read/write.\n");
    }
    fd_set fds;
    FD_ZERO(&fds);
    FD_SET(fd,&fds);
    
    struct timespec ts;
    ts.tv_sec = READ_WRITE_TIMEOUT_SEC;
    
    ret = pselect(fd+1, NULL, &fds, NULL, &ts, NULL);
    if (ret < 0 && errno != EINTR) {
      printf("Error in pselect for write.\n");
      return 0;
    } else if (ret == 0) {
      printf("Timeout while waiting to write.\n");
      return 0;
    }
    ret = sp_nonblocking_write(my_port, command, write_bytes_count);

    FD_ZERO(&fds);
    FD_SET(fd,&fds);
    ts.tv_sec = READ_WRITE_TIMEOUT_SEC;
     
    ret = pselect(fd+1, &fds, NULL, NULL, &ts, NULL);
    if (ret < 0 && errno != EINTR) {
      printf("Error in pselect for write.\n");
      return 0;
    } else if (ret == 0) {
      printf("Timeout while waiting to write.\n");
      return 0;
    }
    ret = sp_nonblocking_read(my_port, response, (size_t)read_count);
  
  }

  if (ret < read_count) {
    printf("Successful write to serial, but failed on serial read.\n");
    return 0;
  } else {
    return ret;
  }

}
Пример #5
0
int Arduino_write(const void *buffer, size_t bufferSize)
{
    if(!serialPort) return 0;
    return sp_nonblocking_write(serialPort, buffer, bufferSize) == SP_OK;
}