From b8a62fef38de5d15f75d70c465a2d62af6f87729 Mon Sep 17 00:00:00 2001 From: Robert Hoelzl Date: Fri, 18 Jan 2019 12:13:58 +0100 Subject: [PATCH 01/10] Support more than 64 Device without sourcecode modification to avoid, that the source code has to be modified, when more than 64 devices shall be supported, the corresponding defines (COM_MAXDEVICES) can be optionally set in the makefile now. This is especially important on WSL (Windows Subsystem for Linux), which installs 192 serial devices, no matter if there is a real hardware counterpart on the windows side. --- rs232-linux.c | 4 +++- rs232-win.c | 10 +++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/rs232-linux.c b/rs232-linux.c index c8e629a..89b98f2 100644 --- a/rs232-linux.c +++ b/rs232-linux.c @@ -69,7 +69,9 @@ typedef struct { int handle; } COMDevice; -#define COM_MAXDEVICES 64 +#if !defined(COM_MAXDEVICES) + #define COM_MAXDEVICES 64 +#endif static COMDevice comDevices[COM_MAXDEVICES]; static int noDevices = 0; diff --git a/rs232-win.c b/rs232-win.c index 67839d6..1615f31 100644 --- a/rs232-win.c +++ b/rs232-win.c @@ -6,7 +6,7 @@ The MIT License (MIT) - Copyright (c) 2013-2015 Frédéric Meslin, Florent Touchard + Copyright (c) 2013-2015 Fr�d�ric Meslin, Florent Touchard Email: fredericmeslin@hotmail.com Website: www.fredslab.net Twitter: @marzacdev @@ -48,11 +48,15 @@ typedef struct { } COMDevice; /*****************************************************************************/ -#define COM_MAXDEVICES 64 +#if !defined(COM_MAXDEVICES) + #define COM_MAXDEVICES 64 +#endif static COMDevice comDevices[COM_MAXDEVICES]; static int noDevices = 0; -#define COM_MINDEVNAME 16384 +#if !defined(COM_MINDEVNAME) + #define COM_MINDEVNAME 16384 +#endif const char * comPtn = "COM???"; /*****************************************************************************/ From f3ae4e56f846d2804d8544d352aaf2e40e1e30f4 Mon Sep 17 00:00:00 2001 From: Robert Hoelzl Date: Fri, 18 Jan 2019 14:59:38 +0100 Subject: [PATCH 02/10] Avoid compilation warnings The Linuxversion defined __USE_MISC without checking if it was already defined in one of the headers. This might result in compiler warnings. Furthermore the Windowsversion passed size_t types to uint32_t, which caused compiler warnings on MSVC 64bit --- rs232-linux.c | 15 ++++++++++++--- rs232-win.c | 14 +++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/rs232-linux.c b/rs232-linux.c index 89b98f2..0184328 100644 --- a/rs232-linux.c +++ b/rs232-linux.c @@ -39,12 +39,16 @@ #include "rs232.h" #include -#define __USE_MISC // For CRTSCTS +#if !defined(__USE_MISC) + #define __USE_MISC // For CRTSCTS +#endif #include #include #include -#define __USE_SVID // For strdup +#if !defined(__USE_SVID) + #define __USE_SVID // For strdup +#endif #include #include #include @@ -178,7 +182,7 @@ void comClose(int index) if (index >= noDevices || index < 0) return; COMDevice * com = &comDevices[index]; - if (com->handle < 0) + if (com->handle < 0) return; tcdrain(com->handle); close(com->handle); @@ -216,6 +220,11 @@ int comRead(int index, char * buffer, size_t len) return res; } +int comReadBlocking(int index, char * buffer, size_t len, unsigned timeout) +{ + return 0; +} + /*****************************************************************************/ int _BaudFlag(int BaudRate) { diff --git a/rs232-win.c b/rs232-win.c index 1615f31..e60af52 100644 --- a/rs232-win.c +++ b/rs232-win.c @@ -137,7 +137,7 @@ int comEnumerate() size_t size = COM_MINDEVNAME; char * list = (char *) malloc(size); SetLastError(0); - QueryDosDeviceA(NULL, list, size); + QueryDosDeviceA(NULL, list, (uint32_t) size); while (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { size *= 2; char * nlist = realloc(list, size); @@ -147,7 +147,7 @@ int comEnumerate() } list = nlist; SetLastError(0); - QueryDosDeviceA(NULL, list, size); + QueryDosDeviceA(NULL, list, (uint32_t) size); } // Gather all COM ports int port; @@ -165,7 +165,7 @@ int comEnumerate() void comTerminate() { - comCloseAll(); + comCloseAll(); } int comGetNoPorts() @@ -247,10 +247,10 @@ int comOpen(int index, int baudrate) void comClose(int index) { - if (index < 0 || index >= noDevices) + if (index < 0 || index >= noDevices) return; COMDevice * com = &comDevices[index]; - if (!com->handle) + if (!com->handle) return; CloseHandle(com->handle); com->handle = 0; @@ -269,7 +269,7 @@ int comWrite(int index, const char * buffer, size_t len) return 0; COMDevice * com = &comDevices[index]; uint32_t bytes = 0; - WriteFile(com->handle, buffer, len, &bytes, NULL); + WriteFile(com->handle, buffer, (uint32_t) len, &bytes, NULL); return bytes; } @@ -279,7 +279,7 @@ int comRead(int index, char * buffer, size_t len) return 0; COMDevice * com = &comDevices[index]; uint32_t bytes = 0; - ReadFile(com->handle, buffer, len, &bytes, NULL); + ReadFile(com->handle, buffer, (uint32_t) len, &bytes, NULL); return bytes; } From 4c65a7a50b3a2e32fa265234c7553b4805f302be Mon Sep 17 00:00:00 2001 From: Robert Hoelzl Date: Fri, 18 Jan 2019 17:57:32 +0100 Subject: [PATCH 03/10] Add Support for Baudrates > 230 kBit to the Linux Wrapper If the corresponding macros are available from the OS up to 4MBit are now supported. --- rs232-linux.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/rs232-linux.c b/rs232-linux.c index 0184328..fd2a777 100644 --- a/rs232-linux.c +++ b/rs232-linux.c @@ -247,6 +247,19 @@ int _BaudFlag(int BaudRate) case 57600: return B57600; break; case 115200: return B115200; break; case 230400: return B230400; break; +#if defined(B500000) + case 500000: return B500000; break; + case 576000: return B576000; break; + case 921600: return B921600; break; + case 1000000: return B1000000; break; + case 1152000: return B1152000; break; + case 1500000: return B1500000; break; + case 2000000: return B2000000; break; + case 2500000: return B2500000; break; + case 3000000: return B3000000; break; + case 3500000: return B3500000; break; + case 4000000: return B4000000; break; +#endif default : return B0; break; } } From 1891cce39eb538a348ca7f9187f3836e9bac6192 Mon Sep 17 00:00:00 2001 From: Robert Hoelzl Date: Fri, 18 Jan 2019 19:24:40 +0100 Subject: [PATCH 04/10] Add blocking read the comReadBlocking() allows to specify a timeout. It will wait until 'len' bytes are read or 'timeout' expired. --- rs232-linux.c | 23 ++++++++++++++++++++++- rs232-win.c | 27 +++++++++++++++++++++++++++ rs232.h | 16 ++++++++++++++-- 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/rs232-linux.c b/rs232-linux.c index fd2a777..b94a3f0 100644 --- a/rs232-linux.c +++ b/rs232-linux.c @@ -222,7 +222,28 @@ int comRead(int index, char * buffer, size_t len) int comReadBlocking(int index, char * buffer, size_t len, unsigned timeout) { - return 0; + fd_set set; + struct timeval to; + int rv, res; + + if (index >= noDevices || index < 0) + return 0; + if (comDevices[index].handle <= 0) + return 0; + + FD_ZERO(&set); /* clear the set */ + FD_SET(comDevices[index].handle, &set); + + to.tv_sec = timeout / 1000; + to.tv_usec = (timeout % 1000) * 1000; + + rv = select(comDevices[index].handle + 1, &set, NULL, NULL, &to); + if(rv <= 0) + return 0; + res = (int) read(comDevices[index].handle, buffer, len); + if (res < 0) + res = 0; + return res; } /*****************************************************************************/ diff --git a/rs232-win.c b/rs232-win.c index e60af52..5e6bc96 100644 --- a/rs232-win.c +++ b/rs232-win.c @@ -283,6 +283,33 @@ int comRead(int index, char * buffer, size_t len) return bytes; } +int comReadBlocking(int index, char * buffer, size_t len, unsigned timeout) +{ + if (index < 0 || index >= noDevices) + return 0; + COMDevice * com = &comDevices[index]; + COMMTIMEOUTS timeouts; + + timeouts.ReadIntervalTimeout = MAX_DWORD; + timeouts.ReadTotalTimeoutMultiplier = 0; + timeouts.ReadTotalTimeoutConstant = (uint32_t) timeout; + timeouts.WriteTotalTimeoutConstant = 0; + timeouts.WriteTotalTimeoutMultiplier = 0; + SetCommTimeouts(com->handle, &timeouts); + + uint32_t bytes = 0; + ReadFile(com->handle, buffer, (uint32_t) len, &bytes, NULL); + + timeouts.ReadIntervalTimeout = MAX_DWORD; + timeouts.ReadTotalTimeoutMultiplier = 0; + timeouts.ReadTotalTimeoutConstant = 0; + timeouts.WriteTotalTimeoutConstant = 0; + timeouts.WriteTotalTimeoutMultiplier = 0; + SetCommTimeouts(com->handle, &timeouts); + + return bytes; +} + /*****************************************************************************/ const char * findPattern(const char * string, const char * pattern, int * value) { diff --git a/rs232.h b/rs232.h index ca610d0..6a1c51e 100644 --- a/rs232.h +++ b/rs232.h @@ -6,7 +6,7 @@ The MIT License (MIT) - Copyright (c) 2013-2015 Frédéric Meslin, Florent Touchard + Copyright (c) 2013-2015 Fr�d�ric Meslin, Florent Touchard Email: fredericmeslin@hotmail.com Website: www.fredslab.net Twitter: @marzacdev @@ -142,12 +142,24 @@ extern "C" { * \fn int comRead(int index, const char * buffer, size_t len) * \brief Read data from the port (non-blocking) * \param[in] index port index - * \param[in] buffer pointer to receive buffer + * \param[out] buffer pointer to receive buffer * \param[in] len length of receive buffer in bytes * \return number of bytes transferred */ int comRead(int index, char * buffer, size_t len); + + /** + * \brief A blocking version of comRead(). + * \param[in] index port index + * \param[in] buffer pointer to receive buffer + * \param[out] len length of receive buffer in bytes + * \param[in] maximum number of ms to wait until 'len' bytes are received. + * \return number of bytes transferred + */ + int comReadBlocking(int index, char * buffer, size_t len, unsigned timeout); + + #ifdef __cplusplus } #endif From 7b5828869bc9800b68b46b0ee0ead06f625ca4ee Mon Sep 17 00:00:00 2001 From: Robert Hoelzl Date: Mon, 21 Jan 2019 12:15:17 +0100 Subject: [PATCH 05/10] Add support for Parity Bit Now also Parity Bits like ODD and EVEN can be enforced. --- rs232-linux.c | 18 +++++++++++++++--- rs232-win.c | 21 ++++++++++++++------- rs232.h | 28 ++++++++++++++++++++-------- 3 files changed, 49 insertions(+), 18 deletions(-) diff --git a/rs232-linux.c b/rs232-linux.c index b94a3f0..be282a2 100644 --- a/rs232-linux.c +++ b/rs232-linux.c @@ -138,10 +138,22 @@ const char * comGetPortName(int index) { } /*****************************************************************************/ -int comOpen(int index, int baudrate) +int comOpen(int index, int baudrate_and_parity) { + int parity; if (index >= noDevices || index < 0) return 0; + switch (baudrate_and_parity & PARITY_BITMASK) + { + case PARITY_NONE: parity = 0; break; + case PARITY_ODD: parity = PARENB|PARODD; break; + case PARITY_EVEN: parity = PARENB; break; +#if !defined(_DARWIN_C_SOURCE) + case PARITY_SPACE: parity = PARENB|CMSPAR; break; + case PARITY_MARK: parity = PARENB|CMSPAR|PARODD; break; +#endif + default: return 0; + } // Close if already open COMDevice * com = &comDevices[index]; if (com->handle >= 0) comClose(index); @@ -159,9 +171,9 @@ int comOpen(int index, int baudrate) config.c_iflag |= IGNPAR | IGNBRK; config.c_oflag &= ~(OPOST | ONLCR | OCRNL); config.c_cflag &= ~(PARENB | PARODD | CSTOPB | CSIZE | CRTSCTS); - config.c_cflag |= CLOCAL | CREAD | CS8; + config.c_cflag |= CLOCAL | CREAD | CS8 | parity; config.c_lflag &= ~(ICANON | ISIG | ECHO); - int flag = _BaudFlag(baudrate); + int flag = _BaudFlag(baudrate_and_parity & BAUDRATE_BITMASK); cfsetospeed(&config, flag); cfsetispeed(&config, flag); // Timeouts configuration diff --git a/rs232-win.c b/rs232-win.c index 5e6bc96..efe9648 100644 --- a/rs232-win.c +++ b/rs232-win.c @@ -203,18 +203,18 @@ const char * comGetInternalName(int index) } /*****************************************************************************/ -int comOpen(int index, int baudrate) +int comOpen(int index, int baudrate_and_parity) { DCB config; COMMTIMEOUTS timeouts; - if (index < 0 || index >= noDevices) + if (index < 0 || index >= noDevices) return 0; // Close if already open COMDevice * com = &comDevices[index]; if (com->handle) comClose(index); // Open COM port void * handle = CreateFileA(comGetInternalName(index), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); - if (handle == INVALID_HANDLE_VALUE) + if (handle == INVALID_HANDLE_VALUE) return 0; com->handle = handle; // Prepare read / write timeouts @@ -227,14 +227,21 @@ int comOpen(int index, int baudrate) SetCommTimeouts(handle, &timeouts); // Prepare serial communication format GetCommState(handle, &config); - config.BaudRate = baudrate; - config.fBinary = true; - config.fParity = 0; + config.BaudRate = baudrate_and_parity & BAUDRATE_BITMASK; + config.fBinary = 1; + config.fParity = 1; config.fErrorChar = 0; config.fNull = 0; config.fAbortOnError = 0; config.ByteSize = 8; - config.Parity = 0; + switch (baudrate_and_parity & PARITY_BITMASK) + { + case PARITY_NONE: config.Parity = 0; break; + case PARITY_ODD: config.Parity = 1; break; + case PARITY_EVEN: config.Parity = 2; break; + case PARITY_SPACE: config.Parity = 3; break; + case PARITY_MARK: config.Parity = 4; break; + } config.StopBits = 0; config.EvtChar = '\n'; // Set the port state diff --git a/rs232.h b/rs232.h index 6a1c51e..271aa22 100644 --- a/rs232.h +++ b/rs232.h @@ -57,7 +57,16 @@ extern "C" { * Website: www.fredslab.net
* Twitter: \@marzacdev
*/ - + +#define PARITY_NONE 0x00000000 //see comOpen() +#define PARITY_EVEN 0x10000000 //see comOpen() +#define PARITY_ODD 0x20000000 //see comOpen() +#define PARITY_SPACE 0x30000000 //see comOpen() +#define PARITY_MARK 0x40000000 //see comOpen() + +#define PARITY_BITMASK 0xF0000000 +#define BAUDRATE_BITMASK 0x0FFFFFFF + /*****************************************************************************/ /** * \fn int comEnumerate() @@ -76,7 +85,7 @@ extern "C" { /** * \fn int comTerminate() * \brief Release ports and memory resources used by the library - */ + */ void comTerminate(); /** @@ -84,7 +93,7 @@ extern "C" { * \brief Get port user-friendly name * \param[in] index port index * \return null terminated port name - */ + */ const char * comGetPortName(int index); /** @@ -92,7 +101,7 @@ extern "C" { * \brief Get port operating-system name * \param[in] index port index * \return null terminated port name - */ + */ const char * comGetInternalName(int index); /** @@ -100,7 +109,7 @@ extern "C" { * \brief Try to find a port given its user-friendly name * \param[in] name port name (case sensitive) * \return index of found port or -1 if not enumerated - */ + */ int comFindPort(const char * name); /*****************************************************************************/ @@ -109,10 +118,13 @@ extern "C" { * \brief Try to open a port at a specific baudrate * \brief (No parity, single stop bit, no hardware flow control) * \param[in] index port index - * \param[in] baudrate port baudrate + * \param[in] baudrate port baudrate plus parity (see PARITY_*). + * i.E. 9600|PARITY_EVEN. + * Optionally also only a baudrate may be specified. In this + * case Parity is PARITY_NONE * \return 1 if opened, 0 if not available - */ - int comOpen(int index, int baudrate); + */ + int comOpen(int index, int baudrate_and_parity); /** * \fn void comClose(int index) From b1022d127068ae050a0ab3b5abcb4ede15774480 Mon Sep 17 00:00:00 2001 From: Robert Hoelzl Date: Mon, 21 Jan 2019 15:00:03 +0100 Subject: [PATCH 06/10] Update README to version 0.22 --- README | 4 ++-- README.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README b/README index 6cac813..53566cd 100644 --- a/README +++ b/README @@ -1,5 +1,5 @@ Cross-platform serial / RS232 library -Version 0.21, 11/10/2015 +Version 0.22, 21/01/2019 The MIT License (MIT) Supported platforms: @@ -7,7 +7,7 @@ Supported platforms: - Linux - MacOS X -Copyright (c) 2007 - 2015 Frédéric Meslin +Copyright (c) 2007 - 2015 Fr�d�ric Meslin Contact: fredericmeslin@hotmail.com, @marzacdev Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/README.md b/README.md index 2a38508..971a62b 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # rs232 C / C++ RS232 cross-platform serial library -Version 0.21, 11/10/2015 +Version 0.22, 19/01/2019 Supported platforms: - Windows (XP / Win7, possibly 8 and 10) - Linux - MacOS X -Copyright (c) 2013-2015 Frédéric Meslin +Copyright (c) 2013-2015 Fr�d�ric Meslin Email: fredericmeslin@hotmail.com Website: www.fredslab.net Twitter: @marzacdev From 976a249bef5f4d327724854cb5b96730cc74728e Mon Sep 17 00:00:00 2001 From: Robert Hoelzl Date: Thu, 21 Nov 2019 19:26:49 +0100 Subject: [PATCH 07/10] Refactored Parity Bit Settings of Linux Driver version. --- rs232-linux.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/rs232-linux.c b/rs232-linux.c index be282a2..01f202a 100644 --- a/rs232-linux.c +++ b/rs232-linux.c @@ -140,20 +140,8 @@ const char * comGetPortName(int index) { /*****************************************************************************/ int comOpen(int index, int baudrate_and_parity) { - int parity; if (index >= noDevices || index < 0) return 0; - switch (baudrate_and_parity & PARITY_BITMASK) - { - case PARITY_NONE: parity = 0; break; - case PARITY_ODD: parity = PARENB|PARODD; break; - case PARITY_EVEN: parity = PARENB; break; -#if !defined(_DARWIN_C_SOURCE) - case PARITY_SPACE: parity = PARENB|CMSPAR; break; - case PARITY_MARK: parity = PARENB|CMSPAR|PARODD; break; -#endif - default: return 0; - } // Close if already open COMDevice * com = &comDevices[index]; if (com->handle >= 0) comClose(index); @@ -171,8 +159,16 @@ int comOpen(int index, int baudrate_and_parity) config.c_iflag |= IGNPAR | IGNBRK; config.c_oflag &= ~(OPOST | ONLCR | OCRNL); config.c_cflag &= ~(PARENB | PARODD | CSTOPB | CSIZE | CRTSCTS); - config.c_cflag |= CLOCAL | CREAD | CS8 | parity; + config.c_cflag |= CLOCAL | CREAD | CS8; config.c_lflag &= ~(ICANON | ISIG | ECHO); + switch (baudrate_and_parity & PARITY_BITMASK) + { + case PARITY_NONE: break; + case PARITY_ODD: config.c_cflag |= PARENB|PARODD; break; + case PARITY_EVEN: config.c_cflag |= PARENB; break; + case PARITY_SPACE: config.c_cflag |= PARENB|CMSPAR; break; + case PARITY_MARK: config.c_cflag |= PARENB|CMSPAR|PARODD; break; + } int flag = _BaudFlag(baudrate_and_parity & BAUDRATE_BITMASK); cfsetospeed(&config, flag); cfsetispeed(&config, flag); From 4f5c57cfb8e5adf6d72aba95cbd3ac62311657bc Mon Sep 17 00:00:00 2001 From: Robert Hoelzl Date: Thu, 21 Nov 2019 19:27:19 +0100 Subject: [PATCH 08/10] Add support for setting DTR/RTS Lines --- rs232-linux.c | 28 +++++++++++++++++++++++++++- rs232-win.c | 29 ++++++++++++++++++++++++++--- rs232.h | 28 +++++++++++++++++++++++----- 3 files changed, 76 insertions(+), 9 deletions(-) diff --git a/rs232-linux.c b/rs232-linux.c index 01f202a..c8fbe5c 100644 --- a/rs232-linux.c +++ b/rs232-linux.c @@ -52,6 +52,7 @@ #include #include #include +#include /*****************************************************************************/ /** Base name for COM devices */ @@ -311,4 +312,29 @@ void _AppendDevices(const char * base) closedir(dirp); } -#endif // unix + +int comSetDtr(int index, int state) +{ + int cmd = state ? TIOCMBIS : TIOCMBIC; + int flag = TIOCM_DTR; + if (index >= noDevices || index < 0) + return 0; + if (comDevices[index].handle <= 0) + return 0; + return ioctl(comDevices[index].handle, cmd, &flag) != -1; +} + + +int comSetRts(int index, int state) +{ + int cmd = state ? TIOCMBIS : TIOCMBIC; + int flag = TIOCM_RTS; + if (index >= noDevices || index < 0) + return 0; + if (comDevices[index].handle <= 0) + return 0; + return ioctl(comDevices[index].handle, cmd, &flag) != -1; +} + + +##endif // unix diff --git a/rs232-win.c b/rs232-win.c index efe9648..3b54acf 100644 --- a/rs232-win.c +++ b/rs232-win.c @@ -112,6 +112,11 @@ typedef struct _DCB { #define OPEN_EXISTING 3 #define MAX_DWORD 0xFFFFFFFF +#define SETRTS 3 +#define CLRRTS 4 +#define SETDTR 5 +#define CLRDTR 6 + /*****************************************************************************/ /** Windows system functions */ void * __stdcall CreateFileA(const char * lpFileName, uint32_t dwDesiredAccess, uint32_t dwShareMode, void * lpSecurityAttributes, uint32_t dwCreationDisposition, uint32_t dwFlagsAndAttributes, void * hTemplateFile); @@ -129,6 +134,7 @@ bool __stdcall GetCommTimeouts(void * hFile, COMMTIMEOUTS * lpCommTimeouts); bool __stdcall SetCommState(void * hFile, DCB * lpDCB); bool __stdcall SetCommTimeouts(void * hFile, COMMTIMEOUTS * lpCommTimeouts); bool __stdcall SetupComm(void * hFile, uint32_t dwInQueue, uint32_t dwOutQueue); +bool __stdcall EscapeCommFunction(void * hFile, uint32_t dwFunc); /*****************************************************************************/ int comEnumerate() @@ -165,7 +171,7 @@ int comEnumerate() void comTerminate() { - comCloseAll(); + comCloseAll(); } int comGetNoPorts() @@ -254,10 +260,10 @@ int comOpen(int index, int baudrate_and_parity) void comClose(int index) { - if (index < 0 || index >= noDevices) + if (index < 0 || index >= noDevices) return; COMDevice * com = &comDevices[index]; - if (!com->handle) + if (!com->handle) return; CloseHandle(com->handle); com->handle = 0; @@ -357,4 +363,21 @@ const char * findPattern(const char * string, const char * pattern, int * value) return sp; } + +int comSetDtr(int index, int state) +{ + if (index < 0 || index >= noDevices) + return 0; + return EscapeCommFunction(comDevices[index].handle, state ? SETDTR :CLRDTR); +} + + +int comSetRts(int index, int state) +{ + if (index < 0 || index >= noDevices) + return 0; + return EscapeCommFunction(comDevices[index].handle, state ? SETRTS :CLRRTS); +} + + #endif // _WIN32 diff --git a/rs232.h b/rs232.h index 271aa22..554d60f 100644 --- a/rs232.h +++ b/rs232.h @@ -85,7 +85,7 @@ extern "C" { /** * \fn int comTerminate() * \brief Release ports and memory resources used by the library - */ + */ void comTerminate(); /** @@ -93,7 +93,7 @@ extern "C" { * \brief Get port user-friendly name * \param[in] index port index * \return null terminated port name - */ + */ const char * comGetPortName(int index); /** @@ -101,7 +101,7 @@ extern "C" { * \brief Get port operating-system name * \param[in] index port index * \return null terminated port name - */ + */ const char * comGetInternalName(int index); /** @@ -109,7 +109,7 @@ extern "C" { * \brief Try to find a port given its user-friendly name * \param[in] name port name (case sensitive) * \return index of found port or -1 if not enumerated - */ + */ int comFindPort(const char * name); /*****************************************************************************/ @@ -123,7 +123,7 @@ extern "C" { * Optionally also only a baudrate may be specified. In this * case Parity is PARITY_NONE * \return 1 if opened, 0 if not available - */ + */ int comOpen(int index, int baudrate_and_parity); /** @@ -172,6 +172,24 @@ extern "C" { int comReadBlocking(int index, char * buffer, size_t len, unsigned timeout); + /** + * \brief Set DTR Line + * \param[in] index port index + * \param[in] new state of DTR line + * \return 0 on failure otherwise 1 + */ + int comSetDtr(int index, int state); + + + /** + * \brief Set RTS Line + * \param[in] index port index + * \param[in] new state of RTS line + * \return 0 on failure otherwise 1 + */ + int comSetRts(int index, int state); + + #ifdef __cplusplus } #endif From eb8938494f7dd9459d5ed105aabdd3075f1ffbcd Mon Sep 17 00:00:00 2001 From: Robert Hoelzl Date: Thu, 28 Nov 2019 16:03:37 +0100 Subject: [PATCH 09/10] macOS/Darwin Port did not work any more since 976a24 During Refactoring conditional defines for macOS were removed accidentially. --- rs232-linux.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rs232-linux.c b/rs232-linux.c index c8fbe5c..b664f0f 100644 --- a/rs232-linux.c +++ b/rs232-linux.c @@ -167,8 +167,10 @@ int comOpen(int index, int baudrate_and_parity) case PARITY_NONE: break; case PARITY_ODD: config.c_cflag |= PARENB|PARODD; break; case PARITY_EVEN: config.c_cflag |= PARENB; break; +#if !defined(_DARWIN_C_SOURCE) case PARITY_SPACE: config.c_cflag |= PARENB|CMSPAR; break; case PARITY_MARK: config.c_cflag |= PARENB|CMSPAR|PARODD; break; +#endif } int flag = _BaudFlag(baudrate_and_parity & BAUDRATE_BITMASK); cfsetospeed(&config, flag); @@ -337,4 +339,4 @@ int comSetRts(int index, int state) } -##endif // unix +#endif // unix From 306f1a59b171598b9b4c568a6cd257d60d2df7d1 Mon Sep 17 00:00:00 2001 From: Robert Hoelzl Date: Tue, 21 Jan 2020 20:05:43 +0100 Subject: [PATCH 10/10] Deactivate XON/XOFF Control Flow on linux In contrary to Windows Linux had XON/XOFF activated, which caused swallowing of the characters 0x11 (input) and 0x13 (output). --- rs232-linux.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rs232-linux.c b/rs232-linux.c index b664f0f..eaa8bdd 100644 --- a/rs232-linux.c +++ b/rs232-linux.c @@ -156,7 +156,7 @@ int comOpen(int index, int baudrate_and_parity) struct termios config; memset(&config, 0, sizeof(config)); tcgetattr(handle, &config); - config.c_iflag &= ~(INLCR | ICRNL); + config.c_iflag &= ~(INLCR | ICRNL | IXON | IXOFF); config.c_iflag |= IGNPAR | IGNBRK; config.c_oflag &= ~(OPOST | ONLCR | OCRNL); config.c_cflag &= ~(PARENB | PARODD | CSTOPB | CSIZE | CRTSCTS);