X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Fcommon%2Fserial.c;h=f6efe3e26465e30d42411adcd08e7d922479701f;hb=5c2d46d1dbfa636fb3cdbeaffdc0e9ca77c66ee1;hp=0b9b1c8c390917bf7be44b13beaaf1467da49cc3;hpb=1fdb75e14528abd62ebe727537512c741a5759da;p=libsigrok.git diff --git a/hardware/common/serial.c b/hardware/common/serial.c index 0b9b1c8c..f6efe3e2 100644 --- a/hardware/common/serial.c +++ b/hardware/common/serial.c @@ -31,13 +31,14 @@ #include #include #include +#include // FIXME: Must be moved, or rather passed as function argument. #ifdef _WIN32 HANDLE hdl; #endif -char *serial_port_glob[] = { +const char *serial_port_glob[] = { /* Linux */ "/dev/ttyS*", "/dev/ttyUSB*", @@ -51,11 +52,14 @@ char *serial_port_glob[] = { 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; @@ -66,9 +70,9 @@ GSList *list_serial_ports(void) ports = g_slist_append(ports, strdup(g.gl_pathv[j])); globfree(&g); } +#endif return ports; -#endif } int serial_open(const char *pathname, int flags) @@ -86,11 +90,17 @@ 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 } @@ -103,16 +113,49 @@ int serial_flush(int fd) { #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 @@ -137,8 +180,10 @@ void serial_restore_params(int fd, void *backup) } /* - * 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) @@ -148,6 +193,7 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, if (!GetCommState(hdl, &dcb)) { /* TODO: Error handling. */ + return SR_ERR; } /* TODO: Rename 'speed' to 'baudrate'. */ @@ -178,6 +224,7 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, if (!SetCommState(hdl, &dcb)) { /* TODO: Error handling. */ + return SR_ERR; } #else struct termios term; @@ -200,13 +247,13 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, baud = B460800; break; default: - return SIGROK_ERR; + return SR_ERR; } if (tcgetattr(fd, &term) < 0) - return SIGROK_ERR; + return SR_ERR; if (cfsetispeed(&term, baud) < 0) - return SIGROK_ERR; + return SR_ERR; term.c_cflag &= ~CSIZE; switch (bits) { @@ -217,7 +264,7 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, term.c_cflag |= CS7; break; default: - return SIGROK_ERR; + return SR_ERR; } term.c_cflag &= ~CSTOPB; @@ -227,7 +274,7 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, case 2: term.c_cflag |= CSTOPB; default: - return SIGROK_ERR; + return SR_ERR; } term.c_cflag &= ~(IXON | IXOFF | CRTSCTS); @@ -238,7 +285,7 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, case 1: term.c_cflag |= CRTSCTS; default: - return SIGROK_ERR; + return SR_ERR; } term.c_iflag &= ~IGNPAR; @@ -254,12 +301,12 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, term.c_cflag |= PARENB | PARODD; break; default: - return SIGROK_ERR; + return SR_ERR; } if (tcsetattr(fd, TCSADRAIN, &term) < 0) - return SIGROK_ERR; + return SR_ERR; #endif - return SIGROK_OK; + return SR_OK; }