博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java中modbus协议连接
阅读量:5999 次
发布时间:2019-06-20

本文共 5809 字,大约阅读时间需要 19 分钟。

hot3.png

  1. modbus在java中的使用,首先maven的pom中引入modbus4j包
com.infiniteautomation
modbus4j
3.0.3

    2. 我们创建类:ModBus4JTCPClient,创建ModbusMaster连接对象,以及读取寄存器方法

package io.powerx.test;import org.apache.commons.lang3.StringUtils;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.ModbusResponse;import com.serotonin.modbus4j.msg.WriteRegisterRequest;import com.serotonin.modbus4j.msg.WriteRegistersRequest;public class ModBus4JTCPClient {	private ModbusFactory modbusFactory;	public ModbusFactory getModbusFactory() {		if (modbusFactory == null) {			modbusFactory = new ModbusFactory();		}		return modbusFactory;	}	/**	 * 	 * @param ip	 * @param port	 * @return	 * @throws ModbusInitException	 */	public ModbusMaster getMaster(String ip, int port) throws ModbusInitException {		ModbusMaster m = null;		IpParameters tcpParameters = new IpParameters();		tcpParameters.setHost(ip);		tcpParameters.setPort(port);		m = getModbusFactory().createTcpMaster(tcpParameters, false);		m.setTimeout(2000);		m.init();		return m;	}	/**	 * Holding Register类型	 * 	 * @param master	 * @param modbusId	 * @param registerId	 * @param registerNumber	 * @param analysisFlag	 * @return	 * @throws ModbusTransportException	 * @throws ErrorResponseException	 * @throws ModbusInitException	 */	public Number readHoldingRegister(ModbusMaster master, int modbusId, int registerId, int registerNumber,			String analysisFlag) throws ModbusTransportException, ErrorResponseException, ModbusInitException {		Number value = null;		if ("u".equals(analysisFlag)) {			value = holdingRegister_UNSIGNED(master, modbusId, registerId, registerNumber);		} else if ("s".equals(analysisFlag)) {			value = holdingRegister_SIGNED(master, modbusId, registerId, registerNumber);		}		return value;	}	public Number holdingRegister_UNSIGNED(ModbusMaster master, int modbusId, int registerId, int registerNumber)			throws ModbusTransportException, ErrorResponseException {		BaseLocator
loc = BaseLocator.holdingRegister(modbusId, registerId - 1, (registerNumber == 1) ? DataType.TWO_BYTE_INT_UNSIGNED : DataType.FOUR_BYTE_INT_UNSIGNED); return master.getValue(loc); } public Number holdingRegister_SIGNED(ModbusMaster master, int modbusId, int registerId, int registerNumber) throws ModbusTransportException, ErrorResponseException { return master.getValue(BaseLocator.holdingRegister(modbusId, registerId - 1, (registerNumber == 1) ? DataType.TWO_BYTE_INT_SIGNED : DataType.FOUR_BYTE_INT_SIGNED)); } // Input Registers public void readInputRegisters(ModbusMaster master, int registerId, int registerNumber, int modbusId, String analysisFlag) throws ModbusTransportException, ErrorResponseException, ModbusInitException { Number value = null; //z代表是正数,s代表可能是负数 if ("z".equals(analysisFlag)) { value = inputRegister_UNSIGNED(master, modbusId, registerId, registerNumber); } else if ("f".equals(analysisFlag)) { value = inputRegister_SIGNED(master, modbusId, registerId, registerNumber); } } public Number inputRegister_UNSIGNED(ModbusMaster master, int modbusId, int registerId, int registerNumber) throws ModbusTransportException, ErrorResponseException { return master.getValue(BaseLocator.inputRegister(modbusId, registerId - 1, (registerNumber == 1) ? DataType.TWO_BYTE_INT_UNSIGNED : DataType.FOUR_BYTE_INT_UNSIGNED)); } public Number inputRegister_SIGNED(ModbusMaster master, int modbusId, int registerId, int registerNumber) throws ModbusTransportException, ErrorResponseException { return master.getValue(BaseLocator.inputRegister(modbusId, registerId - 1, (registerNumber == 1) ? DataType.TWO_BYTE_INT_SIGNED : DataType.FOUR_BYTE_INT_SIGNED)); } /** * 写入寄存器 * * @param master * @param registerId * @param modbusId * @param sdata * @return * @throws ModbusTransportException * @throws ModbusInitException */ public boolean writeRegister(ModbusMaster master, int registerId, int modbusId, short sdata) throws ModbusTransportException, ModbusInitException { // 创建请求对象 WriteRegisterRequest request = new WriteRegisterRequest(modbusId, registerId - 1, sdata); // 发送请求并获取响应对象 ModbusResponse response = master.send(request); if (response.isException()) { return false; } else { return true; } } public boolean writeRegisters(ModbusMaster master, int registerId, int modbusId, short[] sdata) throws ModbusTransportException, ModbusInitException { // 创建请求对象 WriteRegistersRequest request = new WriteRegistersRequest(modbusId, registerId - 1, sdata); // 发送请求并获取响应对象 ModbusResponse response = master.send(request); if (response.isException()) { return false; } else { return true; } } public void writeHoldingRegister(ModbusMaster master, int registerId, int modbusId, Number writeValue, int dataType) throws ModbusTransportException, ErrorResponseException, ModbusInitException { BaseLocator
locator = BaseLocator.holdingRegister(modbusId, registerId - 1, (dataType == 1) ? DataType.TWO_BYTE_INT_UNSIGNED : DataType.FOUR_BYTE_INT_UNSIGNED); master.setValue(locator, writeValue); }}

    3. 我们试一下在main方法中调用

int registerId=100;		int modbusId=1;		ModBus4JTCPClient modbusTcp = new ModBus4JTCPClient();				ModbusMaster master = modbusTcp.getMaster("localhost", 502);	//192.168.1.108						modbusTcp.readInputRegisters(master, registerId, 1, modbusId, "u");

 

转载于:https://my.oschina.net/liketome/blog/1856449

你可能感兴趣的文章
让VMware ESX中的虚拟机随esx开机自动启动
查看>>
Electron Cash钱包存BCH教程
查看>>
自定义key解决zabbix端口监听取值不准确的问题
查看>>
入门级----黑盒测试、白盒测试、手工测试、自动化测试、探索性测试、单元测试、性能测试、数据库性能、压力测试、安全性测试、SQL注入、缓冲区溢出、环境测试...
查看>>
composer 安装 ubuntu 12.04
查看>>
微服务(二)hystrix
查看>>
Performing a thread dump in Linux or Windows--reference
查看>>
推荐系统中常用算法 以及优点缺点对比
查看>>
cocos2d-x v3.2环境配置(现在3.x版本号可以配置该)
查看>>
穷举法解决旅行商问题
查看>>
Go语言标准库之JSON编解码
查看>>
winpcap 发送数据包
查看>>
cisco 出现 %Error opening tftp://255.255.255.255 错误解决办法
查看>>
VIM编辑器
查看>>
IE主页被篡改 地址框变灰
查看>>
linux上架设l2tp+ipsec ***服务器
查看>>
Facebook和用户界面会如何扭曲你说的话
查看>>
Android 单元测试用法 简介
查看>>
安卓混合开发之Cordova,NativeWebView两种实现
查看>>
git设置socks代理
查看>>