X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Fcommon%2Fserial.c;h=b7ec2da3fe7d491ab946eb45b5a12d1e7cc00103;hb=d3b38ad389a6186e2822d62b20b9f0d9d9e7c21c;hp=a18ad0b494073962f1bc7fb182b4e1c95db3b2e3;hpb=5ddb0cc7d4850375a9ef741012d229ead9a91490;p=libsigrok.git diff --git a/hardware/common/serial.c b/hardware/common/serial.c index a18ad0b4..b7ec2da3 100644 --- a/hardware/common/serial.c +++ b/hardware/common/serial.c @@ -278,14 +278,6 @@ SR_PRIV int serial_read(struct sr_serial_dev_inst *serial, void *buf, #else /* Returns the number of bytes read, or -1 upon failure. */ ret = read(serial->fd, buf, count); - if (ret < 0) - /* - * Should be sr_err(), but that would yield lots of - * "Resource temporarily unavailable" messages. - */ - sr_spew("Read error: %s (fd %d).", strerror(errno), serial->fd); - else - sr_spew("Read %d/%d bytes (fd %d).", ret, count, serial->fd); #endif return ret; @@ -430,7 +422,7 @@ SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate, if (dtr != -1) { sr_spew("Setting DTR %s.", dtr ? "high" : "low"); - if (rts) + if (dtr) dcb.fDtrControl = DTR_CONTROL_ENABLE; else dcb.fDtrControl = DTR_CONTROL_DISABLE; @@ -507,7 +499,7 @@ SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate, case 230400: baud = B230400; break; -#ifndef __APPLE__ +#if !defined(__APPLE__) && !defined(__OpenBSD__) case 460800: baud = B460800; break; @@ -597,8 +589,12 @@ SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate, return SR_ERR; } - /* Do not translate carriage return to newline on input. */ - term.c_iflag &= ~(ICRNL); + /* Turn off all serial port cooking. */ + term.c_iflag &= ~(ISTRIP | INLCR | ICRNL); + term.c_oflag &= ~(ONLCR | OCRNL | ONOCR); +#if !defined(__NetBSD__) + term.c_oflag &= ~OFILL; +#endif /* Disable canonical mode, and don't echo input characters. */ term.c_lflag &= ~(ICANON | ECHO); @@ -639,8 +635,8 @@ SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate, * * @param serial Previously initialized serial port structure. * @param paramstr A serial communication parameters string, in the form - * of /, for example "9600/8n1" or - * "600/7o2". + * of /, for example "9600/8n1" or + * "600/7o2" or "460800/8n1/flow=2" where flow is 0 for none, 1 for rts/cts and 2 for xon/xoff. * * @return SR_OK upon success, SR_ERR upon failure. */ @@ -650,11 +646,12 @@ SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial, { GRegex *reg; GMatchInfo *match; - int speed, databits, parity, stopbits, rts, dtr, i; + int speed, databits, parity, stopbits, flow, rts, dtr, i; char *mstr, **opts, **kv; - speed = databits = parity = stopbits = 0; + speed = databits = parity = stopbits = flow = 0; rts = dtr = -1; + sr_spew("Parsing parameters from \"%s\".", paramstr); 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))) @@ -707,6 +704,17 @@ SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial, sr_dbg("invalid value for dtr: %c", kv[1][0]); speed = 0; } + } else if (!strncmp(kv[0], "flow", 4)) { + if (kv[1][0] == '0') + flow = 0; + else if (kv[1][0] == '1') + flow = 1; + else if (kv[1][0] == '2') + flow = 2; + else { + sr_dbg("invalid value for flow: %c", kv[1][0]); + speed = 0; + } } g_strfreev(kv); } @@ -718,11 +726,13 @@ SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial, g_match_info_unref(match); g_regex_unref(reg); - if (speed) - return serial_set_params(serial, speed, databits, parity, stopbits, - 0, rts, dtr); - else + if (speed) { + return serial_set_params(serial, speed, databits, parity, + stopbits, flow, rts, dtr); + } else { + sr_dbg("Could not infer speed from parameter string."); return SR_ERR_ARG; + } } /** @@ -831,7 +841,7 @@ SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial, if (len > 0) { ibuf += len; } else if (len == 0) { - sr_spew("Error: Only read 0 bytes."); + /* No logging, already done in serial_read(). */ } else { /* Error reading byte, but continuing anyway. */ }