Skip to content

Commit 3033c94

Browse files
authored
Merge pull request #598 from dkaukov/dtr-and-rts
Add method to set DTR and RTS together
2 parents 4163f1e + 68e961e commit 3033c94

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

src/main/c/Posix/SerialPort_Posix.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,33 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearDTR(JNI
11411141
return JNI_TRUE;
11421142
}
11431143

1144+
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setDTRandRTS(JNIEnv *env, jobject obj, jlong serialPortPointer, jboolean dtr, jboolean rts)
1145+
{
1146+
struct serialPort *port = (serialPort*)(intptr_t)serialPortPointer;
1147+
int status;
1148+
1149+
// Read current status
1150+
if (ioctl(port->handle, TIOCMGET, &status)) {
1151+
port->errorNumber = errno;
1152+
return JNI_FALSE;
1153+
}
1154+
1155+
// Modify bits
1156+
if (dtr) status |= TIOCM_DTR;
1157+
else status &= ~TIOCM_DTR;
1158+
1159+
if (rts) status |= TIOCM_RTS;
1160+
else status &= ~TIOCM_RTS;
1161+
1162+
// Write combined status
1163+
if (ioctl(port->handle, TIOCMSET, &status)) {
1164+
port->errorNumber = errno;
1165+
return JNI_FALSE;
1166+
}
1167+
1168+
return JNI_TRUE;
1169+
}
1170+
11441171
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCTS(JNIEnv *env, jobject obj, jlong serialPortPointer)
11451172
{
11461173
int modemBits = 0;

src/main/c/Posix/com_fazecast_jSerialComm_SerialPort.h

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/c/Windows/SerialPort_Windows.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,48 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearDTR(JNI
14161416
return JNI_TRUE;
14171417
}
14181418

1419+
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setDTRandRTS(JNIEnv *env, jobject obj, jlong serialPortPointer, jboolean dtr, jboolean rts)
1420+
{
1421+
serialPort *port = (serialPort*)(intptr_t)serialPortPointer;
1422+
HANDLE hComm = (HANDLE)(intptr_t)port->handle;
1423+
1424+
port->errorLineNumber = __LINE__ + 1;
1425+
1426+
DWORD funcFlags = 0;
1427+
1428+
// Unfortunately, Windows API doesn't offer atomic combined DTR/RTS control.
1429+
// So we must call EscapeCommFunction separately for each line.
1430+
// This still typically results in a single USB control transfer on Windows CDC drivers.
1431+
1432+
// Set or clear DTR
1433+
if (dtr) {
1434+
if (!EscapeCommFunction(hComm, SETDTR)) {
1435+
port->errorNumber = GetLastError();
1436+
return JNI_FALSE;
1437+
}
1438+
} else {
1439+
if (!EscapeCommFunction(hComm, CLRDTR)) {
1440+
port->errorNumber = GetLastError();
1441+
return JNI_FALSE;
1442+
}
1443+
}
1444+
1445+
// Set or clear RTS
1446+
if (rts) {
1447+
if (!EscapeCommFunction(hComm, SETRTS)) {
1448+
port->errorNumber = GetLastError();
1449+
return JNI_FALSE;
1450+
}
1451+
} else {
1452+
if (!EscapeCommFunction(hComm, CLRRTS)) {
1453+
port->errorNumber = GetLastError();
1454+
return JNI_FALSE;
1455+
}
1456+
}
1457+
1458+
return JNI_TRUE;
1459+
}
1460+
14191461
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCTS(JNIEnv *env, jobject obj, jlong serialPortPointer)
14201462
{
14211463
DWORD modemStatus = 0;

src/main/java/com/fazecast/jSerialComm/SerialPort.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ public final int getLastErrorCode()
852852
private native boolean clearRTS(long portHandle); // Clear RTS line to 0
853853
private native boolean setDTR(long portHandle); // Set DTR line to 1
854854
private native boolean clearDTR(long portHandle); // Clear DTR line to 0
855+
private native boolean setDTRandRTS(long portHandle, boolean dtr, boolean rts);
855856
private native boolean getCTS(long portHandle); // Returns whether the CTS signal is 1
856857
private native boolean getDSR(long portHandle); // Returns whether the DSR signal is 1
857858
private native boolean getDCD(long portHandle); // Returns whether the DCD signal is 1
@@ -1062,6 +1063,19 @@ public final boolean clearDTR()
10621063
}
10631064

10641065
/**
1066+
* Sets the DTR and RTS lines to the specified values.
1067+
* @param dtr - the desired state of the DTR line.
1068+
* @param rts - the desired state of the RTS line.
1069+
* @return true if successful, false if not.
1070+
*/
1071+
public final boolean setDTRandRTS(boolean dtr, boolean rts)
1072+
{
1073+
isDtrEnabled = dtr;
1074+
isRtsEnabled = rts;
1075+
return (androidPort != null) ? androidPort.setDTRandRTS(dtr, rts) : ((portHandle == 0) || setDTRandRTS(portHandle, dtr, rts));
1076+
}
1077+
1078+
/**
10651079
* Returns whether the CTS line is currently asserted.
10661080
* @return Whether or not the CTS line is asserted.
10671081
*/

src/main/java/com/fazecast/jSerialComm/android/AndroidPort.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ public long closePortNative()
219219
public abstract boolean clearRTS();
220220
public abstract boolean setDTR();
221221
public abstract boolean clearDTR();
222+
public abstract boolean setDTRandRTS(boolean dtr, boolean rts);
222223
public abstract boolean getCTS();
223224
public abstract boolean getDSR();
224225
public abstract boolean getDCD();

0 commit comments

Comments
 (0)