001package jmri.jmrix.loconet.ms100;
002
003import java.util.Vector;
004
005import jmri.jmrix.loconet.LnPacketizer;
006import jmri.jmrix.loconet.LnPortController;
007import jmri.jmrix.loconet.LocoNetSystemConnectionMemo;
008
009/**
010 * Provide access to LocoNet via a MS100 attached to a serial com port.
011 * Normally controlled by the jmri.jmrix.loconet.ms100.ConnectionConfig class.
012 * <p>
013 * By default, this attempts to use 16600 baud. If that fails, it falls back to
014 * 16457 baud. Neither the baud rate configuration nor the "option 1" option are
015 * used.
016 *
017 * @author Bob Jacobsen Copyright (C) 2001
018 */
019public class MS100Adapter extends LnPortController {
020
021    public MS100Adapter() {
022        super(new LocoNetSystemConnectionMemo());
023        option2Name = "CommandStation"; // NOI18N
024        option3Name = "TurnoutHandle"; // NOI18N
025        options.put(option2Name, new Option(Bundle.getMessage("CommandStationTypeLabel"), commandStationNames, false));
026        options.put(option3Name, new Option(Bundle.getMessage("TurnoutHandling"),
027                new String[]{Bundle.getMessage("HandleNormal"), Bundle.getMessage("HandleSpread"), Bundle.getMessage("HandleOneOnly"), Bundle.getMessage("HandleBoth")})); // I18N
028        options.put("LoconetUpdateSlotOnMessageCreation",                                       // NOI18N
029                new Option(Bundle.getMessage("LoconetUpdateSlotOnMessageCreationLabel"),        // I18N
030                new String[]{Bundle.getMessage("ButtonNo"),Bundle.getMessage("ButtonYes")} ));  // I18N
031
032    }
033
034    Vector<String> portNameVector = null;
035
036    @Override
037    public String openPort(String portName, String appName) {
038
039        // get and open the primary port
040        currentSerialPort = activatePort(portName, log);
041        if (currentSerialPort == null) {
042            log.error("failed to connect MS100 to {}", portName);
043            return Bundle.getMessage("SerialPortNotFound", portName);
044        }
045        log.info("Connecting MS100 via {} {}", portName, currentSerialPort);
046        
047        // try to set it for communication via SerialDriver
048        // fixed baud rate
049        setBaudRate(currentSerialPort, 16600);
050        configureLeads(currentSerialPort, true, false);  // for MS100 power
051        setFlowControl(currentSerialPort, FlowControl.NONE);
052        
053        setComPortTimeouts(currentSerialPort, Blocking.READ_SEMI_BLOCKING, 100);
054
055        // report status
056        reportPortStatus(log, portName);
057
058        opened = true;
059
060        return null; // indicates OK return
061    }
062
063    /**
064     * set up all of the other objects to operate with a MS100 connected to this
065     * port
066     */
067    @Override
068    public void configure() {
069
070        setCommandStationType(getOptionState(option2Name));
071        setTurnoutHandling(getOptionState(option3Name));
072        // connect to a packetizing traffic controller
073        LnPacketizer packets = new LnPacketizer(this.getSystemConnectionMemo());
074        packets.setLoconetUpdateSlotOnMessageCreation(Bundle.getMessage("ButtonYes").equals(getOptionState("LoconetUpdateSlotOnMessageCreation")));
075        packets.connectPort(this);
076
077        // create memo
078        this.getSystemConnectionMemo().setLnTrafficController(packets);
079        // do the common manager config
080        this.getSystemConnectionMemo().configureCommandStation(commandStationType,
081                mTurnoutNoRetry, mTurnoutExtraSpace, mTranspondingAvailable, mInterrogateAtStart, mLoconetProtocolAutoDetect);
082        this.getSystemConnectionMemo().configureManagers();
083
084        // start operation
085        packets.startThreads();
086    }
087
088    @Override
089    public boolean status() {
090        return opened;
091    }
092
093    /**
094     * {@inheritDoc}
095     *
096     * Just a message saying it's fixed
097     */
098    @Override
099    public String[] validBaudRates() {
100        return new String[]{"fixed at 16,600 baud"};
101    }
102
103    /**
104     * {@inheritDoc}
105     */
106    @Override
107    public int[] validBaudNumbers() {
108        return new int[]{16600};
109    }
110
111    @Override
112    public int defaultBaudIndex() {
113        return 0;
114    }
115
116    /**
117     * Set the second port option. Only to be used after construction, but
118     * before the openPort call
119     */
120    @Override
121    public void configureOption2(String value) {
122        super.configureOption2(value);
123        log.debug("configureOption2: {}", value);
124        setCommandStationType(value);
125    }
126
127    // private control members
128    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MS100Adapter.class);
129
130}