From 4dff8ad93f6204bbb686fd39ffa6c15a170fc760 Mon Sep 17 00:00:00 2001
From: pe-jot <oe5pjn@oevsv.at>
Date: Tue, 23 Feb 2021 16:24:04 +0100
Subject: [PATCH] Added handling of serial port DCB for Windows

---
 src/ptt.c | 78 ++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 46 insertions(+), 32 deletions(-)

diff --git a/src/ptt.c b/src/ptt.c
index 2a943006..6cb65293 100644
--- a/src/ptt.c
+++ b/src/ptt.c
@@ -180,17 +180,51 @@ typedef int HANDLE;
 
 #if __WIN32__
 
-#define RTS_ON(fd) 	EscapeCommFunction(fd,SETRTS);
-#define RTS_OFF(fd) 	EscapeCommFunction(fd,CLRRTS);
-#define DTR_ON(fd)    	EscapeCommFunction(fd,SETDTR);
-#define DTR_OFF(fd)	EscapeCommFunction(fd,CLRDTR);
+void setRTS(HANDLE fd, unsigned char state)
+{
+	if (!EscapeCommFunction(fd, state ? SETRTS : CLRRTS)) {
+		return;
+	}
+
+	DCB dcb;
+	if (!GetCommState(fd, &dcb)) {
+		return;
+	}
+
+	dcb.fRtsControl = state ? RTS_CONTROL_ENABLE : RTS_CONTROL_DISABLE;
+
+	SetCommState(fd, &dcb);
+}
+
+void setDTR(HANDLE fd, unsigned char state)
+{
+	if (!EscapeCommFunction(fd, state ? SETDTR : CLRDTR)) {
+		return;
+	}
+
+	DCB dcb;
+	if (!GetCommState(fd, &dcb)) {
+		return;
+	}
+
+	dcb.fDtrControl = state ? DTR_CONTROL_ENABLE : DTR_CONTROL_DISABLE;
+
+	SetCommState(fd, &dcb);
+}
 
 #else
 
-#define RTS_ON(fd) 	{ int stuff; ioctl (fd, TIOCMGET, &stuff); stuff |= TIOCM_RTS;  ioctl (fd, TIOCMSET, &stuff); }
-#define RTS_OFF(fd) 	{ int stuff; ioctl (fd, TIOCMGET, &stuff); stuff &= ~TIOCM_RTS; ioctl (fd, TIOCMSET, &stuff); }
-#define DTR_ON(fd)    	{ int stuff; ioctl (fd, TIOCMGET, &stuff); stuff |= TIOCM_DTR;  ioctl (fd, TIOCMSET, &stuff); }
-#define DTR_OFF(fd)	{ int stuff; ioctl (fd, TIOCMGET, &stuff); stuff &= ~TIOCM_DTR;	ioctl (fd, TIOCMSET, &stuff); }
+void setRTS(HANDLE fd, unsigned char state)
+{
+	int bit = TIOCM_RTS;
+	ioctl(fd, state ? TIOCMBIS : TIOCMBIC, &bit);
+}
+
+void setDTR(HANDLE fd, unsigned char state)
+{
+	int bit = TIOCM_DTR;
+	ioctl(fd, state ? TIOCMBIS : TIOCMBIC, &bit);
+}
 
 #define LPT_IO_ADDR 0x378
 
@@ -1182,21 +1216,11 @@ void ptt_set (int ot, int chan, int ptt_signal)
 
 	  if (save_audio_config_p->achan[chan].octrl[ot].ptt_line == PTT_LINE_RTS) {
 
-	    if (ptt) {
-	      RTS_ON(ptt_fd[chan][ot]);
-	    }
-	    else {
-	      RTS_OFF(ptt_fd[chan][ot]);
-	    }
+	    setRTS(ptt_fd[chan][ot], ptt);
 	  }
 	  else if (save_audio_config_p->achan[chan].octrl[ot].ptt_line == PTT_LINE_DTR) {
 
-	    if (ptt) {
-	      DTR_ON(ptt_fd[chan][ot]);
-	    }
-	    else {
-	      DTR_OFF(ptt_fd[chan][ot]);
-	    }
+	    setDTR(ptt_fd[chan][ot], ptt);
 	  }
 
 /* 
@@ -1205,21 +1229,11 @@ void ptt_set (int ot, int chan, int ptt_signal)
 
 	  if (save_audio_config_p->achan[chan].octrl[ot].ptt_line2 == PTT_LINE_RTS) {
 
-	    if (ptt2) {
-	      RTS_ON(ptt_fd[chan][ot]);
-	    }
-	    else {
-	      RTS_OFF(ptt_fd[chan][ot]);
-	    }
+	    setRTS(ptt_fd[chan][ot], ptt2);
 	  }
 	  else if (save_audio_config_p->achan[chan].octrl[ot].ptt_line2 == PTT_LINE_DTR) {
 
-	    if (ptt2) {
-	      DTR_ON(ptt_fd[chan][ot]);
-	    }
-	    else {
-	      DTR_OFF(ptt_fd[chan][ot]);
-	    }
+		setDTR(ptt_fd[chan][ot], ptt2);
 	  }
 	  /* else neither one */