001package jmri.jmrix.ztc.ztc611;
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 ZTC611 connected via an FTDI virtual comm
011 * port.
012 *
013 * @author Bob Jacobsen Copyright (C) 2002
014 * @author Paul Bender, Copyright (C) 2003-2017
015 */
016public class ZTC611Adapter extends XNetSerialPortController {
017
018    public ZTC611Adapter() {
019        super();
020        option1Name = "FlowControl"; // NOI18N
021        options.put(option1Name, new Option(Bundle.getMessage("XconnectionUsesLabel", Bundle.getMessage("CSTypeZtc640")), 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 ZTC611 to {}", portName);
030            return Bundle.getMessage("SerialPortNotFound", portName);
031        }
032        log.info("Connecting ZTC611 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        
040        // find and configure flow control
041        FlowControl flow = FlowControl.NONE;  // no flow control is first in the elite setup,
042        // since it doesn't seem to work with flow
043        // control enabled.
044        if (!getOptionState(option1Name).equals(validOption1[0])) {
045            flow = FlowControl.RTSCTS;
046        }
047        setFlowControl(currentSerialPort, flow);
048
049        // report status
050        reportPortStatus(log, portName);
051
052        opened = true;
053
054        return null; // indicates OK return
055    }
056
057    /**
058     * set up all of the other objects to operate with a ZTC611 connected to
059     * this port
060     */
061    @Override
062    public void configure() {
063        // connect to a packetizing traffic controller
064        XNetTrafficController packets = new ZTC611XNetPacketizer(new LenzCommandStation());
065        packets.connectPort(this);
066
067        // start operation
068        // packets.startThreads();
069        this.getSystemConnectionMemo().setXNetTrafficController(packets);
070        new XNetInitializationManager()
071                .memo(this.getSystemConnectionMemo())
072                .setDefaults()
073                .turnoutManager(ZTC611XNetTurnoutManager.class)
074                .init();
075    }
076
077    // base class methods for the XNetSerialPortController interface
078    @Override
079    public boolean status() {
080        return opened;
081    }
082
083    /**
084     * {@inheritDoc}
085     */
086    @Override
087    public String[] validBaudRates() {
088        return Arrays.copyOf(validSpeeds, validSpeeds.length);
089    }
090
091    /**
092     * {@inheritDoc}
093     */
094    @Override
095    public int[] validBaudNumbers() {
096        return Arrays.copyOf(validSpeedValues, validSpeedValues.length);
097    }
098
099    protected final String[] validSpeeds = new String[]{Bundle.getMessage("Baud9600")};
100    protected final int[] validSpeedValues = new int[]{19200};
101
102    @Override
103    public int defaultBaudIndex() {
104        return 0;
105    }
106
107    // meanings are assigned to these above, so make sure the order is consistent
108    protected final String[] validOption1 = new String[]{Bundle.getMessage("FlowOptionNoRecomm"), Bundle.getMessage("FlowOptionHw")};
109
110    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ZTC611Adapter.class);
111
112}