001package jmri.jmrix.easydcc.serialdriver;
002
003import jmri.jmrix.easydcc.EasyDccPortController;
004import jmri.jmrix.easydcc.EasyDccSystemConnectionMemo;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * Implements SerialPortAdapter for the EasyDCC system.
010 * <p>
011 * This connects an EasyDCC command station via a serial com port.
012 * Normally controlled by the SerialDriverFrame class.
013 * <p>
014 * The current implementation only handles the 9,600 baud rate, and does not use
015 * any other options at configuration time.
016 *
017 * @author Bob Jacobsen Copyright (C) 2001, 2002
018 */
019public class SerialDriverAdapter extends EasyDccPortController {
020
021    public SerialDriverAdapter() {
022        super(new EasyDccSystemConnectionMemo("E", "EasyDCC via Serial")); // pass customized user name
023        setManufacturer(jmri.jmrix.easydcc.EasyDccConnectionTypeList.EASYDCC);
024    }
025
026    @Override
027    public String openPort(String portName, String appName) {
028
029        // get and open the primary port
030        currentSerialPort = activatePort(portName, log);
031        if (currentSerialPort == null) {
032            log.error("failed to connect EasyDCC to {}", portName);
033            return Bundle.getMessage("SerialPortNotFound", portName);
034        }
035        log.info("Connecting EasyDCC to {} {}", portName, currentSerialPort);
036        
037        // try to set it for communication via SerialDriver
038        // baud rate is fixed at 9600
039        setBaudRate(currentSerialPort, 9600);
040        configureLeads(currentSerialPort, true, true);
041        setFlowControl(currentSerialPort, FlowControl.NONE);
042
043        // report status
044        reportPortStatus(log, portName);
045
046        opened = true;
047
048        return null; // indicates OK return
049    }
050
051    /**
052     * Set up all of the other objects to operate with an EasyDCC command
053     * station connected to this port.
054     */
055    @Override
056    public void configure() {
057        // connect to the traffic controller, which is provided via the memo
058        log.debug("set tc for memo {}", getSystemConnectionMemo().getUserName());
059
060        getSystemConnectionMemo().getTrafficController().connectPort(this);
061
062        // do the common manager config
063        getSystemConnectionMemo().configureManagers();
064    }
065
066    // Base class methods for the EasyDccPortController interface
067
068    /**
069     * {@inheritDoc}
070     */
071    @Override
072    public boolean status() {
073        return opened;
074    }
075
076    /**
077     * {@inheritDoc}
078     * Currently only 9,600 bps.
079     */
080    @Override
081    public String[] validBaudRates() {
082        return new String[]{Bundle.getMessage("Baud9600")};
083    }
084
085    /**
086     * {@inheritDoc}
087     */
088    @Override
089    public int[] validBaudNumbers() {
090        return new int[]{9600};
091    }
092
093    @Override
094    public int defaultBaudIndex() {
095        return 0;
096    }
097
098    // private control members
099    private final static Logger log = LoggerFactory.getLogger(SerialDriverAdapter.class);
100
101}