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