Modbus在Android上的應用之Modbus RTU Master

Modbus RTU

如果不瞭解Modbus協議的同學,可以看我之前寫的文章。Modbus在Android上的應用之Modbus TCP Master

Modbus協議包括ASCII、RTU和TCP等,並沒有規定物理層。此協議定義了控制器能夠認識和使用的消息結構,而不管它們是經過哪種網絡進行通信的。Modbus的ASCII、RTU協議規定了消息、數據結構、命令和應答方式,數據通訊採用Master/Slave方式,Master端發送數據請求消息,Slave端接收到正確的消息之後就可以發送數據到Master端以響應請求,Master端也可以直接發送消息修改Slave端的數據,實現雙向讀寫。

Modbus協議需要對數據進行校驗,串行協議中除了奇偶校驗之外,ASCII模式採用LRC校驗,RTU模式採用16位CRC校驗,但是TCP模式沒有額外規定校驗,因爲TCP協議是一個面向連接的可靠協議。

Modbus RTU協議結構

在這裏插入圖片描述
地址碼:就是從站地址號。每個從機都必須有唯一的地址碼,並且只有符合地址碼的從機才能響應請求。
功能碼:主機發送的功能碼告訴從機執行什麼任務。
一般常用的公共功能代碼如下:(十六進制顯示)
讀線圈狀態----------------01
讀離散輸入寄存器-------02
讀保持寄存器-------------03
讀輸入寄存器-------------04
寫單個線圈----------------05
寫單個保持寄存器-------06
寫多個線圈----------------0F
寫多個保持寄存器-------10
數據區:數據區包含需要從機執行什麼動作或者從機採集的返回信息,這些信息可以是數值、參考地址等。例如:功能碼告訴從機讀取寄存器的值,則數據區必須包含要讀取寄存器的起始地址和讀取寄存器個數。
錯誤校驗碼:主機或者從機可用校驗碼判別接收的信息是否出錯。有時候,由於電子噪聲或者一些其他的干擾,信息在傳輸的過程中可能會發生細微的變化,錯誤校驗碼保證了主機或者從機在對傳輸過程中錯誤的信息不起作用,可以理解爲接收錯誤的信息不執行響應請求,這樣增加了系統的安全和效率。

一般Modbus RTU採用16位CRC校驗。循環冗餘碼(CRC)由發送端計算,放置於發送消息的尾部,接收端對接收的信息重新計算CRC碼嗎,比較計算得到的CRC碼與接收到的是否相符,如果不相符,則表明數據有錯,不進行消息響應。

如何在Android上實現Modbus RTU Master?

  1. 首先要獲取到SerialPort實例
    我之前寫了三種方法可以獲取到SerialPort實例。
    1、快速使用Android串口
    2、Android串口使用2之使用Google官方庫android-serialport-api
    3、Android串口使用3之使用CMake工具完成android-serialport-api庫的移植
//第1種使用serialport第三方庫
public ModbusRtuMaster(SerialHelper serialHelper) {
        this.serialHelper = serialHelper;
}

//第2、3種使用Google官方庫android-serialport-api
public ModbusRtuMaster(SerialPort serialPort) {
        this.serialPort = serialPort;
}

無論是哪種方式獲取,目的都是獲取輸出流,發送數據。

//第1種
this.serialHelper.send(sendBytes);

