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 NCE PH5 to {}", portName);
036            return Bundle.getMessage("SerialPortNotFound", portName);
037        }
038        log.info("Connecting NCE PH5 to {} {}", 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        tc.connectPort(this);
068
069        this.getSystemConnectionMemo().configureManagers();
070        tc.csm = new Ph5CmdStationMemory();
071    }
072
073    // base class methods for the NcePortController interface
074
075    @Override
076    public boolean status() {
077        return opened;
078    }
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    public String[] validBaudRates() {
085        return Arrays.copyOf(validSpeeds, validSpeeds.length);
086    }
087
088    /**
089     * {@inheritDoc}
090     */
091    @Override
092    public int[] validBaudNumbers() {
093        return Arrays.copyOf(validSpeedValues, validSpeedValues.length);
094    }
095
096    private String[] validSpeeds = new String[]{Bundle.getMessage("Baud9600")};
097    private int[] validSpeedValues = new int[]{9600};
098
099    @Override
100    public int defaultBaudIndex() {
101        return 0;
102    }
103
104    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(Ph5DriverAdapter.class);
105
106}