/*
* ============================================================================
* GNU General Public License
* ============================================================================
*
* Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com
* @author Matthew Lohbihler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.serotonin.modbus4j;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.serotonin.messaging.MessageControl;
import com.serotonin.modbus4j.base.KeyedModbusLocator;
import com.serotonin.modbus4j.base.RangeAndOffset;
import com.serotonin.modbus4j.base.ReadFunctionGroup;
import com.serotonin.modbus4j.base.SlaveProfile;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.code.ExceptionCode;
import com.serotonin.modbus4j.code.FunctionCode;
import com.serotonin.modbus4j.code.RegisterRange;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.InvalidDataConversionException;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.msg.ModbusRequest;
import com.serotonin.modbus4j.msg.ModbusResponse;
import com.serotonin.modbus4j.msg.ReadCoilsRequest;
import com.serotonin.modbus4j.msg.ReadDiscreteInputsRequest;
import com.serotonin.modbus4j.msg.ReadHoldingRegistersRequest;
import com.serotonin.modbus4j.msg.ReadInputRegistersRequest;
import com.serotonin.modbus4j.msg.ReadResponse;
import com.serotonin.modbus4j.msg.WriteCoilRequest;
import com.serotonin.modbus4j.msg.WriteMaskRegisterRequest;
import com.serotonin.modbus4j.msg.WriteRegisterRequest;
import com.serotonin.modbus4j.msg.WriteRegistersRequest;
import com.serotonin.util.ArrayUtils;
import com.serotonin.util.ProgressiveTask;
abstract public class ModbusMaster extends Modbus {
private int timeout = 500;
private int retries = 2;
private final Map<Integer, SlaveProfile> slaveProfiles = new HashMap<Integer, SlaveProfile>();
protected boolean initialized;
abstract public void init() throws ModbusInitException;
public boolean isInitialized() {
return initialized;
}
abstract public void destroy();
abstract public ModbusResponse send(ModbusRequest request) throws ModbusTransportException;
/**
* Convenience method. See getValue(ModbusLocator)
*/
public Object getValue(int slaveId, int registerId, int dataType) throws ModbusTransportException,
ErrorResponseException {
RangeAndOffset rao = new RangeAndOffset(registerId);
return getValue(slaveId, rao.getRange(), rao.getOffset(), dataType);
}
/**
* Convenience method. See getValue(ModbusLocator)
*/
public Object getValue(int slaveId, int range, int offset, int dataType) throws ModbusTransportException,
ErrorResponseException {
return getValue(new ModbusLocator(slaveId, range, offset, dataType));
}
/**
* Convenience method. See getValue(ModbusLocator)
*/
public Boolean getValue(int slaveId, int registerId, byte bit) throws ModbusTransportException,
ErrorResponseException {
RangeAndOffset rao = new RangeAndOffset(registerId);
return getValue(slaveId, rao.getRange(), rao.getOffset(), bit);
}
/**
* Convenience method. See getValue(ModbusLocator)
*/
public Boolean getValue(int slaveId, int range, int offset, byte bit) throws ModbusTransportException,
ErrorResponseException {
return (Boolean) getValue(new ModbusLocator(slaveId, range, offset, bit));
}
/**
* Returns a value from the modbus network according to the given locator information. Various data types are
* allowed to be requested including multi-word types. The determination of the correct request message to send is
* handled automatically.
*
* @param locator
* the information required to locate the value in the modbus network.
* @return an object representing the value found. This will be one of Boolean, Short, Integer, Long, BigInteger,
* Float, or Double. See the DataType enumeration for details on which type to expect.
* @throws ModbusTransportException
* if there was an IO error or other technical failure while sending the message
* @throws ErrorResponseException
* if the response returned from the slave was an exception.
*/
public Object getValue(ModbusLocator locator) throws ModbusTransportException, ErrorResponseException {
BatchRead<String> batch = new BatchRead<String>();
batch.addLocator("", locator);
BatchResults<String> result = send(batch);
return result.getValue("");
}
/**
* Convenience method. See setValue(ModbusLocator, Object)
*/
public void setValue(int slaveId, int registerId, int dataType, Object value) throws ModbusTransportException,
ErrorResponseException {
RangeAndOffset rao = new RangeAndOffset(registerId);
setValue(slaveId, rao.getRange(), rao.getOffset(), dataType, value);
}
/**
* Convenience method. See setValue(ModbusLocator, Object)
*/
public void setValue(int slaveId, int range, int offset, int dataType, Object value)
throws ModbusTransportException, ErrorResponseException {
setValue(new ModbusLocator(slaveId, range, offset, dataType), value);
}
/**
* Convenience method. See setValue(ModbusLocator, Object)
*/
public void setValue(int slaveId, int registerId, byte bit, boolean value) throws ModbusTransportException,
ErrorResponseException {
RangeAndOffset rao = new RangeAndOffset(registerId);
setValue(slaveId, rao.getRange(), rao.getOffset(), bit, value);
}
/**
* Convenience method. See setValue(ModbusLocator, Object)
*/
public void setValue(int slaveId, int range, int offset, byte bit, boolean value) throws ModbusTransportException,
ErrorResponseException {
setValue(new ModbusLocator(slaveId, range, offset, bit), value);
}
/**
* Sets the given value in the modbus network according to the given locator information. Various data types are
* allowed to be set including including multi-word types. The determination of the correct write message to send is
* handled automatically.
*
* @param locator
* the information required to locate the value in the modbus network.
* @value an object representing the value to be set. This will be one of Boolean, Short, Integer, Long, BigInteger,
* Float, or Double. See the DataType enumeration for details on which type to expect.
* @throws ModbusTransportException
* if there was an IO error or other technical failure while sending the message
* @throws ErrorResponseException
* if the response returned from the slave was an exception.
*/
public void setValue(ModbusLocator locator, Object value) throws ModbusTra
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Modbus协议特点 -标准、开放,用户可以免费、放心地使用Modbus协议,不需要交纳许可证费,也不会侵犯知识产权。目前,支持Modbus的厂家超过400家,支持Modbus的产品超过600种。 -Modbus可以支持多种电气接口,如RS-232、RS-485等,还可以在各种介质上传送,如双绞线、光纤、无线等。 -Modbus的帧格式简单、紧凑,通俗易懂。用户使用容易,厂商开发简单。 Modbus协议需要对数据进行校验,串行协议中除有奇偶校验外,ASCII模式采用LRC校验,RTU模式采用16位CRC校验.
资源推荐
资源详情
资源评论
收起资源包目录
java写的modbus接口 (111个子文件)
seroUtils.jar 386KB
commons-codec-1.6.jar 227KB
modbus4J.jar 107KB
ModbusMaster.java 19KB
ModbusLocator.java 14KB
MasterTest.java 13KB
BasicProcessImage.java 11KB
BatchRead.java 10KB
ModbusUtils.java 8KB
ListenerTest.java 8KB
AsciiMessage.java 8KB
TcpMaster.java 6KB
BatchTest.java 6KB
UdpMaster.java 5KB
TcpSlave.java 5KB
UdpSlave.java 5KB
ModbusResponse.java 5KB
ModbusRequest.java 4KB
WriteMaskRegisterRequest.java 4KB
DataType.java 4KB
ProcessImage.java 4KB
ModbusValue.java 4KB
MasterTest2.java 3KB
WriteRegistersRequest.java 3KB
KeyedModbusLocator.java 3KB
WriteCoilsRequest.java 3KB
ModbusMessage.java 3KB
AsciiMaster.java 3KB
RtuMaster.java 3KB
ReadBinaryRequest.java 3KB
ReadNumericRequest.java 3KB
ExceptionCode.java 3KB
RegisterRange.java 3KB
WriteRegisterRequest.java 3KB
WriteCoilRequest.java 3KB
ModbusFactory.java 3KB
WriteMaskRegisterResponse.java 2KB
XaMessageRequest.java 2KB
ReadFunctionGroup.java 2KB
Test.java 2KB
ExceptionRequest.java 2KB
WriteRegistersResponse.java 2KB
WriteCoilResponse.java 2KB
WriteCoilsResponse.java 2KB
WriteRegisterResponse.java 2KB
XaMessageResponse.java 2KB
RtuMessageRequest.java 2KB
XaWaitingRoomKey.java 2KB
ReadHoldingRegistersRequest.java 2KB
ReadInputRegistersRequest.java 2KB
EncapMessageRequest.java 2KB
RangeAndOffset.java 2KB
ReadResponse.java 2KB
ReportSlaveIdResponse.java 2KB
XaMessage.java 2KB
BaseRequestHandler.java 2KB
ReadDiscreteInputsRequest.java 2KB
RtuMessageResponse.java 2KB
AsciiMessageRequest.java 2KB
SerialSlave.java 2KB
AsciiSlave.java 2KB
RtuSlave.java 2KB
ReadCoilsRequest.java 2KB
SerialMaster.java 2KB
ReadExceptionStatusRequest.java 2KB
AsciiMessageResponse.java 2KB
SlaveAndRange.java 2KB
RtuMessageParser.java 2KB
ModbusTransportException.java 2KB
RtuMessage.java 2KB
ReportSlaveIdRequest.java 2KB
SerialWaitingRoomKey.java 2KB
EncapWaitingRoomKey.java 2KB
EncapMessageResponse.java 2KB
ReadExceptionStatusResponse.java 2KB
ModbusSlaveSet.java 2KB
EncapMessage.java 2KB
AsciiRequestHandler.java 2KB
XaRequestHandler.java 2KB
EncapRequestHandler.java 2KB
RtuRequestHandler.java 2KB
ReadTest.java 2KB
ErrorResponseException.java 2KB
BaseMessageParser.java 2KB
ExceptionResponse.java 2KB
Modbus.java 2KB
FunctionCode.java 2KB
Test.java 2KB
IpParameters.java 2KB
AsciiMessageParser.java 2KB
EncapMessageParser.java 2KB
SlaveProfile.java 2KB
XaMessageParser.java 2KB
ExceptionResult.java 2KB
ReadHoldingRegistersResponse.java 2KB
ReadDiscreteInputsResponse.java 2KB
ReadInputRegistersResponse.java 2KB
ReadCoilsResponse.java 1KB
IllegalFunctionException.java 1KB
ModbusInitException.java 1KB
共 111 条
- 1
- 2
资源评论
大胡子大叔
- 粉丝: 5086
- 资源: 43
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功