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