+ sr_spew("FD %d: Writing %d bytes.", fd, 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 if ((size_t)ret != count)
+ sr_spew("FD %d: Only 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)
+{
+ sr_spew("FD %d: Reading %d bytes.", fd, 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 if ((size_t)ret != count) {
+ sr_spew("FD %d: Only read %d/%d bytes.", fd, ret, count);
+ }
+
+ return ret;
+#endif
+}
+
+/**
+ * Create a backup of the current parameters of the specified serial port.
+ *
+ * @param fd File descriptor of the serial port.
+ *
+ * @return Pointer to a struct termios upon success, NULL upon errors.
+ * It is the caller's responsibility to g_free() the pointer if no
+ * longer needed.
+ */
+SR_PRIV void *serial_backup_params(int fd)
+{
+ sr_dbg("FD %d: Creating serial parameters backup.", fd);
+