001package jmri.jmrix.tams.serialdriver;
002
003import java.util.Arrays;
004import jmri.jmrix.tams.TamsPortController;
005import jmri.jmrix.tams.TamsSystemConnectionMemo;
006import jmri.jmrix.tams.TamsTrafficController;
007
008/**
009 * Implements SerialPortAdapter for the TAMS system.
010 * <p>
011 * This connects an TAMS command station via a serial com port.
012 * <p>
013 * Based on work by Bob Jacobsen
014 *
015 * @author Kevin Dickerson Copyright (C) 2012
016 */
017public class SerialDriverAdapter extends TamsPortController {
018
019    public SerialDriverAdapter() {
020        super(new TamsSystemConnectionMemo());
021        setManufacturer(jmri.jmrix.tams.TamsConnectionTypeList.TAMS);
022    }
023
024    @Override
025    public String openPort(String portName, String appName) {
026
027        // get and open the primary port
028        currentSerialPort = activatePort(portName, log);
029        if (currentSerialPort == null) {
030            log.error("failed to connect TAMS to {}", portName);
031            return Bundle.getMessage("SerialPortNotFound", portName);
032        }
033        log.info("Connecting TAMS 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(mBaudRate);
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 NCE command station
052     * connected to this port
053     */
054    @Override
055    public void configure() {
056        TamsTrafficController tc = new TamsTrafficController();
057        this.getSystemConnectionMemo().setTamsTrafficController(tc);
058        tc.setAdapterMemo(this.getSystemConnectionMemo());
059
060        tc.connectPort(this);
061
062        this.getSystemConnectionMemo().configureManagers();
063    }
064
065    // base class methods for the TamsPortController interface
066
067    @Override
068    public boolean status() {
069        return opened;
070    }
071
072    /**
073     * {@inheritDoc}
074     */
075    @Override
076    public String[] validBaudRates() {
077        return Arrays.copyOf(validSpeeds, validSpeeds.length);
078    }
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    public int[] validBaudNumbers() {
085        return Arrays.copyOf(validSpeedValues, validSpeedValues.length);
086    }
087
088    private final String[] validSpeeds = new String[]{Bundle.getMessage("Baud57600"),
089            Bundle.getMessage("Baud2400"), Bundle.getMessage("Baud9600"),
090            Bundle.getMessage("Baud19200")};
091    private final int[] validSpeedValues = new int[]{57600, 2400, 9600, 19200};
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}