X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;ds=inline;f=hardware%2Fcommon%2Fserial.c;h=cebca29e418dd833007c37572a747d2c84d85cbc;hb=1ff7712c93fb61e7710de6c2b4241652445f368e;hp=67a4ab73a2b259d4d8c268fc32f51156b7051886;hpb=d02a535e05513ba8d2d4a82b49180d60ef410e1c;p=libsigrok.git
diff --git a/hardware/common/serial.c b/hardware/common/serial.c
index 67a4ab73..cebca29e 100644
--- a/hardware/common/serial.c
+++ b/hardware/common/serial.c
@@ -17,19 +17,25 @@
* along with this program. If not, see .
*/
-#include
#include
#include
#include
#include
#include
+#ifdef _WIN32
+#include
+#else
+#include
#include
+#endif
#include
#include
+#include
-#include "sigrok.h"
-
-
+// FIXME: Must be moved, or rather passed as function argument.
+#ifdef _WIN32
+HANDLE hdl;
+#endif
char *serial_port_glob[] = {
/* Linux */
@@ -40,86 +46,206 @@ char *serial_port_glob[] = {
"/dev/ttys*",
"/dev/tty.USB-*",
"/dev/tty.Modem-*",
- NULL
+ NULL,
};
-
GSList *list_serial_ports(void)
{
+#ifdef _WIN32
+ /* TODO */
+#else
glob_t g;
GSList *ports;
- int i, j;
+ unsigned int i, j;
ports = NULL;
- for(i = 0; serial_port_glob[i]; i++)
- {
- if(!glob(serial_port_glob[i], 0, NULL, &g))
- {
- for(j = 0; j < g.gl_pathc; j++)
- ports = g_slist_append(ports, strdup(g.gl_pathv[j]));
- globfree(&g);
- }
+ for (i = 0; serial_port_glob[i]; i++) {
+ if (glob(serial_port_glob[i], 0, NULL, &g))
+ continue;
+ for (j = 0; j < g.gl_pathc; j++)
+ ports = g_slist_append(ports, strdup(g.gl_pathv[j]));
+ globfree(&g);
}
return ports;
+#endif
}
-
int serial_open(const char *pathname, int flags)
{
-
+#ifdef _WIN32
+ /* FIXME: Don't hardcode COM1. */
+ hdl = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0,
+ OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
+ if (hdl == INVALID_HANDLE_VALUE) {
+ /* TODO: Error handling. */
+ }
+ return 0;
+#else
return open(pathname, flags);
+#endif
}
-
int serial_close(int fd)
{
-
+#ifdef _WIN32
+ CloseHandle(hdl);
+#else
return close(fd);
+#endif
}
+int serial_flush(int fd)
+{
+
+ return tcflush(fd, TCIOFLUSH);
+}
void *serial_backup_params(int fd)
{
+#ifdef _WIN32
+ /* TODO */
+#else
struct termios *term;
term = malloc(sizeof(struct termios));
tcgetattr(fd, term);
return term;
+#endif
}
-
void serial_restore_params(int fd, void *backup)
{
+#ifdef _WIN32
+ /* TODO */
+#else
+ tcsetattr(fd, TCSADRAIN, (struct termios *)backup);
+#endif
+}
- tcsetattr(fd, TCSADRAIN, (struct termios *) backup);
+/*
+ * 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)
+{
+#ifdef _WIN32
+ DCB dcb;
-}
+ if (!GetCommState(hdl, &dcb)) {
+ /* TODO: Error handling. */
+ }
+ /* TODO: Rename 'speed' to 'baudrate'. */
+ switch(speed) {
+ case 115200:
+ dcb.BaudRate = CBR_115200;
+ break;
+ case 57600:
+ dcb.BaudRate = CBR_57600;
+ break;
+ case 38400:
+ dcb.BaudRate = CBR_38400;
+ break;
+ case 19200:
+ dcb.BaudRate = CBR_19200;
+ break;
+ case 9600:
+ dcb.BaudRate = CBR_9600;
+ break;
+ default:
+ /* TODO: Error handling. */
+ break;
+ }
+ dcb.ByteSize = bits;
+ dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */
+ dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */
-/* flowcontrol 1 = rts/cts 2 = xon/xoff */
-int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, int flowcontrol)
-{
+ if (!SetCommState(hdl, &dcb)) {
+ /* TODO: Error handling. */
+ }
+#else
struct termios term;
-
- /* only supporting what we need really -- currently just the OLS driver */
- if(speed != 115200 || bits != 8 || parity != 0 || stopbits != 1 || flowcontrol != 2)
+ speed_t baud;
+
+ switch (speed) {
+ case 9600:
+ baud = B9600;
+ break;
+ case 38400:
+ baud = B38400;
+ break;
+ case 57600:
+ baud = B57600;
+ break;
+ case 115200:
+ baud = B115200;
+ break;
+ case 460800:
+ baud = B460800;
+ break;
+ default:
return SIGROK_ERR;
+ }
- if(tcgetattr(fd, &term) < 0)
+ if (tcgetattr(fd, &term) < 0)
return SIGROK_ERR;
- if(cfsetispeed(&term, B115200) < 0)
+ if (cfsetispeed(&term, baud) < 0)
return SIGROK_ERR;
+
term.c_cflag &= ~CSIZE;
- term.c_cflag |= CS8;
+ switch (bits) {
+ case 8:
+ term.c_cflag |= CS8;
+ break;
+ case 7:
+ term.c_cflag |= CS7;
+ break;
+ default:
+ return SIGROK_ERR;
+ }
+
term.c_cflag &= ~CSTOPB;
- term.c_cflag |= IXON | IXOFF;
- term.c_iflag |= IGNPAR;
- if(tcsetattr(fd, TCSADRAIN, &term) < 0)
+ switch (stopbits) {
+ case 1:
+ break;
+ case 2:
+ term.c_cflag |= CSTOPB;
+ default:
+ return SIGROK_ERR;
+ }
+
+ term.c_cflag &= ~(IXON | IXOFF | CRTSCTS);
+ switch (flowcontrol) {
+ case 2:
+ term.c_cflag |= IXON | IXOFF;
+ break;
+ case 1:
+ term.c_cflag |= CRTSCTS;
+ default:
return SIGROK_ERR;
+ }
- return SIGROK_OK;
-}
+ term.c_iflag &= ~(IGNPAR | PARODD | PARENB);
+ switch (parity) {
+ case 0:
+ term.c_iflag |= IGNPAR;
+ break;
+ case 1:
+ term.c_iflag |= PARENB;
+ break;
+ case 2:
+ term.c_iflag |= PARENB | PARODD;
+ break;
+ default:
+ return SIGROK_ERR;
+ }
+ if (tcsetattr(fd, TCSADRAIN, &term) < 0)
+ return SIGROK_ERR;
+#endif
+ return SIGROK_OK;
+}