001package jmri.jmrix.nce.serialdriver;
002
003import java.util.Arrays;
004
005import jmri.jmrix.nce.NceCmdStationMemory;
006import jmri.jmrix.nce.NcePortController;
007import jmri.jmrix.nce.NceSystemConnectionMemo;
008import jmri.jmrix.nce.NceTrafficController;
009
010/**
011 * Implements SerialPortAdapter for the NCE system.
012 * <p>
013 * This connects an NCE command station via a serial com port. Normally
014 * controlled by the SerialDriverFrame class.
015 *
016 * @author Bob Jacobsen Copyright (C) 2001, 2002
017 * @author Ken Cameron Copyright (C) 2013, 2023
018 */
019public class SerialDriverAdapter extends NcePortController {
020
021    public SerialDriverAdapter() {
022        super(new NceSystemConnectionMemo());
023        option1Name = "Eprom"; // NOI18N
024        // the default is 2006 or later
025        options.put(option1Name, new Option("Command Station EPROM", new String[]{"2006 or later", "2004 or earlier"}));
026        // TODO I18N
027        setManufacturer(jmri.jmrix.nce.NceConnectionTypeList.NCE);
028    }
029
030    @Override
031    public String openPort(String portName, String appName) {
032
033        // get and open the primary port
034        currentSerialPort = activatePort(portName, log);
035        if (currentSerialPort == null) {
036            log.error("failed to connect NCE to {}", portName);
037            return Bundle.getMessage("SerialPortNotFound", portName);
038        }
039        log.info("Connecting NCE to {} {}", portName, currentSerialPort);
040        
041        // try to set it for communication via SerialDriver
042        // find the baud rate value, configure comm options
043        int baud = currentBaudNumber(mBaudRate);
044        setBaudRate(currentSerialPort, baud);
045        configureLeads(currentSerialPort, true, true);
046        setFlowControl(currentSerialPort, FlowControl.NONE);
047
048        // report status
049        reportPortStatus(log, portName);
050
051        opened = true;
052
053        return null; // indicates OK return
054    }
055
056    /**
057     * Set up all of the other objects to operate with an NCE command station
058     * connected to this port.
059     */
060    @Override
061    public void configure() {
062        NceTrafficController tc = new NceTrafficController();
063        this.getSystemConnectionMemo().setNceTrafficController(tc);
064        tc.setAdapterMemo(this.getSystemConnectionMemo());
065
066        if (getOptionState(option1Name).equals(getOptionChoices(option1Name)[0])) {
067            // setting binary mode
068            this.getSystemConnectionMemo().configureCommandStation(NceTrafficController.OPTION_2006);
069            this.getSystemConnectionMemo().setNceCmdGroups(~NceTrafficController.CMDS_USB);
070        } else {
071            this.getSystemConnectionMemo().configureCommandStation(NceTrafficController.OPTION_2004);
072            this.getSystemConnectionMemo().setNceCmdGroups(~NceTrafficController.CMDS_USB);
073        }
074
075        tc.connectPort(this);
076
077        this.getSystemConnectionMemo().configureManagers();
078        tc.csm = new NceCmdStationMemory();
079    }
080
081    // base class methods for the NcePortController interface
082
083    @Override
084    public boolean status() {
085        return opened;
086    }
087
088    /**
089     * {@inheritDoc}
090     */
091    @Override
092    public String[] validBaudRates() {
093        return Arrays.copyOf(validSpeeds, validSpeeds.length);
094    }
095
096    /**
097     * {@inheritDoc}
098     */
099    @Override
100    public int[] validBaudNumbers() {
101        return Arrays.copyOf(validSpeedValues, validSpeedValues.length);
102    }
103
104    private String[] validSpeeds = new String[]{Bundle.getMessage("Baud9600")};
105    private int[] validSpeedValues = new int[]{9600};
106
107    @Override
108    public int defaultBaudIndex() {
109        return 0;
110    }
111
112    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SerialDriverAdapter.class);
113
114}