Add ability to retrieve DCD line status

This commit is contained in:
hedgecrw85 2019-03-07 13:57:54 -06:00
parent 4f4d9ebd7b
commit 11896f44cc
35 changed files with 91 additions and 40 deletions

View File

@ -5,7 +5,7 @@ apply plugin: 'osgi'
group = 'com.fazecast'
archivesBaseName = 'jSerialComm'
version = '2.4.1'
version = '2.4.2'
ext.moduleName = 'com.fazecast.jSerialComm'
assert hasProperty('java6Home'): "Set the property 'java6Home' in your gradle.properties file pointing to a Java 6 JDK installation"

View File

@ -2,7 +2,7 @@
* SerialPort_Android.c
*
* Created on: Mar 13, 2015
* Last Updated on: Feb 11, 2019
* Last Updated on: Mar 07, 2019
* Author: Will Hedgecock
*
* Copyright (C) 2012-2019 Fazecast, Inc.
@ -671,4 +671,12 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDSR(JNIEn
return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_LE);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDCD(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = 0;
return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_CAR);
}
#endif

View File

@ -253,6 +253,14 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCTS
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDSR
(JNIEnv *, jobject, jlong);
/*
* Class: com_fazecast_jSerialComm_SerialPort
* Method: getDCD
* Signature: (J)Z
*/
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDCD
(JNIEnv *, jobject, jlong);
#ifdef __cplusplus
}
#endif

View File

@ -2,10 +2,10 @@
* SerialPort_Posix.c
*
* Created on: Feb 25, 2012
* Last Updated on: Dec 07, 2018
* Last Updated on: Mar 07, 2019
* Author: Will Hedgecock
*
* Copyright (C) 2012-2018 Fazecast, Inc.
* Copyright (C) 2012-2019 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
@ -28,6 +28,7 @@
#include <poll.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <termios.h>
@ -247,35 +248,44 @@ 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
struct termios options = { 0 };
fcntl(serialPortFD, F_SETFL, 0);
tcgetattr(serialPortFD, &options);
#if defined(__sun__)
options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
options.c_oflag &= ~OPOST;
options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
options.c_cflag &= ~(CSIZE | PARENB);
options.c_cflag |= CS8;
#else
cfmakeraw(&options);
#endif
if (!isDtrEnabled || !isRtsEnabled)
options.c_cflag &= ~HUPCL;
options.c_iflag |= BRKINT;
tcsetattr(serialPortFD, TCSANOW, &options);
// Configure the port parameters and timeouts
if (Java_com_fazecast_jSerialComm_SerialPort_configPort(env, obj, serialPortFD))
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_TRUE);
else
// Ensure that multiple root users cannot access the device simultaneously
if (flock(serialPortFD, LOCK_EX | LOCK_NB) == -1)
{
// Close the port if there was a problem setting the parameters
ioctl(serialPortFD, TIOCNXCL);
tcdrain(serialPortFD);
while ((close(serialPortFD) == -1) && (errno != EBADF));
serialPortFD = -1;
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
}
else
{
// 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);
#if defined(__sun__)
options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
options.c_oflag &= ~OPOST;
options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
options.c_cflag &= ~(CSIZE | PARENB);
options.c_cflag |= CS8;
#else
cfmakeraw(&options);
#endif
if (!isDtrEnabled || !isRtsEnabled)
options.c_cflag &= ~HUPCL;
options.c_iflag |= BRKINT;
tcsetattr(serialPortFD, TCSANOW, &options);
// Configure the port parameters and timeouts
if (Java_com_fazecast_jSerialComm_SerialPort_configPort(env, obj, serialPortFD))
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_TRUE);
else
{
// Close the port if there was a problem setting the parameters
ioctl(serialPortFD, TIOCNXCL);
tcdrain(serialPortFD);
while ((close(serialPortFD) == -1) && (errno != EBADF));
serialPortFD = -1;
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
}
}
}
@ -482,6 +492,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNat
tcsetattr(serialPortFD, TCSANOW, &options);
// Close the port
flock(serialPortFD, LOCK_UN);
while ((close(serialPortFD) == -1) && (errno != EBADF));
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
return JNI_TRUE;
@ -782,3 +793,11 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDSR(JNIEn
int modemBits = 0;
return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_LE);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDCD(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = 0;
return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_CAR);
}

View File

