Пример #1
0
int serial_set_params(struct serial_device_t *serial,
                      int baudrate,
                      int bits,
                      int parity,
                      int stopbits,
                      int flowcontrol,
                      int rts,
                      int dtr)
{
  if (!serial) {
    log_error("serial_set_params: invalid serial port device.");
    return -DEVICE_CONN_ERROR;
  }
  if (!serial->data) {
    log_error("serial_set_params: cannot configure unopened serial port %s.",
              serial->port);
    return -DEVICE_CONN_ERROR;
  }
  log_debug("serial_set_params: setting serial parameters on port %s.",
            serial->port);
  struct sp_port_config *config;
  sp_new_config(&config);
  sp_set_config_baudrate(config, baudrate);
  sp_set_config_bits(config, bits);
  sp_set_config_parity(config, parity);
  sp_set_config_stopbits(config, stopbits);
  sp_set_config_rts(config, flowcontrol == 1 ? SP_RTS_FLOW_CONTROL : rts);
  sp_set_config_cts(config, flowcontrol == 1 ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE);
  sp_set_config_dtr(config, dtr);
  sp_set_config_dsr(config, SP_DSR_IGNORE);
  sp_set_config_xon_xoff(config, flowcontrol == 2 ? SP_XONXOFF_INOUT : SP_XONXOFF_DISABLED);
  int ret = sp_set_config(serial->data, config);
  sp_free_config(config);
  char *error;
  switch (ret) {
  case SP_ERR_ARG:
    log_error("serial_set_params: invalid serial port parameters.");
    return -DEVICE_CONF_ERROR;
  case SP_ERR_FAIL:
    error = sp_last_error_message();
    log_error("serial_set_params: error setting serial port parameters (%d): %s.",
              sp_last_error_code(), error);
    sp_free_error_message(error);
    return -DEVICE_CONN_ERROR;
  }
  return NO_ERROR;
}
Пример #2
0
int Arduino_connect(const char *serialPortName, int baudrate)
{
    struct sp_port_config *serialPortConfiguration;
    enum sp_return r;

    if(serialPort) sp_free_port(serialPort);

    // Find the specified serial port
    if(sp_get_port_by_name(serialPortName, &serialPort) != SP_OK)
    {
        fprintf(stderr, "Cannot find the serial port\n");
        return 0;
    }

    // Open the serial port
    if(sp_open(serialPort, SP_MODE_READ_WRITE) != SP_OK)
    {
        fprintf(stderr, "Cannot open the serial port\n");
        return 0;
    }

    // Configure the serial port
    sp_new_config(&serialPortConfiguration);
    sp_set_config_baudrate(serialPortConfiguration, 9600);
    sp_set_config_parity(serialPortConfiguration, SP_PARITY_NONE);
    sp_set_config_bits(serialPortConfiguration, 8);
    sp_set_config_stopbits(serialPortConfiguration, 1);
    sp_set_config_flowcontrol(serialPortConfiguration, SP_FLOWCONTROL_NONE);

    if(sp_set_config(serialPort, serialPortConfiguration) != SP_OK)
    {
        fprintf(stderr, "Cannot configure the serial port\n");
        return 0;
    }

    sp_free_config(serialPortConfiguration);

    return 1;
}
Пример #3
0
/**
 * Set serial parameters for the specified serial port.
 *
 * @param serial Previously initialized serial port structure.
 * @param baudrate The baudrate to set.
 * @param bits The number of data bits to use.
 * @param parity The parity setting to use (0 = none, 1 = even, 2 = odd).
 * @param stopbits The number of stop bits to use (1 or 2).
 * @param flowcontrol The flow control settings to use (0 = none, 1 = RTS/CTS,
 *                    2 = XON/XOFF).
 *
 * @return SR_OK upon success, SR_ERR upon failure.
 */
SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate,
			      int bits, int parity, int stopbits,
			      int flowcontrol, int rts, int dtr)
{
	int ret;
	char *error;
	struct sp_port_config *config;

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

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

	sr_spew("Setting serial parameters on port %s (fd %d).", serial->port,
		serial->fd);

	sp_new_config(&config);
	sp_set_config_baudrate(config, baudrate);
	sp_set_config_bits(config, bits);
	switch (parity) {
	case 0:
		sp_set_config_parity(config, SP_PARITY_NONE);
		break;
	case 1:
		sp_set_config_parity(config, SP_PARITY_EVEN);
		break;
	case 2:
		sp_set_config_parity(config, SP_PARITY_ODD);
		break;
	default:
		return SR_ERR_ARG;
	}
	sp_set_config_stopbits(config, stopbits);
	sp_set_config_rts(config, flowcontrol == 1 ? SP_RTS_FLOW_CONTROL : rts);
	sp_set_config_cts(config, flowcontrol == 1 ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE);
	sp_set_config_dtr(config, dtr);
	sp_set_config_dsr(config, SP_DSR_IGNORE);
	sp_set_config_xon_xoff(config, flowcontrol == 2 ? SP_XONXOFF_INOUT : SP_XONXOFF_DISABLED);

	ret = sp_set_config(serial->data, config);
	sp_free_config(config);

	switch (ret) {
	case SP_ERR_ARG:
		sr_err("Invalid arguments for setting serial port parameters.");
		return SR_ERR_ARG;
	case SP_ERR_FAIL:
		error = sp_last_error_message();
		sr_err("Error setting serial port parameters: %s.", error);
		sp_free_error_message(error);
		return SR_ERR;
	}

	return SR_OK;
}