Allow returning of native error code if port does not open successfully

This commit is contained in:
Will Hedgecock 2022-01-12 10:00:19 -06:00
parent c1de299db2
commit 73596910e6
32 changed files with 17 additions and 17 deletions

View File

@ -2,7 +2,7 @@
* SerialPort_Posix.c
*
* Created on: Feb 25, 2012
* Last Updated on: Jan 04, 2022
* Last Updated on: Jan 11, 2022
* Author: Will Hedgecock
*
* Copyright (C) 2012-2022 Fazecast, Inc.
@ -315,6 +315,8 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
if (!port || (port->handle > 0))
{
(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
port->errorLineNumber = __LINE__ - 3;
port->errorNumber = (!port ? 1 : 2);
return 0;
}
@ -352,7 +354,7 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
// Return a pointer to the serial port data structure
(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
return (port->handle > 0) ? (jlong)(intptr_t)port : 0;
return (port->handle > 0) ? (jlong)(intptr_t)port : -(jlong)(intptr_t)port;
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(JNIEnv *env, jobject obj, jlong serialPortPointer)
@ -671,7 +673,7 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNative
while (close(port->handle) && (errno == EINTR))
errno = 0;
port->handle = -1;
return -1;
return 0;
}
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_bytesAvailable(JNIEnv *env, jobject obj, jlong serialPortPointer)

View File

@ -2,7 +2,7 @@
* SerialPort_Windows.c
*
* Created on: Feb 25, 2012
* Last Updated on: Jan 06, 2022
* Last Updated on: Jan 11, 2022
* Author: Will Hedgecock
*
* Copyright (C) 2012-2022 Fazecast, Inc.
@ -381,6 +381,8 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
if (!port || (port->handle != INVALID_HANDLE_VALUE))
{
(*env)->ReleaseStringChars(env, portNameJString, (const jchar*)portName);
port->errorLineNumber = __LINE__ - 3;
port->errorNumber = (!port ? 1 : 2);
return 0;
}
@ -408,7 +410,7 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
// Return a pointer to the serial port data structure
(*env)->ReleaseStringChars(env, portNameJString, (const jchar*)portName);
return (port->handle != INVALID_HANDLE_VALUE) ? (jlong)(intptr_t)port : 0;
return (port->handle != INVALID_HANDLE_VALUE) ? (jlong)(intptr_t)port : -(jlong)(intptr_t)port;
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(JNIEnv *env, jobject obj, jlong serialPortPointer)
@ -671,15 +673,10 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNative
// Close the port
port->eventListenerRunning = 0;
if (!CloseHandle(port->handle))
{
port->handle = INVALID_HANDLE_VALUE;
port->errorLineNumber = __LINE__ - 3;
port->errorNumber = GetLastError();
return 0;
}
port->errorLineNumber = __LINE__ + 1;
port->errorNumber = (!CloseHandle(port->handle) ? GetLastError() : 0);
port->handle = INVALID_HANDLE_VALUE;
return -1;
return 0;
}
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_bytesAvailable(JNIEnv *env, jobject obj, jlong serialPortPointer)

View File

@ -2,7 +2,7 @@
* SerialPort.java
*
* Created on: Feb 25, 2012
* Last Updated on: Jan 06, 2022
* Last Updated on: Jan 11, 2022
* Author: Will Hedgecock
*
* Copyright (C) 2012-2022 Fazecast, Inc.
@ -524,7 +524,7 @@ public final class SerialPort
static final public int LISTENING_EVENT_PORT_DISCONNECTED = 0x10000000;
// Serial Port Parameters
private volatile long portHandle = -1;
private volatile long portHandle = 0;
private volatile int baudRate = 9600, dataBits = 8, stopBits = ONE_STOP_BIT, parity = NO_PARITY, eventFlags = 0;
private volatile int timeoutMode = TIMEOUT_NONBLOCKING, readTimeout = 0, writeTimeout = 0, flowControl = 0;
private volatile int sendDeviceQueueSize = 4096, receiveDeviceQueueSize = 4096;
@ -687,7 +687,7 @@ public final class SerialPort
*
* @return Source line of latest native code error.
*/
public final synchronized int getLastErrorLocation() { return (portHandle > 0) ? getLastErrorLocation(portHandle) : -1; }
public final synchronized int getLastErrorLocation() { return (portHandle != 0) ? getLastErrorLocation((portHandle > 0) ? portHandle : -portHandle) : -1; }
/**
* Returns the error number returned by the most recent native source code line that failed execution.
@ -697,7 +697,7 @@ public final class SerialPort
*
* @return Error number of the latest native code error.
*/
public final synchronized int getLastErrorCode() { return (portHandle > 0) ? getLastErrorCode(portHandle) : 0; }
public final synchronized int getLastErrorCode() { return (portHandle != 0) ? getLastErrorCode((portHandle > 0) ? portHandle : -portHandle) : 0; }
// Serial Port Setup Methods
private static native void initializeLibrary(); // Initializes the JNI code

View File

@ -119,6 +119,7 @@ public class SerialPortTest
System.out.println("\nOpening " + ubxPort.getSystemPortName() + ": " + ubxPort.getDescriptivePortName() + " - " + ubxPort.getPortDescription() + ": " + openedSuccessfully);
if (!openedSuccessfully)
{
System.out.println("Error code was " + ubxPort.getLastErrorCode() + " at Line " + ubxPort.getLastErrorLocation());
inputScanner.close();
return;
}