Modbus通信协议:

  • 主要分为三个子协议
    • RTU
    • ASCII
    • TCP

Modbus RTU:——传输的是字节数组(bit[])
通信:读写
输出:可以读写
输入:只能读

  • 存储区:输出线圈、输入线圈、输出寄存器、输入寄存器

    • 线圈:代表一个布尔量、最小单位是一个布尔(1或者0),
    • 寄存器:一个寄存器代表16个最小单位,主要用于存储数据
    • 存储区代号:
      • 输出线圈: 0(代号)
        • 00001-09999(标准存储区地址范围)
        • 000001-065536(扩展存储区地址范围)
      • 输入线圈:1
        • 10001-19999
      • 输出寄存器:4
        • 40001-49999
      • 输入寄存器:3
        • 30001-39999
  • 存储区范围:5位和6位

    • 5位:标准地址-Y XXXX
    • 6位:扩展地址-Y XXXXX

功能代码

验证4个常用功能码,仿真软件上面有F=01,F=02,F=03和F=04来显示

0x01:读线圈
0x02:读离散量输入
0x03:读保持寄存器
0x04:读输入寄存器

测试使用Modbus Slave(下载) 模拟

saveid:看资料”从站在modbus总线上可以有多个”,仿真软件就能模拟一个从站,就是ID=1,当然可以修改成ID=2
功能码:4个功能码,对应写4个方法,,仿真软件上的F=1,或者F=2,3,4
addr:一开始看代码4个方法addr都是从0开始,是否重复?答案是:4个功能码表示4个区域或者设备,addr表示各自区域的地址编号。

  • Connection设置:设置连接参数

  • 地址设置: F8 ,可以通过Setup或者右击点位的

  • 设置不同存储区
  • 读取从站

代码实现

1、依赖下载

阿里云不能直接下载
<repositories><repository><releases><enabled>false</enabled></releases><snapshots><enabled>true</enabled></snapshots><id>ias-snapshots</id><name>Infinite Automation Snapshot Repository</name><url>https://maven.mangoautomation.net/repository/ias-snapshot/</url></repository><repository><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots><id>ias-releases</id><name>Infinite Automation Release Repository</name><url>https://maven.mangoautomation.net/repository/ias-release/</url></repository></repositories><dependencies> <dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13-beta-3</version><scope>test</scope></dependency><dependency><groupId>com.infiniteautomation</groupId><artifactId>modbus4j</artifactId><version>3.0.3</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.9</version></dependency></dependencies>

2、测试读取

  • 读取04(寄存器)
