Skip to content

Commit 29578e7

Browse files
committed
Clean up setDTRandRTS native functions (add error line numbers + use in initialization)
1 parent 3033c94 commit 29578e7

File tree

2 files changed

+55
-84
lines changed

2 files changed

+55
-84
lines changed

src/main/c/Posix/SerialPort_Posix.c

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
* SerialPort_Posix.c
33
*
44
* Created on: Feb 25, 2012
5-
* Last Updated on: Apr 22, 2024
5+
* Last Updated on: Jul 01, 2025
66
* Author: Will Hedgecock
77
*
8-
* Copyright (C) 2012-2024 Fazecast, Inc.
8+
* Copyright (C) 2012-2025 Fazecast, Inc.
99
*
1010
* This file is part of jSerialComm.
1111
*
@@ -526,14 +526,7 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
526526
pthread_mutex_unlock(&criticalSection);
527527

528528
// Quickly set the desired RTS/DTR line status immediately upon opening
529-
if (isDtrEnabled)
530-
Java_com_fazecast_jSerialComm_SerialPort_setDTR(env, obj, (jlong)(intptr_t)port);
531-
else
532-
Java_com_fazecast_jSerialComm_SerialPort_clearDTR(env, obj, (jlong)(intptr_t)port);
533-
if (isRtsEnabled)
534-
Java_com_fazecast_jSerialComm_SerialPort_setRTS(env, obj, (jlong)(intptr_t)port);
535-
else
536-
Java_com_fazecast_jSerialComm_SerialPort_clearRTS(env, obj, (jlong)(intptr_t)port);
529+
Java_com_fazecast_jSerialComm_SerialPort_setDTRandRTS(env, obj, (jlong)(intptr_t)port, isDtrEnabled, isRtsEnabled);
537530

