GSList *list_serial_ports(void)
{
+ GSList *ports;
+
#ifdef _WIN32
/* TODO */
+ ports = NULL;
+ ports = g_slist_append(ports, strdup("COM1"));
#else
glob_t g;
- GSList *ports;
unsigned int i, j;
ports = NULL;
ports = g_slist_append(ports, strdup(g.gl_pathv[j]));
globfree(&g);
}
+#endif
return ports;
-#endif
}
int serial_open(const char *pathname, int flags)
#endif
}
+/*
+ * Close the serial port.
+ * Returns 0 upon success, -1 upon failure.
+ */
int serial_close(int fd)
{
#ifdef _WIN32
- CloseHandle(hdl);
+ /* Returns non-zero upon success, 0 upon failure. */
+ return (CloseHandle(hdl) == 0) ? -1 : 0;
#else
+ /* Returns 0 upon success, -1 upon failure. */
return close(fd);
#endif
}
{
#ifdef _WIN32
/* Returns non-zero upon success, 0 upon failure. */
- if (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0)
- return -1;
- else
- return 0;
+ return (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) ? -1 : 0;
#else
/* Returns 0 upon success, -1 upon failure. */
return tcflush(fd, TCIOFLUSH);
#endif
}
+/*
+ * Write a number of bytes to the specified serial port.
+ * Returns the number of bytes written, or -1 upon failure.
+ */
+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
+ /* Returns the number of bytes written, or -1 upon failure. */
+ return write(fd, buf, count);
+#endif
+}
+
+/*
+ * Read a number of bytes from the specified serial port.
+ * Returns the number of bytes read, or -1 upon failure.
+ */
+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
+ /* Returns the number of bytes read, or -1 upon failure. */
+ return read(fd, buf, count);
+#endif
+}
+
void *serial_backup_params(int fd)
{
#ifdef _WIN32
}
/*
- * flowcontrol 1 = rts/cts 2 = xon/xoff
- * parity 0 = none, 1 = even, 2 = odd
+ * Set serial parameters.
+ *
+ * flowcontrol: 1 = rts/cts, 2 = xon/xoff
+ * parity: 0 = none, 1 = even, 2 = odd
*/
int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
int flowcontrol)
if (!GetCommState(hdl, &dcb)) {
/* TODO: Error handling. */
+ return SIGROK_ERR;
}
/* TODO: Rename 'speed' to 'baudrate'. */
if (!SetCommState(hdl, &dcb)) {
/* TODO: Error handling. */
+ return SIGROK_ERR;
}
#else
struct termios term;
g_debug("ols: sending cmd 0x%.2x", command);
buf[0] = command;
- if (write(fd, buf, 1) != 1)
+ if (serial_write(fd, buf, 1) != 1)
return SIGROK_ERR;
return SIGROK_OK;
buf[2] = (data & 0xff0000) >> 16;
buf[3] = (data & 0xff00) >> 8;
buf[4] = data & 0xff;
- if (write(fd, buf, 5) != 5)
+ if (serial_write(fd, buf, 5) != 5)
return SIGROK_ERR;
return SIGROK_OK;
g_poll(fds, devcnt, 1);
for (i = 0; i < devcnt; i++) {
if (fds[i].revents == G_IO_IN) {
- if (read(fds[i].fd, buf, 4) == 4) {
+ if (serial_read(fds[i].fd, buf, 4) == 4) {
if (!strncmp(buf, "1SLO", 4)
|| !strncmp(buf, "1ALS", 4)) {
if (!strncmp(buf, "1SLO", 4))
if (revents == G_IO_IN
&& num_transfers / num_channels <= limit_samples) {
- if (read(fd, &byte, 1) != 1)
+ if (serial_read(fd, &byte, 1) != 1)
return FALSE;
sample[num_bytes++] = byte;
int serial_open(const char *pathname, int flags);
int serial_close(int fd);
int serial_flush(int fd);
+int serial_write(int fd, const void *buf, size_t count);
+int serial_read(int fd, void *buf, size_t count);
void *serial_backup_params(int fd);
void serial_restore_params(int fd, void *backup);
int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,