com.ghgande.j2mod.modbus.net.UDPMasterConnection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of j2mod Show documentation
Show all versions of j2mod Show documentation
A Modbus TCP/UDP/Serial Master and Slave implementation
/*
* Copyright 2002-2016 jamod & j2mod development teams
*
* 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.
*/
package com.ghgande.j2mod.modbus.net;
import com.ghgande.j2mod.modbus.Modbus;
import com.ghgande.j2mod.modbus.io.AbstractModbusTransport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetAddress;
/**
* Class that implements a UDPMasterConnection.
*
* @author Dieter Wimberger
* @author Steve O'Hara (4NG)
* @version 2.0 (March 2016)
*/
public class UDPMasterConnection {
private static final Logger logger = LoggerFactory.getLogger(UDPMasterConnection.class);
//instance attributes
private UDPMasterTerminal terminal;
private int timeout = Modbus.DEFAULT_TIMEOUT;
private boolean connected;
private InetAddress address;
private int port = Modbus.DEFAULT_PORT;
/**
* Constructs a UDPMasterConnection instance
* with a given destination address.
*
* @param adr the destination InetAddress.
*/
public UDPMasterConnection(InetAddress adr) {
address = adr;
}
/**
* Opens this UDPMasterConnection.
*
* @throws Exception if there is a network failure.
*/
public void connect() throws Exception {
if (!connected) {
terminal = new UDPMasterTerminal(address);
terminal.setPort(port);
terminal.setTimeout(timeout);
terminal.activate();
connected = true;
}
}
/**
* Closes this UDPMasterConnection.
*/
public void close() {
if (connected) {
try {
terminal.deactivate();
}
catch (Exception ex) {
logger.debug("Exception occurred while closing UDPMasterConnection", ex);
}
connected = false;
}
}
/**
* Returns the ModbusTransport associated with this
* UDPMasterConnection.
*
* @return the connection's ModbusTransport.
*/
public AbstractModbusTransport getModbusTransport() {
return terminal == null ? null : terminal.getTransport();
}
/**
* Returns the terminal used for handling the package traffic.
*
* @return a UDPTerminal instance.
*/
public AbstractUDPTerminal getTerminal() {
return terminal;
}
/**
* Returns the timeout for this UDPMasterConnection.
*
* @return the timeout as int.
*/
public synchronized int getTimeout() {
return timeout;
}
/**
* Sets the timeout for this UDPMasterConnection.
*
* @param timeout the timeout as int.
*/
public synchronized void setTimeout(int timeout) {
this.timeout = timeout;
if (terminal != null) {
terminal.setTimeout(timeout);
}
}
/**
* Returns the destination port of this
* UDPMasterConnection.
*
* @return the port number as int.
*/
public int getPort() {
return port;
}
/**
* Sets the destination port of this
* UDPMasterConnection.
* The default is defined as Modbus.DEFAULT_PORT.
*
* @param port the port number as int.
*/
public void setPort(int port) {
this.port = port;
}
/**
* Returns the destination InetAddress of this
* UDPMasterConnection.
*
* @return the destination address as InetAddress.
*/
public InetAddress getAddress() {
return address;
}
/**
* Sets the destination InetAddress of this
* UDPMasterConnection.
*
* @param adr the destination address as InetAddress.
*/
public void setAddress(InetAddress adr) {
address = adr;
}
/**
* Tests if this UDPMasterConnection is connected.
*
* @return true if connected, false otherwise.
*/
public boolean isConnected() {
return connected;
}
}