001package jmri.jmrix.mrc.serialdriver;
002
003import jmri.jmrix.mrc.MrcPacketizer;
004import jmri.jmrix.mrc.MrcPortController;
005import jmri.jmrix.mrc.MrcSystemConnectionMemo;
006
007/**
008 * Implements SerialPortAdapter for the MRC system. This connects an MRC command
009 * station via a serial com port. Normally controlled by the SerialDriverFrame
010 * class.
011 * <p>
012 * The current implementation only handles the 9,600 baud rate, and does not use
013 * any other options at configuration time.
014 *
015 * @author Bob Jacobsen Copyright (C) 2001, 2002, 2023
016 */
017public class SerialDriverAdapter extends MrcPortController {
018
019    public SerialDriverAdapter() {
020        super(new MrcSystemConnectionMemo());
021        setManufacturer(jmri.jmrix.mrc.MrcConnectionTypeList.MRC);
022        options.put("CabAddress", new Option("Cab Address:", validOption1, false)); // NOI18N
023    }
024
025    @Override
026    public String openPort(String portName, String appName) {
027        // get and open the primary port
028        currentSerialPort = activatePort(this.getSystemPrefix(), portName, log, 1, Parity.ODD);
029        if (currentSerialPort == null) {
030            log.error("failed to connect MRC to {}", portName);
031            return Bundle.getMessage("SerialPortNotFound", portName);
032        }
033        log.info("Connecting MRC to {} {}", portName, currentSerialPort);
034
035        // try to set it for communication via SerialDriver
036        // find the baud rate value, configure comm options
037        int baud = currentBaudNumber(getCurrentBaudRate());
038        setBaudRate(currentSerialPort, baud);
039        configureLeads(currentSerialPort, true, true);
040        setFlowControl(currentSerialPort, FlowControl.NONE);
041
042        // report status
043        reportPortStatus(log, portName);
044
045        opened = true;
046
047        return null; // indicates OK return
048    }
049
050    /**
051     * set up all of the other objects to operate with an serial command station
052     * connected to this port
053     */
054    @Override
055    public void configure() {
056        MrcPacketizer packets = new MrcPacketizer();
057        packets.connectPort(this);
058        this.getSystemConnectionMemo().setMrcTrafficController(packets);
059
060        packets.setAdapterMemo(this.getSystemConnectionMemo());
061        packets.setCabNumber(Integer.parseInt(getOptionState("CabAddress")));// NOI18N
062
063        this.getSystemConnectionMemo().configureManagers();
064
065        packets.startThreads();
066    }
067
068    @Override
069    public boolean status() {
070        return opened;
071    }
072
073    /**
074     * {@inheritDoc}
075     */
076    @Override
077    public String[] validBaudRates() {
078        return new String[]{"38,400 bps"}; // NOI18N
079    }
080
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public int[] validBaudNumbers() {
086        return new int[]{38400};
087    }
088
089    @Override
090    public int defaultBaudIndex() {
091        return 0;
092    }
093
094    // private control members
095
096    protected String[] validOption1 = new String[]{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"};// NOI18N
097
098    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SerialDriverAdapter.class);
099
100}