001package jmri.jmrix.can.adapters.lawicell;
002
003import java.util.Arrays;
004
005import jmri.jmrix.can.TrafficController;
006
007/**
008 * Implements SerialPortAdapter for the LAWICELL protocol.
009 *
010 * @author Bob Jacobsen Copyright (C) 2001, 2002, 2008
011 * @author Andrew Crosland Copyright (C) 2008
012 */
013public class SerialDriverAdapter extends PortController {
014
015    public SerialDriverAdapter() {
016        super(new jmri.jmrix.can.CanSystemConnectionMemo());
017        option1Name = "Protocol"; // NOI18N
018        options.put(option1Name, new Option(Bundle.getMessage("ConnectionProtocol"),
019                jmri.jmrix.can.ConfigurationManager.getSystemOptions()));
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);
027        if (currentSerialPort == null) {
028            log.error("failed to connect CAN Lawicell to {}", portName);
029            return Bundle.getMessage("SerialPortNotFound", portName);
030        }
031        log.info("Connecting CAN Lawicell 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     * Set up all of the other objects to operate with a CAN RS adapter
050     * connected to this port.
051     */
052    @Override
053    public void configure() {
054
055        // Register the CAN traffic controller being used for this connection
056        TrafficController tc = new LawicellTrafficController();
057        this.getSystemConnectionMemo().setTrafficController(tc);
058
059        // Now connect to the traffic controller
060        log.debug("Connecting port");
061        tc.connectPort(this);
062
063        // send a request for version information, set 125kbps, open channel
064        log.debug("send version request");
065        jmri.jmrix.can.CanMessage m
066                = new jmri.jmrix.can.CanMessage(new int[]{'V', 13, 'S', '4', 13, 'O', 13}, tc.getCanid());
067        m.setTranslated(true);
068        tc.sendCanMessage(m, null);
069
070        // do central protocol-specific configuration
071        this.getSystemConnectionMemo().setProtocol(getOptionState(option1Name));
072
073        this.getSystemConnectionMemo().configureManagers();
074    }
075
076    @Override
077    public boolean status() {
078        return opened;
079    }
080
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public String[] validBaudRates() {
086        return Arrays.copyOf(validSpeeds, validSpeeds.length);
087    }
088
089    @Override
090    public int[] validBaudNumbers() {
091        return Arrays.copyOf(validSpeedValues, validSpeedValues.length);
092    }
093
094    protected String[] validSpeeds = new String[]{Bundle.getMessage("Baud57600"),
095            Bundle.getMessage("Baud115200"), Bundle.getMessage("Baud230400"),
096            Bundle.getMessage("Baud250000"), Bundle.getMessage("Baud333333"),
097            Bundle.getMessage("Baud460800"), Bundle.getMessage("Baud500000")};
098    protected int[] validSpeedValues = new int[]{57600, 115200, 230400, 250000, 333333, 460800, 500000};
099
100    @Override
101    public int defaultBaudIndex() {
102        return 0;
103    }
104
105    // private control members
106
107    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SerialDriverAdapter.class);
108
109}