From: Martin Ling Date: Thu, 14 Nov 2013 19:45:42 +0000 (+0000) Subject: Use negative values in sp_port_config to retain existing settings. X-Git-Tag: libserialport-0.1.0~125 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=a46f0960a2329b149213ce7252f5ecf25b8bd691;p=libserialport.git Use negative values in sp_port_config to retain existing settings. --- diff --git a/serialport.c b/serialport.c index 3f6b35c..0c1de1b 100644 --- a/serialport.c +++ b/serialport.c @@ -745,12 +745,10 @@ static int set_flowcontrol(struct sp_port_data *data, int flowcontrol) static int set_rts(struct sp_port_data *data, int rts) { #ifdef _WIN32 - if (rts != -1) { - if (rts) - data->dcb.fRtsControl = RTS_CONTROL_ENABLE; - else - data->dcb.fRtsControl = RTS_CONTROL_DISABLE; - } + if (rts) + data->dcb.fRtsControl = RTS_CONTROL_ENABLE; + else + data->dcb.fRtsControl = RTS_CONTROL_DISABLE; #else data->rts = rts; #endif @@ -760,12 +758,10 @@ static int set_rts(struct sp_port_data *data, int rts) static int set_dtr(struct sp_port_data *data, int dtr) { #ifdef _WIN32 - if (dtr != -1) { - if (dtr) - data->dcb.fDtrControl = DTR_CONTROL_ENABLE; - else - data->dcb.fDtrControl = DTR_CONTROL_DISABLE; - } + if (dtr) + data->dcb.fDtrControl = DTR_CONTROL_ENABLE; + else + data->dcb.fDtrControl = DTR_CONTROL_DISABLE; #else data->dtr = dtr; #endif @@ -820,19 +816,20 @@ 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) 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(&data, config->baudrate)); - TRY(set_bits(&data, config->bits)); - TRY(set_parity(&data, config->parity)); - TRY(set_stopbits(&data, config->stopbits)); - TRY(set_flowcontrol(&data, config->flowcontrol)); - TRY(set_rts(&data, config->rts)); - TRY(set_dtr(&data, config->dtr)); + TRY_SET(baudrate); + TRY_SET(bits); + TRY_SET(parity); + TRY_SET(stopbits); + TRY_SET(flowcontrol); + TRY_SET(rts); + TRY_SET(dtr); TRY(apply_config(port, &data)); return SP_OK;