4.10. IPeripheralCommandParser

added in version 1.6.0

An interface to provide functions to generate commands for the peripheral control.

This interface is only for models which support the peripheral. For the supporting models, refer to Supported Peripherals .

  • Constant

    Name

    Contents

    ParseResult

    Parse result constants

  • Method

    Name

    Contents

    createSendCommands

    Generates a command to receive the response from the peripheral.

    parse

    Analyzes the response of the command (command generated with the createSendCommands ) for the peripheral control.

4.10.1. ParseResult

added in version 1.6.0

Parse result constants

  • Declaration

    enum ParseResult {
        Invalid,
        Success,
        Failure
    }
    
  • Constants

    Name

    Contents

    Invalid

    Parse invalid

    Success

    Parse success

    Failure

    Parse failure

4.10.2. createSendCommands

added in version 1.6.0

Generates a command to receive the response from the peripheral.

  • Declaration

    byte[] createSendCommands();
    
  • Parameter

    None

  • Return value

    Contents

    Type

    Generated command.

    byte[]

  • Examples

    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;
                }
            }
            ...
            }
        }
    }
    

    Refer to Communication.java.

4.10.3. parse

added in version 1.6.0

Analyzes the response of the command (command generated with the createSendCommands ) for the peripheral control.

  • Declaration

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

    Name

    Contents

    Type

    response

    Command response

    byte[]

    length

    Length of the command response

    int