001package jmri.jmrix.can.adapters.gridconnect.sproggen5.serialdriver;
002
003import jmri.jmrix.can.ConfigurationManager;
004import jmri.jmrix.can.ConfigurationManager.ProgModeSwitch;
005import jmri.jmrix.can.TrafficController;
006import jmri.jmrix.can.adapters.gridconnect.GcSerialDriverAdapter;
007import jmri.jmrix.can.adapters.gridconnect.canrs.MergTrafficController;
008
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Implements SerialPortAdapter for SPROG Generation 5.
014 * <p>
015 * This connects a SPROG Generation 5 via a serial com port (real or virtual).
016 * Normally controlled by the SerialDriverFrame class.
017 *
018 * @author Andrew Crosland Copyright (C) 2008
019 * @author Bob Jacobsen Copyright (C) 2009
020 * @author Andrew Crosland Copyright (C) 2019, 2021
021 */
022public class Sprog3PlusSerialDriverAdapter extends GcSerialDriverAdapter {
023
024    public Sprog3PlusSerialDriverAdapter() {
025        super("S", purejavacomm.SerialPort.FLOWCONTROL_RTSCTS_IN + purejavacomm.SerialPort.FLOWCONTROL_RTSCTS_OUT);
026        option2Name = "CANID";
027        options.put(option2Name, new Option(Bundle.getMessage("JMRICANID"), new String[]{"127", "126", "125", "124", "123", "122", "121", "120"}));
028        _progMode = ConfigurationManager.ProgModeSwitch.SPROG3PLUS;
029    }
030
031    protected ProgModeSwitch _progMode;
032
033    /**
034     * Set up all of the other objects to operate with a SPROG Gen 5
035     * connected to this port.
036     */
037    @Override
038    public void configure() {
039
040        // Register the CAN traffic controller being used for this connection
041        TrafficController tc = new MergTrafficController();
042        try {
043            tc.setCanId(Integer.parseInt(getOptionState(option2Name)));
044        } catch (Exception e) {
045            log.error("Cannot parse CAN ID - check your preference settings", e);
046            log.error("Now using default CAN ID");
047        }
048
049        this.getSystemConnectionMemo().setTrafficController(tc);
050
051        // Now connect to the traffic controller
052        log.debug("Connecting port");
053        tc.connectPort(this);
054
055        this.getSystemConnectionMemo().setProtocol(getOptionState(option1Name));
056        this.getSystemConnectionMemo().setSubProtocol(ConfigurationManager.SubProtocol.CBUS);
057        this.getSystemConnectionMemo().setProgModeSwitch(_progMode);
058        this.getSystemConnectionMemo().setSupportsCVHints(true);
059        this.getSystemConnectionMemo().setPowerOnArst(false);
060
061        // do central protocol-specific configuration
062        //jmri.jmrix.can.ConfigurationManager.configure(getOptionState(option1Name));
063        this.getSystemConnectionMemo().configureManagers();
064    }
065
066    /**
067     * {@inheritDoc}
068     */
069    @Override
070    public String[] validBaudRates() {
071        return new String[]{Bundle.getMessage("Baud460800")};
072    }
073
074    /**
075     * And the corresponding values.
076     */
077    @Override
078    public int[] validBaudNumbers() {
079        return new int[]{460800};
080    }
081
082    @Override
083    public int defaultBaudIndex() {
084        return 0;
085    }
086
087    private final static Logger log = LoggerFactory.getLogger(Sprog3PlusSerialDriverAdapter.class);
088
089}