serial port: support for keeping the port always opened

Closing serial port causes Arduino reset on Mac OSX.
This commit is contained in:
ole00 2022-04-22 19:49:58 +01:00
parent b5d6fa6683
commit d518805925
1 changed files with 16 additions and 0 deletions

View File

@ -123,14 +123,25 @@ static inline int serialDeviceRead(SerialDeviceHandle deviceHandle, char* buffer
#include <termios.h>
#define SerialDeviceHandle int
#define DEFAULT_SERIAL_DEVICE_NAME "/dev/ttyUSB0"
#define INVALID_HANDLE -1
#ifdef NO_CLOSE
static SerialDeviceHandle serH = INVALID_HANDLE;
#endif
static inline SerialDeviceHandle serialDeviceOpen(char* deviceName) {
SerialDeviceHandle h;
#ifdef NO_CLOSE
if (serH != INVALID_HANDLE) {
return serH;
}
#endif
h = open(deviceName, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (h != INVALID_HANDLE) {
@ -153,6 +164,9 @@ static inline SerialDeviceHandle serialDeviceOpen(char* deviceName) {
//ensure no leftover bytes exist on the serial line
tcdrain(h);
tcflush(h, TCIOFLUSH); //flush both queues
#ifdef NO_CLOSE
serH = h;
#endif
return h;
} else {
return INVALID_HANDLE;
@ -164,7 +178,9 @@ void serialDeviceCheckName(char* name, int maxSize) {
}
static inline void serialDeviceClose(SerialDeviceHandle deviceHandle) {
#ifndef NO_CLOSE
close(deviceHandle);
#endif
}
static inline int serialDeviceWrite(SerialDeviceHandle deviceHandle, char* buffer, int bytesToWrite) {