📄 commandmanager.java
字号:
/* * Command.java * * Created on June 13, 2003, 4:21 PM */package gov.nist.examples.bps.reader.baselistener;import java.io.*;import java.util.*;/** * * @author Deruelle Olivier */public class CommandManager { public static final String GET_BASE_VERSION="get the base version"; public static final String GET_BASE_ATTENUATION="get the base attenuation"; public static final String SET_BASE_ATTENUATION="set the base attenuation"; public static final String LONG_ATTENUATION="long_range"; public static final String MEDIUM_ATTENUATION="medium_range"; public static final String SHORTER_ATTENUATION="shorter_range"; public static final String SHORTEST_ATTENUATION="shortest_range"; public static final String SYNCHRONOUS_TRANSFER_MODE="synchronous"; public static final String ASYNCHRONOUS_TRANSFER_MODE="asynchronous"; protected OutputStream os; protected InputStream is; protected BaseCommunicator baseCommunicator; protected Vector activeBadges; /** Creates a new instance of CommandManager */ public CommandManager( OutputStream os, InputStream is,BaseCommunicator baseCommunicator ) { this.os=os; this.is=is; this.baseCommunicator=baseCommunicator; activeBadges=new Vector(); } public void wakeupBase() { try{ int i=1; baseCommunicator.reconnect(); StringBuffer inputBuffer = new StringBuffer(); int newData = 0; SerialDebug.println(); while (i!=10) { SerialDebug.println("Trying to wake up the base..."); char c='\\'; os.write(c); //SerialDebug.println("Sent to the base: - char: \\ (dec/hex code: 92/5C)"); // 2. newData = is.read(); inputBuffer.append((char)newData); String s=new String(inputBuffer); //SerialDebug.println("Received from the base: - char: "+s+" (dec code: "+newData+")"); if ((char)newData=='0') { SerialDebug.println("Base woke up!"); break; } i++; } if (i==10) SerialDebug.println("Base is dead!"); SerialDebug.println(); } catch (Exception e) { e.printStackTrace(); } } /*******************************************************************************/ /******************** GET_BASE_VERSION *********************************/ /*******************************************************************************/ public String getBaseVersion() { try { String baseVersion=null; String majorBaseVersion=null; String minorBaseVersion=null; String productCode=null; wakeupBase(); SerialDebug.println("CommandManager, getBaseVersion() required..."); StringBuffer inputBuffer = new StringBuffer(); int newData = 0; /* Sequence: 1. send: '\' prepare the base for a command 2. receive: 0x30 (=0) 3. send: 0x30 4. send: 'a' 5. receive: 0x30 for correctness, an other byte otherwise is error and stop 6. send: '.' 7. receive: 0x30 8. send: '.' 9. receive: 'a' echo command 10. send: '.' 11. receive: Major version byte 12. send: '.' 13. receive: Minor version byte 14. send: '.' 15. receive: Product code */ // 1. char c='\\'; os.write(c); SerialDebug.println("Sent to the base: - char: \\ (dec/hex code: 92/5C)"); // 2. newData = is.read(); inputBuffer.append((char)newData); String s=new String(inputBuffer); SerialDebug.println("Received from the base: - char: "+s+" (dec code: "+newData+")"); // 3. c='0'; os.write(c); SerialDebug.println("Sent to the base: - char: 0 (dec/hex code: 48/30)"); // 4. c='a'; os.write(c); SerialDebug.println("Sent to the base: - char: a (dec/hex code: 97/61)"); // 5. newData = is.read(); inputBuffer = new StringBuffer(); inputBuffer.append((char)newData); s=new String(inputBuffer); SerialDebug.println("Received from the base: - char: "+s+" (dec code: "+newData+")"); if ((char)newData!='0') { SerialDebug.println("ERROR code returned from the base!!!!"); return null; } // 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. int cpt=1; while (cpt!=6) { c='.'; os.write(c); SerialDebug.println("Sent to the base: - char: . (dec/hex code: 46/2E)"); newData = is.read(); inputBuffer = new StringBuffer(); inputBuffer.append((char)newData); s=new String(inputBuffer); SerialDebug.println("Received from the base: - char: "+s+" (dec code: "+newData+")"); if (cpt==3) { // major version byte: majorBaseVersion=s; } else if (cpt==4) { // minor version byte: minorBaseVersion=s; } else if (cpt==5) { // product code byte: productCode=s; } cpt++; } baseVersion=majorBaseVersion+minorBaseVersion+productCode; return baseVersion; } catch (Exception e) { e.printStackTrace(); return null; } } /*******************************************************************************/ /******************** GET_BASE_RX_ATTENUATION **************************/ /*******************************************************************************/ public String getBaseRxAttenuation() { try { String attenuation=null; wakeupBase(); SerialDebug.println("CommandManager, getBaseRxAttenuation() required..."); StringBuffer inputBuffer = new StringBuffer(); int newData = 0; /* Sequence: 1. send: '\' wake up base 2. receive: 0x30 (=0) 3. send: 0x30 4. send: 'e' 5. receive: 0x30 for correctness, an other byte otherwise is error and stop 6. send: '.' 7. receive: 0x30 8. send: '.' 9. receive: 'e' echo command 10. send: '.' 11. receive: Attenuation */ // 1. char c='\\'; os.write(c); SerialDebug.println("Sent to the base: - char: \\ (dec/hex code: 92/5C)"); // 2. newData = is.read(); inputBuffer.append((char)newData); String s=new String(inputBuffer); SerialDebug.println("Received from the base: - char: "+s+" (dec code: "+newData+")"); // 3. c='0'; os.write(c); SerialDebug.println("Sent to the base: - char: 0 (dec/hex code: 48/30)"); // 4. c='e'; os.write(c); SerialDebug.println("Sent to the base: - char: e (dec/hex code: 101/65)"); // 5. newData = is.read(); inputBuffer = new StringBuffer(); inputBuffer.append((char)newData); s=new String(inputBuffer); SerialDebug.println("Received from the base: - char: "+s+" (dec code: "+newData+")"); if ((char)newData!='0') { SerialDebug.println("ERROR code returned from the base!!!!"); return null; } // 6. 7. 8. 9. 10. 11. int cpt=1; while (cpt!=4) { c='.'; os.write(c); SerialDebug.println("Sent to the base: - char: . (dec/hex code: 46/2E)"); newData = is.read(); inputBuffer = new StringBuffer(); inputBuffer.append((char)newData); s=new String(inputBuffer); SerialDebug.print("Received from the base: -"); if (cpt==3) { SerialDebug.println(" attenuation char: "+s+" (dec code: "+newData+")"); // attenuation byte: if (s.equals("0")) { return LONG_ATTENUATION; } if (s.equals("4") ) { return MEDIUM_ATTENUATION; } if (s.equals("8") ) { return SHORTER_ATTENUATION; } if (s.equals("<") ) { return SHORTEST_ATTENUATION; } } else SerialDebug.println(" char: "+s+" (dec code: "+newData+")"); cpt++; } return null; } catch (Exception e) { e.printStackTrace(); return null; } } /*******************************************************************************/ /******************** SET_BASE_RX_ATTENUATION **************************/ /*******************************************************************************/ public void setBaseRxAttenuation(String range) { try { if (!range.equals(LONG_ATTENUATION) && !range.equals(MEDIUM_ATTENUATION) && !range.equals(SHORTER_ATTENUATION) && !range.equals(SHORTEST_ATTENUATION) ) { SerialDebug.println("ERROR: RANGE MODE is: "+ "CommandManager.LONG_ATTENUATION or "+ "CommandManager.MEDIUM_ATTENUATION or "+ "CommandManager.SHORTER_ATTENUATION or "+ "CommandManager.SHORTEST_ATTENUATION"); return; } wakeupBase(); SerialDebug.println("CommandManager, setBaseRxAttenuation() required..."); StringBuffer inputBuffer = new StringBuffer(); int newData = 0; /* Sequence: 1. send: '\' wake up base 2. receive: 0x30 (=0) 3. send: 0x30 4. send: 'd' 5. send: Attenuation:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -