001package jmri.jmrix.lenz.hornbyelite; 002 003import java.util.Arrays; 004 005import jmri.jmrix.lenz.*; 006 007/** 008 * Provide access to XpressNet via the Hornby Elite's built in USB port. 009 * Normally controlled by the lenz.hornbyelite.EliteFrame class. 010 * 011 * @author Bob Jacobsen Copyright (C) 2002 012 * @author Paul Bender, Copyright (C) 2003,2008-2010 013 */ 014public class EliteAdapter extends XNetSerialPortController { 015 016 public EliteAdapter() { 017 super(new EliteXNetSystemConnectionMemo()); 018 option1Name = "FlowControl"; // NOI18N 019 options.put(option1Name, new Option(Bundle.getMessage("HornbyEliteConnectionLabel"), validOption1)); 020 this.manufacturerName = EliteConnectionTypeList.HORNBY; 021 } 022 023 @Override 024 public String openPort(String portName, String appName) { 025 // get and open the primary port 026 currentSerialPort = activatePort(portName, log); 027 if (currentSerialPort == null) { 028 log.error("failed to connect Elite Adapter to {}", portName); 029 return Bundle.getMessage("SerialPortNotFound", portName); 030 } 031 log.info("Connecting Elite Adapter 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 039 // find and configure flow control 040 FlowControl flow = FlowControl.NONE; // no flow control is first in the elite setup, 041 // since it doesn't seem to work with flow 042 // control enabled. 043 if (!getOptionState(option1Name).equals(validOption1[0])) { 044 flow = FlowControl.RTSCTS; 045 } 046 setFlowControl(currentSerialPort, flow); 047 048 // report status 049 reportPortStatus(log, portName); 050 051 opened = true; 052 053 return null; // indicates OK return 054 } 055 056 /** 057 * Set up all of the other objects to operate with the Hornby Elite 058 * connected to this port. 059 */ 060 @Override 061 public void configure() { 062 // connect to a packetizing traffic controller 063 XNetTrafficController packets = new XNetPacketizer(new HornbyEliteCommandStation()); 064 packets.connectPort(this); 065 066 // start operation 067 this.getSystemConnectionMemo().setXNetTrafficController(packets); 068 new XNetInitializationManager() 069 .memo(this.getSystemConnectionMemo()) 070 .powerManager(XNetPowerManager.class) 071 .throttleManager(EliteXNetThrottleManager.class) 072 .programmer(EliteXNetProgrammer.class) 073 .programmerManager(XNetProgrammerManager.class) 074 .turnoutManager(EliteXNetTurnoutManager.class) 075 .lightManager(XNetLightManager.class) 076 .init(); 077 } 078 079 // base class methods for the XNetSerialPortController interface 080 081 @Override 082 public boolean status() { 083 return opened; 084 } 085 086 /** 087 * {@inheritDoc} 088 */ 089 @Override 090 public String[] validBaudRates() { 091 return Arrays.copyOf(validSpeeds, validSpeeds.length); 092 } 093 094 /** 095 * {@inheritDoc} 096 */ 097 @Override 098 public int[] validBaudNumbers() { 099 return Arrays.copyOf(validSpeedValues, validSpeedValues.length); 100 } 101 102 /** 103 * validOption1 controls flow control option. 104 */ 105 protected final String[] validSpeeds = new String[]{Bundle.getMessage("Baud9600"), 106 Bundle.getMessage("Baud19200"), Bundle.getMessage("Baud38400"), 107 Bundle.getMessage("Baud57600"), Bundle.getMessage("Baud115200")}; 108 protected final int[] validSpeedValues = new int[]{9600, 19200, 38400, 57600, 115200}; 109 110 @Override 111 public int defaultBaudIndex() { 112 return 0; 113 } 114 115 // meanings are assigned to these above, so make sure the order is consistent 116 protected final String[] validOption1 = new String[]{Bundle.getMessage("FlowOptionNo"), Bundle.getMessage("FlowOptionHw")}; 117 118 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(EliteAdapter.class); 119 120}