zoukankan      html  css  js  c++  java
  • 私.Modbus测试_ZC02_串口方式

    ZC:另外一些信息在 “Modbus资料 - codeskill_android - 博客园.html”(https://www.cnblogs.com/codeskilla/p/9998416.html

    1、文章:modbus4j串口通讯 - u010655348的博客 - CSDN博客.html(https://blog.csdn.net/u010655348/article/details/52665228

    2、包:路径:"...C_IDEJava_3rdmodbus_java1_旧一点的"

      2.1、modbus4J.jar  源码为:modbus4J-src.zip  (版本:应该可能为 1.1)

      2.2、seroUtils.jar  源码为:mlohbihler-sero-utils-0b14066efbbc.zip  (版本:不祥)

      2.3、RXTXcomm.jar  源码:没有  (版本:不祥)

    3、代码:

    package com.ModBusZ;
    
    import java.util.Arrays;
    
    import com.serotonin.io.serial.SerialParameters;
    import com.serotonin.modbus4j.ModbusFactory;
    import com.serotonin.modbus4j.ModbusMaster;
    import com.serotonin.modbus4j.exception.ModbusInitException;
    import com.serotonin.modbus4j.exception.ModbusTransportException;
    import com.serotonin.modbus4j.msg.ReadDiscreteInputsRequest;
    import com.serotonin.modbus4j.msg.ReadDiscreteInputsResponse;
    import com.serotonin.modbus4j.msg.ReadHoldingRegistersRequest;
    import com.serotonin.modbus4j.msg.ReadHoldingRegistersResponse;
    import com.serotonin.modbus4j.msg.WriteRegistersRequest;
    import com.serotonin.modbus4j.msg.WriteRegistersResponse;
    
    public class TestModbusDemo
    {
        //设定MODBUS网络上从站地址
        private final static int SLAVE_ADDRESS = 1;
        // 串行波特率
        private final static int BAUD_RATE = 9600;
    
        public static void main(String[] args)
        {
            SerialParameters serialParameters = new SerialParameters();
            // 设定MODBUS通讯的串行口
            serialParameters.setCommPortId("COM3");
            // 设定成无奇偶校验
            serialParameters.setParity(0);
            // 设定成数据位是8位
            serialParameters.setDataBits(8);
            // 设定为1个停止位
            serialParameters.setStopBits(1);
            // 设定端口名称
            serialParameters.setPortOwnerName("Numb nuts");
            // 设定端口波特率
            serialParameters.setBaudRate(BAUD_RATE);
        
            // 创建ModbusFactory工厂实例
            ModbusFactory modbusFactory = new ModbusFactory();
            // 创建ModbusMaster实例
            ModbusMaster master = modbusFactory.createRtuMaster(serialParameters);
            
            // 初始化
            try
            {
                master.init();
                readHoldingRegistersTest(master,SLAVE_ADDRESS,0,100);
            } catch (ModbusInitException e) {
                e.printStackTrace();
            } finally {
                master.destroy();
            }
        }// mian(...)
    
        /**
        * 读开关量型的输入信号
        * @param master 主站
        * @param slaveId 从站地址
        * @param start 起始偏移量
        * @param len 待读的开关量的个数
        */
        private static void readDiscreteInputTest(ModbusMaster master, int slaveId, int start, int len)
        {
            try
            {
                ReadDiscreteInputsRequest request = new ReadDiscreteInputsRequest(slaveId, start, len);
                ReadDiscreteInputsResponse response = (ReadDiscreteInputsResponse) master.send(request);
                if (response.isException())
                System.out.println("Exception response: message=" + response.getExceptionMessage());
                else
                System.out.println(Arrays.toString(response.getBooleanData()));
            }
            catch (ModbusTransportException e) {
                e.printStackTrace();
            }
        }
    
        /**
        * 读保持寄存器上的内容
        * @param master 主站
        * @param slaveId 从站地址
        * @param start 起始地址的偏移量
        * @param len 待读寄存器的个数
        */
        private static void readHoldingRegistersTest(ModbusMaster master,
                int slaveId, int start, int len)
        {
            try
            {
                ReadHoldingRegistersRequest request = 
                        new ReadHoldingRegistersRequest(slaveId, start, len);
                ReadHoldingRegistersResponse response =
                        (ReadHoldingRegistersResponse) master.send(request);
                if (response.isException())
                    System.out.println("Exception response: message="
                            + response.getExceptionMessage());
                else
                {
                    System.out.println(Arrays.toString(response.getShortData()));
                    short[] list = response.getShortData();
                    for(int i = 0; i < list.length; i++)
                        System.out.print(list[i] + " ");
                }
            } catch (ModbusTransportException e) {
                e.printStackTrace();
            }
        }
    
        /**
        * 批量写数据到保持寄存器
        * @param master 主站
        * @param slaveId 从站地址
        * @param start 起始地址的偏移量
        * @param values 待写数据
        */
        public static void writeRegistersTest(ModbusMaster master, int slaveId, int start, short[] values)
        {
            try
            {
                WriteRegistersRequest request = new WriteRegistersRequest(slaveId, start, values);
                WriteRegistersResponse response = (WriteRegistersResponse) master.send(request);
                if (response.isException())
                    System.out.println("Exception response: message="
                            + response.getExceptionMessage());
                else
                    System.out.println("Success");
            }
            catch (ModbusTransportException e) {
                e.printStackTrace();
            }
        }
    }

    4、

    5、

  • 相关阅读:
    AC自动机+全概率+记忆化DP UVA 11468 Substring
    java POI技术之导出数据优化(15万条数据1分多钟)
    验证IP端与数据库Ip端是否重复!!!
    JAVA中IP和整数相互转化(含有掩码的计算)
    Nginx搭建反向代理服务器过程详解
    session原理及实现共享
    Linux部署多个tomcat
    linux下怎么修改mysql的字符集编码
    linux yum 安装mysql
    VM虚拟机下的Linux不能上网
  • 原文地址:https://www.cnblogs.com/codeskilla/p/10017674.html
Copyright © 2011-2022 走看看