001package jmri.jmrix.zimo.mxulf; 002 003import java.util.Arrays; 004import jmri.jmrix.zimo.Mx1CommandStation; 005import jmri.jmrix.zimo.Mx1Packetizer; 006import jmri.jmrix.zimo.Mx1PortController; 007import jmri.jmrix.zimo.Mx1SystemConnectionMemo; 008import org.slf4j.Logger; 009import org.slf4j.LoggerFactory; 010 011/** 012 * Provide access to Zimo's MX-1 on an attached serial com port. Normally 013 * controlled by the zimo.mxulf.mxulfFrame class. 014 * 015 * @author Bob Jacobsen Copyright (C) 2002 016 * 017 * Adapted for use with Zimo MXULF by Kevin Dickerson 018 */ 019public class SerialDriverAdapter extends Mx1PortController { 020 021 public SerialDriverAdapter() { 022 super(new Mx1SystemConnectionMemo()); 023 this.manufacturerName = jmri.jmrix.zimo.Mx1ConnectionTypeList.ZIMO; 024 option1Name = "FlowControl"; // NOI18N 025 options.put(option1Name, new Option("MXULF connection uses : ", validOption1)); 026 this.getSystemConnectionMemo().setConnectionType(Mx1SystemConnectionMemo.MXULF); 027 } 028 029 @Override 030 public String openPort(String portName, String appName) { 031 // get and open the primary port 032 currentSerialPort = activatePort(portName, log); 033 if (currentSerialPort == null) { 034 log.error("failed to connect Zimo MXULF to {}", portName); 035 return Bundle.getMessage("SerialPortNotFound", portName); 036 } 037 log.info("Connecting Zimo MXULF 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 FlowControl flow = FlowControl.RTSCTS; // default, but also defaults in selectedOption1 045 if (getOptionState(option1Name).equals(validOption1[1])) { 046 flow = FlowControl.NONE; 047 } 048 setFlowControl(currentSerialPort, flow); 049 050 // report status 051 reportPortStatus(log, portName); 052 053 opened = true; 054 055 return null; // indicates OK return 056 } 057 058 /** 059 * Can the port accept additional characters? The state of CTS determines 060 * this, as there seems to be no way to check the number of queued bytes and 061 * buffer length. This might go false for short intervals, but it might also 062 * stick off if something goes wrong. 063 */ 064 @Override 065 public boolean okToSend() { 066 return currentSerialPort.getCTS(); 067 } 068 069 /** 070 * set up all of the other objects to operate with a MX-1 connected to this 071 * port 072 */ 073 @Override 074 public void configure() { 075 Mx1CommandStation cs = new Mx1CommandStation(getSystemConnectionMemo().getSystemPrefix(), getSystemConnectionMemo().getUserName()); 076 // connect to a packetizing traffic controller 077 Mx1Packetizer packets = new Mx1Packetizer(cs, Mx1Packetizer.BINARY); 078 packets.connectPort(this); 079 080 getSystemConnectionMemo().setMx1TrafficController(packets); 081 getSystemConnectionMemo().configureManagers(); 082 083 // start operation 084 packets.startThreads(); 085 } 086 087 @Override 088 public boolean status() { 089 return opened; 090 } 091 092 /** 093 * {@inheritDoc} 094 */ 095 @Override 096 public String[] validBaudRates() { 097 return Arrays.copyOf(validSpeeds, validSpeeds.length); 098 } 099 100 /** 101 * {@inheritDoc} 102 */ 103 @Override 104 public int[] validBaudNumbers() { 105 return Arrays.copyOf(validSpeedValues, validSpeedValues.length); 106 } 107 108 protected String[] validSpeeds = new String[]{Bundle.getMessage("Baud9600Zimo"), 109 Bundle.getMessage("Baud1200"), Bundle.getMessage("Baud2400"), 110 Bundle.getMessage("Baud4800"), Bundle.getMessage("Baud19200"), 111 Bundle.getMessage("Baud38400")}; 112 protected int[] validSpeedValues = new int[]{9600, 1200, 2400, 4800, 19200, 38400}; 113 114 @Override 115 public int defaultBaudIndex() { 116 return 0; 117 } 118 119 // meanings are assigned to these above, so make sure the order is consistent 120 protected String[] validOption1 = new String[]{Bundle.getMessage("FlowOptionHwRecomm"), Bundle.getMessage("FlowOptionNo")}; 121 122 //protected String selectedOption1=validOption1[0]; 123 124 private final static Logger log = LoggerFactory.getLogger(SerialDriverAdapter.class); 125 126}