001package jmri.jmrix.maple.serialdriver;
002
003import java.util.Arrays;
004import jmri.jmrix.maple.MapleSystemConnectionMemo;
005import jmri.jmrix.maple.SerialPortController;
006
007/**
008 * Provide access to Maple via a serial com port. Normally controlled by the
009 * maple.serialdriver.SerialDriverFrame class.
010 *
011 * @author Bob Jacobsen Copyright (C) 2002
012 */
013public class SerialDriverAdapter extends SerialPortController {
014
015    public SerialDriverAdapter() {
016        super(new MapleSystemConnectionMemo());
017        this.manufacturerName = jmri.jmrix.maple.SerialConnectionTypeList.MAPLE;
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, 2); // 2 stop bits
025        if (currentSerialPort == null) {
026            log.error("failed to connect Maple to {}", portName);
027            return Bundle.getMessage("SerialPortNotFound", portName);
028        }
029        log.info("Connecting Maple 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? Yes, always
048     * @return always true.
049     */
050    public boolean okToSend() {
051        return true;
052    }
053
054    /**
055     * Set up all of the other objects to operate connected to this port.
056     */
057    @Override
058    public void configure() {
059        // connect to the traffic controller
060        ((MapleSystemConnectionMemo) getSystemConnectionMemo()).getTrafficController().connectPort(this);
061        ((MapleSystemConnectionMemo) getSystemConnectionMemo()).configureManagers();
062    }
063
064    // base class methods for the SerialPortController interface
065
066    /**
067     * {@inheritDoc}
068     */
069    @Override
070    public boolean status() {
071        return opened;
072    }
073
074    /**
075     * {@inheritDoc}
076     */
077    @Override
078    public String[] validBaudRates() {
079        return Arrays.copyOf(validSpeeds, validSpeeds.length);
080    }
081
082    /**
083     * {@inheritDoc}
084     */
085    @Override
086    public int[] validBaudNumbers() {
087        return Arrays.copyOf(validSpeedValues, validSpeedValues.length);
088    }
089
090    protected String[] validSpeeds = new String[]{Bundle.getMessage("Baud9600"),
091            Bundle.getMessage("Baud19200"), Bundle.getMessage("Baud57600")};
092    protected int[] validSpeedValues = new int[]{9600, 19200, 57600};
093
094    @Override
095    public int defaultBaudIndex() {
096        return 1;
097    }
098
099    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SerialDriverAdapter.class);
100
101}