001package jmri.jmrix.srcp; 002 003/** 004 * Encodes a message to an SRCP server. The SRCPReply class handles the response 005 * from the command station. 006 * 007 * The {@link SRCPReply} class handles the response from the command station. 008 * 009 * @author Bob Jacobsen Copyright (C) 2001, 2004, 2008 010 */ 011public class SRCPMessage extends jmri.jmrix.AbstractMRMessage { 012 013 public SRCPMessage() { 014 super(); 015 } 016 017 // create a new one 018 public SRCPMessage(int i) { 019 super(i); 020 } 021 022 // copy one 023 public SRCPMessage(SRCPMessage m) { 024 super(m); 025 } 026 027 // from String 028 public SRCPMessage(String m) { 029 super(m); 030 } 031 032 // diagnose format 033 /** 034 * Detrmine if the message turns off track power 035 * @return true if the messages is a track power off message,false otherwise 036 */ 037 public boolean isKillMain() { 038 String s = toString(); 039 return s.contains("POWER OFF") && s.contains("SET"); 040 } 041 042 /** 043 * Detrmine if the message turns on track power 044 * @return true if the messages is a track power on message,false otherwise 045 */ 046 public boolean isEnableMain() { 047 String s = toString(); 048 return s.contains("POWER ON") && s.contains("SET"); 049 } 050 051 // static methods to return a formatted message 052 053 /** 054 * @return an SRCPMessage to turn the track power on 055 */ 056 static public SRCPMessage getEnableMain() { 057 SRCPMessage m = new SRCPMessage("SET 1 POWER ON\n"); 058 m.setBinary(false); 059 return m; 060 } 061 062 /** 063 * @return an SRCPMessage to turn the track power off 064 */ 065 static public SRCPMessage getKillMain() { 066 SRCPMessage m = new SRCPMessage("SET 1 POWER OFF\n"); 067 m.setBinary(false); 068 return m; 069 } 070 071 072 /** 073 * @param bus a bus number 074 * @return an SRCPMessage to initialize programming on the given bus. 075 */ 076 static public SRCPMessage getProgMode(int bus) { 077 String msg = "INIT " + bus + " SM NMRA\n"; 078 SRCPMessage m = new SRCPMessage(msg); 079 return m; 080 } 081 082 /** 083 * @param bus a bus number 084 * @return an SRCPMessage to terminate programming on the given bus. 085 */ 086 static public SRCPMessage getExitProgMode(int bus) { 087 String msg = "TERM " + bus + " SM\n"; 088 SRCPMessage m = new SRCPMessage(msg); 089 return m; 090 } 091 092 /** 093 * @param bus a bus number 094 * @param cv the CV to read. 095 * @return an SRCPMessage to read the given CV in direct mode the given bus. 096 */ 097 static public SRCPMessage getReadDirectCV(int bus, int cv) { 098 String msg = "GET " + bus + " SM 0 CV " + cv + "\n"; 099 SRCPMessage m = new SRCPMessage(msg); 100 m.setTimeout(LONG_TIMEOUT); 101 return m; 102 } 103 104 /** 105 * @param bus a bus number 106 * @param cv the CV to read. 107 * @param val a value for the CV. 108 * @return an SRCPMessage to check the given cv has the given val using the given bus. 109 */ 110 static public SRCPMessage getConfirmDirectCV(int bus, int cv, int val) { 111 String msg = "VERIFY " + bus + " SM 0 CV " + cv + " " + val + "\n"; 112 SRCPMessage m = new SRCPMessage(msg); 113 m.setTimeout(LONG_TIMEOUT); 114 return m; 115 116 } 117 118 /** 119 * @param bus a bus number 120 * @param cv the CV to write. 121 * @param val a value for the CV. 122 * @return an SRCPMessage to write the given value to the provided cv using the given bus. 123 */ 124 static public SRCPMessage getWriteDirectCV(int bus, int cv, int val) { 125 String msg = "SET " + bus + " SM 0 CV " + cv + " " + val + "\n"; 126 SRCPMessage m = new SRCPMessage(msg); 127 m.setTimeout(LONG_TIMEOUT); 128 return m; 129 } 130 131 /** 132 * @param bus a bus number 133 * @param cv the CV to read. 134 * @param bit the bit to read. 135 * @return an SRCPMessage to read the given bit of the given CV uisng the provided bus. 136 */ 137 static public SRCPMessage getReadDirectBitCV(int bus, int cv, int bit) { 138 String msg = "GET " + bus + " SM 0 CVBIT " + cv + " " + bit + "\n"; 139 SRCPMessage m = new SRCPMessage(msg); 140 m.setTimeout(LONG_TIMEOUT); 141 return m; 142 } 143 144 /** 145 * @param bus a bus number 146 * @param cv the CV to read. 147 * @param bit the bit to read. 148 * @param val the value to check 149 * @return an SRCPMessage to verify the given bit of the given CV has the given val uisng the provided bus. 150 */ 151 static public SRCPMessage getConfirmDirectBitCV(int bus, int cv, int bit, int val) { 152 String msg = "VERIFY " + bus + " SM 0 CVBIT " + cv + " " + bit + " " + val + "\n"; 153 SRCPMessage m = new SRCPMessage(msg); 154 m.setTimeout(LONG_TIMEOUT); 155 return m; 156 157 } 158 159 /** 160 * @param bus a bus number 161 * @param cv the CV to write. 162 * @param bit the bit to write 163 * @param val the value to write 164 * @return an SRCPMessage to write the given value to the given bit of the given CV uisng the provided bus. 165 */ 166 static public SRCPMessage getWriteDirectBitCV(int bus, int cv, int bit, int val) { 167 String msg = "SET " + bus + " SM 0 CVBIT " + cv + " " + bit + " " + val + "\n"; 168 SRCPMessage m = new SRCPMessage(msg); 169 m.setTimeout(LONG_TIMEOUT); 170 return m; 171 } 172 173 /** 174 * @param bus a bus number 175 * @param reg a register to read. Restricted to valuse less than 8. 176 * @return an SRCPMessage to read the provided register using the given bus. 177 * @throws IllegalArgumentException if the register value is out of range. 178 */ 179 static public SRCPMessage getReadRegister(int bus, int reg) { 180 if (reg > 8) { 181 throw new IllegalArgumentException("register number too large: " + reg); 182 } 183 String msg = "GET " + bus + " SM 0 REG " + reg + "\n"; 184 SRCPMessage m = new SRCPMessage(msg); 185 m.setTimeout(LONG_TIMEOUT); 186 return m; 187 } 188 189 /** 190 * @param bus a bus number 191 * @param reg a register to read. Restricted to valuse less than 8. 192 * @param val a value for the register 193 * @return an SRCPMessage to verify the provided register has the expected val using the given bus. 194 * @throws IllegalArgumentException if the register value is out of range. 195 */ 196 static public SRCPMessage getConfirmRegister(int bus, int reg, int val) { 197 if (reg > 8) { 198 throw new IllegalArgumentException("register number too large: " + reg); 199 } 200 String msg = "VERIFY " + bus + " SM 0 REG " + reg + " " + val + "\n"; 201 SRCPMessage m = new SRCPMessage(msg); 202 m.setTimeout(LONG_TIMEOUT); 203 return m; 204 } 205 206 /** 207 * @param bus a bus number 208 * @param reg a register to write. Restricted to valuse less than 8. 209 * @param val a value for the register 210 * @return an SRCPMessage to write the given value to the provided register using the given bus. 211 * @throws IllegalArgumentException if the register value is out of range. 212 */ 213 static public SRCPMessage getWriteRegister(int bus, int reg, int val) { 214 if (reg > 8) { 215 throw new IllegalArgumentException("register number too large: " + reg); 216 } 217 String msg = "SET " + bus + " SM 0 REG " + reg + " " + val + "\n"; 218 SRCPMessage m = new SRCPMessage(msg); 219 m.setTimeout(LONG_TIMEOUT); 220 return m; 221 } 222 223 @SuppressWarnings("hiding") // changes constant value from superclass 224 static final int LONG_TIMEOUT = 180000; // e.g. for programming options 225 226 // private final static Logger log = LoggerFactory.getLogger(SRCPMessage.class); 227 228} 229 230