Package jmri.jmrix
Class AbstractSerialPortController
- java.lang.Object
-
- jmri.jmrix.AbstractPortController
-
- jmri.jmrix.AbstractSerialPortController
-
- All Implemented Interfaces:
PortAdapter
,SerialPortAdapter
- Direct Known Subclasses:
AcelaPortController
,BiDiBSerialPortController
,CdBPortController
,Dcc4PcPortController
,DCCppSerialPortController
,DCCppSimulatorPortController
,EasyDccPortController
,GcPortController
,IEEE802154PortController
,InternalAdapter
,LnPortController
,MrcPortController
,Mx1PortController
,NcePortController
,Port
,PortController
,PortController
,QsiPortController
,RfidPortController
,SerialAdapter
,SerialPortAdapter
,SerialPortController
,SerialPortController
,SerialPortController
,SerialPortController
,SerialPortController
,SerialPortController
,SerialSensorAdapter
,SpeedoPortController
,SprogPortController
,TamsPortController
,XNetSerialPortController
,XNetSimulatorPortController
,XpaPortController
public abstract class AbstractSerialPortController extends AbstractPortController implements SerialPortAdapter
Provide an abstract base for *PortController classes.This is complicated by the lack of multiple inheritance. SerialPortAdapter is an Interface, and its implementing classes also inherit from various PortController types. But we want some common behaviors for those, so we put them here.
- See Also:
SerialPortAdapter
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class jmri.jmrix.AbstractPortController
AbstractPortController.Option
-
-
Field Summary
Fields Modifier and Type Field Description protected java.lang.String
mBaudRate
protected java.lang.String
mPort
(package private) java.util.Vector<java.lang.String>
portNameVector
-
Fields inherited from class jmri.jmrix.AbstractPortController
allowConnectionRecovery, manufacturerName, opened, option1Name, option2Name, option3Name, option4Name, options, reconnectinterval, reconnectMaxAttempts, reconnectMaxInterval
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractSerialPortController(SystemConnectionMemo connectionMemo)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
closeConnection()
Abstract class for controllers to close the connection.void
configureBaudRate(java.lang.String rate)
Set the baud rate description by port speed description.void
configureBaudRateFromIndex(int index)
Set the baud rate description by index (integer) from validBaudRates[].void
configureBaudRateFromNumber(java.lang.String indexString)
Set the baud rate description by port speed number (as a string) from validBaudRates[].protected void
configureLeadsAndFlowControl(purejavacomm.SerialPort serialPort, int flow)
Set the flow control, while also setting RTS and DTR to active.protected void
configureLeadsAndFlowControl(purejavacomm.SerialPort serialPort, int flow, boolean rts, boolean dtr)
Set the control leads and flow control.void
connect()
Open the connection.int
currentBaudNumber(java.lang.String currentBaudRate)
Convert a baud rate I18N String to an int number, e.g. "9,600 baud" to 9600.int
defaultBaudIndex()
Get the index of the default port speed for this adapter from the validSpeeds and validRates arrays.int
getCurrentBaudIndex()
java.lang.String
getCurrentBaudNumber()
To store as XML attribute, get a string to represent current port speed.java.lang.String
getCurrentBaudRate()
java.lang.String
getCurrentPortName()
java.util.Vector<java.lang.String>
getPortNames()
Provide a vector of valid port names, each a String.java.lang.String
handlePortBusy(purejavacomm.PortInUseException p, java.lang.String portName, org.slf4j.Logger log)
Standard error handling for port-busy case.java.lang.String
handlePortNotFound(purejavacomm.NoSuchPortException p, java.lang.String portName, org.slf4j.Logger log)
Standard error handling for port-not-found case.protected void
reconnectFromLoop(int retryNum)
Abstract class for ports to attempt a single re-connection attempt.protected void
resetupConnection()
Re-setup the connection.void
setPort(java.lang.String port)
Remember the associated port name.protected void
setPortEventLogging(purejavacomm.SerialPort port)
Set event logging.int[]
validBaudNumbers()
Get an array of valid baud rate numbers; used to store/load adapter speed option.java.lang.String[]
validBaudRates()
Get an array of valid baud rate strings; used to display valid options in Connections Preferences.-
Methods inherited from class jmri.jmrix.AbstractPortController
configureOption1, configureOption2, configureOption3, configureOption4, dispose, getDisabled, getInputStream, getManufacturer, getOption1Name, getOption2Name, getOption3Name, getOption4Name, getOptionChoices, getOptionDisplayName, getOptions, getOptionState, getOutputStream, getReconnectMaxAttempts, getReconnectMaxInterval, getSystemConnectionMemo, getSystemPrefix, getUserName, isDirty, isOptionAdvanced, isOptionTypePassword, isOptionTypeText, isRestartRequired, purgeStream, reconnect, recover, safeSleep, setClosed, setDisabled, setManufacturer, setOpened, setOptionState, setReconnectMaxAttempts, setReconnectMaxInterval, setSystemConnectionMemo, setSystemPrefix, setUserName, status
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface jmri.jmrix.PortAdapter
dispose, getDisabled, getInputStream, getOption1Name, getOption2Name, getOption3Name, getOption4Name, getOptionChoices, getOptionDisplayName, getOptions, getOptionState, getOutputStream, getReconnectMaxAttempts, getReconnectMaxInterval, getSystemConnectionMemo, getSystemPrefix, getUserName, isDirty, isOptionAdvanced, isOptionTypePassword, isOptionTypeText, isRestartRequired, recover, setDisabled, setOptionState, setReconnectMaxAttempts, setReconnectMaxInterval, setSystemConnectionMemo, setSystemPrefix, setUserName
-
Methods inherited from interface jmri.jmrix.SerialPortAdapter
configure, configureOption1, configureOption2, configureOption3, configureOption4, getManufacturer, openPort, setManufacturer, status
-
-
-
-
Field Detail
-
mPort
protected java.lang.String mPort
-
mBaudRate
protected java.lang.String mBaudRate
-
portNameVector
java.util.Vector<java.lang.String> portNameVector
-
-
Constructor Detail
-
AbstractSerialPortController
protected AbstractSerialPortController(SystemConnectionMemo connectionMemo)
-
-
Method Detail
-
handlePortBusy
public java.lang.String handlePortBusy(purejavacomm.PortInUseException p, java.lang.String portName, org.slf4j.Logger log)
Standard error handling for port-busy case.- Specified by:
handlePortBusy
in interfaceSerialPortAdapter
- Parameters:
p
- the exception being handled, if additional information from it is desiredportName
- name of the port being accessedlog
- where to log a status message- Returns:
- Localized message, in case separate presentation to user is desired
- See Also:
AbstractSerialPortController
-
handlePortNotFound
public java.lang.String handlePortNotFound(purejavacomm.NoSuchPortException p, java.lang.String portName, org.slf4j.Logger log)
Standard error handling for port-not-found case.- Parameters:
p
- no such port exception.portName
- port name.log
- system log.- Returns:
- human readable string with error detail.
-
connect
public void connect() throws java.io.IOException
Open the connection.- Specified by:
connect
in interfacePortAdapter
- Throws:
java.io.IOException
- if unable to connect
-
setPort
public void setPort(java.lang.String port)
Remember the associated port name.- Specified by:
setPort
in interfaceSerialPortAdapter
- Parameters:
port
- name of the port
-
getCurrentPortName
public java.lang.String getCurrentPortName()
- Specified by:
getCurrentPortName
in interfacePortAdapter
- Specified by:
getCurrentPortName
in interfaceSerialPortAdapter
- Specified by:
getCurrentPortName
in classAbstractPortController
-
configureLeadsAndFlowControl
protected void configureLeadsAndFlowControl(purejavacomm.SerialPort serialPort, int flow, boolean rts, boolean dtr)
Set the control leads and flow control. This handles any necessary ordering.- Parameters:
serialPort
- Port to be updatedflow
- flow control mode from (@link purejavacomm.SerialPort}rts
- set RTS active if truedtr
- set DTR active if true
-
configureLeadsAndFlowControl
protected void configureLeadsAndFlowControl(purejavacomm.SerialPort serialPort, int flow)
Set the flow control, while also setting RTS and DTR to active.- Parameters:
serialPort
- Port to be updatedflow
- flow control mode from (@link purejavacomm.SerialPort}
-
configureBaudRate
public void configureBaudRate(java.lang.String rate)
Set the baud rate description by port speed description.Only to be used after construction, but before the openPort call.
- Specified by:
configureBaudRate
in interfaceSerialPortAdapter
- Parameters:
rate
- the baud rate as I18N description, eg. "28,800 baud"
-
configureBaudRateFromNumber
public void configureBaudRateFromNumber(java.lang.String indexString)
Set the baud rate description by port speed number (as a string) from validBaudRates[].Only to be used after construction, but before the openPort call.
- Specified by:
configureBaudRateFromNumber
in interfaceSerialPortAdapter
- Parameters:
indexString
- the port speed as unformatted number string, eg. "28800"
-
configureBaudRateFromIndex
public void configureBaudRateFromIndex(int index)
Set the baud rate description by index (integer) from validBaudRates[]. Invalid indexes are ignored.- Specified by:
configureBaudRateFromIndex
in interfaceSerialPortAdapter
- Parameters:
index
- the index to select from speeds[] array
-
defaultBaudIndex
public int defaultBaudIndex()
Description copied from interface:SerialPortAdapter
Get the index of the default port speed for this adapter from the validSpeeds and validRates arrays.- Specified by:
defaultBaudIndex
in interfaceSerialPortAdapter
- Returns:
- -1 to indicate not supported, unless overridden in adapter
-
getCurrentBaudRate
public java.lang.String getCurrentBaudRate()
- Specified by:
getCurrentBaudRate
in interfaceSerialPortAdapter
-
getCurrentBaudNumber
public java.lang.String getCurrentBaudNumber()
To store as XML attribute, get a string to represent current port speed.- Specified by:
getCurrentBaudNumber
in interfaceSerialPortAdapter
- Returns:
- speed as number string
-
getCurrentBaudIndex
public int getCurrentBaudIndex()
- Specified by:
getCurrentBaudIndex
in interfaceSerialPortAdapter
-
validBaudRates
public java.lang.String[] validBaudRates()
Get an array of valid baud rate strings; used to display valid options in Connections Preferences.- Specified by:
validBaudRates
in interfaceSerialPortAdapter
- Returns:
- array of I18N display strings of port speed settings valid for this serial adapter,
must match order and values from
SerialPortAdapter.validBaudNumbers()
-
validBaudNumbers
public int[] validBaudNumbers()
Get an array of valid baud rate numbers; used to store/load adapter speed option.- Specified by:
validBaudNumbers
in interfaceSerialPortAdapter
- Returns:
- integer array of speeds, must match order and values from
SerialPortAdapter.validBaudRates()
-
currentBaudNumber
public int currentBaudNumber(java.lang.String currentBaudRate)
Convert a baud rate I18N String to an int number, e.g. "9,600 baud" to 9600.Uses the validBaudNumbers() and validBaudRates() methods to do this.
- Parameters:
currentBaudRate
- a rate from validBaudRates()- Returns:
- baudrate as integer if available and matching first digits in currentBaudRate, 0 if baudrate not supported by this adapter, -1 if no match (configuration system should prevent this)
-
setPortEventLogging
protected void setPortEventLogging(purejavacomm.SerialPort port)
Set event logging.- Parameters:
port
- Serial port to configure
-
getPortNames
public java.util.Vector<java.lang.String> getPortNames()
Provide a vector of valid port names, each a String.- Specified by:
getPortNames
in interfaceSerialPortAdapter
- Returns:
- port names.
-
closeConnection
protected void closeConnection()
Abstract class for controllers to close the connection. Called prior to any re-connection attempts. Each serial port adapter should handle this and it should be abstract.- Overrides:
closeConnection
in classAbstractPortController
-
resetupConnection
protected void resetupConnection()
Re-setup the connection. Called when the physical connection has reconnected and can be linked to this connection. Each port adapter should handle this and it should be abstract.- Overrides:
resetupConnection
in classAbstractPortController
-
reconnectFromLoop
protected void reconnectFromLoop(int retryNum)
Abstract class for ports to attempt a single re-connection attempt. Called from within main reconnect thread. Attempts a re-connection to the serial port from the main reconnect thread.- Overrides:
reconnectFromLoop
in classAbstractPortController
- Parameters:
retryNum
- Reconnection attempt number.
-
-