001package jmri.jmrix.grapevine.serialdriver;
002
003import java.util.Arrays;
004import jmri.jmrix.grapevine.GrapevineSystemConnectionMemo;
005import jmri.jmrix.grapevine.SerialPortController;
006import jmri.jmrix.grapevine.SerialTrafficController;
007
008/**
009 * Provide access to ProTrak Grapevine via a serial com port. Normally
010 * controlled by the serialdriver.SerialDriverFrame class.
011 *
012 * @author Bob Jacobsen Copyright (C) 2006, 2007, 2023
013 */
014public class SerialDriverAdapter extends SerialPortController {
015
016    /**
017     * Create a new SerialDriverAdapter.
018     */
019    public SerialDriverAdapter() {
020        // needs to provide a SystemConnectionMemo
021        super(new GrapevineSystemConnectionMemo());
022        this.manufacturerName = jmri.jmrix.grapevine.SerialConnectionTypeList.PROTRAK;
023    }
024
025    /**
026     * {@inheritDoc}
027     */
028    @Override
029    public String openPort(String portName, String appName) {
030
031        // get and open the primary port
032        currentSerialPort = activatePort(portName, log);
033        if (currentSerialPort == null) {
034            log.error("failed to connect Grapevine to {}", portName);
035            return Bundle.getMessage("SerialPortNotFound", portName);
036        }
037        log.info("Connecting Grapevine to {} {}", portName, currentSerialPort);
038        
039        // try to set it for communication via SerialDriver
040        // find the baud rate value, configure comm options
041        int baud = currentBaudNumber(mBaudRate);
042        setBaudRate(currentSerialPort, baud);
043        configureLeads(currentSerialPort, true, true);
044        setFlowControl(currentSerialPort, FlowControl.NONE);
045
046        // report status
047        reportPortStatus(log, portName);
048
049        opened = true;
050
051        return null; // indicates OK return
052    }
053
054    /**
055     * Can the port accept additional characters?
056     *
057     * @return Yes, always
058     */
059    public boolean okToSend() {
060        return true;
061    }
062
063    /**
064     * Set up all of the other objects to operate connected to this port.
065     */
066    @Override
067    public void configure() {
068        log.debug("SerialDriverAdapter configure() with prefix = {}", this.getSystemConnectionMemo().getSystemPrefix());
069        // connect to the traffic controller
070        SerialTrafficController control = new SerialTrafficController(this.getSystemConnectionMemo());
071        control.connectPort(this);
072        control.setSystemConnectionMemo(this.getSystemConnectionMemo());
073        log.debug("SimulatorAdapter configure() set tc for memo {}", getSystemConnectionMemo().getUserName());
074        this.getSystemConnectionMemo().setTrafficController(control);
075        // do the common manager config
076        getSystemConnectionMemo().configureManagers();
077    }
078
079    // base class methods for the SerialPortController interface
080
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public boolean status() {
086        return opened;
087    }
088
089    /**
090     * {@inheritDoc}
091     */
092    @Override
093    public String[] validBaudRates() {
094        return Arrays.copyOf(validSpeeds, validSpeeds.length);
095    }
096
097    /**
098     * {@inheritDoc}
099     */
100    @Override
101    public int[] validBaudNumbers() {
102        return Arrays.copyOf(validSpeedValues, validSpeedValues.length);
103    }
104
105    protected String[] validSpeeds = new String[]{Bundle.getMessage("Baud38400")};
106    protected int[] validSpeedValues = new int[]{38400};
107
108    @Override
109    public int defaultBaudIndex() {
110        return 0;
111    }
112
113    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SerialDriverAdapter.class);
114
115}