Lte3000CommandHelper.java 5.31 KB
package com.example.mina.helper;

import org.apache.commons.lang3.ArrayUtils;

import java.nio.charset.StandardCharsets;

public class Lte3000CommandHelper {

//20-  21-! 22-" 23-# 24-$ 25-% 26-& 27-' 28-( 29-) 2a-* 2b-+ 2c-, 2d-- 2e-. 2f-/
//30-0 31-1 32-2 33-3 34-4 35-5 36-6 37-7 38-8 39-9
// 3a-: 3b-; 3c-< 3d-= 3e-> 3f-? 40-@
// 41-A 42-B 43-C 44-D 45-E 46-F 47-G 48-H 49-I 4a-J 4b-K 4c-L 4d-M 4e-N 4f-O 50-P 51-Q 52-R 53-S 54-T 55-U 56-V 57-W 58-X 59-Y 5a-Z
// 5b-[ 5c-\ 5d-] 5e-^ 5f-_ 60-`
// 61-a 62-b 63-c 64-d 65-e 66-f 67-g 68-h 69-i 6a-j 6b-k 6c-l 6d-m 6e-n 6f-o 70-p 71-q 72-r 73-s 74-t 75-u 76-v 77-w 78-x 79-y 7a-z
// 7b-{ 7c-| 7d-} 7e-~

    ////def for Quinteck:
//#define STX 0x02
//#define ETX 0x03
//#define ACK 0x06
//#define NAK 0x15
//#define RBM_ADDR_H 0x50
//#define RBM_ADDR_L 0x00
//#define NEXUS_ADDR_H 0x40
//#define NEXUS_ADDR_L 0x00

    public static final byte STX = 0X02;
    public static final byte ETX = 0X03;
    public static final byte ACK = 0X06;
    public static final byte NAK = 0X15;

    public static void main(String[] args) {
        byte[] bs = Lte3000CommandHelper.genCommandGetCross(1);



        System.exit(0);

    }

    public static void setCHK(byte[] bs) {
        bs[bs.length - 1] = 0;

        for (int i = 0; i < bs.length - 1; i++) {
            bs[bs.length - 1] ^= bs[i];
        }
    }

    public static int getBcdPort(byte a, byte b, byte c) {
        //return (CommandHelper.BCD_REV[a] - 30) * 100 + (CommandHelper.BCD_REV[b] - 30) * 10 + (CommandHelper.BCD_REV[c] - 30);
        return Integer.parseInt(new StringBuilder().append(a - 0x30).append(b - 0x30).append(c - 0x30).toString());
    }

    /**
     * input: "-12.2"
     * @param a sign
     * @param b tenth
     * @param c single
     * @param d dot
     * @param e tenths
     * @return int -12 if -12.2
     */
    public static int getBcdAttn(byte a, byte b, byte c, byte d, byte e) {
        String port = new StringBuilder()
                //.append((char)a) // sign -/+
                .append(b - 0x30).append(c - 0x30) // integer
                //.append((char)d) // .
                //.append(e - 0x30)
                .toString(); // tenths.
        return Integer.parseInt(port);
    }

    public static byte[] genCommandSetAttn(int col, int val){
        byte[] command = new byte[18];
        byte[] vCol = intToReversedBytes(col);
        byte[] vVal = intToReversedBytes(val);
        command[0] = STX;
        command[1] = 0x30;
        command[2] = 0x30;
        command[3] = 'X';
        command[4] = 'G';
        command[5] = 'M';
        command[6] = 'O';
        command[7] = vCol.length > 2 ? vCol[2] : 0x30;
        command[8] = vCol.length > 1 ? vCol[1] : 0x30;
        command[9] = vCol.length > 0 ? vCol[0] : 0x30;
        command[10] = '@';
        command[11] = '-';
        command[12] = vVal.length > 1 ? vVal[1] : 0x30;
        command[13] = vVal.length > 0 ? vVal[0] : 0x30;
        command[14] = '.';
        command[15] = '0';
        command[16] = ETX;
        command[17] = 0;

        setCHK(command);

        return command;
    }

    public static byte[] genCommandGetAttn(int col){
        byte[] command = new byte[12];
        byte[] vCol = intToReversedBytes(col);
        command[0] = STX;
        command[1] = 0x30;
        command[2] = 0x30;
        command[3] = 'X';
        command[4] = 'G';
        command[5] = 'R';
        command[6] = 'O';
        command[7] = vCol.length > 2 ? vCol[2] : 0x30;
        command[8] = vCol.length > 1 ? vCol[1] : 0x30;
        command[9] = vCol.length > 0 ? vCol[0] : 0x30;
        command[10] = ETX;
        command[11] = 0;

        setCHK(command);

        return command;
    }


    public static byte[] genCommandSetCross(int row, int col){
        byte[] command = new byte[12];
        byte[] vRow = intToReversedBytes(row);
        byte[] vCol = intToReversedBytes(col);
        command[0] = STX;
        command[1] = 0x30;
        command[2] = 0x30;
        command[3] = 'S';
        command[4] = vCol.length > 2 ? vCol[2] : 0x30;
        command[5] = vCol.length > 1 ? vCol[1] : 0x30;
        command[6] = vCol.length > 0 ? vCol[0] : 0x30;
        command[7] = vRow.length > 2 ? vRow[2] : 0x30;
        command[8] = vRow.length > 1 ? vRow[1] : 0x30;
        command[9] = vRow.length > 0 ? vRow[0] : 0x30;
        command[10] = ETX;
        command[11] = 0;

        setCHK(command);

        return command;
    }

    /**
     * 1234 => "1234" => ['4','3','2','1']
     * 1 => [31]
     * 12 => [32,31]
     * 123 => [33,32,31]
     * @param value value to convert
     * @return result
     */
    public static byte[] intToReversedBytes(int value) {
        byte[] result = String.valueOf(value).getBytes(StandardCharsets.US_ASCII);
        ArrayUtils.reverse(result);
        return result;
    }

    public static byte[] genCommandGetCross(int col){
        byte[] command = new byte[9];
        byte[] value = String.valueOf(col).getBytes(StandardCharsets.US_ASCII);
        ArrayUtils.reverse(value);
        command[0] = STX;
        command[1] = 0x30;
        command[2] = 0x30;
        command[3] = 'O';
        command[4] = value.length >= 3 ? value[2] : 0x30;
        command[5] = value.length >= 2 ? value[1] : 0x30;
        command[6] = value.length >= 1 ? value[0] : 0x30;
        command[7] = ETX;
        command[8] = 0;

        setCHK(command);

        return command;
    }



}