]> sigrok.org Git - libsigrok.git/commitdiff
serial: add serial_set_paramstr()
authorBert Vermeulen <redacted>
Sun, 2 Sep 2012 13:02:24 +0000 (15:02 +0200)
committerBert Vermeulen <redacted>
Sun, 2 Sep 2012 13:42:56 +0000 (15:42 +0200)
hardware/common/serial.c
libsigrok-internal.h

index ca79b9bb7f6947551b6fd5fb91ffdf332e4ec9dc..51934ed859114dcaf16f673521eee4a0c07c8f73 100644 (file)
@@ -322,3 +322,48 @@ SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
 
        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;
+}
+
index b45b078670f2e0fa6a9efadbfa2966fa38002154..8bbae93b2c6a7b57c227dace91bbfda5434eeb04 100644 (file)
@@ -112,6 +112,7 @@ SR_PRIV void *serial_backup_params(int fd);
 SR_PRIV void serial_restore_params(int fd, void *backup);
 SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
                              int stopbits, int flowcontrol);
+SR_PRIV int serial_set_paramstr(int fd, const char *paramstr);
 
 /*--- hardware/common/ezusb.c -----------------------------------------------*/