From: Martin Ling Date: Thu, 14 Nov 2013 22:43:03 +0000 (+0000) Subject: Add sp_set_flowcontrol helper function. X-Git-Tag: libserialport-0.1.0~116 X-Git-Url: https://sigrok.org/gitweb/?a=commitdiff_plain;h=18fc2dd12bdc4965f705d3bd6d1ebfda3abdbe80;hp=824dcb45618146cac9103e313e30290f749940d3;p=libserialport.git Add sp_set_flowcontrol helper function. --- diff --git a/libserialport.h.in b/libserialport.h.in index 678a6fa..9ec7bf1 100644 --- a/libserialport.h.in +++ b/libserialport.h.in @@ -132,6 +132,14 @@ enum { SP_XONXOFF_INOUT = 3 }; +/* Standard flow control combinations. */ +enum { + SP_FLOWCONTROL_NONE = 0, + SP_FLOWCONTROL_XONXOFF = 1, + SP_FLOWCONTROL_RTSCTS = 2, + SP_FLOWCONTROL_DTRDSR = 3 +}; + /* Enumeration */ int sp_get_port_by_name(const char *portname, struct sp_port **port_ptr); void sp_free_port(struct sp_port *port); @@ -148,12 +156,15 @@ int sp_read(struct sp_port *port, void *buf, size_t count); int sp_write(struct sp_port *port, const void *buf, size_t count); int sp_flush(struct sp_port *port); -/* Port configuration */ +/* Basic port configuration */ int sp_set_config(struct sp_port *port, struct sp_port_config *config); int sp_set_baudrate(struct sp_port *port, int baudrate); int sp_set_bits(struct sp_port *port, int bits); int sp_set_parity(struct sp_port *port, int parity); int sp_set_stopbits(struct sp_port *port, int stopbits); +int sp_set_flowcontrol(struct sp_port *port, int flowcontrol); + +/* Advanced port configuration */ int sp_set_rts(struct sp_port *port, int rts); int sp_set_cts(struct sp_port *port, int cts); int sp_set_dtr(struct sp_port *port, int dtr); diff --git a/serialport.c b/serialport.c index 732714b..98c1a77 100644 --- a/serialport.c +++ b/serialport.c @@ -939,22 +939,55 @@ static int apply_config(struct sp_port *port, struct sp_port_data *data) } #define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0) -#define TRY_SET(x) do { if (config->x >= 0) TRY(set_##x(&data, config->x)); } while (0) +#define TRY_SET(x, y) do { if (y >= 0) TRY(set_##x(&data, y)); } while (0) +#define TRY_SET_CONFIG(x) TRY_SET(x, config->x) int sp_set_config(struct sp_port *port, struct sp_port_config *config) { struct sp_port_data data; TRY(start_config(port, &data)); - TRY_SET(baudrate); - TRY_SET(bits); - TRY_SET(parity); - TRY_SET(stopbits); - TRY_SET(rts); - TRY_SET(cts); - TRY_SET(dtr); - TRY_SET(dsr); - TRY_SET(xon_xoff); + TRY_SET_CONFIG(baudrate); + TRY_SET_CONFIG(bits); + TRY_SET_CONFIG(parity); + TRY_SET_CONFIG(stopbits); + TRY_SET_CONFIG(rts); + TRY_SET_CONFIG(cts); + TRY_SET_CONFIG(dtr); + TRY_SET_CONFIG(dsr); + TRY_SET_CONFIG(xon_xoff); + TRY(apply_config(port, &data)); + + return SP_OK; +} + +int sp_set_flowcontrol(struct sp_port *port, int flowcontrol) +{ + struct sp_port_data data; + + TRY(start_config(port, &data)); + + if (flowcontrol == SP_FLOWCONTROL_XONXOFF) + TRY_SET(xon_xoff, SP_XONXOFF_INOUT); + else + TRY_SET(xon_xoff, SP_XONXOFF_DISABLED); + + if (flowcontrol == SP_FLOWCONTROL_RTSCTS) { + TRY_SET(rts, SP_RTS_FLOW_CONTROL); + TRY_SET(cts, SP_CTS_FLOW_CONTROL); + } else { + TRY_SET(rts, SP_RTS_ON); + TRY_SET(cts, SP_CTS_IGNORE); + } + + if (flowcontrol == SP_FLOWCONTROL_DTRDSR) { + TRY_SET(dtr, SP_DTR_FLOW_CONTROL); + TRY_SET(dsr, SP_DSR_FLOW_CONTROL); + } else { + TRY_SET(dtr, SP_DTR_ON); + TRY_SET(dsr, SP_DSR_IGNORE); + } + TRY(apply_config(port, &data)); return SP_OK;