//第2、3種
this.mOutputStream = this.serialPort.getOutputStream();
this.mOutputStream.write(sendBytes);
  1. 組裝Modbus RTU消息幀
     /**
     * 組裝Modbus RTU消息幀
     * @param slave 從站地址號
     * @param function_code 功能碼
     * @param starting_address 讀取寄存器起始地址 / 寫入寄存器地址 / 寫入寄存器起始地址
     * @param quantity_of_x 讀取寄存器個數 / 寫入寄存器個數
     * @param output_value 需要寫入單個寄存器的數值
     * @param output_values 需要寫入多個寄存器的數組
     * @return 將整個消息幀轉成byte[]
     * @throws ModbusError Modbus錯誤
     */
    synchronized private byte[] execute(int slave, int function_code, int starting_address, int quantity_of_x,
                                        int output_value, int[] output_values) throws ModbusError {
        //檢查參數是否符合協議規定
        if (slave < 0 || slave > 0xff) {
            throw new ModbusError(ModbusErrorType.ModbusInvalidArgumentError, "Invalid slave " + slave);
        }
        if (starting_address < 0 || starting_address > 0xffff) {
            throw new ModbusError(ModbusErrorType.ModbusInvalidArgumentError, "Invalid starting_address " + starting_address);
        }
        if (quantity_of_x < 1 || quantity_of_x > 0xff) {
            throw new ModbusError(ModbusErrorType.ModbusInvalidArgumentError, "Invalid quantity_of_x " + quantity_of_x);
        }

        // 構造request
        ByteArrayWriter request = new ByteArrayWriter();
        //寫入從站地址號
        request.writeInt8(slave);
        //根據功能碼組裝數據區
        //如果爲讀取寄存器指令
        if (function_code == ModbusFunction.READ_COILS || function_code == ModbusFunction.READ_DISCRETE_INPUTS
                || function_code == ModbusFunction.READ_INPUT_REGISTERS || function_code == ModbusFunction.READ_HOLDING_REGISTERS) {
            request.writeInt8(function_code);
            request.writeInt16(starting_address);
            request.writeInt16(quantity_of_x);

        } else if (function_code == ModbusFunction.WRITE_SINGLE_COIL || function_code == ModbusFunction.WRITE_SINGLE_REGISTER) {//寫單個寄存器指令
            if (function_code == ModbusFunction.WRITE_SINGLE_COIL)
                if (output_value != 0) output_value = 0xff00;//如果爲線圈寄存器(寫1時爲 FF 00,寫0時爲00 00)
            request.writeInt8(function_code);
            request.writeInt16(starting_address);
            request.writeInt16(output_value);

        } else if (function_code == ModbusFunction.WRITE_COILS) {//寫多個線圈寄存器
            request.writeInt8(function_code);
            request.writeInt16(starting_address);
            request.writeInt16(quantity_of_x);

            //計算寫入字節數
            int writeByteCount = (quantity_of_x / 8) + 1;/////// 滿足關係-> (w /8) + 1
            //寫入數量 == 8 ,則寫入字節數爲1
            if (quantity_of_x % 8 == 0) {
                writeByteCount -= 1;
            }
            request.writeInt8(writeByteCount);

            int index = 0;
            //如果寫入數據數量 > 8 ,則需要拆分開來
            int start = 0;//數組開始位置
            int end = 7;//數組結束位置
            int[] splitData = new int[8];
            //循環寫入拆分數組,直到剩下最後一組 元素個數 <= 8 的數據
            while (writeByteCount > 1) {
                writeByteCount--;
                int sIndex = 0;
                for (index = start; index <= end; index++){
                    splitData [sIndex++] = output_values[index];
                }
                //數據反轉 對於是否要反轉要看你傳過來的數據,如果高低位順序正確則不用反轉
                splitData = reverseArr(splitData);
                //寫入拆分數組
                request.writeInt8(toDecimal(splitData));
                start = index;
                end += 8;
            }
            //寫入最後剩下的數據
            int last = quantity_of_x - index;
            int[] tData = new int[last];
            System.arraycopy(output_values, index, tData, 0, last);
            //數據反轉 對於是否要反轉要看你傳過來的數據,如果高低位順序正確則不用反轉
            tData = reverseArr(tData);
            request.writeInt8(toDecimal(tData));
        } else if (function_code == ModbusFunction.WRITE_HOLDING_REGISTERS) {//寫多個保持寄存器
            request.writeInt8(function_code);
            request.writeInt16(starting_address);
            request.writeInt16(quantity_of_x);
            request.writeInt8(2 * quantity_of_x);
            //寫入數據
            for (int v : output_values) {
                request.writeInt16(v);
            }
        } else {
            throw new ModbusError(ModbusErrorType.ModbusFunctionNotSupportedError, "Not support function " + function_code);
        }
        byte[] bytes = request.toByteArray();
        //計算CRC校驗碼
        int crc = CRC16.compute(bytes);
        request.writeInt16Reversal(crc);
        bytes = request.toByteArray();
        return bytes;
    }