538531
// Ensure that multiple root users cannot access the device simultaneously
539532
if (!disableExclusiveLock && flock(port->handle, LOCK_EX | LOCK_NB))
@@ -1068,9 +1061,9 @@ JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_setEventListenin
10681061
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setBreak(JNIEnv *env, jobject obj, jlong serialPortPointer)
10691062
{
10701063
serialPort *port = (serialPort*)(intptr_t)serialPortPointer;
1071-
port->errorLineNumber = __LINE__ + 1;
10721064
if (ioctl(port->handle, TIOCSBRK))
10731065
{
1066+
port->errorLineNumber = __LINE__ - 2;
10741067
port->errorNumber = errno;
10751068
return JNI_FALSE;
10761069
}
@@ -1080,9 +1073,9 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setBreak(JNI
10801073
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearBreak(JNIEnv *env, jobject obj, jlong serialPortPointer)
10811074
{
10821075
serialPort *port = (serialPort*)(intptr_t)serialPortPointer;
1083-
port->errorLineNumber = __LINE__ + 1;
10841076
if (ioctl(port->handle, TIOCCBRK))
10851077
{
1078+
port->errorLineNumber = __LINE__ - 2;
10861079
port->errorNumber = errno;
10871080
return JNI_FALSE;
10881081
}
@@ -1093,9 +1086,9 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setRTS(JNIEn
10931086
{
10941087
const int modemBits = TIOCM_RTS;
10951088
serialPort *port = (serialPort*)(intptr_t)serialPortPointer;
1096-
port->errorLineNumber = __LINE__ + 1;
10971089
if (ioctl(port->handle, TIOCMBIS, &modemBits))
10981090
{
1091+
port->errorLineNumber = __LINE__ - 2;
10991092
port->errorNumber = errno;
11001093
return JNI_FALSE;
11011094
}
@@ -1106,9 +1099,9 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearRTS(JNI
11061099
{
11071100
const int modemBits = TIOCM_RTS;
11081101
serialPort *port = (serialPort*)(intptr_t)serialPortPointer;
1109-
port->errorLineNumber = __LINE__ + 1;
11101102
if (ioctl(port->handle, TIOCMBIC, &modemBits))
11111103
{
1104+
port->errorLineNumber = __LINE__ - 2;
11121105
port->errorNumber = errno;
11131106
return JNI_FALSE;
11141107
}
@@ -1119,9 +1112,9 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setDTR(JNIEn
11191112
{
11201113
const int modemBits = TIOCM_DTR;
11211114
serialPort *port = (serialPort*)(intptr_t)serialPortPointer;
1122-
port->errorLineNumber = __LINE__ + 1;
11231115
if (ioctl(port->handle, TIOCMBIS, &modemBits))
11241116
{
1117+
port->errorLineNumber = __LINE__ - 2;
11251118
port->errorNumber = errno;
11261119
return JNI_FALSE;
11271120
}
@@ -1132,9 +1125,9 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearDTR(JNI
11321125
{
11331126
const int modemBits = TIOCM_DTR;
11341127
serialPort *port = (serialPort*)(intptr_t)serialPortPointer;
1135-
port->errorLineNumber = __LINE__ + 1;
11361128
if (ioctl(port->handle, TIOCMBIC, &modemBits))
11371129
{
1130+
port->errorLineNumber = __LINE__ - 2;
11381131
port->errorNumber = errno;
11391132
return JNI_FALSE;
11401133
}
@@ -1143,29 +1136,35 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearDTR(JNI
11431136

11441137
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setDTRandRTS(JNIEnv *env, jobject obj, jlong serialPortPointer, jboolean dtr, jboolean rts)
11451138
{
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;
1139+
int modemBits = 0;
1140+
serialPort *port = (serialPort*)(intptr_t)serialPortPointer;
11581141

1159-
if (rts) status |= TIOCM_RTS;
1160-
else status &= ~TIOCM_RTS;
1142+
// Read current modem bits
1143+
if (ioctl(port->handle, TIOCMGET, &modemBits))
1144+
{
1145+
port->errorLineNumber = __LINE__ - 2;
1146+
port->errorNumber = errno;
1147+
return JNI_FALSE;
1148+
}
11611149

1162-
// Write combined status
1163-
if (ioctl(port->handle, TIOCMSET, &status)) {
1164-
port->errorNumber = errno;
1165-
return JNI_FALSE;
1166-
}
1150+
// Modify bits
1151+
if (dtr)
1152+
modemBits |= TIOCM_DTR;
1153+
else
1154+
modemBits &= ~TIOCM_DTR;
1155+
if (rts)
1156+
modemBits |= TIOCM_RTS;
1157+
else
1158+
modemBits &= ~TIOCM_RTS;
11671159

1168-
return JNI_TRUE;
1160+
// Write combined bits
1161+
if (ioctl(port->handle, TIOCMSET, &modemBits))
1162+
{
1163+
port->errorLineNumber = __LINE__ - 2;
1164+
port->errorNumber = errno;
1165+
return JNI_FALSE;
1166+
}
1167+
return JNI_TRUE;
11691168
}
11701169

11711170
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCTS(JNIEnv *env, jobject obj, jlong serialPortPointer)

src/main/c/Windows/SerialPort_Windows.c

Lines changed: 20 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
* SerialPort_Windows.c
33
*
44
* Created on: Feb 25, 2012
5-
* Last Updated on: May 15, 2024
5+
* Last Updated on: Jul 01, 2025
66
* Author: Will Hedgecock
77
*
8-
* Copyright (C) 2012-2024 Fazecast, Inc.
8+
* Copyright (C) 2012-2025 Fazecast, Inc.
99
*
1010
* This file is part of jSerialComm.
1111
*
@@ -896,14 +896,7 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
896896
LeaveCriticalSection(&criticalSection);
897897

898898
// Quickly set the desired RTS/DTR line status immediately upon opening
899-
if (isDtrEnabled)
900-
Java_com_fazecast_jSerialComm_SerialPort_setDTR(env, obj, (jlong)(intptr_t)port);
901-
else
902-
Java_com_fazecast_jSerialComm_SerialPort_clearDTR(env, obj, (jlong)(intptr_t)port);
903-
if (isRtsEnabled)
904-
Java_com_fazecast_jSerialComm_SerialPort_setRTS(env, obj, (jlong)(intptr_t)port);
905-
else
906-
Java_com_fazecast_jSerialComm_SerialPort_clearRTS(env, obj, (jlong)(intptr_t)port);
899+
Java_com_fazecast_jSerialComm_SerialPort_setDTRandRTS(env, obj, (jlong)(intptr_t)port, isDtrEnabled, isRtsEnabled);
907900

908901
// Configure the port parameters and timeouts
909902
if (!Java_com_fazecast_jSerialComm_SerialPort_configPort(env, obj, (jlong)(intptr_t)port))
@@ -1418,44 +1411,23 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearDTR(JNI
14181411

14191412
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setDTRandRTS(JNIEnv *env, jobject obj, jlong serialPortPointer, jboolean dtr, jboolean rts)
14201413
{
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;
1414+
// Unfortunately, Windows API doesn't offer atomic combined DTR/RTS control.
1415+
// So we must call EscapeCommFunction separately for each line.
1416+
// This still typically results in a single USB control transfer on Windows CDC drivers.
1417+
serialPort *port = (serialPort*)(intptr_t)serialPortPointer;
1418+
if (!EscapeCommFunction(port->handle, dtr ? SETDTR : CLRDTR))
1419+
{
1420+
port->errorLineNumber = __LINE__ - 2;
1421+
port->errorNumber = GetLastError();
1422+
return JNI_FALSE;
1423+
}
1424+
if (!EscapeCommFunction(port->handle, rts ? SETRTS : CLRRTS))
1425+
{
1426+
port->errorLineNumber = __LINE__ - 2;
1427+
port->errorNumber = GetLastError();
1428+
return JNI_FALSE;
1429+
}
1430+
return JNI_TRUE;
14591431
}
14601432

14611433
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCTS(JNIEnv *env, jobject obj, jlong serialPortPointer)

0 commit comments

Comments
 (0)