001package jmri.jmrix.lenz.ztc640;
002
003import java.util.Arrays;
004import jmri.jmrix.lenz.LenzCommandStation;
005import jmri.jmrix.lenz.XNetInitializationManager;
006import jmri.jmrix.lenz.XNetSerialPortController;
007import jmri.jmrix.lenz.XNetTrafficController;
008
009/**
010 * Provide access to XpressNet via a ZTC640 connected via an FTDI virtual comm
011 * port. Normally controlled by the lenz.ztc640.ZTC640Frame class.
012 *
013 * @author Bob Jacobsen Copyright (C) 2002
014 * @author Paul Bender, Copyright (C) 2003-2010
015 */
016public class ZTC640Adapter extends XNetSerialPortController {
017
018    public ZTC640Adapter() {
019        super();
020        option1Name = "FlowControl"; // NOI18N
021        options.put(option1Name, new Option(Bundle.getMessage("ZTC640ConnectionLabel"), validOption1));
022    }
023
024    @Override
025    public String openPort(String portName, String appName) {
026        // get and open the primary port
027        currentSerialPort = activatePort(portName, log);
028        if (currentSerialPort == null) {
029            log.error("failed to connect ZTC640 to {}", portName);
030            return Bundle.getMessage("SerialPortNotFound", portName);
031        }
032        log.info("Connecting ZTC640 to {} {}", portName, currentSerialPort);
033        
034        // try to set it for communication via SerialDriver
035        // find the baud rate value, configure comm options
036        int baud = currentBaudNumber(mBaudRate);
037        setBaudRate(currentSerialPort, baud);
038        configureLeads(currentSerialPort, true, true);
039        FlowControl flow = FlowControl.RTSCTS; // default, but also default for getOptionState(option1Name)
040        if (!getOptionState(option1Name).equals(validOption1[0])) {
041            flow = FlowControl.NONE;
042        }
043        setFlowControl(currentSerialPort, flow);
044
045        // report status
046        reportPortStatus(log, portName);
047
048        opened = true;
049
050        return null; // indicates OK return
051    }
052
053    /**
054     * Set up all of the other objects to operate with a ZTC640 connected to
055     * this port.
056     */
057    @Override
058    public void configure() {
059        // connect to a packetizing traffic controller
060        XNetTrafficController packets = new ZTC640XNetPacketizer(new LenzCommandStation());
061        packets.connectPort(this);
062
063        this.getSystemConnectionMemo().setXNetTrafficController(packets);
064        new XNetInitializationManager()
065                .memo(this.getSystemConnectionMemo())
066                .setDefaults()
067                .versionCheck()
068                .setTimeout(30000)
069                .init();
070    }
071
072    // base class methods for the XNetSerialPortController interface
073
074    @Override
075    public boolean status() {
076        return opened;
077    }
078
079    /**
080     * {@inheritDoc}
081     */
082    @Override
083    public String[] validBaudRates() {
084        return Arrays.copyOf(validSpeeds, validSpeeds.length);
085    }
086
087    /**
088     * {@inheritDoc}
089     */
090    @Override
091    public int[] validBaudNumbers() {
092        return Arrays.copyOf(validSpeedValues, validSpeedValues.length);
093    }
094
095    protected final String[] validSpeeds = new String[]{Bundle.getMessage("Baud19200")};
096    protected final int[] validSpeedValues = new int[]{19200};
097
098    @Override
099    public int defaultBaudIndex() {
100        return 0;
101    }
102
103    // meanings are assigned to these above, so make sure the order is consistent
104    protected final String[] validOption1 = new String[]{Bundle.getMessage("FlowOptionNoRecomm"), Bundle.getMessage("FlowOptionHw")};
105
106    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ZTC640Adapter.class);
107
108}