ModbusErrorType

/**
 * 常見的Modbus通訊錯誤
 */
public enum ModbusErrorType {
    ModbusError,
    ModbusFunctionNotSupportedError,
    ModbusDuplicatedKeyError,
    ModbusMissingKeyError,
    ModbusInvalidBlockError,
    ModbusInvalidArgumentError,
    ModbusOverlapBlockError,
    ModbusOutOfBlockError,
    ModbusInvalidResponseError,
    ModbusInvalidRequestError,
    ModbusTimeoutError
}

ModbusError

public class ModbusError extends Exception {
    private int code;

    public ModbusError(int code, String message) {
        super(!TextUtils.isEmpty(message) ? message : "Modbus Error: Exception code = " + code);
        this.code = code;
    }

    public ModbusError(int code) {
        this(code, null);
    }

    public ModbusError(ModbusErrorType type, String message) {
        super(type.name() + ": " + message);
    }

    public ModbusError(String message) {
        super(message);
    }

    public int getCode() {
        return this.code;
    }
}

ModbusFunction

/**
 * 功能碼(十進制顯示)
 */
public class ModbusFunction {

    //讀線圈寄存器
    public static final int READ_COILS = 1;

    //讀離散輸入寄存器
    public static final int READ_DISCRETE_INPUTS = 2;

    //讀保持寄存器
    public static final int READ_HOLDING_REGISTERS = 3;

    //讀輸入寄存器
    public static final int READ_INPUT_REGISTERS = 4;

    //寫單個線圈寄存器
    public static final int WRITE_SINGLE_COIL = 5;

    //寫單個保持寄存器
    public static final int WRITE_SINGLE_REGISTER = 6;

    //寫入多個線圈寄存器
    public static final int WRITE_COILS = 15;

    //寫入多個保持寄存器
    public static final int WRITE_HOLDING_REGISTERS = 16;
}

ByteArrayWriter

public class ByteArrayWriter extends ByteArrayOutputStream {
    public ByteArrayWriter() {
        super();
    }

    public void writeInt8(byte b)
    {
        this.write(b);
    }

    public void writeInt8(int b)
    {
        this.write((byte)b);
    }

    public void writeInt16(int n) {
        byte[] bytes = ByteUtil.fromInt16(n);
        this.write(bytes, 0, bytes.length);
    }

    public void writeInt16Reversal(int n){
        byte[] bytes=ByteUtil.fromInt16Reversal(n);
        this.write(bytes,0,bytes.length);
    }

    public void writeInt32(int n) {
        byte[] bytes = ByteUtil.fromInt32(n);
        this.write(bytes, 0, bytes.length);
    }

    public void writeBytes(byte[] bs,int len){
        this.write(bs,0,len);
    }

}

ByteUtil

public class ByteUtil {

    public static String toHexString(byte[] input, String separator) {
        if (input==null) return null;

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < input.length; i++) {
            if (separator != null && sb.length() > 0) {
                sb.append(separator);
            }
            String str = Integer.toHexString(input[i] & 0xff);
            if (str.length() == 1) str = "0" + str;
            sb.append(str);
        }
        return sb.toString();
    }

    public static String toHexString(byte[] input) {
        return toHexString(input, " ");
    }

    public static byte[] fromInt32(int input){
        byte[] result=new byte[4];
        result[3]=(byte)(input >> 24 & 0xFF);
        result[2]=(byte)(input >> 16 & 0xFF);
        result[1]=(byte)(input >> 8 & 0xFF);
        result[0]=(byte)(input & 0xFF);
        return result;
    }

    public static byte[] fromInt16(int input){
        byte[] result=new byte[2];
        result[0]=(byte)(input >> 8 & 0xFF);
        result[1]=(byte)(input & 0xFF);
        return result;
    }

    public static byte[] fromInt16Reversal(int input){
        byte[] result=new byte[2];
        result[1]=(byte)(input>>8&0xFF);
        result[0]=(byte)(input&0xFF);
        return result;
    }

}

