X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Fcommon%2Fserial.c;h=0be403af9b87e3a456bd94647382d3a36c1d6dc0;hb=5c51e09868ea24c0eb77908aa3a9d585a46a3158;hp=ca79b9bb7f6947551b6fd5fb91ffdf332e4ec9dc;hpb=f38b9763fa5ba8f4b481718b245e1258a307d2a6;p=libsigrok.git diff --git a/hardware/common/serial.c b/hardware/common/serial.c index ca79b9bb..0be403af 100644 --- a/hardware/common/serial.c +++ b/hardware/common/serial.c @@ -316,9 +316,57 @@ SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity, return SR_ERR; } + /* Some default parameters */ + 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; +} +