/** * Set serial parameters for the specified serial port. * * @param serial Previously initialized serial port structure. * @param[in] baudrate The baudrate to set. * @param[in] bits The number of data bits to use (5, 6, 7 or 8). * @param[in] parity The parity setting to use (0 = none, 1 = even, 2 = odd). * @param[in] stopbits The number of stop bits to use (1 or 2). * @param[in] flowcontrol The flow control settings to use (0 = none, * 1 = RTS/CTS, 2 = XON/XOFF). * @param[in] rts Status of RTS line (0 or 1; required by some interfaces). * @param[in] dtr Status of DTR line (0 or 1; required by some interfaces). * * @retval SR_OK Success. * @retval SR_ERR Failure. * * @private */ 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->data) { sr_dbg("Cannot configure unopened serial port %s.", serial->port); return SR_ERR; } sr_spew("Setting serial parameters on port %s.", serial->port); 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 (%d): %s.", sp_last_error_code(), error); sp_free_error_message(error); return SR_ERR; } return SR_OK; }
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; }