@ -3,7 +3,7 @@ COMPILE = cl
LINK = link
CFLAGS = /c /O2 /GF /GL /MT /EHsc /fp:precise /J /nologo /TP
LDFLAGS = /DLL /LTCG /NOASSEMBLY /NOLOGO
INCLUDES = /I"$(JDK_HOME)/include" /I"$(JDK_HOME)/include/win32"
INCLUDES = /I"$(JDK_HOME)\include" /I"$(JDK_HOME)\include\win32"
LIBRARIES = Advapi32.lib SetupAPI.lib
DELETE = @del /q /f
RMDIR = @rd /q /s

View File

@ -2,10 +2,10 @@
* SerialPort_Windows.c
*
* Created on: Feb 25, 2012
* Last Updated on: Oct 31, 2018
* Last Updated on: Mar 07, 2019
* Author: Will Hedgecock
*
* Copyright (C) 2012-2018 Fazecast, Inc.
* Copyright (C) 2012-2019 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
@ -895,4 +895,13 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDSR(JNIEn
return GetCommModemStatus(serialPortHandle, &modemStatus) && (modemStatus & MS_DSR_ON);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDCD(JNIEnv *env, jobject obj, jlong serialPortFD)
{
HANDLE serialPortHandle = (HANDLE)serialPortFD;
if (serialPortHandle == INVALID_HANDLE_VALUE)
return JNI_FALSE;
DWORD modemStatus = 0;
return GetCommModemStatus(serialPortHandle, &modemStatus) && (modemStatus & MS_RLSD_ON);
}
#endif

View File

@ -2,7 +2,7 @@
* SerialPort.java
*
* Created on: Feb 25, 2012
* Last Updated on: Feb 11, 2019
* Last Updated on: Mar 07, 2019
* Author: Will Hedgecock
*
* Copyright (C) 2012-2019 Fazecast, Inc.
@ -42,7 +42,7 @@ import java.util.Date;
* This class provides native access to serial ports and devices without requiring external libraries or tools.
*
* @author Will Hedgecock &lt;will.hedgecock@fazecast.com&gt;
* @version 2.4.1
* @version 2.4.2
* @see java.io.InputStream
* @see java.io.OutputStream
*/
@ -520,6 +520,7 @@ public final class SerialPort
private final native boolean preclearDTR(); // Clear DTR line to 0 prior to opening
private final native boolean getCTS(long portHandle); // Returns whether the CTS signal is 1
private final native boolean getDSR(long portHandle); // Returns whether the DSR signal is 1
private final native boolean getDCD(long portHandle); // Returns whether the DCD signal is 1
/**
* Returns the number of bytes available without blocking if {@link #readBytes(byte[], long)} were to be called immediately
@ -663,6 +664,12 @@ public final class SerialPort
* @return Whether or not the DSR line is asserted.
*/
public final boolean getDSR() { return getDSR(portHandle); }
/**
* Returns whether the DCD line is currently asserted.
* @return Whether or not the DCD line is asserted.
*/
public final boolean getDCD() { return getDCD(portHandle); }
// Default Constructor
private SerialPort() {}

View File

@ -31,7 +31,7 @@ import java.util.EventListener;
* This interface must be implemented to enable simple event-based serial port I/O.
*
* @author Will Hedgecock &lt;will.hedgecock@fazecast.com&gt;
* @version 2.4.1
* @version 2.4.2
* @see java.util.EventListener
*/
public interface SerialPortDataListener extends EventListener

View File

@ -31,7 +31,7 @@ import java.util.EventObject;
* This class describes an asynchronous serial port event.
*
* @author Will Hedgecock &lt;will.hedgecock@fazecast.com&gt;
* @version 2.4.1
* @version 2.4.2
* @see java.util.EventObject
*/
public final class SerialPortEvent extends EventObject

View File

@ -31,7 +31,7 @@ import java.io.IOException;
* This class describes a serial port IO exception.
*
* @author Will Hedgecock &lt;will.hedgecock@fazecast.com&gt;
* @version 2.4.1
* @version 2.4.2
* @see java.io.IOException
*/
public final class SerialPortIOException extends IOException

View File

@ -31,7 +31,7 @@ package com.fazecast.jSerialComm;
* <i>Note</i>: Using this interface will negate any serial port read timeout settings since they make no sense in an asynchronous context.
*
* @author Will Hedgecock &lt;will.hedgecock@fazecast.com&gt;
* @version 2.4.1
* @version 2.4.2
* @see com.fazecast.jSerialComm.SerialPortDataListener
* @see java.util.EventListener
*/

View File

@ -31,7 +31,7 @@ import java.io.InterruptedIOException;
* This class describes a serial port timeout exception.
*
* @author Will Hedgecock &lt;will.hedgecock@fazecast.com&gt;
* @version 2.4.1
* @version 2.4.2
* @see java.io.InterruptedIOException
*/
public final class SerialPortTimeoutException extends InterruptedIOException