+ if ((ret = tcflush(fd, TCIOFLUSH)) < 0)
+ sr_err("Error flushing serial port: %s.", strerror(errno));
+
+ return ret;
+#endif
+}
+
+/**
+ * Write a number of bytes to the specified serial port.
+ *
+ * @param fd File descriptor of the serial port.
+ * @param buf Buffer containing the bytes to write.
+ * @param count Number of bytes to write.
+ *
+ * @return The number of bytes written, or -1 upon failure.
+ */
+SR_PRIV int serial_write(int fd, const void *buf, size_t count)
+{
+#ifdef _WIN32
+ DWORD tmp = 0;
+
+ /* FIXME */
+ /* Returns non-zero upon success, 0 upon failure. */
+ WriteFile(hdl, buf, count, &tmp, NULL);
+#else
+ ssize_t ret;
+
+ /* Returns the number of bytes written, or -1 upon failure. */
+ ret = write(fd, buf, count);
+ if (ret < 0)
+ sr_err("FD %d: Write error: %s.", fd, strerror(errno));
+ else
+ sr_spew("FD %d: Wrote %d/%d bytes.", fd, ret, count);
+
+ return ret;
+#endif
+}
+
+/**
+ * Read a number of bytes from the specified serial port.
+ *
+ * @param fd File descriptor of the serial port.
+ * @param buf Buffer where to store the bytes that are read.
+ * @param count The number of bytes to read.
+ *
+ * @return The number of bytes read, or -1 upon failure.
+ */
+SR_PRIV int serial_read(int fd, void *buf, size_t count)
+{
+#ifdef _WIN32
+ DWORD tmp = 0;
+
+ /* FIXME */
+ /* Returns non-zero upon success, 0 upon failure. */
+ return ReadFile(hdl, buf, count, &tmp, NULL);
+#else
+ ssize_t ret;
+
+ /* Returns the number of bytes read, or -1 upon failure. */
+ ret = read(fd, buf, count);
+ if (ret < 0) {
+ /*
+ * Should be sr_err(), but that would yield lots of
+ * "Resource temporarily unavailable" messages.
+ */
+ sr_spew("FD %d: Read error: %s.", fd, strerror(errno));
+ } else {
+ sr_spew("FD %d: Read %d/%d bytes.", fd, ret, count);
+ }
+
+ return ret;