]> sigrok.org Git - libserialport.git/blobdiff - serialport.c
Add functions for setting individual port parameters.
[libserialport.git] / serialport.c
index 83cb52913687e94c3bd50b0a0cdc798cb7bb4038..6a6e305bb2988ecc20d421b9a1f1b576f33dd2b4 100644 (file)
@@ -58,6 +58,19 @@ struct sp_port_data {
 #endif
 };
 
+/* Helper functions for configuring ports. */
+static int start_config(struct sp_port *port, struct sp_port_data *data);
+static int set_baudrate(struct sp_port_data *data, int baudrate);
+static int set_bits(struct sp_port_data *data, int bits);
+static int set_parity(struct sp_port_data *data, int parity);
+static int set_stopbits(struct sp_port_data *data, int stopbits);
+static int set_rts(struct sp_port_data *data, int rts);
+static int set_cts(struct sp_port_data *data, int cts);
+static int set_dtr(struct sp_port_data *data, int dtr);
+static int set_dsr(struct sp_port_data *data, int dsr);
+static int set_xon_xoff(struct sp_port_data *data, int xon_xoff);
+static int apply_config(struct sp_port *port, struct sp_port_data *data);
+
 int sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
 {
        struct sp_port *port;
@@ -399,6 +412,7 @@ int sp_open(struct sp_port *port, int flags)
                return SP_ERR_FAIL;
 #else
        int flags_local = 0;
+       struct sp_port_data data;
 
        /* Map 'flags' to the OS-specific settings. */
        if (flags & SP_MODE_RDWR)
@@ -410,6 +424,22 @@ int sp_open(struct sp_port *port, int flags)
 
        if ((port->fd = open(port->name, flags_local)) < 0)
                return SP_ERR_FAIL;
+
+       start_config(port, &data);
+
+       /* Turn off all serial port cooking. */
+       data.term.c_iflag &= ~(ISTRIP | INLCR | ICRNL);
+       data.term.c_oflag &= ~(ONLCR | OCRNL | ONOCR);
+#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
+       data.term.c_oflag &= ~OFILL;
+#endif
+       /* Disable canonical mode, and don't echo input characters. */
+       data.term.c_lflag &= ~(ICANON | ECHO);
+
+       /* Ignore modem status lines; enable receiver */
+       data.term.c_cflag |= (CLOCAL | CREAD);
+
+       apply_config(port, &data);
 #endif
 
        return SP_OK;
@@ -879,18 +909,6 @@ static int apply_config(struct sp_port *port, struct sp_port_data *data)
 #else
        int controlbits;
 
-       /* Turn off all serial port cooking. */
-       data->term.c_iflag &= ~(ISTRIP | INLCR | ICRNL);
-       data->term.c_oflag &= ~(ONLCR | OCRNL | ONOCR);
-#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
-       data->term.c_oflag &= ~OFILL;
-#endif
-       /* Disable canonical mode, and don't echo input characters. */
-       data->term.c_lflag &= ~(ICANON | ECHO);
-
-       /* Ignore modem status lines; enable receiver */
-       data->term.c_cflag |= (CLOCAL | CREAD);
-
        /* Asymmetric use of RTS/CTS not supported yet. */
        if ((data->rts == SP_RTS_FLOW_CONTROL) != (data->cts == SP_CTS_FLOW_CONTROL))
                return SP_ERR_ARG;
@@ -943,6 +961,24 @@ int sp_set_config(struct sp_port *port, struct sp_port_config *config)
        return SP_OK;
 }
 
+#define CREATE_SETTER(x) int sp_set_##x(struct sp_port *port, int x) { \
+       struct sp_port_data data; \
+       TRY(start_config(port, &data)); \
+       TRY(set_##x(&data, x)); \
+       TRY(apply_config(port, &data)); \
+       return SP_OK; \
+}
+
+CREATE_SETTER(baudrate)
+CREATE_SETTER(bits)
+CREATE_SETTER(parity)
+CREATE_SETTER(stopbits)
+CREATE_SETTER(rts)
+CREATE_SETTER(cts)
+CREATE_SETTER(dtr)
+CREATE_SETTER(dsr)
+CREATE_SETTER(xon_xoff)
+
 int sp_last_error_code(void)
 {
 #ifdef _WIN32