Skip to content

ModbusTcp

参考

https://github.com/xingshuangs/iot-communication

实践

java
        <dependency>
            <groupId>com.github.xingshuangs</groupId>
            <artifactId>iot-communication</artifactId>
            <version>1.5.5</version>
        </dependency>
java
import com.alibaba.fastjson.JSON;
import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ModbusUtil {
    public String sendModbusTcpMsg(String json) {
        Map<String, String> res=new HashMap<>();
        res.put("status","fail");
        res.put("msg","");

        try {
            Map<String,Object> map = (Map<String,Object>)JSON.parse(json);

            //创建链接
            String ip = (String)map.get("ip");
            ModbusTcp plc=new ModbusTcp(ip);

            //程序编号
            Integer program_no=(Integer)map.get("program_no");
            plc.writeInt32(2000, program_no);
            //程序名称
            String program_name=(String)map.get("program_name");
            plc.writeString(2003,program_name);
            //程序段数
            Integer program_row_qu=(Integer)map.get("program_row_qu");
            plc.writeInt32(2012, program_row_qu);
            //段循环
            List<Map<String, Object>> rowCycleList = (List<Map<String, Object>>) map.get("row_cycle_list");
            if (rowCycleList != null && rowCycleList.size() > 0){
                int i=2013;
                for (Map<String, Object> temp : rowCycleList) {
                    //开始段
                    Integer startRow = (Integer) temp.get("start_row");
                    plc.writeInt32(i, startRow);
                    i++;
                    //结束段
                    Integer endRow = (Integer) temp.get("end_row");
                    plc.writeInt32(i, endRow);
                    i++;
                    //循环次数
                    Integer cycleQu = (Integer) temp.get("cycle_qu");
                    plc.writeInt32(i, cycleQu);
                    i++;
                }
            }
            //段程序
            List<Map<String, Object>> rowProgramList = (List<Map<String, Object>>) map.get("row_program_list");
            if (rowProgramList != null && rowProgramList.size() > 0){
                int i=2025;
                for (Map<String, Object> temp : rowProgramList) {
                    //温度
                    Double temperature = (Double) temp.get("temperature");
                    plc.writeFloat64(i, temperature);
                    i++;
                    //时长
                    Integer duration = (Integer) temp.get("duration");
                    plc.writeInt32(i, duration);
                    i+=5;
                }
            }

            plc.close();

            res.put("status","success");
            res.put("msg","操作成功");
        }catch (Exception e){
            res.put("msg",e.getMessage());
        }

        return JSON.toJSONString(res);
    }
}

使用

modsim32(服务端)

建立链接

端口、模式、地址选择

modscan32(客户端)

填写对应的端口和模式等,即可模拟读取数据

© 2025-甘草味道的博客