4.10. IPeripheralCommandParser

added in version 1.6.0

外部機器制御用コマンドの応答を解析する機能を提供するインターフェイスです。

本インターフェイスは、外部機器に対応しているモデル専用です。 対応しているモデルは 対応外部機器 を参照ください。

  • 列挙体

    名称

    説明

    ParseResult

    解析結果定数

  • メソッド

    名称

    説明

    createSendCommands

    外部機器からの応答を受け取るコマンドを生成します。

    parse

    createSendCommands で生成したコマンドを送信後の、外部機器からのコマンド応答を解析します。

4.10.1. ParseResult

added in version 1.6.0

解析結果定数

  • 宣言

    enum ParseResult {
        Invalid,
        Success,
        Failure
    }
    
  • 定数

    名称

    説明

    Invalid

    不定(コマンド応答未完了)

    Success

    成功

    Failure

    失敗

4.10.2. createSendCommands

added in version 1.6.0

外部機器からの応答を受け取るコマンドを生成します。

  • 宣言

    byte[] createSendCommands();
    
  • 引数

    なし

  • 戻り値

    説明

    生成されたコマンド列

    byte[]

  • 実装例

    public void run() {
        Communication.Result communicateResult = Communication.Result.ErrorOpenPort;
        boolean result = false;
    
        synchronized (mLock) {
            try {
            if (mPort == null) {
                mPort = StarIOPort.getPort(mPortName, mPortSettings, mTimeout, mContext);
            }
    
            communicateResult = Communication.Result.ErrorWritePort;
            StarPrinterStatus status = mPort.retreiveStatus();
            if (status.rawLength == 0) {
                throw new StarIOPortException("Unable to communicate with printer.");
            }
    
            communicateResult = Communication.Result.ErrorWritePort;
            byte[] sendData = mParser.createSendCommands();
            mPort.writePort(sendData, 0, sendData.length);
    
            List<Byte> receiveDataList = new ArrayList<>();
            byte[] readBuffer = new byte[1024];
    
            long start = System.currentTimeMillis(); while (true) {
                if (1000 < (System.currentTimeMillis() - start)) {
                    communicateResult = Communication.Result.ErrorReadPort;
                    break;
                }
    
                try {
                    Thread.sleep(10);
                }
                catch (InterruptedException ignore) { }
    
                int receiveSize = mPort.readPort(readBuffer, 0, readBuffer.length);
    
                if (0 < receiveSize) {
                    for (int i = 0; i < receiveSize; i++) {
                        receiveDataList.add(readBuffer[i]);
                    }
                }
                else {
                    continue;
                }
    
                byte[] receiveData = new byte[receiveDataList.size()];
                int receiveDataLength = receiveDataList.size();
    
                for (int i = 0; i < receiveDataLength; i++) {
                    receiveData[i] = receiveDataList.get(i);
                }
    
                if (mParser.parse(receiveData, receiveDataLength) == ParseResult.Success) {
                    result = true;
                    communicateResult = Communication.Result.Success;
                    break;
                }
            }
            ...
            }
        }
    }
    

    Communication.javaを参照ください。

4.10.3. parse

added in version 1.6.0

createSendCommands で生成したコマンドを送信後の、外部機器からのコマンド応答を解析します。

  • 宣言

    ParseResult parse(byte[] response, int length);
    
  • 引数

    名称

    説明

    response

    コマンド応答

    byte[]

    length

    コマンド応答の長さ

    int