001package jmri.jmrix.zimo.mx1;
002
003import java.util.Arrays;
004import jmri.jmrix.zimo.Mx1CommandStation;
005import jmri.jmrix.zimo.Mx1Packetizer;
006import jmri.jmrix.zimo.Mx1PortController;
007import jmri.jmrix.zimo.Mx1SystemConnectionMemo;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Provide access to Zimo's MX-1 on an attached serial com port. Adapted for
013 * use with Zimo MX-1 by Sip Bosch.
014 *
015 * @author Bob Jacobsen Copyright (C) 2002
016 */
017public class Mx1Adapter extends Mx1PortController {
018
019    public Mx1Adapter() {
020        super(new Mx1SystemConnectionMemo());
021        option1Name = "FlowControl"; // NOI18N
022        options.put(option1Name, new Option("MX-1 connection uses : ", validOption1));
023        this.manufacturerName = jmri.jmrix.zimo.Mx1ConnectionTypeList.ZIMO;
024    }
025
026    @Override
027    public String openPort(String portName, String appName) {
028        // get and open the primary port
029        currentSerialPort = activatePort(portName, log);
030        if (currentSerialPort == null) {
031            log.error("failed to connect ZIMO MX1 to {}", portName);
032            return Bundle.getMessage("SerialPortNotFound", portName);
033        }
034        log.info("Connecting ZIMO MX1 to {} {}", portName, currentSerialPort);
035        
036        // try to set it for communication via SerialDriver
037        // find the baud rate value, configure comm options
038        int baud = currentBaudNumber(mBaudRate);
039        setBaudRate(currentSerialPort, baud);
040        configureLeads(currentSerialPort, true, true);
041        // find and configure flow control
042        FlowControl flow = FlowControl.RTSCTS; // default, but also defaults in selectedOption1
043        if (getOptionState(option1Name).equals(validOption1[1])) {
044            flow = FlowControl.NONE;
045        }
046        setFlowControl(currentSerialPort, flow);
047
048        // report status
049        reportPortStatus(log, portName);
050
051        opened = true;
052
053        return null; // indicates OK return
054    }
055
056    /**
057     * Can the port accept additional characters? The state of CTS determines
058     * this, as there seems to be no way to check the number of queued bytes and
059     * buffer length. This might go false for short intervals, but it might also
060     * stick off if something goes wrong.
061     *
062     * @return true if more data can be sent; false otherwise
063     */
064    @Override
065    public boolean okToSend() {
066        return currentSerialPort.getCTS();
067    }
068
069    /**
070     * set up all of the other objects to operate with a MX-1 connected to this
071     * port
072     */
073    @Override
074    public void configure() {
075        Mx1CommandStation cs = new Mx1CommandStation(getSystemConnectionMemo().getSystemPrefix(), getSystemConnectionMemo().getUserName());
076        // connect to a packetizing traffic controller
077        Mx1Packetizer packets = new Mx1Packetizer(cs, Mx1Packetizer.ASCII);
078        packets.connectPort(this);
079
080        getSystemConnectionMemo().setMx1TrafficController(packets);
081        getSystemConnectionMemo().configureManagers();
082
083        // start operation
084        packets.startThreads();
085    }
086
087    // base class methods for the ZimoPortController interface
088
089    @Override
090    public boolean status() {
091        return opened;
092    }
093
094    /**
095     * {@inheritDoc}
096     */
097    @Override
098    public String[] validBaudRates() {
099        return Arrays.copyOf(validSpeeds, validSpeeds.length);
100    }
101
102    /**
103     * {@inheritDoc}
104     */
105    @Override
106    public int[] validBaudNumbers() {
107        return Arrays.copyOf(validSpeedValues, validSpeedValues.length);
108    }
109
110    protected String[] validSpeeds = new String[]{Bundle.getMessage("Baud1200"),
111            Bundle.getMessage("Baud2400"), Bundle.getMessage("Baud4800"),
112            Bundle.getMessage("Baud9600"), Bundle.getMessage("Baud19200"),
113            Bundle.getMessage("Baud38400")};
114    protected int[] validSpeedValues = new int[]{1200, 2400, 4800, 9600, 19200, 38400};
115
116    @Override
117    public int defaultBaudIndex() {
118        return 0;
119    }
120
121    // meanings are assigned to these above, so make sure the order is consistent
122    protected String[] validOption1 = new String[]{Bundle.getMessage("FlowOptionHwRecomm"), Bundle.getMessage("FlowOptionNo")};
123    protected String[] validOption2 = new String[]{"3", "5"};
124    //protected String selectedOption1=validOption1[0];
125
126    private final static Logger log = LoggerFactory.getLogger(Mx1Adapter.class);
127
128}