From: Uwe Hermann Date: Thu, 22 Apr 2010 01:39:02 +0000 (+0200) Subject: Win: Fix compile, serial port code (unfinished). X-Git-Tag: libsigrok-0.1.0~551 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=926b866cb624cebf2efca098088dba11afa8ae96;p=libsigrok.git Win: Fix compile, serial port code (unfinished). --- diff --git a/hardware/common/serial.c b/hardware/common/serial.c index 50a884e9..9ab3169c 100644 --- a/hardware/common/serial.c +++ b/hardware/common/serial.c @@ -17,17 +17,26 @@ * along with this program. If not, see . */ -#include #include #include #include #include #include +#ifdef _WIN32 +#include +#else +#include #include +#endif #include #include #include +// FIXME: Must be moved, or rather passed as function argument. +#ifdef _WIN32 +HANDLE hdl; +#endif + char *serial_port_glob[] = { /* Linux */ "/dev/ttyS*", @@ -42,6 +51,9 @@ char *serial_port_glob[] = { GSList *list_serial_ports(void) { +#ifdef _WIN32 + /* TODO */ +#else glob_t g; GSList *ports; unsigned int i, j; @@ -57,37 +69,96 @@ GSList *list_serial_ports(void) } 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 } 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 } /* flowcontrol 1 = rts/cts 2 = xon/xoff */ 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. */ + + if (!SetCommState(hdl, &dcb)) { + /* TODO: Error handling. */ + } +#else struct termios term; /* Only supporting what we need really, currently the OLS driver. */ @@ -106,6 +177,7 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, term.c_iflag |= IGNPAR; if (tcsetattr(fd, TCSADRAIN, &term) < 0) return SIGROK_ERR; +#endif return SIGROK_OK; } diff --git a/hardware/openbench-logic-sniffer/ols.c b/hardware/openbench-logic-sniffer/ols.c index 0718a303..5742da19 100644 --- a/hardware/openbench-logic-sniffer/ols.c +++ b/hardware/openbench-logic-sniffer/ols.c @@ -24,11 +24,17 @@ #include #include #include +#ifndef _WIN32 #include +#endif #include #include #include +#ifdef _WIN32 +/* TODO */ +#else #include +#endif #include #include @@ -219,7 +225,12 @@ static int hw_init(char *deviceinfo) * respond with g_poll(). */ g_message("probing %s...", (char *)l->data); +#ifdef _WIN32 + // FIXME + // hdl = serial_open(l->data, 0); +#else fd = serial_open(l->data, O_RDWR | O_NONBLOCK); +#endif if (fd != -1) { serial_params[devcnt] = serial_backup_params(fd); serial_set_params(fd, 115200, 8, 0, 1, 2); @@ -401,7 +412,12 @@ static int set_configuration_samplerate(struct sigrok_device_instance *sdi, flag_reg &= ~FLAG_DEMUX; divider = (CLOCK_RATE / samplerate) - 1; } +#ifdef _WIN32 + // FIXME + // divider = htonl(divider); +#else divider = htonl(divider); +#endif g_message("setting samplerate to %" PRIu64 " Hz (divider %u, demux %s)", samplerate, divider, flag_reg & FLAG_DEMUX ? "on" : "off"); @@ -560,7 +576,10 @@ static int receive_data(int fd, int revents, void *user_data) * This is the main loop telling us a timeout was reached, or * we've acquired all the samples we asked for -- we're done. */ +#ifndef _WIN32 + /* TODO: Move to serial.c? */ tcflush(fd, TCIOFLUSH); +#endif serial_close(fd); packet.type = DF_END; packet.length = 0;