001package jmri.jmrix.acela.serialdriver;
002
003import jmri.jmrix.acela.AcelaPortController;
004import jmri.jmrix.acela.AcelaSystemConnectionMemo;
005import jmri.jmrix.acela.AcelaTrafficController;
006
007/**
008 * Implements SerialPortAdapter for the Acela system. This connects an Acela
009 * interface to the CTI network via a serial com port. Normally controlled by
010 * the SerialDriverFrame 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 *
017 * @author Bob Coleman, Copyright (C) 2007, 2008 Based on MRC example, modified
018 * to establish Acela support.
019 */
020public class SerialDriverAdapter extends AcelaPortController {
021
022    public SerialDriverAdapter() {
023        super(new AcelaSystemConnectionMemo());
024        log.debug("opening Acela serial connection from memo");
025        setManufacturer(jmri.jmrix.acela.AcelaConnectionTypeList.CTI);
026    }
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 Acela to {}", portName);
035            return Bundle.getMessage("SerialPortNotFound", portName);
036        }
037        log.info("Connecting Acela 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     * Set up all of the other objects to operate with a serial command station
056     * connected to this port.
057     */
058    @Override
059    public void configure() {
060        // connect to the traffic controller
061        AcelaTrafficController control = new AcelaTrafficController();
062        control.connectPort(this);
063
064        getSystemConnectionMemo().setAcelaTrafficController(control);
065        getSystemConnectionMemo().configureManagers();
066    }
067
068    // base class methods for the AcelaPortController interface
069
070    @Override
071    public boolean status() {
072        return opened;
073    }
074
075    /**
076     * {@inheritDoc}
077     */
078    @Override
079    public String[] validBaudRates() {
080        // Really just want 9600 Baud for Acela
081        return new String[]{Bundle.getMessage("Baud9600")};
082    }
083
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    public int[] validBaudNumbers() {
089    // Only 9600 Baud for Acela
090        return new int[]{9600};
091    }
092
093    @Override
094    public int defaultBaudIndex() {
095        return 0;
096    }
097    
098    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SerialDriverAdapter.class);
099
100}