Class AbstractSerialPortController
- All Implemented Interfaces:
PortAdapter,SerialPortAdapter
- Direct Known Subclasses:
AcelaPortController,BiDiBSerialPortController,CdBPortController,Dcc4PcPortController,DCCppSerialPortController,DCCppSimulatorPortController,DemoSerialPort,EasyDccPortController,GcPortController,IEEE802154PortController,InternalAdapter,LnPortController,MrcPortController,Mx1PortController,NcePortController,Port,PortController,PortController,QsiPortController,RfidPortController,SerialAdapter,SerialPortAdapter,SerialPortController,SerialPortController,SerialPortController,SerialPortController,SerialPortController,SerialPortController,SpeedoPortController,SprogPortController,TamsPortController,XNetSerialPortController,XNetSimulatorPortController,XpaPortController
The intent is to hide, to the extent possible, all the references to the actual serial library in use within this class. Subclasses then rely on methods here to maniplate the content of the protected currentSerialPort variable/
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnumerate the possible timeout choicesstatic enumEnumerate the possible flow control choicesNested classes/interfaces inherited from class jmri.jmrix.AbstractPortController
AbstractPortController.Option -
Field Summary
FieldsFields inherited from class jmri.jmrix.AbstractPortController
allowConnectionRecovery, manufacturerName, opened, option1Name, option2Name, option3Name, option4Name, options, reconnectinterval, reconnectMaxAttempts, reconnectMaxInterval -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractSerialPortController(SystemConnectionMemo connectionMemo) -
Method Summary
Modifier and TypeMethodDescriptionstatic SerialPortactivatePort(String systemPrefix, String portName, org.slf4j.Logger log, int stop_bits, SerialPort.Parity parity) Do the formal opening of the port, set the port for blocking reads without timeout, set the port to 8 data bits, the indicated number of stop bits and parity, and purge the port's input stream.protected final SerialPortactivatePort(String portName, org.slf4j.Logger log) Do the formal opening of the port, set the port for blocking reads without timeout, set the port to 8 data bits, 1 stop bit, no parity and purge the port's input stream.protected final SerialPortactivatePort(String portName, org.slf4j.Logger log, int stop_bits) Do the formal opening of the port, set the port for blocking reads without timeout, set the port to 8 data bits, the indicated number of stop bits, no parity, and purge the port's input stream.protected voidAbstract class for controllers to close the connection.protected final voidcloseSerialPort(SerialPort serialPort) Cleanly close the specified portfinal voidconfigureBaudRate(String rate) Set the baud rate description by port speed description.final voidconfigureBaudRateFromIndex(int index) Set the baud rate description by index (integer) from validBaudRates[].final voidconfigureBaudRateFromNumber(String indexString) Set the baud rate description by port speed number (as a string) from validBaudRates[].protected final voidconfigureLeads(SerialPort serialPort, boolean rts, boolean dtr) Set the control leads.protected final voidconfigureLeadsAndFlowControl(SerialPort serialPort, int flow) Set the flow control for jmri.jmrix.purejavacomm, while also setting RTS and DTR to active.protected voidconfigureLeadsAndFlowControl(SerialPort serialPort, int flow, boolean rts, boolean dtr) Set the control leads and flow control for jmri.jmrix.purejavacomm.voidconnect()Open the connection.final intcurrentBaudNumber(String currentBaudRate) Convert a baud rate I18N String to an int number, e.g. "9,600 baud" to 9600.intGet the index of the default port speed for this adapter from the validSpeeds and validRates arrays.Provide the actual serial port names.final intfinal StringTo store as XML attribute, get a string to represent current port speed.Overridden in simulator adapter classes to return "";protected final AbstractSerialPortController.FlowControlgetFlowControl(SerialPort serialPort) get the flow control mode back from the actual port.Get the InputStream from the port.Get the outputStream to the port.Get a string with the serial port settings.handlePortBusy(PortInUseException p, String portName, org.slf4j.Logger log) Standard error handling for jmri.jmrix.purejavacomm port-busy case.static StringhandlePortNotFound(String systemPrefix, String portName, org.slf4j.Logger log, Exception ex) Standard error handling for the general port-not-found case.handlePortNotFound(NoSuchPortException p, String portName, org.slf4j.Logger log) Specific error handling for jmri.jmrix.purejavacomm port-not-found case.booleanIs the serial port open?voidReplace the serial port with a fake serial port and close the old serial port.protected final voidreportPortStatus(org.slf4j.Logger log, String portName) Report the connection status.protected voidRe-setup the connection.protected final voidsetBaudRate(SerialPort serialPort, int baud) Set the baud rate on the portprotected final voidsetComPortTimeouts(SerialPort serialPort, AbstractSerialPortController.Blocking blocking, int timeout) protected final voidsetDataListener(SerialPort serialPort, SerialPortDataListener serialPortDataListener) Add a data listener to the specified portprotected final voidsetFlowControl(SerialPort serialPort, AbstractSerialPortController.FlowControl flow) Configure the flow control settings.voidRemember the associated port name.int[]Get an array of valid baud rate numbers; used to store/load adapter speed option.String[]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, getManufacturer, getOption1Name, getOption2Name, getOption3Name, getOption4Name, getOptionChoices, getOptionDisplayName, getOptions, getOptionState, getReconnectMaxAttempts, getReconnectMaxInterval, getSystemConnectionMemo, getSystemPrefix, getUserName, isDirty, isOptionAdvanced, isOptionTypePassword, isOptionTypeText, isRestartRequired, purgeStream, reconnect, reconnectFromLoop, recover, safeSleep, setClosed, setDisabled, setManufacturer, setOpened, setOptionState, setReconnectMaxAttempts, setReconnectMaxInterval, setSystemConnectionMemo, setSystemPrefix, setUserName, statusMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface jmri.jmrix.PortAdapter
dispose, getDisabled, getOption1Name, getOption2Name, getOption3Name, getOption4Name, getOptionChoices, getOptionDisplayName, getOptions, getOptionState, getReconnectMaxAttempts, getReconnectMaxInterval, getSystemConnectionMemo, getSystemPrefix, getUserName, isDirty, isOptionAdvanced, isOptionTypePassword, isOptionTypeText, isRestartRequired, recover, setDisabled, setOptionState, setReconnectMaxAttempts, setReconnectMaxInterval, setSystemConnectionMemo, setSystemPrefix, setUserNameMethods inherited from interface jmri.jmrix.SerialPortAdapter
configure, configureOption1, configureOption2, configureOption3, configureOption4, getManufacturer, getPortNames, openPort, setManufacturer, status
-
Field Details
-
currentSerialPort
-
mPort
-
mBaudRate
-
-
Constructor Details
-
AbstractSerialPortController
-
-
Method Details
-
handlePortBusy
Standard error handling for jmri.jmrix.purejavacomm port-busy case.- 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
-
handlePortNotFound
Specific error handling for jmri.jmrix.purejavacomm port-not-found case.- Parameters:
p- no such port exception.portName- port name.log- system log.- Returns:
- human readable string with error detail.
-
handlePortNotFound
public static String handlePortNotFound(String systemPrefix, String portName, org.slf4j.Logger log, Exception ex) Standard error handling for the general port-not-found case.- Parameters:
systemPrefix- the system prefixportName- port name.log- system log, passed so logging comes from bottom level classex- Underlying Exception that caused this failure- Returns:
- human readable string with error detail.
-
connect
Open the connection.- Specified by:
connectin interfacePortAdapter- Throws:
IOException- if unable to connect
-
activatePort
Do the formal opening of the port, set the port for blocking reads without timeout, set the port to 8 data bits, 1 stop bit, no parity and purge the port's input stream.Does not do the rest of the setup implied in the
SerialPortAdapter.openPort(java.lang.String, java.lang.String)method. This is usually followed by calls tosetBaudRate(jmri.jmrix.SerialPort, int),configureLeads(jmri.jmrix.SerialPort, boolean, boolean)andsetFlowControl(jmri.jmrix.SerialPort, jmri.jmrix.AbstractSerialPortController.FlowControl).- Parameters:
portName- local system name for the desired portlog- Logger to use for errors, passed so that errors are logged from low-level class- Returns:
- the serial port object for later use
-
activatePort
Do the formal opening of the port, set the port for blocking reads without timeout, set the port to 8 data bits, the indicated number of stop bits, no parity, and purge the port's input stream.Does not do the rest of the setup implied in the
SerialPortAdapter.openPort(java.lang.String, java.lang.String)method. This is usually followed by calls tosetBaudRate(jmri.jmrix.SerialPort, int),configureLeads(jmri.jmrix.SerialPort, boolean, boolean)andsetFlowControl(jmri.jmrix.SerialPort, jmri.jmrix.AbstractSerialPortController.FlowControl).- Parameters:
portName- local system name for the desired portlog- Logger to use for errors, passed so that errors are logged from low-level class'stop_bits- The number of stop bits, either 1 or 2- Returns:
- the serial port object for later use
-
activatePort
public static SerialPort activatePort(String systemPrefix, String portName, org.slf4j.Logger log, int stop_bits, SerialPort.Parity parity) Do the formal opening of the port, set the port for blocking reads without timeout, set the port to 8 data bits, the indicated number of stop bits and parity, and purge the port's input stream.Does not do the rest of the setup implied in the
SerialPortAdapter.openPort(java.lang.String, java.lang.String)method. This is usually followed by calls tosetBaudRate(jmri.jmrix.SerialPort, int),configureLeads(jmri.jmrix.SerialPort, boolean, boolean)andsetFlowControl(jmri.jmrix.SerialPort, jmri.jmrix.AbstractSerialPortController.FlowControl).- Parameters:
systemPrefix- the system prefixportName- local system name for the desired portlog- Logger to use for errors, passed so that errors are logged from low-level class'stop_bits- The number of stop bits, either 1 or 2parity- one of the defined parity contants- Returns:
- the serial port object for later use
-
setComPortTimeouts
protected final void setComPortTimeouts(SerialPort serialPort, AbstractSerialPortController.Blocking blocking, int timeout) -
setPort
Remember the associated port name.- Specified by:
setPortin interfaceSerialPortAdapter- Parameters:
port- name of the port
-
getCurrentPortName
Overridden in simulator adapter classes to return "";- Specified by:
getCurrentPortNamein interfacePortAdapter- Specified by:
getCurrentPortNamein interfaceSerialPortAdapter- Specified by:
getCurrentPortNamein classAbstractPortController
-
getActualPortNames
Provide the actual serial port names. As a public static method, this can be accessed outside the jmri.jmrix package to get the list of names for e.g. context reports.- Returns:
- the port names in the form they can later be used to open the port
-
configureLeadsAndFlowControl
protected void configureLeadsAndFlowControl(SerialPort serialPort, int flow, boolean rts, boolean dtr) Set the control leads and flow control for jmri.jmrix.purejavacomm. This handles any necessary ordering.- Parameters:
serialPort- Port to be updatedflow- flow control mode from (@link jmri.jmrix.purejavacomm.SerialPort}rts- set RTS active if truedtr- set DTR active if true
-
setBaudRate
Set the baud rate on the port- Parameters:
serialPort- Port to be updatedbaud- baud rate to be set
-
configureLeads
Set the control leads.- Parameters:
serialPort- Port to be updatedrts- set RTS active if truedtr- set DTR active if true
-
setFlowControl
protected final void setFlowControl(SerialPort serialPort, AbstractSerialPortController.FlowControl flow) Configure the flow control settings. Keep this in synch with the FlowControl enum.- Parameters:
serialPort- Port to be updatedflow- set which kind of flow control to use
-
getFlowControl
get the flow control mode back from the actual port.- Parameters:
serialPort- Port to be examined- Returns:
- flow control setting observed in the port
-
setDataListener
protected final void setDataListener(SerialPort serialPort, SerialPortDataListener serialPortDataListener) Add a data listener to the specified port- Parameters:
serialPort- Port to be updatedserialPortDataListener- the listener to add
-
closeSerialPort
Cleanly close the specified port- Parameters:
serialPort- Port to be closed
-
configureLeadsAndFlowControl
Set the flow control for jmri.jmrix.purejavacomm, while also setting RTS and DTR to active.- Parameters:
serialPort- Port to be updatedflow- flow control mode from (@link jmri.jmrix.purejavacomm.SerialPort}
-
reportPortStatus
Report the connection status. Typically used after the connection is complete- Parameters:
log- The low-level logger to get this reported against the right classportName- low-level name of selected port
-
getInputStream
Description copied from class:AbstractPortControllerGet the InputStream from the port.- Specified by:
getInputStreamin interfacePortAdapter- Specified by:
getInputStreamin classAbstractPortController- Returns:
- the InputStream from the port
-
getOutputStream
Description copied from class:AbstractPortControllerGet the outputStream to the port.- Specified by:
getOutputStreamin interfacePortAdapter- Specified by:
getOutputStreamin classAbstractPortController- Returns:
- the outputStream to the port
-
configureBaudRate
Set the baud rate description by port speed description.Only to be used after construction, but before the openPort call.
- Specified by:
configureBaudRatein interfaceSerialPortAdapter- Parameters:
rate- the baud rate as I18N description, eg. "28,800 baud"
-
configureBaudRateFromNumber
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:
configureBaudRateFromNumberin interfaceSerialPortAdapter- Parameters:
indexString- the port speed as unformatted number string, eg. "28800"
-
configureBaudRateFromIndex
Set the baud rate description by index (integer) from validBaudRates[]. Invalid indexes are ignored.- Specified by:
configureBaudRateFromIndexin interfaceSerialPortAdapter- Parameters:
index- the index to select from speeds[] array
-
defaultBaudIndex
Description copied from interface:SerialPortAdapterGet the index of the default port speed for this adapter from the validSpeeds and validRates arrays.- Specified by:
defaultBaudIndexin interfaceSerialPortAdapter- Returns:
- -1 to indicate not supported, unless overridden in adapter
-
getCurrentBaudRate
- Specified by:
getCurrentBaudRatein interfaceSerialPortAdapter
-
getCurrentBaudNumber
To store as XML attribute, get a string to represent current port speed.- Specified by:
getCurrentBaudNumberin interfaceSerialPortAdapter- Returns:
- speed as number string
-
getCurrentBaudIndex
- Specified by:
getCurrentBaudIndexin interfaceSerialPortAdapter
-
validBaudRates
Get an array of valid baud rate strings; used to display valid options in Connections Preferences.- Specified by:
validBaudRatesin 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
Get an array of valid baud rate numbers; used to store/load adapter speed option.- Specified by:
validBaudNumbersin interfaceSerialPortAdapter- Returns:
- integer array of speeds, must match order and values from
SerialPortAdapter.validBaudRates()
-
currentBaudNumber
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)
-
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:
closeConnectionin classAbstractPortController
-
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:
resetupConnectionin classAbstractPortController
-
isPortOpen
Is the serial port open? The LocoNet simulator uses this class but doesn't open the port.- Returns:
- true if the port is open, false otherwise
-
replacePortWithFakePort
Replace the serial port with a fake serial port and close the old serial port. Note that you can only replace the port once. This call is used when you want to close the port and reopen it for some special task, for example upload firmware. -
getPortSettingsString
Get a string with the serial port settings.- Returns:
- the settings as a string
-