Skip to content

Commit

Permalink
Merge pull request #13171 from danielb987/SerialPortComplete
Browse files Browse the repository at this point in the history
Complete the move to new serial library
  • Loading branch information
bobjacobsen committed Jun 19, 2024
2 parents b967f6f + cd46624 commit 99e5227
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 1,040 deletions.
202 changes: 8 additions & 194 deletions java/src/jmri/jmrix/AbstractSerialPortController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package jmri.jmrix;

import java.io.*;
import java.util.Enumeration;
import java.util.Vector;

import jmri.SystemConnectionMemo;
Expand All @@ -27,7 +26,7 @@ protected AbstractSerialPortController(SystemConnectionMemo connectionMemo) {
protected SerialPort currentSerialPort = null;

/**
* Standard error handling for purejavacomm port-busy case.
* Standard error handling for jmri.jmrix.purejavacomm port-busy case.
*
* @param p the exception being handled, if additional information
* from it is desired
Expand All @@ -44,27 +43,7 @@ public String handlePortBusy(jmri.jmrix.purejavacomm.PortInUseException p, Strin
}

/**
* Standard error handling for purejavacomm port-busy case.
*
* @param p the exception being handled, if additional information
* from it is desired
* @param portName name of the port being accessed
* @param log where to log a status message
* @return Localized message, in case separate presentation to user is
* desired
*/
//@Deprecated(forRemoval=true) // with PureJavaComm
@Override
public String handlePortBusy(purejavacomm.PortInUseException p, String portName, org.slf4j.Logger log) {
log.error("{} port is in use: {}", portName, p.getMessage());
/*JmriJOptionPane.showMessageDialog(null, "Port is in use",
"Error", JmriJOptionPane.ERROR_MESSAGE);*/
ConnectionStatus.instance().setConnectionState(this.getSystemPrefix(), portName, ConnectionStatus.CONNECTION_DOWN);
return Bundle.getMessage("SerialPortInUse", portName);
}

/**
* Specific error handling for purejavacomm port-not-found case.
* Specific error handling for jmri.jmrix.purejavacomm port-not-found case.
* @param p no such port exception.
* @param portName port name.
* @param log system log.
Expand All @@ -77,20 +56,6 @@ public String handlePortNotFound(jmri.jmrix.purejavacomm.NoSuchPortException p,
return Bundle.getMessage("SerialPortNotFound", portName);
}

/**
* Specific error handling for purejavacomm port-not-found case.
* @param p no such port exception.
* @param portName port name.
* @param log system log.
* @return human readable string with error detail.
*/
//@Deprecated(forRemoval=true) // with PureJavaComm
public String handlePortNotFound(purejavacomm.NoSuchPortException p, String portName, org.slf4j.Logger log) {
log.error("Serial port {} not found", portName);
ConnectionStatus.instance().setConnectionState(this.getSystemPrefix(), portName, ConnectionStatus.CONNECTION_DOWN);
return Bundle.getMessage("SerialPortNotFound", portName);
}

/**
* Standard error handling for the general port-not-found case.
* @param systemPrefix the system prefix
Expand Down Expand Up @@ -265,7 +230,7 @@ public static Vector<String> getActualPortNames() {
* ordering.
*
* @param serialPort Port to be updated
* @param flow flow control mode from (@link purejavacomm.SerialPort}
* @param flow flow control mode from (@link jmri.jmrix.purejavacomm.SerialPort}
* @param rts set RTS active if true
* @param dtr set DTR active if true
*/
Expand All @@ -289,35 +254,6 @@ protected void configureLeadsAndFlowControl(jmri.jmrix.purejavacomm.SerialPort s
serialPort.setDTR(dtr);
}

/**
* Set the control leads and flow control for purejavacomm. This handles any necessary
* ordering.
*
* @param serialPort Port to be updated
* @param flow flow control mode from (@link purejavacomm.SerialPort}
* @param rts set RTS active if true
* @param dtr set DTR active if true
*/
//@Deprecated(forRemoval=true) // Removed with PureJavaComm
protected void configureLeadsAndFlowControl(purejavacomm.SerialPort serialPort, int flow, boolean rts, boolean dtr) {
// (Jan 2018) PJC seems to mix termios and ioctl access, so it's not clear
// what's preserved and what's not. Experimentally, it seems necessary
// to write the control leads, set flow control, and then write the control
// leads again.
serialPort.setRTS(rts);
serialPort.setDTR(dtr);

try {
if (flow != purejavacomm.SerialPort.FLOWCONTROL_NONE) {
serialPort.setFlowControlMode(flow);
}
} catch (purejavacomm.UnsupportedCommOperationException e) {
log.warn("Could not set flow control, ignoring");
}
if (flow!=purejavacomm.SerialPort.FLOWCONTROL_RTSCTS_OUT) serialPort.setRTS(rts); // not connected in some serial ports and adapters
serialPort.setDTR(dtr);
}

/**
* Set the baud rate on the port
*
Expand Down Expand Up @@ -488,7 +424,7 @@ final protected void closeSerialPort(SerialPort serialPort){
}

/**
* Set the flow control for purejavacomm, while also setting RTS and DTR to active.
* Set the flow control for jmri.jmrix.purejavacomm, while also setting RTS and DTR to active.
*
* @param serialPort Port to be updated
* @param flow flow control mode from (@link jmri.jmrix.purejavacomm.SerialPort}
Expand All @@ -498,17 +434,6 @@ final protected void configureLeadsAndFlowControl(jmri.jmrix.purejavacomm.Serial
configureLeadsAndFlowControl(serialPort, flow, true, true);
}

/**
* Set the flow control for purejavacomm, while also setting RTS and DTR to active.
*
* @param serialPort Port to be updated
* @param flow flow control mode from (@link purejavacomm.SerialPort}
*/
//@Deprecated(forRemoval=true) // with PureJavaComm
final protected void configureLeadsAndFlowControl(purejavacomm.SerialPort serialPort, int flow) {
configureLeadsAndFlowControl(serialPort, flow, true, true);
}

/**
* Report the connection status.
* Typically used after the connection is complete
Expand Down Expand Up @@ -781,88 +706,6 @@ final public int currentBaudNumber(String currentBaudRate) {
return -1;
}

/**
* Set event logging.
* @param port Serial port to configure
*/
//@Deprecated(forRemoval=true) // with PureJavaComm
protected void setPortEventLogging(purejavacomm.SerialPort port) {
// arrange to notify later
try {
port.addEventListener(new purejavacomm.SerialPortEventListener() {
@Override
public void serialEvent(purejavacomm.SerialPortEvent e) {
int type = e.getEventType();
switch (type) {
case purejavacomm.SerialPortEvent.DATA_AVAILABLE:
log.info("SerialEvent: DATA_AVAILABLE is {}", e.getNewValue()); // NOI18N
return;
case purejavacomm.SerialPortEvent.OUTPUT_BUFFER_EMPTY:
log.info("SerialEvent: OUTPUT_BUFFER_EMPTY is {}", e.getNewValue()); // NOI18N
return;
case purejavacomm.SerialPortEvent.CTS:
log.info("SerialEvent: CTS is {}", e.getNewValue()); // NOI18N
return;
case purejavacomm.SerialPortEvent.DSR:
log.info("SerialEvent: DSR is {}", e.getNewValue()); // NOI18N
return;
case purejavacomm.SerialPortEvent.RI:
log.info("SerialEvent: RI is {}", e.getNewValue()); // NOI18N
return;
case purejavacomm.SerialPortEvent.CD:
log.info("SerialEvent: CD is {}", e.getNewValue()); // NOI18N
return;
case purejavacomm.SerialPortEvent.OE:
log.info("SerialEvent: OE (overrun error) is {}", e.getNewValue()); // NOI18N
return;
case purejavacomm.SerialPortEvent.PE:
log.info("SerialEvent: PE (parity error) is {}", e.getNewValue()); // NOI18N
return;
case purejavacomm.SerialPortEvent.FE:
log.info("SerialEvent: FE (framing error) is {}", e.getNewValue()); // NOI18N
return;
case purejavacomm.SerialPortEvent.BI:
log.info("SerialEvent: BI (break interrupt) is {}", e.getNewValue()); // NOI18N
return;
default:
log.info("SerialEvent of unknown type: {} value: {}", type, e.getNewValue()); // NOI18N
}
}
}
);
} catch (java.util.TooManyListenersException ex) {
log.warn("cannot set listener for SerialPortEvents; was one already set?");
}

try {
port.notifyOnFramingError(true);
} catch (Exception e) {
log.debug("Could not notifyOnFramingError", e); // NOI18N
}

try {
port.notifyOnBreakInterrupt(true);
} catch (Exception e) {
log.debug("Could not notifyOnBreakInterrupt", e); // NOI18N
}

try {
port.notifyOnParityError(true);
} catch (Exception e) {
log.debug("Could not notifyOnParityError", e); // NOI18N
}

try {
port.notifyOnOverrunError(true);
} catch (Exception e) {
log.debug("Could not notifyOnOverrunError", e); // NOI18N
}

port.notifyOnCarrierDetect(true);
port.notifyOnCTS(true);
port.notifyOnDSR(true);
}

/**
* {@inheritDoc}
* Each serial port adapter should handle this and it should be abstract.
Expand All @@ -879,39 +722,6 @@ protected void closeConnection(){}
@Override
protected void resetupConnection(){}

/**
* {@inheritDoc}
* Attempts a re-connection to the serial port from the main reconnect
* thread.
*/
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings( value="SLF4J_FORMAT_SHOULD_BE_CONST",
justification="I18N of Info Message")
//@Deprecated(forRemoval=true) // with purejavacomm
@Override
protected void reconnectFromLoop(int retryNum){
try {
log.info("Retrying Connection attempt {} for {}", retryNum,mPort);
Enumeration<purejavacomm.CommPortIdentifier> portIDs = purejavacomm.CommPortIdentifier.getPortIdentifiers();
while (portIDs.hasMoreElements()) {
purejavacomm.CommPortIdentifier id = portIDs.nextElement();
// filter out line printers
if (id.getPortType() != purejavacomm.CommPortIdentifier.PORT_PARALLEL) // accumulate the names in a vector
{
if (id.getName().equals(mPort)) {
log.info(Bundle.getMessage("ReconnectPortReAppear", mPort));
openPort(mPort, "jmri");
}
}
}
if (retryNum % 10==0) {
log.info(Bundle.getMessage("ReconnectSerialTip"));
}
} catch (RuntimeException e) {
log.warn(Bundle.getMessage("ReconnectFail",(mPort == null ? "null" : mPort)));

}
}


public static class SerialPort {

Expand Down Expand Up @@ -1005,6 +815,10 @@ public boolean getDCD() {
return this.serialPort.getDCD();
}

public boolean getRI() {
return this.serialPort.getRI();
}

/**
* Configure the flow control settings. Keep this in synch with the
* FlowControl enum.
Expand Down
14 changes: 0 additions & 14 deletions java/src/jmri/jmrix/SerialPortAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,6 @@ default Vector<String> getPortNames() {
@Override
void configureOption4(String value);

/**
* Error handling for busy port at open.
*
* @param p the exception being handled, if additional information
* from it is desired.
* @param portName name of the port being accessed.
* @param log where to log a status message.
* @return Localized message, in case separate presentation to user is
* desired.
* @see jmri.jmrix.AbstractSerialPortController
*/
//@Deprecated(forRemoval=true) // with PureJavaComm
String handlePortBusy(purejavacomm.PortInUseException p, String portName, org.slf4j.Logger log);

/**
* Get the System Manufacturers Name.
*/
Expand Down
2 changes: 1 addition & 1 deletion java/src/jmri/jmrix/dcc4pc/Dcc4PcTrafficController.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private void forwardToPort(AbstractMRMessage m) {
portWarn(e);
}
}
purejavacomm.SerialPort port;
jmri.jmrix.purejavacomm.SerialPort port;

@Override
public void connectPort(jmri.jmrix.AbstractPortController p) {
Expand Down
22 changes: 11 additions & 11 deletions java/src/jmri/jmrix/dcc4pc/serialdriver/SerialDriverAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import purejavacomm.CommPortIdentifier;
import purejavacomm.NoSuchPortException;
import purejavacomm.PortInUseException;
import purejavacomm.UnsupportedCommOperationException;
import jmri.jmrix.purejavacomm.CommPortIdentifier;
import jmri.jmrix.purejavacomm.NoSuchPortException;
import jmri.jmrix.purejavacomm.PortInUseException;
import jmri.jmrix.purejavacomm.UnsupportedCommOperationException;

/**
* Implements SerialPortAdapter for the Dcc4Pc system.
Expand All @@ -37,25 +37,25 @@ public SerialDriverAdapter() {
setManufacturer(Dcc4PcConnectionTypeList.DCC4PC);
}

purejavacomm.SerialPort activeSerialPort = null;
jmri.jmrix.purejavacomm.SerialPort activeSerialPort = null;

@Override
public String openPort(String portName, String appName) {

try {
CommPortIdentifier portID = CommPortIdentifier.getPortIdentifier(portName);
try {
activeSerialPort = (purejavacomm.SerialPort) portID.open(appName, 2000);
activeSerialPort = portID.open(appName, 2000);
} catch (PortInUseException p) {
return handlePortBusy(p, portName, log);
}
try {
activeSerialPort.setSerialPortParams(115200,
purejavacomm.SerialPort.DATABITS_8,
purejavacomm.SerialPort.STOPBITS_1,
purejavacomm.SerialPort.PARITY_NONE);
jmri.jmrix.purejavacomm.SerialPort.DATABITS_8,
jmri.jmrix.purejavacomm.SerialPort.STOPBITS_1,
jmri.jmrix.purejavacomm.SerialPort.PARITY_NONE);
configureLeadsAndFlowControl(activeSerialPort,
purejavacomm.SerialPort.FLOWCONTROL_NONE);
jmri.jmrix.purejavacomm.SerialPort.FLOWCONTROL_NONE);
} catch (UnsupportedCommOperationException e) {
log.error("Cannot set serial parameters on port {}: {}", portName, e.getMessage());
}
Expand Down Expand Up @@ -89,7 +89,7 @@ public void setHandshake(int mode) {
}
}

public purejavacomm.SerialPort getSerialPort() {
public jmri.jmrix.purejavacomm.SerialPort getSerialPort() {
return activeSerialPort;
}

Expand Down
4 changes: 2 additions & 2 deletions java/src/jmri/jmrix/pricom/downloader/LoaderPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import javax.swing.JSeparator;
import javax.swing.JTextArea;

import purejavacomm.*;
import jmri.jmrix.purejavacomm.*;

/**
* Pane for downloading software updates to PRICOM products
Expand Down Expand Up @@ -387,7 +387,7 @@ public String openPort(String portName, String appName) {
// get and open the primary port
CommPortIdentifier portID = CommPortIdentifier.getPortIdentifier(portName);
try {
activeSerialPort = (SerialPort) portID.open(appName, 2000); // name of program, msec to wait
activeSerialPort = portID.open(appName, 2000); // name of program, msec to wait
} catch (PortInUseException p) {
handlePortBusy(p, portName);
return "Port " + p + " already in use";
Expand Down
Loading

0 comments on commit 99e5227

Please sign in to comment.