import com.intelligt.modbus.jlibmodbus.Modbus;import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;import com.intelligt.modbus.jlibmodbus.master.ModbusMaster;import com.intelligt.modbus.jlibmodbus.master.ModbusMasterFactory;import com.intelligt.modbus.jlibmodbus.tcp.TcpParameters;import java.net.InetAddress;import java.net.UnknownHostException;public class TestModbus {public static void main(String[] args) throws UnknownHostException {// 设置主机TCP参数TcpParameters tcpParameters = new TcpParameters();// 设置TCP的ip地址InetAddress address = InetAddress.getByName("127.0.0.1");// TCP参数设置ip地址tcpParameters.setHost(address);// TCP设置长连接tcpParameters.setKeepAlive(true);// TCP设置端口,这里设置是默认端口502tcpParameters.setPort(Modbus.TCP_PORT);// 创建一个主机ModbusMaster master = ModbusMasterFactory.createModbusMasterTCP(tcpParameters);Modbus.setAutoIncrementTransactionId(true);int slaveId = 1;//从机地址int offset = 0;//寄存器读取开始地址int quantity = 10;//读取的寄存器数量try {if (!master.isConnected()){master.connect(); //开启连接}// 读取对应从机的数据,readInputRegisters读取的写寄存器,功能码04int[] registerValues4 = master.readInputRegisters(slaveId,offset,quantity);for (int value : registerValues4){System.out.println("Address:"+offset++ +",Value:"+value);}}catch (Exception e){e.printStackTrace();}finally {try {master.disconnect();} catch (ModbusIOException e) {e.printStackTrace();}}}}

3、读取工具类

import com.serotonin.modbus4j.BatchRead;import com.serotonin.modbus4j.BatchResults;import com.serotonin.modbus4j.ModbusFactory;import com.serotonin.modbus4j.ModbusMaster;import com.serotonin.modbus4j.code.DataType;import com.serotonin.modbus4j.exception.ErrorResponseException;import com.serotonin.modbus4j.exception.ModbusInitException;import com.serotonin.modbus4j.exception.ModbusTransportException;import com.serotonin.modbus4j.ip.IpParameters;import com.serotonin.modbus4j.locator.BaseLocator;public class Modbus4jReadUtils {static ModbusFactory modbusFactory;static {if (modbusFactory == null){modbusFactory = new ModbusFactory();}}/**** 初始化连接 * @return ModbusMaster*/public static ModbusMaster getMaster() throws ModbusInitException{IpParameters params = new IpParameters();params.setHost("localhost");params.setPort(502);// modbusFactory.createRtuMaster(wapper); //RTU 协议// modbusFactory.createUdpMaster(params);//UDP 协议// modbusFactory.createAsciiMaster(wrapper);//ASCII 协议//ModbusMaster master = modbusFactory.creaModbusMaster master = modbusFactory.createTcpMaster(params,false);master.init();return master;}/*** *读取[01 Coil Status 0x]类型 开关数据 * @author zhengfuping * @param slaveId 从站编号 * @param offset 偏移位置 * @return Boolean */public static Boolean readCoilStatus(int slaveId,int offset) throws ModbusInitException, ErrorResponseException, ModbusTransportException {BaseLocator<Boolean> loc = BaseLocator.coilStatus(slaveId,offset);Boolean value = getMaster().getValue(loc);return value;}/** * 读取[02 Input Status 1x]类型 开关数据 * * @param slaveId 从站编号 * @param offset 偏移位 * @return */public static Boolean readInputStatus(int slaveId, int offset)throws ModbusTransportException, ErrorResponseException, ModbusInitException {// 02 Input StatusBaseLocator<Boolean> loc = BaseLocator.inputStatus(slaveId, offset);Boolean value = getMaster().getValue(loc);return value;}/** * 读取[03 Holding Register类型 2x]模拟量数据 * @param slaveId * @param offset 位置 * @param dataType 数据类型,来自com.serotonin.modbus4j.code.DataType */public static Number readHoldingRegister(int slaveId, int offset, int dataType)throws ModbusTransportException, ErrorResponseException, ModbusInitException {// 03 Holding Register类型数据读取BaseLocator<Number> loc = BaseLocator.holdingRegister(slaveId, offset, dataType);Number value = getMaster().getValue(loc);return value;}/** * 读取[04 Input Registers 3x]类型 模拟量数据 */public static Number readInputRegisters(int slaveId, int offset, int dataType)throws ModbusTransportException, ErrorResponseException, ModbusInitException {// 04 Input Registers类型数据读取BaseLocator<Number> loc = BaseLocator.inputRegister(slaveId, offset, dataType);Number value = getMaster().getValue(loc);return value;}//批量读取public static void batchRead(Integer count) throws ModbusInitException, ErrorResponseException, ModbusTransportException {BatchRead<Integer> batch = new BatchRead<Integer>();for (int i = 0; i <= count; i++) {batch.addLocator(i,BaseLocator.holdingRegister(1,i, DataType.TWO_BYTE_INT_SIGNED));}ModbusMaster master =getMaster();batch.setContiguousRequests(false);BatchResults<Integer> results = master.send(batch);for (int i = 0; i <= count; i++) {System.err.println(results.getValue(i));}}public static void main(String[] args) {try {// 01测试Boolean v011 = readCoilStatus(1,1);Boolean v012 = readCoilStatus(1,2);Boolean v013 = readCoilStatus(1,3);System.out.println("v011:" + v011);System.out.println("v012:" + v012);System.out.println("v013:" + v013);// 02测试Boolean v021 = readInputStatus(1, 1);Boolean v022 = readInputStatus(1, 2);Boolean v023 = readInputStatus(1, 3);System.out.println("v021:" + v021);System.out.println("v022:" + v022);System.out.println("v023:" + v023);// 03测试Number v031 = readHoldingRegister(1, 1, DataType.TWO_BYTE_INT_SIGNED);//signedNumber v032 = readHoldingRegister(1, 2, DataType.TWO_BYTE_INT_SIGNED);// 同上System.out.println("v031:" + v031);System.out.println("v032:" + v032);// 04测试Number v041 = readInputRegisters(1, 1, DataType.TWO_BYTE_INT_SIGNED);//Number v042 = readInputRegisters(1, 2, DataType.TWO_BYTE_INT_SIGNED);//System.out.println("v041:" + v041);System.out.println("v042:" + v042);//批量读取batchRead(9);} catch (Exception e) {e.printStackTrace();}}}

读取结果:

4、写入工具类

import com.serotonin.modbus4j.ModbusFactory;import com.serotonin.modbus4j.ModbusMaster;import com.serotonin.modbus4j.code.DataType;import com.serotonin.modbus4j.exception.ErrorResponseException;import com.serotonin.modbus4j.exception.ModbusInitException;import com.serotonin.modbus4j.exception.ModbusTransportException;import com.serotonin.modbus4j.ip.IpParameters;import com.serotonin.modbus4j.locator.BaseLocator;import com.serotonin.modbus4j.msg.*;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class Modbus4jWriteUtils {static Log log = LogFactory.getLog(Modbus4jWriteUtils.class);/** * 工厂。 */static ModbusFactory modbusFactory;static {if (modbusFactory == null) {modbusFactory = new ModbusFactory();}}/** * 获取tcpMaster * * @return * @throws ModbusInitException */public static ModbusMaster getMaster() throws ModbusInitException {IpParameters params = new IpParameters();params.setHost("localhost");params.setPort(502);ModbusMaster tcpMaster = modbusFactory.createTcpMaster(params, false);tcpMaster.init();return tcpMaster;}/** * 写 [01 Coil Status(0x)]写一个 function ID = 5 * @param slaveId slave的ID * @param writeOffset 位置 * @param writeValue 值 */public static boolean writeCoil(int slaveId, int writeOffset, boolean writeValue)throws ModbusTransportException, ModbusInitException {// 获取masterModbusMaster tcpMaster = getMaster();// 创建请求WriteCoilRequest request = new WriteCoilRequest(slaveId, writeOffset, writeValue);// 发送请求并获取响应对象WriteCoilResponse response = (WriteCoilResponse) tcpMaster.send(request);if (response.isException()) {return false;} else {return true;}}/** * 写[01 Coil Status(0x)] 写多个 function ID = 15 * @param slaveId * @param startOffset开始位置 * @param bdata 写入的数据 * @return 是否写入成功 */public static boolean writeCoils(int slaveId, int startOffset, boolean[] bdata)throws ModbusTransportException, ModbusInitException {// 获取masterModbusMaster tcpMaster = getMaster();// 创建请求WriteCoilsRequest request = new WriteCoilsRequest(slaveId, startOffset, bdata);// 发送请求并获取响应对象WriteCoilsResponse response = (WriteCoilsResponse) tcpMaster.send(request);if (response.isException()) {return false;} else {return true;}}/*** * 写[03 Holding Register(4x)] 写一个 function ID = 6 * @param slaveId * @param writeOffset * @param writeValue */public static boolean writeRegister(int slaveId, int writeOffset, short writeValue)throws ModbusTransportException, ModbusInitException {// 获取masterModbusMaster tcpMaster = getMaster();// 创建请求对象WriteRegisterRequest request = new WriteRegisterRequest(slaveId, writeOffset, writeValue);WriteRegisterResponse response = (WriteRegisterResponse) tcpMaster.send(request);if (response.isException()) {log.error(response.getExceptionMessage());return false;} else {return true;}}/** * * 写入[03 Holding Register(4x)]写多个 function ID=16 * @param slaveId * @param startOffset 起始位置偏移量值 * @param sdata 写入的数据 */public static boolean writeRegisters(int slaveId, int startOffset, short[] sdata)throws ModbusTransportException, ModbusInitException {// 获取masterModbusMaster tcpMaster = getMaster();// 创建请求对象WriteRegistersRequest request = new WriteRegistersRequest(slaveId, startOffset, sdata);// 发送请求并获取响应对象ModbusResponse response = tcpMaster.send(request);if (response.isException()) {log.error(response.getExceptionMessage());return false;} else {return true;}}/** * 写入数字类型的模拟量(如:写入Float类型的模拟量、Double类型模拟量、整数类型Short、Integer、Long) * * @param slaveId * @param offset * @param value 写入值,Number的子类,例如写入Float浮点类型,Double双精度类型,以及整型short,int,long * @param dataType com.serotonin.modbus4j.code.DataType */public static void writeHoldingRegister(int slaveId, int offset, Number value, int dataType)throws ModbusTransportException, ErrorResponseException, ModbusInitException {// 获取masterModbusMaster tcpMaster = getMaster();// 类型BaseLocator<Number> locator = BaseLocator.holdingRegister(slaveId, offset, dataType);tcpMaster.setValue(locator, value);}public static void main(String[] args) {try {// 测试01单个写入(0x)boolean t01 = writeCoil(1, 0, true);System.out.println("T01:" + t01);// 测试02批量写入(0x)boolean t02 = writeCoils(1, 0, new boolean[] { true, false, true });System.out.println("T02:" + t02);// 测试03单个写入(4x)short v = -3;boolean t03 = writeRegister(1, 0, v);System.out.println("T03:" + t03);// 测试04批量写入(4x)boolean t04 = writeRegisters(1, 0, new short[] { -309, 390, 91 });System.out.println("t04:" + t04);//写模拟量writeHoldingRegister(1,8, 100, DataType.TWO_BYTE_INT_SIGNED);} catch (Exception e) {e.printStackTrace();}}}