Skip to content

Commit e471b0b

Browse files
dontechPeter Dons Tychsen
andauthored
Minor updates to library to support msvc and XON/XOFF (#53)
* fix msvc warning * add XON/XOFF * default to DTR off * fix build on msvc that does not have vasprintf * fix missing PATH_MAX on msvc * fix missing PATH_MAX on msvc --------- Co-authored-by: Peter Dons Tychsen <extptychsen@falcom.net>
1 parent 12fd98b commit e471b0b

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

simple_uart.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
#ifdef _MSC_VER
2+
#define _CRT_SECURE_NO_WARNINGS
3+
#ifndef PATH_MAX
4+
#define PATH_MAX MAX_PATH
5+
#endif /* PATH_MAX */
6+
#endif /* _MSC_VER */
7+
18
#define _GNU_SOURCE
29
#include <ctype.h>
310
#include <errno.h>
@@ -252,8 +259,18 @@ static int simple_uart_set_config(struct simple_uart *sc, int speed, const char
252259
} else {
253260
options.fRtsControl = RTS_CONTROL_DISABLE;
254261
}
262+
// XON/XOFF
263+
if (HAS_OPTION('X')) {
264+
options.fOutX = TRUE;
265+
options.fInX = TRUE;
266+
} else {
267+
options.fOutX = FALSE;
268+
options.fInX = FALSE;
269+
}
270+
255271
/* mandatory options */
256272
options.fBinary = TRUE;
273+
options.fDtrControl = FALSE;
257274
/* assign to port */
258275
if (!SetCommState(sc->port, &options))
259276
return win32_errno();
@@ -359,6 +376,11 @@ static int simple_uart_set_config(struct simple_uart *sc, int speed, const char
359376
else
360377
options.c_cflag &= ~CRTSCTS;
361378

379+
/* clear and set software flow control */
380+
options.c_iflag &= (tcflag_t) ~(IXON | IXOFF | IXANY);
381+
if (HAS_OPTION('X'))
382+
options.c_iflag |= IXON | IXOFF;
383+
362384
// raw input mode
363385
options.c_lflag &= (tcflag_t) ~(ICANON | ECHO | ECHOE | ISIG);
364386

@@ -742,14 +764,14 @@ int simple_uart_describe(const char *uart, char *description, size_t max_desc_le
742764
int simple_uart_set_logfile(struct simple_uart *uart, const char *logfile, ...)
743765
{
744766
va_list ap;
745-
char *buffer;
767+
char buffer[PATH_MAX];
746768
int len;
747769

748770
if (!uart || !logfile)
749771
return -EINVAL;
750772

751773
va_start(ap, logfile);
752-
len = vasprintf(&buffer, logfile, ap);
774+
len = vsnprintf(buffer, sizeof(buffer), logfile, ap);
753775
va_end(ap);
754776
if (len < 0)
755777
return -errno;
@@ -760,10 +782,8 @@ int simple_uart_set_logfile(struct simple_uart *uart, const char *logfile, ...)
760782
uart->logfile = fopen(buffer, "a");
761783
if (!uart->logfile) {
762784
int e = -errno;
763-
free(buffer);
764785
return e;
765786
}
766-
free(buffer);
767787
return 0;
768788
}
769789

0 commit comments

Comments
 (0)