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
10681061JNIEXPORT 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
10801073JNIEXPORT 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
11441137JNIEXPORT 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
11711170JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCTS (JNIEnv * env , jobject obj , jlong serialPortPointer )
0 commit comments