001package jmri.jmrix.cmri.serial.serialdriver;
002
003import java.util.Arrays;
004
005import jmri.jmrix.cmri.CMRISystemConnectionMemo;
006import jmri.jmrix.cmri.serial.SerialPortAdapter;
007import jmri.jmrix.cmri.serial.SerialTrafficController;
008
009/**
010 * Provide access to C/MRI via a serial com port. Normally controlled by the
011 * cmri.serial.serialdriver.SerialDriverFrame class.
012 *
013 * @author Bob Jacobsen Copyright (C) 2002, 2023
014 */
015public class SerialDriverAdapter extends SerialPortAdapter {
016
017    public SerialDriverAdapter() {
018        super(new CMRISystemConnectionMemo());
019        this.manufacturerName = jmri.jmrix.cmri.CMRIConnectionTypeList.CMRI;
020    }
021
022    @Override
023    public String openPort(String portName, String appName) {
024
025        // get and open the primary port
026        currentSerialPort = activatePort(portName, log, 2);
027        if (currentSerialPort == null) {
028            log.error("failed to connect C/MRI to {}", portName);
029            return Bundle.getMessage("SerialPortNotFound", portName);
030        }
031        log.info("Connecting C/MRI to {} {}", portName, currentSerialPort);
032        
033        // try to set it for communication via SerialDriver
034        // find the baud rate value, configure comm options
035        int baud = currentBaudNumber(mBaudRate);
036        setBaudRate(currentSerialPort, baud);
037        configureLeads(currentSerialPort, true, true);
038        setFlowControl(currentSerialPort, FlowControl.NONE);
039
040        // report status
041        reportPortStatus(log, portName);
042
043        opened = true;
044
045        return null; // indicates OK return
046    }
047
048    /**
049     * Can the port accept additional characters? Yes, always
050     * @return always true.
051     */
052    public boolean okToSend() {
053        return true;
054    }
055
056    /**
057     * set up all of the other objects to operate connected to this port
058     */
059    @Override
060    public void configure() {
061        // connect to the traffic controller
062        SerialTrafficController tc = new SerialTrafficController();
063        tc.connectPort(this);
064        ((CMRISystemConnectionMemo)getSystemConnectionMemo()).setTrafficController(tc);
065        ((CMRISystemConnectionMemo)getSystemConnectionMemo()).configureManagers();
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 Arrays.copyOf(validSpeeds, validSpeeds.length);
079    }
080
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public int[] validBaudNumbers() {
086        return Arrays.copyOf(validSpeedValues, validSpeedValues.length);
087    }
088
089    protected String[] validSpeeds = new String[]{Bundle.getMessage("Baud9600"),
090            Bundle.getMessage("Baud19200"), Bundle.getMessage("Baud28800"),
091            Bundle.getMessage("Baud57600"), Bundle.getMessage("Baud115200")};
092    protected int[] validSpeedValues = new int[]{9600, 19200, 28800, 57600, 115200};
093
094    @Override
095    public int defaultBaudIndex() {
096        return 1;
097    }
098
099    // private control members
100
101    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SerialDriverAdapter.class);
102
103}