Removed separate function for setting flow control parameters.
This commit is contained in:
parent
44882a4cef
commit
3957c116fb
|
@ -125,8 +125,18 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
|
|||
int serialPortFD = -1;
|
||||
if ((serialPortFD = open(portName, O_RDWR | O_NOCTTY | O_NONBLOCK)) > 0)
|
||||
{
|
||||
// Clear any serial port flags and set up raw, non-canonical port parameters
|
||||
if (isatty(serialPortFD))
|
||||
{
|
||||
struct termios options = { 0 };
|
||||
fcntl(serialPortFD, F_SETFL, 0);
|
||||
ioctl(serialPortFD, TCGETS, &options);
|
||||
cfmakeraw(&options);
|
||||
ioctl(serialPortFD, TCSETS, &options);
|
||||
}
|
||||
|
||||
// Configure the port parameters and timeouts
|
||||
if (Java_com_fazecast_jSerialComm_SerialPort_configPort(env, obj, serialPortFD) && Java_com_fazecast_jSerialComm_SerialPort_configFlowControl(env, obj, serialPortFD) &&
|
||||
if (Java_com_fazecast_jSerialComm_SerialPort_configPort(env, obj, serialPortFD) &&
|
||||
Java_com_fazecast_jSerialComm_SerialPort_configEventFlags(env, obj, serialPortFD))
|
||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_TRUE);
|
||||
else
|
||||
|
@ -153,30 +163,31 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
|
|||
int byteSizeInt = (*env)->GetIntField(env, obj, dataBitsField);
|
||||
int stopBitsInt = (*env)->GetIntField(env, obj, stopBitsField);
|
||||
int parityInt = (*env)->GetIntField(env, obj, parityField);
|
||||
int flowControl = (*env)->GetIntField(env, obj, flowControlField);
|
||||
tcflag_t byteSize = (byteSizeInt == 5) ? CS5 : (byteSizeInt == 6) ? CS6 : (byteSizeInt == 7) ? CS7 : CS8;
|
||||
tcflag_t stopBits = ((stopBitsInt == com_fazecast_jSerialComm_SerialPort_ONE_STOP_BIT) || (stopBitsInt == com_fazecast_jSerialComm_SerialPort_ONE_POINT_FIVE_STOP_BITS)) ? 0 : CSTOPB;
|
||||
tcflag_t parity = (parityInt == com_fazecast_jSerialComm_SerialPort_NO_PARITY) ? 0 : (parityInt == com_fazecast_jSerialComm_SerialPort_ODD_PARITY) ? (PARENB | PARODD) : (parityInt == com_fazecast_jSerialComm_SerialPort_EVEN_PARITY) ? PARENB : (parityInt == com_fazecast_jSerialComm_SerialPort_MARK_PARITY) ? (PARENB | CMSPAR | PARODD) : (PARENB | CMSPAR);
|
||||
|
||||
// Clear any serial port flags
|
||||
if (isatty(serialPortFD))
|
||||
fcntl(serialPortFD, F_SETFL, 0);
|
||||
|
||||
// Set raw-mode to allow the use of ioctl()
|
||||
if (isatty(serialPortFD))
|
||||
ioctl(serialPortFD, TCGETS, &options);
|
||||
else
|
||||
return JNI_FALSE;
|
||||
cfmakeraw(&options);
|
||||
tcflag_t CTSRTSEnabled = (((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_CTS_ENABLED) > 0) ||
|
||||
((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_RTS_ENABLED) > 0)) ? CRTSCTS : 0;
|
||||
tcflag_t XonXoffInEnabled = ((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_XONXOFF_IN_ENABLED) > 0) ? IXOFF : 0;
|
||||
tcflag_t XonXoffOutEnabled = ((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_XONXOFF_OUT_ENABLED) > 0) ? IXON : 0;
|
||||
|
||||
// Set updated port parameters
|
||||
options.c_cflag = (byteSize | stopBits | parity | CLOCAL | CREAD);
|
||||
if (parityInt == com_fazecast_jSerialComm_SerialPort_SPACE_PARITY)
|
||||
options.c_cflag &= ~PARODD;
|
||||
options.c_iflag &= ~(INPCK | IGNPAR | PARMRK | ISTRIP);
|
||||
if (byteSizeInt < 8)
|
||||
options.c_iflag |= ISTRIP;
|
||||
if (parityInt != 0)
|
||||
options.c_iflag |= (INPCK | IGNPAR);
|
||||
if (isatty(serialPortFD))
|
||||
{
|
||||
ioctl(serialPortFD, TCGETS, &options);
|
||||
options.c_cflag = (byteSize | stopBits | parity | CLOCAL | CREAD | CTSRTSEnabled);
|
||||
if (parityInt == com_fazecast_jSerialComm_SerialPort_SPACE_PARITY)
|
||||
options.c_cflag &= ~PARODD;
|
||||
options.c_iflag &= ~(INPCK | IGNPAR | PARMRK | ISTRIP);
|
||||
if (byteSizeInt < 8)
|
||||
options.c_iflag |= ISTRIP;
|
||||
if (parityInt != 0)
|
||||
options.c_iflag |= (INPCK | IGNPAR);
|
||||
options.c_iflag |= (XonXoffInEnabled | XonXoffOutEnabled);
|
||||
}
|
||||
else
|
||||
return JNI_FALSE;
|
||||
|
||||
// Set baud rate
|
||||
unsigned int baudRateCode = getBaudRateCode(baudRate);
|
||||
|
@ -197,42 +208,6 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
|
|||
return ((retVal == 0) ? JNI_TRUE : JNI_FALSE);
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configFlowControl(JNIEnv *env, jobject obj, jlong serialPortFD)
|
||||
{
|
||||
if (serialPortFD <= 0)
|
||||
return JNI_FALSE;
|
||||
struct termios options = { 0 };
|
||||
|
||||
// Get port parameters from Java class
|
||||
int baudRate = (*env)->GetIntField(env, obj, baudRateField);
|
||||
unsigned int baudRateCode = getBaudRateCode(baudRate);
|
||||
int flowControl = (*env)->GetIntField(env, obj, flowControlField);
|
||||
tcflag_t CTSRTSEnabled = (((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_CTS_ENABLED) > 0) ||
|
||||
((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_RTS_ENABLED) > 0)) ? CRTSCTS : 0;
|
||||
tcflag_t XonXoffInEnabled = ((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_XONXOFF_IN_ENABLED) > 0) ? IXOFF : 0;
|
||||
tcflag_t XonXoffOutEnabled = ((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_XONXOFF_OUT_ENABLED) > 0) ? IXON : 0;
|
||||
|
||||
// Retrieve existing port configuration
|
||||
if (isatty(serialPortFD))
|
||||
ioctl(serialPortFD, TCGETS, &options);
|
||||
else
|
||||
return JNI_FALSE;
|
||||
|
||||
// Set updated port parameters
|
||||
options.c_cflag |= CTSRTSEnabled;
|
||||
options.c_iflag |= XonXoffInEnabled | XonXoffOutEnabled;
|
||||
|
||||
// Apply changes
|
||||
int retVal = -1;
|
||||
if (isatty(serialPortFD))
|
||||
retVal = ioctl(serialPortFD, TCSETS, &options);
|
||||
else
|
||||
return JNI_FALSE;
|
||||
if (baudRateCode == 0) // Set custom baud rate
|
||||
setBaudRate(serialPortFD, baudRate);
|
||||
return ((retVal == 0) ? JNI_TRUE : JNI_FALSE);
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeouts(JNIEnv *env, jobject obj, jlong serialPortFD)
|
||||
{
|
||||
// Get port timeouts from Java class
|
||||
|
|
|
@ -111,14 +111,6 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNat
|
|||
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort
|
||||
(JNIEnv *, jobject, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_fazecast_jSerialComm_SerialPort
|
||||
* Method: configFlowControl
|
||||
* Signature: (J)Z
|
||||
*/
|
||||
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configFlowControl
|
||||
(JNIEnv *, jobject, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_fazecast_jSerialComm_SerialPort
|
||||
* Method: configTimeouts
|
||||
|
|
|
@ -119,11 +119,15 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
|
|||
int serialPortFD = -1;
|
||||
if ((serialPortFD = open(portName, O_RDWR | O_NOCTTY | O_NONBLOCK)) > 0)
|
||||
{
|
||||
// Clear any serial port flags
|
||||
// Clear any serial port flags and set up raw, non-canonical port parameters
|
||||
struct termios options = { 0 };
|
||||
fcntl(serialPortFD, F_SETFL, 0);
|
||||
tcgetattr(serialPortFD, &options);
|
||||
cfmakeraw(&options);
|
||||
tcsetattr(serialPortFD, TCSANOW, &options);
|
||||
|
||||
// Configure the port parameters and timeouts
|
||||
if (Java_com_fazecast_jSerialComm_SerialPort_configPort(env, obj, serialPortFD) && Java_com_fazecast_jSerialComm_SerialPort_configFlowControl(env, obj, serialPortFD) &&
|
||||
if (Java_com_fazecast_jSerialComm_SerialPort_configPort(env, obj, serialPortFD) &&
|
||||
Java_com_fazecast_jSerialComm_SerialPort_configEventFlags(env, obj, serialPortFD))
|
||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_TRUE);
|
||||
else
|
||||
|
@ -146,23 +150,24 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
|
|||
if (serialPortFD <= 0)
|
||||
return JNI_FALSE;
|
||||
struct termios options = { 0 };
|
||||
struct serial_struct serialInfo = { 0 };
|
||||
|
||||
// Get port parameters from Java class
|
||||
int baudRate = (*env)->GetIntField(env, obj, baudRateField);
|
||||
int byteSizeInt = (*env)->GetIntField(env, obj, dataBitsField);
|
||||
int stopBitsInt = (*env)->GetIntField(env, obj, stopBitsField);
|
||||
int parityInt = (*env)->GetIntField(env, obj, parityField);
|
||||
int flowControl = (*env)->GetIntField(env, obj, flowControlField);
|
||||
tcflag_t byteSize = (byteSizeInt == 5) ? CS5 : (byteSizeInt == 6) ? CS6 : (byteSizeInt == 7) ? CS7 : CS8;
|
||||
tcflag_t stopBits = ((stopBitsInt == com_fazecast_jSerialComm_SerialPort_ONE_STOP_BIT) || (stopBitsInt == com_fazecast_jSerialComm_SerialPort_ONE_POINT_FIVE_STOP_BITS)) ? 0 : CSTOPB;
|
||||
tcflag_t parity = (parityInt == com_fazecast_jSerialComm_SerialPort_NO_PARITY) ? 0 : (parityInt == com_fazecast_jSerialComm_SerialPort_ODD_PARITY) ? (PARENB | PARODD) : (parityInt == com_fazecast_jSerialComm_SerialPort_EVEN_PARITY) ? PARENB : (parityInt == com_fazecast_jSerialComm_SerialPort_MARK_PARITY) ? (PARENB | CMSPAR | PARODD) : (PARENB | CMSPAR);
|
||||
|
||||
// Set raw-mode to allow the use of tcsetattr() and ioctl()
|
||||
tcgetattr(serialPortFD, &options);
|
||||
cfmakeraw(&options);
|
||||
tcflag_t CTSRTSEnabled = (((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_CTS_ENABLED) > 0) ||
|
||||
((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_RTS_ENABLED) > 0)) ? CRTSCTS : 0;
|
||||
tcflag_t XonXoffInEnabled = ((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_XONXOFF_IN_ENABLED) > 0) ? IXOFF : 0;
|
||||
tcflag_t XonXoffOutEnabled = ((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_XONXOFF_OUT_ENABLED) > 0) ? IXON : 0;
|
||||
|
||||
// Set updated port parameters
|
||||
options.c_cflag = (byteSize | stopBits | parity | CLOCAL | CREAD);
|
||||
tcgetattr(serialPortFD, &options);
|
||||
options.c_cflag = (byteSize | stopBits | parity | CLOCAL | CREAD | CTSRTSEnabled);
|
||||
if (parityInt == com_fazecast_jSerialComm_SerialPort_SPACE_PARITY)
|
||||
options.c_cflag &= ~PARODD;
|
||||
options.c_iflag &= ~(INPCK | IGNPAR | PARMRK | ISTRIP);
|
||||
|
@ -170,6 +175,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
|
|||
options.c_iflag |= ISTRIP;
|
||||
if (parityInt != 0)
|
||||
options.c_iflag |= (INPCK | IGNPAR);
|
||||
options.c_iflag |= (XonXoffInEnabled | XonXoffOutEnabled);
|
||||
|
||||
// Set baud rate
|
||||
unsigned int baudRateCode = getBaudRateCode(baudRate);
|
||||
|
@ -187,35 +193,6 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
|
|||
return ((retVal == 0) ? JNI_TRUE : JNI_FALSE);
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configFlowControl(JNIEnv *env, jobject obj, jlong serialPortFD)
|
||||
{
|
||||
if (serialPortFD <= 0)
|
||||
return JNI_FALSE;
|
||||
struct termios options = { 0 };
|
||||
|
||||
// Get port parameters from Java class
|
||||
int baudRate = (*env)->GetIntField(env, obj, baudRateField);
|
||||
unsigned int baudRateCode = getBaudRateCode(baudRate);
|
||||
int flowControl = (*env)->GetIntField(env, obj, flowControlField);
|
||||
tcflag_t CTSRTSEnabled = (((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_CTS_ENABLED) > 0) ||
|
||||
((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_RTS_ENABLED) > 0)) ? CRTSCTS : 0;
|
||||
tcflag_t XonXoffInEnabled = ((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_XONXOFF_IN_ENABLED) > 0) ? IXOFF : 0;
|
||||
tcflag_t XonXoffOutEnabled = ((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_XONXOFF_OUT_ENABLED) > 0) ? IXON : 0;
|
||||
|
||||
// Retrieve existing port configuration
|
||||
tcgetattr(serialPortFD, &options);
|
||||
|
||||
// Set updated port parameters
|
||||
options.c_cflag |= CTSRTSEnabled;
|
||||
options.c_iflag |= (XonXoffInEnabled | XonXoffOutEnabled);
|
||||
|
||||
// Apply changes
|
||||
int retVal = tcsetattr(serialPortFD, TCSANOW, &options);
|
||||
if (baudRateCode == 0) // Set custom baud rate
|
||||
setBaudRate(serialPortFD, baudRate);
|
||||
return ((retVal == 0) ? JNI_TRUE : JNI_FALSE);
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeouts(JNIEnv *env, jobject obj, jlong serialPortFD)
|
||||
{
|
||||
// Get port timeouts from Java class
|
||||
|
|
|
@ -170,11 +170,15 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
|
|||
int serialPortFD = -1;
|
||||
if ((serialPortFD = open(portName, O_RDWR | O_NOCTTY | O_NONBLOCK)) > 0)
|
||||
{
|
||||
// Clear any serial port flags
|
||||
// Clear any serial port flags and set up raw, non-canonical port parameters
|
||||
struct termios options = { 0 };
|
||||
fcntl(serialPortFD, F_SETFL, 0);
|
||||
tcgetattr(serialPortFD, &options);
|
||||
cfmakeraw(&options);
|
||||
tcsetattr(serialPortFD, TCSANOW, &options);
|
||||
|
||||
// Configure the port parameters and timeouts
|
||||
if (Java_com_fazecast_jSerialComm_SerialPort_configPort(env, obj, serialPortFD) && Java_com_fazecast_jSerialComm_SerialPort_configFlowControl(env, obj, serialPortFD) &&
|
||||
if (Java_com_fazecast_jSerialComm_SerialPort_configPort(env, obj, serialPortFD) &&
|
||||
Java_com_fazecast_jSerialComm_SerialPort_configEventFlags(env, obj, serialPortFD))
|
||||
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_TRUE);
|
||||
else
|
||||
|
@ -203,16 +207,18 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
|
|||
int byteSizeInt = (*env)->GetIntField(env, obj, dataBitsField);
|
||||
int stopBitsInt = (*env)->GetIntField(env, obj, stopBitsField);
|
||||
int parityInt = (*env)->GetIntField(env, obj, parityField);
|
||||
int flowControl = (*env)->GetIntField(env, obj, flowControlField);
|
||||
tcflag_t byteSize = (byteSizeInt == 5) ? CS5 : (byteSizeInt == 6) ? CS6 : (byteSizeInt == 7) ? CS7 : CS8;
|
||||
tcflag_t stopBits = ((stopBitsInt == com_fazecast_jSerialComm_SerialPort_ONE_STOP_BIT) || (stopBitsInt == com_fazecast_jSerialComm_SerialPort_ONE_POINT_FIVE_STOP_BITS)) ? 0 : CSTOPB;
|
||||
tcflag_t parity = (parityInt == com_fazecast_jSerialComm_SerialPort_NO_PARITY) ? 0 : (parityInt == com_fazecast_jSerialComm_SerialPort_ODD_PARITY) ? (PARENB | PARODD) : (parityInt == com_fazecast_jSerialComm_SerialPort_EVEN_PARITY) ? PARENB : (parityInt == com_fazecast_jSerialComm_SerialPort_MARK_PARITY) ? (PARENB | CMSPAR | PARODD) : (PARENB | CMSPAR);
|
||||
|
||||
// Set raw-mode to allow the use of tcsetattr() and ioctl()
|
||||
tcgetattr(serialPortFD, &options);
|
||||
cfmakeraw(&options);
|
||||
tcflag_t CTSRTSEnabled = (((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_CTS_ENABLED) > 0) ||
|
||||
((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_RTS_ENABLED) > 0)) ? CRTSCTS : 0;
|
||||
tcflag_t XonXoffInEnabled = ((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_XONXOFF_IN_ENABLED) > 0) ? IXOFF : 0;
|
||||
tcflag_t XonXoffOutEnabled = ((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_XONXOFF_OUT_ENABLED) > 0) ? IXON : 0;
|
||||
|
||||
// Set updated port parameters
|
||||
options.c_cflag = (byteSize | stopBits | parity | CLOCAL | CREAD);
|
||||
tcgetattr(serialPortFD, &options);
|
||||
options.c_cflag = (byteSize | stopBits | parity | CLOCAL | CREAD | CTSRTSEnabled);
|
||||
if (parityInt == com_fazecast_jSerialComm_SerialPort_SPACE_PARITY)
|
||||
options.c_cflag &= ~PARODD;
|
||||
options.c_iflag &= ~(INPCK | IGNPAR | PARMRK | ISTRIP);
|
||||
|
@ -220,6 +226,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
|
|||
options.c_iflag |= ISTRIP;
|
||||
if (parityInt != 0)
|
||||
options.c_iflag |= (INPCK | IGNPAR);
|
||||
options.c_iflag |= (XonXoffInEnabled | XonXoffOutEnabled);
|
||||
|
||||
// Set baud rate
|
||||
speed_t baudRateCode = getBaudRateCode(baudRate);
|
||||
|
@ -239,30 +246,6 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
|
|||
return ((tcsetattr(serialPortFD, TCSANOW, &options) == 0) ? JNI_TRUE : JNI_FALSE);
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configFlowControl(JNIEnv *env, jobject obj, jlong serialPortFD)
|
||||
{
|
||||
if (serialPortFD <= 0)
|
||||
return JNI_FALSE;
|
||||
struct termios options;
|
||||
|
||||
// Get port parameters from Java class
|
||||
int flowControl = (*env)->GetIntField(env, obj, flowControlField);
|
||||
tcflag_t CTSRTSEnabled = ((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_CTS_ENABLED) > 0) ? CCTS_OFLOW : 0;
|
||||
CTSRTSEnabled |= ((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_RTS_ENABLED) > 0) ? CRTS_IFLOW : 0;
|
||||
tcflag_t XonXoffInEnabled = ((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_XONXOFF_IN_ENABLED) > 0) ? IXOFF : 0;
|
||||
tcflag_t XonXoffOutEnabled = ((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_XONXOFF_OUT_ENABLED) > 0) ? IXON : 0;
|
||||
|
||||
// Retrieve existing port configuration
|
||||
tcgetattr(serialPortFD, &options);
|
||||
|
||||
// Set updated port parameters
|
||||
options.c_cflag |= CTSRTSEnabled;
|
||||
options.c_iflag |= (XonXoffInEnabled | XonXoffOutEnabled);
|
||||
|
||||
// Apply changes
|
||||
return (tcsetattr(serialPortFD, TCSANOW, &options) == -1) ? JNI_FALSE : JNI_TRUE;
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeouts(JNIEnv *env, jobject obj, jlong serialPortFD)
|
||||
{
|
||||
// Get port timeouts from Java class
|
||||
|
|
|
@ -257,7 +257,7 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
|
|||
if ((serialPortHandle = CreateFile(portName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_OVERLAPPED, NULL)) != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
// Configure the port parameters and timeouts
|
||||
if (Java_com_fazecast_jSerialComm_SerialPort_configPort(env, obj, (jlong)serialPortHandle) && Java_com_fazecast_jSerialComm_SerialPort_configFlowControl(env, obj, (jlong)serialPortHandle) &&
|
||||
if (Java_com_fazecast_jSerialComm_SerialPort_configPort(env, obj, (jlong)serialPortHandle) &&
|
||||
Java_com_fazecast_jSerialComm_SerialPort_configEventFlags(env, obj, (jlong)serialPortHandle))
|
||||
env->SetBooleanField(obj, isOpenedField, JNI_TRUE);
|
||||
else
|
||||
|
@ -287,37 +287,10 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
|
|||
BYTE byteSize = (BYTE)env->GetIntField(obj, dataBitsField);
|
||||
int stopBitsInt = env->GetIntField(obj, stopBitsField);
|
||||
int parityInt = env->GetIntField(obj, parityField);
|
||||
int flowControl = env->GetIntField(obj, flowControlField);
|
||||
BYTE stopBits = (stopBitsInt == com_fazecast_jSerialComm_SerialPort_ONE_STOP_BIT) ? ONESTOPBIT : (stopBitsInt == com_fazecast_jSerialComm_SerialPort_ONE_POINT_FIVE_STOP_BITS) ? ONE5STOPBITS : TWOSTOPBITS;
|
||||
BYTE parity = (parityInt == com_fazecast_jSerialComm_SerialPort_NO_PARITY) ? NOPARITY : (parityInt == com_fazecast_jSerialComm_SerialPort_ODD_PARITY) ? ODDPARITY : (parityInt == com_fazecast_jSerialComm_SerialPort_EVEN_PARITY) ? EVENPARITY : (parityInt == com_fazecast_jSerialComm_SerialPort_MARK_PARITY) ? MARKPARITY : SPACEPARITY;
|
||||
BOOL isParity = (parityInt == com_fazecast_jSerialComm_SerialPort_NO_PARITY) ? FALSE : TRUE;
|
||||
|
||||
// Retrieve existing port configuration
|
||||
if (!GetCommState(serialPortHandle, &dcbSerialParams))
|
||||
return JNI_FALSE;
|
||||
|
||||
// Set updated port parameters
|
||||
dcbSerialParams.BaudRate = baudRate;
|
||||
dcbSerialParams.ByteSize = byteSize;
|
||||
dcbSerialParams.StopBits = stopBits;
|
||||
dcbSerialParams.Parity = parity;
|
||||
dcbSerialParams.fParity = isParity;
|
||||
dcbSerialParams.fBinary = TRUE;
|
||||
dcbSerialParams.fAbortOnError = FALSE;
|
||||
|
||||
// Apply changes
|
||||
return SetCommState(serialPortHandle, &dcbSerialParams);
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configFlowControl(JNIEnv *env, jobject obj, jlong serialPortFD)
|
||||
{
|
||||
HANDLE serialPortHandle = (HANDLE)serialPortFD;
|
||||
if (serialPortHandle == INVALID_HANDLE_VALUE)
|
||||
return JNI_FALSE;
|
||||
DCB dcbSerialParams = {0};
|
||||
dcbSerialParams.DCBlength = sizeof(DCB);
|
||||
|
||||
// Get flow control parameters from Java class
|
||||
int flowControl = env->GetIntField(obj, flowControlField);
|
||||
BOOL CTSEnabled = (((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_CTS_ENABLED) > 0) ||
|
||||
((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_RTS_ENABLED) > 0));
|
||||
BOOL DSREnabled = (((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_DSR_ENABLED) > 0) ||
|
||||
|
@ -332,6 +305,13 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configFlowCo
|
|||
return JNI_FALSE;
|
||||
|
||||
// Set updated port parameters
|
||||
dcbSerialParams.BaudRate = baudRate;
|
||||
dcbSerialParams.ByteSize = byteSize;
|
||||
dcbSerialParams.StopBits = stopBits;
|
||||
dcbSerialParams.Parity = parity;
|
||||
dcbSerialParams.fParity = isParity;
|
||||
dcbSerialParams.fBinary = TRUE;
|
||||
dcbSerialParams.fAbortOnError = FALSE;
|
||||
dcbSerialParams.fRtsControl = RTSValue;
|
||||
dcbSerialParams.fOutxCtsFlow = CTSEnabled;
|
||||
dcbSerialParams.fOutxDsrFlow = DSREnabled;
|
||||
|
@ -511,11 +491,15 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNat
|
|||
// Close port
|
||||
int numRetries = 10;
|
||||
while (!CloseHandle(serialPortHandle) && (numRetries-- > 0));
|
||||
serialPortHandle = INVALID_HANDLE_VALUE;
|
||||
env->SetLongField(obj, serialPortHandleField, -1l);
|
||||
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
|
||||
if (numRetries > 0)
|
||||
{
|
||||
serialPortHandle = INVALID_HANDLE_VALUE;
|
||||
env->SetLongField(obj, serialPortHandleField, -1l);
|
||||
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
|
||||
return JNI_TRUE;
|
||||
}
|
||||
|
||||
return JNI_TRUE;
|
||||
return JNI_FALSE;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_bytesAvailable(JNIEnv *env, jobject obj, jlong serialPortFD)
|
||||
|
|
|
@ -389,7 +389,6 @@ public final class SerialPort
|
|||
private final native long openPortNative(); // Opens serial port
|
||||
private final native boolean closePortNative(long portHandle); // Closes serial port
|
||||
private final native boolean configPort(long portHandle); // Changes/sets serial port parameters as defined by this class
|
||||
private final native boolean configFlowControl(long portHandle); // Changes/sets flow control parameters as defined by this class
|
||||
private final native boolean configTimeouts(long portHandle); // Changes/sets serial port timeouts as defined by this class
|
||||
private final native boolean configEventFlags(long portHandle); // Changes/sets which serial events to listen for as defined by this class
|
||||
private final native int waitForEvent(long portHandle); // Waits for serial event to occur as specified in eventFlags
|
||||
|
@ -724,7 +723,7 @@ public final class SerialPort
|
|||
if (isOpened)
|
||||
{
|
||||
try { Thread.sleep(200); } catch (Exception e) {}
|
||||
configFlowControl(portHandle);
|
||||
configPort(portHandle);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue