Skip to content

Commit

Permalink
Address #175
Browse files Browse the repository at this point in the history
NRSerialPort wrapper now deals with remote RFC2217 devices as the port
identifyier.
  • Loading branch information
madhephaestus committed May 9, 2020
1 parent 09a9a16 commit 61746b7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
43 changes: 35 additions & 8 deletions src/main/java/gnu/io/NRSerialPort.java
Expand Up @@ -57,19 +57,22 @@
--------------------------------------------------------------------------*/
package gnu.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.TooManyListenersException;

import gnu.io.factory.RFC2217PortCreator;
import gnu.io.factory.RxTxPortCreator;
import gnu.io.rfc2217.TelnetSerialPort;

public class NRSerialPort
{

private RXTXPort serial;
private SerialPort serial;
private String port = null;
private boolean connected = false;
private int baud = 115200;
Expand All @@ -96,7 +99,10 @@ public boolean connect()

try
{
serial = new RxTxPortCreator().createPort(port);
if(port.toLowerCase().startsWith("rfc2217"))
serial = new RFC2217PortCreator().createPort(port);
else
serial = new RxTxPortCreator().createPort(port);
serial.setSerialPortParams(getBaud(), SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
setConnected(true);
}
Expand All @@ -121,13 +127,21 @@ public boolean connect()

public InputStream getInputStream()
{
return serial.getInputStream();
try {
return serial.getInputStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
}


public OutputStream getOutputStream()
{
return serial.getOutputStream();
try {
return serial.getOutputStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
}


Expand Down Expand Up @@ -245,8 +259,9 @@ public int getBaud()
public int enableRs485(boolean busEnableActiveLow, int delayBusEnableBeforeSendMs, int delayBusEnableAfterSendMs) {
if(serial == null)
return -1;

return serial.enableRs485(busEnableActiveLow, delayBusEnableBeforeSendMs, delayBusEnableAfterSendMs);
if(RXTXPort.class.isInstance(serial))
return ((RXTXPort) serial).enableRs485(busEnableActiveLow, delayBusEnableBeforeSendMs, delayBusEnableAfterSendMs);
return -1;
}

public void notifyOnDataAvailable(boolean b)
Expand All @@ -273,7 +288,19 @@ public void removeEventListener()
*/
public RXTXPort getSerialPortInstance()
{
return serial;
if(RXTXPort.class.isInstance(serial))
return (RXTXPort) serial;
return null;
}
/**
* Gets the {@link SerialPort} instance.
* This will return null until {@link #connect()} is successfully called.
* @return The {@link SerialPort} instance or null.
*/
public TelnetSerialPort getTelnetSerialPortInstance()
{
if(TelnetSerialPort.class.isInstance(serial))
return (TelnetSerialPort) serial;
return null;
}

}
15 changes: 15 additions & 0 deletions test/src/test/Rfc2217Test.java
@@ -0,0 +1,15 @@
package test;

import gnu.io.NRSerialPort;

public class Rfc2217Test {

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
NRSerialPort serial = new NRSerialPort("rfc2217://192.168.1.1:2001", 115200);
serial.connect();
Thread.sleep(2000);
serial.disconnect();
}

}

0 comments on commit 61746b7

Please sign in to comment.