From: Martin Ling Date: Wed, 20 Nov 2013 18:56:35 +0000 (+0000) Subject: Add sp_get_signals() function. X-Git-Tag: libserialport-0.1.0~82 X-Git-Url: https://sigrok.org/gitweb/?p=libserialport.git;a=commitdiff_plain;h=8cf7c6978f9fa9c7ec706440080c10755f357b5c Add sp_get_signals() function. --- diff --git a/libserialport.h.in b/libserialport.h.in index 704ffed..79babac 100644 --- a/libserialport.h.in +++ b/libserialport.h.in @@ -201,6 +201,18 @@ enum sp_flowcontrol { SP_FLOWCONTROL_DTRDSR = 3, }; +/** Input signals. */ +enum sp_signal { + /** Clear to send. */ + SP_SIG_CTS = 1, + /** Data set ready. */ + SP_SIG_DSR = 2, + /** Data carrier detect. */ + SP_SIG_DCD = 4, + /** Ring indicator. */ + SP_SIG_RI = 8, +}; + /** A serial port. */ struct sp_port { /** Name used to open the port. */ @@ -540,6 +552,22 @@ enum sp_return sp_drain(struct sp_port *port); * @{ */ +/** + * Gets the status of the control signals for the specified port. + * + * The user should allocate a variable of type "enum sp_signal" and pass a + * pointer to this variable to receive the result. The result is a bitmask + * in which individual signals can be checked by bitwise OR with values of + * the sp_signal enum. + * + * @param port Pointer to port structure. + * @param signals Pointer to variable to receive result. + * + * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG + * if an an invalid port or pointer is passed. + */ +enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals); + /** * Put the port transmit line into the break state. * diff --git a/serialport.c b/serialport.c index ab1baa7..522cafa 100644 --- a/serialport.c +++ b/serialport.c @@ -1298,6 +1298,42 @@ enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flow return SP_OK; } +enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals) +{ + CHECK_PORT(); + + if (!signals) + return SP_ERR_ARG; + + *signals = 0; +#ifdef _WIN32 + DWORD bits; + if (GetCommModemStatus(port->hdl, &bits) == 0) + return SP_ERR_FAIL; + if (bits & MS_CTS_ON) + *signals |= SP_SIG_CTS; + if (bits & MS_DSR_ON) + *signals |= SP_SIG_DSR; + if (bits & MS_RING_ON) + *signals |= SP_SIG_DCD; + if (bits & MS_RLSD_ON) + *signals |= SP_SIG_RI; +#else + int bits; + if (ioctl(port->fd, TIOCMGET, &bits) < 0) + return SP_ERR_FAIL; + if (bits & TIOCM_CTS) + *signals |= SP_SIG_CTS; + if (bits & TIOCM_DSR) + *signals |= SP_SIG_DSR; + if (bits & TIOCM_CAR) + *signals |= SP_SIG_DCD; + if (bits & TIOCM_RNG) + *signals |= SP_SIG_RI; +#endif + return SP_OK; +} + enum sp_return sp_start_break(struct sp_port *port) { CHECK_PORT();