PC app: serial - add support for NO_CLOSE on Windows

This commit is contained in:
ole00 2024-03-31 15:54:21 +01:00
parent 0367a8b127
commit eac70f6c68
1 changed files with 15 additions and 2 deletions

View File

@ -12,6 +12,10 @@ char guessedSerialDevice[512] = {0};
#define DEFAULT_SERIAL_DEVICE_NAME "COM1"
#define INVALID_HANDLE INVALID_HANDLE_VALUE
#ifdef NO_CLOSE
static SerialDeviceHandle serH = INVALID_HANDLE;
#endif
// ideas: https://stackoverflow.com/questions/1388871/how-do-i-get-a-list-of-available-serial-ports-in-win32
static void serialDeviceGuessName(char** deviceName) {
char buf[64 * 1024] = {0};
@ -57,6 +61,12 @@ static void serialDeviceGuessName(char** deviceName) {
static inline SerialDeviceHandle serialDeviceOpen(char* deviceName) {
SerialDeviceHandle h;
#ifdef NO_CLOSE
if (serH != INVALID_HANDLE) {
return serH;
}
#endif
h = CreateFile(
deviceName, //port name
GENERIC_READ | GENERIC_WRITE, //Read/Write
@ -109,7 +119,9 @@ static inline SerialDeviceHandle serialDeviceOpen(char* deviceName) {
}
//ensure no leftover bytes exist on the serial line
result = PurgeComm(h, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
#ifdef NO_CLOSE
serH = h;
#endif
return h;
} else {
return INVALID_HANDLE;
@ -142,7 +154,9 @@ void serialDeviceCheckName(char* name, int maxSize) {
}
static inline void serialDeviceClose(SerialDeviceHandle deviceHandle) {
#ifndef NO_CLOSE
CloseHandle(deviceHandle);
#endif
}
static inline int serialDeviceWrite(SerialDeviceHandle deviceHandle, char* buffer, int bytesToWrite) {
@ -170,7 +184,6 @@ static inline int serialDeviceRead(SerialDeviceHandle deviceHandle, char* buffer
#define DEFAULT_SERIAL_DEVICE_NAME "/dev/ttyUSB0"
#define INVALID_HANDLE -1
#ifdef NO_CLOSE
static SerialDeviceHandle serH = INVALID_HANDLE;
#endif