package com.intelligt.modbus.jlibmodbus.master;
import com.intelligt.modbus.jlibmodbus.Modbus;
import com.intelligt.modbus.jlibmodbus.data.CommStatus;
import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusProtocolException;
import com.intelligt.modbus.jlibmodbus.msg.ModbusRequestBuilder;
import com.intelligt.modbus.jlibmodbus.msg.ModbusRequestFactory;
import com.intelligt.modbus.jlibmodbus.msg.base.ModbusFileRecord;
import com.intelligt.modbus.jlibmodbus.msg.base.ModbusMessage;
import com.intelligt.modbus.jlibmodbus.msg.base.ModbusRequest;
import com.intelligt.modbus.jlibmodbus.msg.base.ModbusResponse;
import com.intelligt.modbus.jlibmodbus.msg.base.mei.MEIReadDeviceIdentification;
import com.intelligt.modbus.jlibmodbus.msg.base.mei.ReadDeviceIdentificationCode;
import com.intelligt.modbus.jlibmodbus.msg.response.*;
import com.intelligt.modbus.jlibmodbus.net.ModbusConnection;
import com.intelligt.modbus.jlibmodbus.net.transport.ModbusTransport;
import com.intelligt.modbus.jlibmodbus.utils.FrameEvent;
import com.intelligt.modbus.jlibmodbus.utils.FrameEventListener;
import com.intelligt.modbus.jlibmodbus.utils.FrameEventListenerList;
import com.intelligt.modbus.jlibmodbus.utils.ModbusExceptionCode;
/*
* Copyright (C) 2016 "Invertor" Factory", JSC
* [http://www.sbp-invertor.ru]
*
* This file is part of JLibModbus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Authors: Vladislav Y. Kochedykov, software engineer.
* email: vladislav.kochedykov@gmail.com
*/
abstract public class ModbusMaster implements FrameEventListenerList {
final private ModbusConnection conn;
final private BroadcastResponse broadcastResponse = new BroadcastResponse();
private int transactionId = 0;
private long requestTime = 0;
public ModbusMaster(ModbusConnection conn) {
this.conn = conn;
}
public int getTransactionId() {
return transactionId;
}
public void setTransactionId(int transactionId) {
this.transactionId = Math.min(Math.abs(transactionId), Modbus.TRANSACTION_ID_MAX_VALUE);
}
protected ModbusConnection getConnection() {
return conn;
}
/**
* this method allows you to implement your own behavior of connect method.
*
* @throws ModbusIOException
*/
protected void connectImpl() throws ModbusIOException {
getConnection().open();
}
/**
* this method allows you to implement your own behavior of disconnect method.
*
* @throws ModbusIOException
*/
protected void disconnectImpl() throws ModbusIOException {
getConnection().close();
}
final public void connect() throws ModbusIOException {
if (!isConnected()) {
connectImpl();
}
}
final public void disconnect() throws ModbusIOException {
if (isConnected()) {
disconnectImpl();
}
}
public boolean isConnected() {
return getConnection().isOpened();
}
protected void sendRequest(ModbusMessage msg) throws ModbusIOException {
ModbusTransport transport = getConnection().getTransport();
if (transport == null)
throw new ModbusIOException("transport is null");
transport.send(msg);
requestTime = System.currentTimeMillis();
}
protected ModbusMessage readResponse(ModbusRequest request) throws ModbusProtocolException, ModbusNumberException, ModbusIOException {
return getConnection().getTransport().readResponse(request);
}
/**
* this function allows you to process your own ModbusRequest.
* Each request class has a compliant response class for instance ReadHoldingRegistersRequest and ReadHoldingRegistersResponse.
* If you process an instance of ReadHoldingRegistersRequest this function exactly either returns a ReadHoldingRegistersResponse instance or throws an exception.
*
* @param request an instance of ModbusRequest.
* @return an instance of ModbusResponse.
* @throws ModbusProtocolException
* @throws ModbusIOException
* @see ModbusRequestFactory
* @see ModbusRequest
* @see ModbusResponse
* @see com.intelligt.modbus.jlibmodbus.msg.request
* @see com.intelligt.modbus.jlibmodbus.msg.request
*/
synchronized public ModbusResponse processRequest(ModbusRequest request) throws ModbusProtocolException, ModbusIOException {
sendRequest(request);
if (request.getServerAddress() != Modbus.BROADCAST_ID) {
do {
try {
ModbusResponse msg = (ModbusResponse) readResponse(request);
request.validateResponse(msg);
/*
* if you have received an ACKNOWLEDGE,
* it means that operation is in processing and you should be waiting for the answer
*/
if (msg.getModbusExceptionCode() != ModbusExceptionCode.ACKNOWLEDGE) {
if (msg.isException())
throw new ModbusProtocolException(msg.getModbusExceptionCode());
return msg;
}
} catch (ModbusNumberException mne) {
Modbus.log().warning(mne.getLocalizedMessage());
}
} while (System.currentTimeMillis() - requestTime < getConnection().getReadTimeout());
/*
* throw an exception if there is a response timeout
*/
throw new ModbusIOException("Response timeout.");
} else {
/*
return because slaves do not respond broadcast requests
*/
broadcastResponse.setFunction(request.getFunction());
return broadcastResponse;
}
}
/**
* ModbusMaster will block for only this amount of time.
* If the timeout expires, a ModbusTransportException is raised, though the ModbusMaster is still valid.
*
* @param timeout the specified timeout, in milliseconds.
*/
public void setResponseTimeout(int timeout) {
try {
getConnection().setReadTimeout(timeout);
} catch (Exception e) {
Modbus.log().warning(e.getLocalizedMessage());
}
}
/**
* This function code is used to read the contents of a contiguous block of holding registers in a
* remote device. The Request PDU specifies the starting register address and the number of
* registers. In the PDU Registers are addressed starting at zero. Therefore registers numbered
* 1-16 are addressed as 0-15.
*
* @param serverAddress a slave address
* @param startAddress starting register address
* @param quantity the number of registers
* @return the register data
* @throws ModbusProtocolException if modbus-exception is received
* @throws ModbusNumberException if response is invalid
* @throws ModbusIOException if remote slave is unavailable
*/
final public int[] readHoldingRegisters(int serverAddress, int startAddress, int quantity) throws
ModbusProtocolException, ModbusNumberException, ModbusIOException {
ModbusRequest request = ModbusRequestBuilder.getInstance().buildReadHoldingRegisters(serverAddress, startAddress, quantity);
ReadHoldingRegistersResponse r
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ModBus开源库jlibmodbus (198个子文件)
build.copyright 52B
.gitignore 938B
jSerialComm-2.0.2.jar 602KB
jlibmodbus-1.2.9.7.jar 239KB
purejavacomm.jar 195KB
jssc-2.8.0.jar 150KB
RXTXcomm.jar 58KB
androidthings-1.0.jar 53KB
ModbusMaster.java 39KB
ModbusRequestBuilder.java 20KB
ModbusTest.java 19KB
Modbus.java 10KB
ModbusMasterSerial.java 9KB
DiagnosticsRequest.java 9KB
CommStatus.java 9KB
DataHolder.java 8KB
MEIReadDeviceIdentification.java 8KB
ModbusMasterTCP.java 8KB
SimpleSlaveTCP.java 7KB
ModbusHoldingRegisters.java 7KB
ExampleRTUOverTCP.java 6KB
SerialPortFactoryTcpServer.java 6KB
ModbusSlave.java 6KB
ExampleRTU.java 6KB
ReadDeviceIdentificationInterface.java 6KB
SerialPortPJC.java 5KB
SerialPortRXTX.java 5KB
SimpleMasterRTU.java 5KB
RequestHandlerSerial.java 5KB
ExampleTCP.java 5KB
SerialPortFactoryTcpClient.java 5KB
DataUtils.java 5KB
SimpleMasterTCP.java 5KB
ReadFileRecordRequest.java 5KB
ModbusFunctionCode.java 5KB
WriteFileRecordRequest.java 5KB
CRC16.java 5KB
SerialPort.java 5KB
ModbusSlaveTCP.java 5KB
WriteMultipleRegistersRequest.java 4KB
ReadWriteMultipleRegistersRequest.java 4KB
ModbusResponseFactory.java 4KB
ModbusRequest.java 4KB
ModbusRequestFactory.java 4KB
SerialPortAT.java 4KB
WriteMultipleCoilsRequest.java 4KB
ModbusCoils.java 4KB
SerialPortJSerialComm.java 4KB
RequestHandlerTCP.java 4KB
MaskWriteRegisterRequest.java 4KB
ReadFileRecordResponse.java 4KB
GetCommEventLogResponse.java 4KB
WriteSingleRegisterRequest.java 4KB
EncapsulatedInterfaceTransportRequest.java 4KB
ModbusSlaveSerial.java 3KB
SerialPortLoopback.java 3KB
SerialUtils.java 3KB
TcpAduHeader.java 3KB
WriteFileRecordResponse.java 3KB
ModbusTransport.java 3KB
ModbusMasterConnectionTCP.java 3KB
SimpleDataHolderBuilder.java 3KB
ModbusMessage.java 3KB
ReadFifoQueueResponse.java 3KB
ReadHoldingRegistersResponse.java 3KB
ModbusCommEventSend.java 3KB
ModbusInputStream.java 3KB
ReadCoilsRequest.java 3KB
ReadCoilsResponse.java 3KB
ReadHoldingRegistersRequest.java 3KB
ModbusSlaveConnectionTCP.java 3KB
LoggingOutputStream.java 3KB
SerialPortJSSC.java 3KB
ModbusResponse.java 3KB
ReadExceptionStatusRequest.java 3KB
ReadFifoQueueRequest.java 3KB
GetCommEventLogRequest.java 3KB
ModbusTransportTCP.java 3KB
ModbusConnection.java 3KB
DiagnosticsSubFunctionCode.java 3KB
WriteSingleCoilRequest.java 3KB
ReportSlaveIdRequest.java 3KB
ByteFifo.java 3KB
ModbusSlaveFactory.java 3KB
InputStreamASCII.java 3KB
ReportSlaveIdResponse.java 3KB
GetCommEventCounterRequest.java 3KB
TcpParameters.java 3KB
AbstractWriteMultipleRequest.java 3KB
ModbusConnectionSerial.java 3KB
EncapsulatedInterfaceTransportResponse.java 3KB
OutputStreamASCII.java 3KB
ReadDiscreteInputsRequest.java 3KB
DiagnosticsResponse.java 3KB
InputStreamTCP.java 3KB
SerialParameters.java 2KB
ReadInputRegistersRequest.java 2KB
IllegalFunctionRequest.java 2KB
LoggingInputStream.java 2KB
ModbusMasterFactory.java 2KB
共 198 条
- 1
- 2
资源评论
丶星辰大海
- 粉丝: 61
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功