001package jmri.jmrix.tmcc.serialdriver;
002
003import java.util.Arrays;
004import jmri.jmrix.tmcc.SerialPortController;
005import jmri.jmrix.tmcc.SerialTrafficController;
006import jmri.jmrix.tmcc.TmccSystemConnectionMemo;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * Provide access to TMCC via a serial com port. Normally controlled by the
012 * tmcc.serialdriver.SerialDriverFrame class.
013 *
014 * @author Bob Jacobsen Copyright (C) 2006
015 */
016public class SerialDriverAdapter extends SerialPortController {
017
018    public SerialDriverAdapter() {
019        super(new TmccSystemConnectionMemo());
020        this.manufacturerName = jmri.jmrix.tmcc.SerialConnectionTypeList.LIONEL;
021    }
022
023    @Override
024    public String openPort(String portName, String appName) {
025    
026    
027        // get and open the primary port
028        currentSerialPort = activatePort(portName, log);
029        if (currentSerialPort == null) {
030            log.error("failed to connect TMCC to {}", portName);
031            return Bundle.getMessage("SerialPortNotFound", portName);
032        }
033        log.info("Connecting TMCC to {} {}", portName, currentSerialPort);
034        
035        // try to set it for communication via SerialDriver
036        // find the baud rate value, configure comm options
037        int baud = currentBaudNumber(mBaudRate);
038        setBaudRate(currentSerialPort, baud);
039        configureLeads(currentSerialPort, true, true);
040        setFlowControl(currentSerialPort, FlowControl.NONE);
041
042        // report status
043        reportPortStatus(log, portName);
044
045        opened = true;
046
047        return null; // indicates OK return
048    
049
050    }
051
052    /**
053     * Can the port accept additional characters?
054     *
055     * @return true
056     */
057    public boolean okToSend() {
058        return true;
059    }
060
061    /**
062     * Set up all of the other objects to operate connected to this port.
063     */
064    @Override
065    public void configure() {
066        // connect to the traffic controller
067        log.debug("set tc for memo {}", getSystemConnectionMemo().getUserName());
068        SerialTrafficController control = new SerialTrafficController(getSystemConnectionMemo());
069        control.connectPort(this);
070        this.getSystemConnectionMemo().setTrafficController(control);
071        // do the common manager config
072        this.getSystemConnectionMemo().configureManagers();
073    }
074
075    // Base class methods for the SerialPortController interface
076
077    @Override
078    public boolean status() {
079        return opened;
080    }
081
082    /**
083     * {@inheritDoc}
084     */
085    @Override
086    public String[] validBaudRates() {
087        return Arrays.copyOf(validSpeeds, validSpeeds.length);
088    }
089
090    /**
091     * {@inheritDoc}
092     */
093    @Override
094    public int[] validBaudNumbers() {
095        return Arrays.copyOf(validSpeedValues, validSpeedValues.length);
096    }
097
098    protected String[] validSpeeds = new String[]{Bundle.getMessage("Baud9600"),
099            Bundle.getMessage("Baud19200"), Bundle.getMessage("Baud57600")};
100    protected int[] validSpeedValues = new int[]{9600, 19200, 57600};
101
102    @Override
103    public int defaultBaudIndex() {
104        return 0;
105    }
106
107    /**
108     * Get an array of valid values for "option 2"; used to display valid
109     * options. May not be null, but may have zero entries.
110     *
111     * @return a single element array containing an empty string
112     */
113    public String[] validOption2() {
114        return new String[]{""};
115    }
116
117    /**
118     * Get a String that says what Option 2 represents May be an empty string,
119     * but will not be null.
120     *
121     * @return an empty string
122     */
123    public String option2Name() {
124        return "";
125    }
126
127    // private control members
128
129    private final static Logger log = LoggerFactory.getLogger(SerialDriverAdapter.class);
130
131}