CRC16


public class CRC16 {
    private static final byte[] crc16_tab_h = { (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,
            (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80,
            (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0,
            (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00,
            (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
            (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81,
            (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0,
            (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01,
            (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
            (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80,
            (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1,
            (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00,
            (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
            (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81,
            (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0,
            (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00,
            (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
            (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81,
            (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0,
            (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00,
            (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
            (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80,
            (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1,
            (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01,
            (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
            (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80,
            (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1,
            (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00,
            (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
            (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81,
            (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1,
            (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01,
            (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
            (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81,
            (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1,
            (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01,
            (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40 };

    private static final byte[] crc16_tab_l = { (byte) 0x00, (byte) 0xC0, (byte) 0xC1, (byte) 0x01,
            (byte) 0xC3, (byte) 0x03, (byte) 0x02, (byte) 0xC2, (byte) 0xC6, (byte) 0x06, (byte) 0x07,
            (byte) 0xC7, (byte) 0x05, (byte) 0xC5, (byte) 0xC4, (byte) 0x04, (byte) 0xCC, (byte) 0x0C,
            (byte) 0x0D, (byte) 0xCD, (byte) 0x0F, (byte) 0xCF, (byte) 0xCE, (byte) 0x0E, (byte) 0x0A,
            (byte) 0xCA, (byte) 0xCB, (byte) 0x0B, (byte) 0xC9, (byte) 0x09, (byte) 0x08, (byte) 0xC8,
            (byte) 0xD8, (byte) 0x18, (byte) 0x19, (byte) 0xD9, (byte) 0x1B, (byte) 0xDB, (byte) 0xDA,
            (byte) 0x1A, (byte) 0x1E, (byte) 0xDE, (byte) 0xDF, (byte) 0x1F, (byte) 0xDD, (byte) 0x1D,
            (byte) 0x1C, (byte) 0xDC, (byte) 0x14, (byte) 0xD4, (byte) 0xD5, (byte) 0x15, (byte) 0xD7,
            (byte) 0x17, (byte) 0x16, (byte) 0xD6, (byte) 0xD2, (byte) 0x12, (byte) 0x13, (byte) 0xD3,
            (byte) 0x11, (byte) 0xD1, (byte) 0xD0, (byte) 0x10, (byte) 0xF0, (byte) 0x30, (byte) 0x31,
            (byte) 0xF1, (byte) 0x33, (byte) 0xF3, (byte) 0xF2, (byte) 0x32, (byte) 0x36, (byte) 0xF6,
            (byte) 0xF7, (byte) 0x37, (byte) 0xF5, (byte) 0x35, (byte) 0x34, (byte) 0xF4, (byte) 0x3C,
            (byte) 0xFC, (byte) 0xFD, (byte) 0x3D, (byte) 0xFF, (byte) 0x3F, (byte) 0x3E, (byte) 0xFE,
            (byte) 0xFA, (byte) 0x3A, (byte) 0x3B, (byte) 0xFB, (byte) 0x39, (byte) 0xF9, (byte) 0xF8,
            (byte) 0x38, (byte) 0x28, (byte) 0xE8, (byte) 0xE9, (byte) 0x29, (byte) 0xEB, (byte) 0x2B,
            (byte) 0x2A, (byte) 0xEA, (byte) 0xEE, (byte) 0x2E, (byte) 0x2F, (byte) 0xEF, (byte) 0x2D,
            (byte) 0xED, (byte) 0xEC, (byte) 0x2C, (byte) 0xE4, (byte) 0x24, (byte) 0x25, (byte) 0xE5,
            (byte) 0x27, (byte) 0xE7, (byte) 0xE6, (byte) 0x26, (byte) 0x22, (byte) 0xE2, (byte) 0xE3,
            (byte) 0x23, (byte) 0xE1, (byte) 0x21, (byte) 0x20, (byte) 0xE0, (byte) 0xA0, (byte) 0x60,
            (byte) 0x61, (byte) 0xA1, (byte) 0x63, (byte) 0xA3, (byte) 0xA2, (byte) 0x62, (byte) 0x66,
            (byte) 0xA6, (byte) 0xA7, (byte) 0x67, (byte) 0xA5, (byte) 0x65, (byte) 0x64, (byte) 0xA4,
            (byte) 0x6C, (byte) 0xAC, (byte) 0xAD, (byte) 0x6D, (byte) 0xAF, (byte) 0x6F, (byte) 0x6E,
            (byte) 0xAE, (byte) 0xAA, (byte) 0x6A, (byte) 0x6B, (byte) 0xAB, (byte) 0x69, (byte) 0xA9,
            (byte) 0xA8, (byte) 0x68, (byte) 0x78, (byte) 0xB8, (byte) 0xB9, (byte) 0x79, (byte) 0xBB,
            (byte) 0x7B, (byte) 0x7A, (byte) 0xBA, (byte) 0xBE, (byte) 0x7E, (byte) 0x7F, (byte) 0xBF,
            (byte) 0x7D, (byte) 0xBD, (byte) 0xBC, (byte) 0x7C, (byte) 0xB4, (byte) 0x74, (byte) 0x75,
            (byte) 0xB5, (byte) 0x77, (byte) 0xB7, (byte) 0xB6, (byte) 0x76, (byte) 0x72, (byte) 0xB2,
            (byte) 0xB3, (byte) 0x73, (byte) 0xB1, (byte) 0x71, (byte) 0x70, (byte) 0xB0, (byte) 0x50,
            (byte) 0x90, (byte) 0x91, (byte) 0x51, (byte) 0x93, (byte) 0x53, (byte) 0x52, (byte) 0x92,
            (byte) 0x96, (byte) 0x56, (byte) 0x57, (byte) 0x97, (byte) 0x55, (byte) 0x95, (byte) 0x94,
            (byte) 0x54, (byte) 0x9C, (byte) 0x5C, (byte) 0x5D, (byte) 0x9D, (byte) 0x5F, (byte) 0x9F,
            (byte) 0x9E, (byte) 0x5E, (byte) 0x5A, (byte) 0x9A, (byte) 0x9B, (byte) 0x5B, (byte) 0x99,
            (byte) 0x59, (byte) 0x58, (byte) 0x98, (byte) 0x88, (byte) 0x48, (byte) 0x49, (byte) 0x89,
            (byte) 0x4B, (byte) 0x8B, (byte) 0x8A, (byte) 0x4A, (byte) 0x4E, (byte) 0x8E, (byte) 0x8F,
            (byte) 0x4F, (byte) 0x8D, (byte) 0x4D, (byte) 0x4C, (byte) 0x8C, (byte) 0x44, (byte) 0x84,
            (byte) 0x85, (byte) 0x45, (byte) 0x87, (byte) 0x47, (byte) 0x46, (byte) 0x86, (byte) 0x82,
            (byte) 0x42, (byte) 0x43, (byte) 0x83, (byte) 0x41, (byte) 0x81, (byte) 0x80, (byte) 0x40 };

    /**
     * 計算CRC16校驗
     *
     * @param data
     *            需要計算的數組
     * @return CRC16校驗值
     */
    public static int compute(byte[] data) {
        return compute(data, 0, data.length);
    }

    /**
     * 計算CRC16校驗
     *
     * @param data
     *            需要計算的數組
     * @param offset
     *            起始位置
     * @param len
     *            長度
     * @return CRC16校驗值
     */
    public static int compute(byte[] data, int offset, int len) {
        return compute(data, offset, len, 0xffff);
    }

    /**
     * 計算CRC16校驗
     *
     * @param data
     *            需要計算的數組
     * @param offset
     *            起始位置
     * @param len
     *            長度
     * @param preval
     *            之前的校驗值
     * @return CRC16校驗值
     */
    public static int compute(byte[] data, int offset, int len, int preval) {
        int ucCRCHi = (preval & 0xff00) >> 8;
        int ucCRCLo = preval & 0x00ff;
        int iIndex;
        for (int i = 0; i < len; ++i) {
            iIndex = (ucCRCLo ^ data[offset + i]) & 0x00ff;
            ucCRCLo = ucCRCHi ^ crc16_tab_h[iIndex];
            ucCRCHi = crc16_tab_l[iIndex];
        }
        int result=((ucCRCHi & 0x00ff) << 8) | (ucCRCLo & 0x00ff) & 0xffff;
        return result;
    }
}

貼上完整代碼

public class ModbusRtuMaster {

    private SerialHelper serialHelper;

    //第1種使用serialport第三方庫
    public ModbusRtuMaster(SerialHelper serialHelper) {
        this.serialHelper = serialHelper;
    }


    /**
     * 組裝Modbus RTU消息幀
     * @param slave 從站地址號
     * @param function_code 功能碼
     * @param starting_address 讀取寄存器起始地址 / 寫入寄存器地址 / 寫入寄存器起始地址
     * @param quantity_of_x 讀取寄存器個數 / 寫入寄存器個數
     * @param output_value 需要寫入單個寄存器的數值
     * @param output_values 需要寫入多個寄存器的數組
     * @return 將整個消息幀轉成byte[]
     * @throws ModbusError Modbus錯誤
     */
    synchronized private byte[] execute(int slave, int function_code, int starting_address, int quantity_of_x,
                                        int output_value, int[] output_values) throws ModbusError {
        //檢查參數是否符合協議規定
        if (slave < 0 || slave > 0xff) {
            throw new ModbusError(ModbusErrorType.ModbusInvalidArgumentError, "Invalid slave " + slave);
        }
        if (starting_address < 0 || starting_address > 0xffff) {
            throw new ModbusError(ModbusErrorType.ModbusInvalidArgumentError, "Invalid starting_address " + starting_address);
        }
        if (quantity_of_x < 1 || quantity_of_x > 0xff) {
            throw new ModbusError(ModbusErrorType.ModbusInvalidArgumentError, "Invalid quantity_of_x " + quantity_of_x);
        }

        // 構造request
        ByteArrayWriter request = new ByteArrayWriter();
        //寫入從站地址號
        request.writeInt8(slave);
        //根據功能碼組裝數據區
        //如果爲讀取寄存器指令
        if (function_code == ModbusFunction.READ_COILS || function_code == ModbusFunction.READ_DISCRETE_INPUTS
                || function_code == ModbusFunction.READ_INPUT_REGISTERS || function_code == ModbusFunction.READ_HOLDING_REGISTERS) {
            request.writeInt8(function_code);
            request.writeInt16(starting_address);
            request.writeInt16(quantity_of_x);

        } else if (function_code == ModbusFunction.WRITE_SINGLE_COIL || function_code == ModbusFunction.WRITE_SINGLE_REGISTER) {//寫單個寄存器指令
            if (function_code == ModbusFunction.WRITE_SINGLE_COIL)
                if (output_value != 0) output_value = 0xff00;//如果爲線圈寄存器(寫1時爲 FF 00,寫0時爲00 00)
            request.writeInt8(function_code);
            request.writeInt16(starting_address);
            request.writeInt16(output_value);

        } else if (function_code == ModbusFunction.WRITE_COILS) {//寫多個線圈寄存器
            request.writeInt8(function_code);
            request.writeInt16(starting_address);
            request.writeInt16(quantity_of_x);

            //計算寫入字節數
            int writeByteCount = (quantity_of_x / 8) + 1;/////// 滿足關係-> (w /8) + 1
            //寫入數量 == 8 ,則寫入字節數爲1
            if (quantity_of_x % 8 == 0) {
                writeByteCount -= 1;
            }
            request.writeInt8(writeByteCount);

            int index = 0;
            //如果寫入數據數量 > 8 ,則需要拆分開來
            int start = 0;//數組開始位置
            int end = 7;//數組結束位置
            int[] splitData = new int[8];
            //循環寫入拆分數組,直到剩下最後一組 元素個數 <= 8 的數據
            while (writeByteCount > 1) {
                writeByteCount--;
                int sIndex = 0;
                for (index = start; index <= end; index++){
                    splitData [sIndex++] = output_values[index];
                }
                //數據反轉 對於是否要反轉要看你傳過來的數據,如果高低位順序正確則不用反轉
                splitData = reverseArr(splitData);
                //寫入拆分數組
                request.writeInt8(toDecimal(splitData));
                start = index;
                end += 8;
            }
            //寫入最後剩下的數據
            int last = quantity_of_x - index;
            int[] tData = new int[last];
            System.arraycopy(output_values, index, tData, 0, last);
            //數據反轉 對於是否要反轉要看你傳過來的數據,如果高低位順序正確則不用反轉
            tData = reverseArr(tData);
            request.writeInt8(toDecimal(tData));
        } else if (function_code == ModbusFunction.WRITE_HOLDING_REGISTERS) {//寫多個保持寄存器
            request.writeInt8(function_code);
            request.writeInt16(starting_address);
            request.writeInt16(quantity_of_x);
            request.writeInt8(2 * quantity_of_x);
            //寫入數據
            for (int v : output_values) {
                request.writeInt16(v);
            }
        } else {
            throw new ModbusError(ModbusErrorType.ModbusFunctionNotSupportedError, "Not support function " + function_code);
        }
        byte[] bytes = request.toByteArray();
        //計算CRC校驗碼
        int crc = CRC16.compute(bytes);
        request.writeInt16Reversal(crc);
        bytes = request.toByteArray();
        return bytes;
    }

    /**
     * 讀多個線圈寄存器
     * @param slave 從站地址
     * @param startAddress 起始地址
     * @param numberOfPoints 讀取線圈寄存器個數
     * @throws ModbusError Modbus錯誤
     */
    public void readCoils(int slave, int startAddress, int numberOfPoints) throws ModbusError {
        byte[] sendBytes = execute(slave, ModbusFunction.READ_COILS, startAddress, numberOfPoints, 0, null);
        this.serialHelper.send(sendBytes);
    }
    //讀單個線圈寄存器
    public void readCoil(int slave, int address) throws ModbusError {
        readCoils(slave, address, 1);
    }

    /**
     * 讀多個保持寄存器
     * @param slave 從站地址
     * @param startAddress 起始地址
     * @param numberOfPoints 讀取保持寄存器個數
     * @throws ModbusError Modbus錯誤
     */
    public void readHoldingRegisters(int slave, int startAddress, int numberOfPoints) throws ModbusError {
        byte[] sendBytes = execute(slave, ModbusFunction.READ_HOLDING_REGISTERS, startAddress, numberOfPoints, 0, null);
        this.serialHelper.send(sendBytes);
    }
    //讀單個保持寄存器
    public void readHoldingRegister(int slave, int address) throws ModbusError {
        readHoldingRegisters(slave, address, 1);
    }


    /**
     * 讀多個輸入寄存器
     * @param slave 從站地址
     * @param startAddress 起始地址
     * @param numberOfPoints 讀取輸入寄存器個數
     * @throws ModbusError Modbus錯誤
     */
    public void readInputRegisters(int slave, int startAddress, int numberOfPoints) throws ModbusError {
        byte[] sendBytes = execute(slave, ModbusFunction.READ_INPUT_REGISTERS, startAddress, numberOfPoints, 0, null);
        this.serialHelper.send(sendBytes);
    }
    //讀單個輸入寄存器
    public void readInputRegister(int slave, int address) throws ModbusError {
        readInputRegisters(slave, address, 1);
    }

    /**
     * 讀多個離散輸入寄存器
     * @param slave 從站地址
     * @param startAddress 起始地址
     * @param numberOfPoints 讀取離散輸入寄存器個數
     * @throws ModbusError Modbus錯誤
     */
    public void readDiscreteInputs(int slave, int startAddress, int numberOfPoints) throws ModbusError {
        byte[] sendBytes = execute(slave, ModbusFunction.READ_DISCRETE_INPUTS, startAddress, numberOfPoints, 0, null);
        this.serialHelper.send(sendBytes);
    }
    //讀單個離散輸入寄存器
    public void readDiscreteInput(int slave, int address) throws ModbusError {
        readDiscreteInputs(slave, address, 1);
    }

    /**
     * 寫單個線圈寄存器
     * @param slave 從站地址
     * @param address 寫入寄存器地址
     * @param value 寫入值(true/false)
     * @throws ModbusError Modbus錯誤
     */
    public void writeSingleCoil(int slave, int address, boolean value) throws ModbusError {
        byte[] sendBytes = execute(slave, ModbusFunction.WRITE_SINGLE_COIL, address, 1, value ? 1 : 0, null);
        this.serialHelper.send(sendBytes);
    }

    /**
     * 寫單個保持寄存器
     * @param slave 從站地址
     * @param address 寫入寄存器地址
     * @param value 寫入值
     * @throws ModbusError Modbus錯誤
     */
    public void writeSingleRegister(int slave, int address, int value) throws ModbusError {
        byte[] sendBytes = execute(slave, ModbusFunction.WRITE_SINGLE_REGISTER, address, 1, value, null);
        this.serialHelper.send(sendBytes);
    }

    /**
     *   寫入多個保持寄存器
     * @param slave 從站地址
     * @param address 寫入寄存器地址
     * @param sCount 寫入寄存器個數
     * @param data 寫入數據
     * @throws ModbusError
     */
    public void writeHoldingRegisters(int slave, int address, int sCount, int [] data) throws ModbusError {
        byte[] sendBytes = execute(slave, ModbusFunction.WRITE_HOLDING_REGISTERS, address, sCount, 0, data);
        this.serialHelper.send(sendBytes);
    }

    /**
     *   寫入多個位
     * @param slave 從站地址
     * @param address 寫入寄存器地址
     * @param bCount 寫入寄存器個數
     * @param data 寫入數據{1,0}
     * @throws ModbusError
     */
    public void writeCoils(int slave, int address,int bCount, int [] data) throws ModbusError {
        byte[] sendBytes = execute(slave, ModbusFunction.WRITE_COILS, address, bCount, 0, data);
        this.serialHelper.send(sendBytes);
    }



    //將數組反轉
    private int[] reverseArr(int[] arr) {
        int[] tem = new int[arr.length];
        for(int i=0; i<arr.length; i++) {
            tem[i] = arr[arr.length-1-i];
        }
        return tem;
    }

    //將int[1,0,0,1,1,0]數組轉換爲十進制數據
    private int toDecimal(int[] data){
        int result = 0;
        if(data != null){
            StringBuilder sData = new StringBuilder();
            for (int d : data){
                sData.append(d);
            }
            try {
                result = Integer.parseInt(sData.toString(), 2);
            } catch (NumberFormatException e) {
                result = -1;
            }

        }
        return result;
    }

}

測試小栗子

        //讀輸出位
        modbusRtuMaster.readCoils(1, 17, 4);
        //讀保持寄存器
        modbusRtuMaster.readHoldingRegisters(1, 27, 4);
        //寫單個保持寄存器
        modbusRtuMaster.writeSingleRegister(1, 27, 1);
        //寫多個保持寄存器
        modbusRtuMaster.writeHoldingRegisters(1, 27, 4, new int[]{1, 2, 3, 4});
        //寫單個位(寫1時爲 FF 00,寫0時爲00 00)
        modbusRtuMaster.writeSingleCoil(1,0, true);
        //寫多個位 每隔8位從右到左取值,所以這個最後需要反轉
        modbusRtuMaster.writeCoils(1, 0, 16, new int[]{0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0});

信息輸出:

>>>>>>>>>>>Send Data = 01 01 00 11 00 04 6d cc
>>>>>>>>>>>Send Data = 01 03 00 1b 00 04 34 0e
>>>>>>>>>>>Send Data = 01 06 00 1b 00 01 38 0d
>>>>>>>>>>>Send Data = 01 10 00 1b 00 04 08 00 01 00 02 00 03 00 04 0a 9d
>>>>>>>>>>>Send Data = 01 05 00 00 ff 00 8c 3a
>>>>>>>>>>>Send Data = 01 0f 00 00 00 10 02 08 06 65 e2

好了,這篇文章源代碼比較多,非常感謝你能看到最後。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章