]> sigrok.org Git - libsigrok.git/blobdiff - hardware/common/serial.c
sr: always turn off ICRNL on serial ports
[libsigrok.git] / hardware / common / serial.c
index de97496a8748d698feed02f9eafe8ce47693a099..ad88958a0d0feb5c6601651687acdb1dd593c4cd 100644 (file)
@@ -30,8 +30,8 @@
 #endif
 #include <stdlib.h>
 #include <glib.h>
-#include "sigrok.h"
-#include "sigrok-internal.h"
+#include "libsigrok.h"
+#include "libsigrok-internal.h"
 
 // FIXME: Must be moved, or rather passed as function argument.
 #ifdef _WIN32
@@ -190,7 +190,7 @@ SR_PRIV void serial_restore_params(int fd, void *backup)
  * flowcontrol: 1 = rts/cts, 2 = xon/xoff
  * parity: 0 = none, 1 = even, 2 = odd
  */
-SR_PRIV int serial_set_params(int fd, int speed, int bits, int parity,
+SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
                              int stopbits, int flowcontrol)
 {
 #ifdef _WIN32
@@ -201,8 +201,7 @@ SR_PRIV int serial_set_params(int fd, int speed, int bits, int parity,
                return SR_ERR;
        }
 
-       /* TODO: Rename 'speed' to 'baudrate'. */
-       switch(speed) {
+       switch (baudrate) {
        /* TODO: Support for higher baud rates. */
        case 115200:
                dcb.BaudRate = CBR_115200;
@@ -235,7 +234,10 @@ SR_PRIV int serial_set_params(int fd, int speed, int bits, int parity,
        struct termios term;
        speed_t baud;
 
-       switch (speed) {
+       if (tcgetattr(fd, &term) < 0)
+               return SR_ERR;
+
+       switch (baudrate) {
        case 9600:
                baud = B9600;
                break;
@@ -256,8 +258,7 @@ SR_PRIV int serial_set_params(int fd, int speed, int bits, int parity,
        default:
                return SR_ERR;
        }
-
-       if (tcgetattr(fd, &term) < 0)
+       if (cfsetospeed(&term, baud) < 0)
                return SR_ERR;
        if (cfsetispeed(&term, baud) < 0)
                return SR_ERR;
@@ -284,13 +285,17 @@ SR_PRIV int serial_set_params(int fd, int speed, int bits, int parity,
                return SR_ERR;
        }
 
-       term.c_cflag &= ~(IXON | IXOFF | CRTSCTS);
+       term.c_iflag &= ~(IXON | IXOFF);
+       term.c_cflag &= ~CRTSCTS;
        switch (flowcontrol) {
-       case 2:
-               term.c_cflag |= IXON | IXOFF;
+       case 0:
+               /* No flow control. */
                break;
        case 1:
                term.c_cflag |= CRTSCTS;
+       case 2:
+               term.c_iflag |= IXON | IXOFF;
+               break;
        default:
                return SR_ERR;
        }
@@ -298,22 +303,71 @@ SR_PRIV int serial_set_params(int fd, int speed, int bits, int parity,
        term.c_iflag &= ~IGNPAR;
        term.c_cflag &= ~(PARODD | PARENB);
        switch (parity) {
-       case 0:
+       case SERIAL_PARITY_NONE:
                term.c_iflag |= IGNPAR;
                break;
-       case 1:
+       case SERIAL_PARITY_EVEN:
                term.c_cflag |= PARENB;
                break;
-       case 2:
+       case SERIAL_PARITY_ODD:
                term.c_cflag |= PARENB | PARODD;
                break;
        default:
                return SR_ERR;
        }
 
+       /* Some default parameters */
+       term.c_iflag &= ~(ICRNL);
+       term.c_lflag &= ~(ICANON | ECHO);
+
        if (tcsetattr(fd, TCSADRAIN, &term) < 0)
                return SR_ERR;
 #endif
 
        return SR_OK;
 }
+
+#define SERIAL_COMM_SPEC "^(\\d+)/([78])([neo])([12])$"
+SR_PRIV int serial_set_paramstr(int fd, const char *paramstr)
+{
+       GRegex *reg;
+       GMatchInfo *match;
+       int speed, databits, parity, stopbits;
+       char *mstr;
+
+       speed = databits = parity = stopbits = 0;
+       reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL);
+       if (g_regex_match(reg, paramstr, 0, &match)) {
+               if ((mstr = g_match_info_fetch(match, 1)))
+                       speed = strtoul(mstr, NULL, 10);
+               g_free(mstr);
+               if ((mstr = g_match_info_fetch(match, 2)))
+                       databits = strtoul(mstr, NULL, 10);
+               g_free(mstr);
+               if ((mstr = g_match_info_fetch(match, 3))) {
+                       switch (mstr[0]) {
+                       case 'n':
+                               parity = SERIAL_PARITY_NONE;
+                               break;
+                       case 'e':
+                               parity = SERIAL_PARITY_EVEN;
+                               break;
+                       case 'o':
+                               parity = SERIAL_PARITY_ODD;
+                               break;
+                       }
+               }
+               g_free(mstr);
+               if ((mstr = g_match_info_fetch(match, 4)))
+                       stopbits = strtoul(mstr, NULL, 10);
+               g_free(mstr);
+       }
+       g_match_info_unref(match);
+       g_regex_unref(reg);
+
+       if (speed)
+               return serial_set_params(fd, speed, databits, parity, stopbits, 0);
+       else
+               return SR_ERR_ARG;
+}
+