001package jmri.jmrix.oaktree.serialdriver;
002
003import java.util.Arrays;
004import jmri.jmrix.oaktree.OakTreeSystemConnectionMemo;
005import jmri.jmrix.oaktree.SerialPortController;
006
007/**
008 * Provide access to Oak Tree via a serial com port. Normally controlled by the
009 * oaktree.serialdriver.SerialDriverFrame class.
010 *
011 * @author Bob Jacobsen Copyright (C) 2006
012 */
013public class SerialDriverAdapter extends SerialPortController {
014
015    public SerialDriverAdapter() {
016        super(new OakTreeSystemConnectionMemo());
017        this.manufacturerName = jmri.jmrix.oaktree.SerialConnectionTypeList.OAK;
018    }
019
020    @Override
021    public String openPort(String portName, String appName) {
022
023        // get and open the primary port
024        currentSerialPort = activatePort(portName, log);
025        if (currentSerialPort == null) {
026            log.error("failed to connect Oak Tree to {}", portName);
027            return Bundle.getMessage("SerialPortNotFound", portName);
028        }
029        log.info("Connecting Oak Tree to {} {}", portName, currentSerialPort);
030        
031        // try to set it for communication via SerialDriver
032        // find the baud rate value, configure comm options
033        int baud = currentBaudNumber(mBaudRate);
034        setBaudRate(currentSerialPort, baud);
035        configureLeads(currentSerialPort, true, true);
036        setFlowControl(currentSerialPort, FlowControl.NONE);
037
038        // report status
039        reportPortStatus(log, portName);
040
041        opened = true;
042
043        return null; // indicates OK return
044    }
045
046    /**
047     * Can the port accept additional characters?
048     *
049     * @return true, always
050     */
051    public boolean okToSend() {
052        return true;
053    }
054
055    /**
056     * Set up all of the other objects to operate connected to this port.
057     */
058    @Override
059    public void configure() {
060        // connect to the traffic controller
061        log.debug("set tc for memo {}", getSystemConnectionMemo().getUserName());
062        ((OakTreeSystemConnectionMemo) getSystemConnectionMemo()).getTrafficController().connectPort(this);
063        // do the common manager config
064        ((OakTreeSystemConnectionMemo) getSystemConnectionMemo()).configureManagers();
065
066    }
067
068    // base class methods for the SerialPortController interface
069
070    @Override
071    public boolean status() {
072        return opened;
073    }
074
075    /**
076     * {@inheritDoc}
077     */
078    @Override
079    public String[] validBaudRates() {
080        return Arrays.copyOf(validSpeeds, validSpeeds.length);
081    }
082
083    /**
084     * {@inheritDoc}
085     */
086    @Override
087    public int[] validBaudNumbers() {
088        return Arrays.copyOf(validSpeedValues, validSpeedValues.length);
089    }
090
091    protected String[] validSpeeds = new String[]{Bundle.getMessage("Baud19200"), Bundle.getMessage("Baud38400")};
092    protected int[] validSpeedValues = new int[]{19200, 38400};
093
094    @Override
095    public int defaultBaudIndex() {
096        return 0;
097    }
098
099    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SerialDriverAdapter.class);
100
101}