• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java ModbusTCPTransaction类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中net.wimpi.modbus.io.ModbusTCPTransaction的典型用法代码示例。如果您正苦于以下问题:Java ModbusTCPTransaction类的具体用法?Java ModbusTCPTransaction怎么用?Java ModbusTCPTransaction使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



ModbusTCPTransaction类属于net.wimpi.modbus.io包,在下文中一共展示了ModbusTCPTransaction类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: executeRequest

import net.wimpi.modbus.io.ModbusTCPTransaction; //导入依赖的package包/类
@Override
public synchronized ModbusResponse executeRequest(ModbusRequest req) throws Exception,ModbusException {
	if (!isOpen()) {
		log.warn("Trying to execute request on closed TCP connection");
		if (!this.open()) {
			log.error("Couldn't open the connection... Aborting.");
			throw new Exception("Connection Closed");
		}
	}
	
	ModbusTCPTransaction trans = new ModbusTCPTransaction(this.connection);
	//trans.setReconnecting(true);
	trans.setRequest(req);
	try {
		trans.execute();
	} catch (ModbusIOException e) {
		//channel closed?
		this.close();
		throw e;
	}
	return trans.getResponse();
}
 
开发者ID:therealchalz,项目名称:softlogger,代码行数:23,代码来源:ModbusTcpChannel.java


示例2: readInputRegister

import net.wimpi.modbus.io.ModbusTCPTransaction; //导入依赖的package包/类
public static int readInputRegister(String ip, int port, int address,
		int slaveId) {
	int data = 0;

	try {
		InetAddress addr = InetAddress.getByName(ip);
		TCPMasterConnection con = new TCPMasterConnection(addr);
		
		//Modbus.DEFAULT_PORT;
		con.setPort(port);
		con.connect();
		
		//这里重点说明下,这个地址和数量一定要对应起来
		ReadInputRegistersRequest req = new ReadInputRegistersRequest(address, 1);
		
		//这个SlaveId一定要正确
		req.setUnitID(slaveId);

		ModbusTCPTransaction trans = new ModbusTCPTransaction(con);

		trans.setRequest(req);

		trans.execute();

		ReadInputRegistersResponse res = (ReadInputRegistersResponse) trans.getResponse();

		data = res.getRegisterValue(0);

		con.close();
	} catch (Exception e) {
		e.printStackTrace();
	}

	return data;
}
 
开发者ID:Dn9x,项目名称:dn-modbus,代码行数:36,代码来源:ModbusUtil.java


示例3: ModbusTcpSlave

import net.wimpi.modbus.io.ModbusTCPTransaction; //导入依赖的package包/类
public ModbusTcpSlave(String slave) {
	super(slave);
	transaction = new ModbusTCPTransaction();
}
 
开发者ID:andrey-desman,项目名称:openhab-hdl,代码行数:5,代码来源:ModbusTcpSlave.java


示例4: readDigitalOutput

import net.wimpi.modbus.io.ModbusTCPTransaction; //导入依赖的package包/类
public static int readDigitalOutput(String ip, int port, int address,
		int slaveId) {
	int data = 0;
	try {
		InetAddress addr = InetAddress.getByName(ip);

		TCPMasterConnection con = new TCPMasterConnection(addr);
		con.setPort(port);
		con.connect();

		ReadCoilsRequest req = new ReadCoilsRequest(address, 1);

		req.setUnitID(slaveId);

		ModbusTCPTransaction trans = new ModbusTCPTransaction(con);

		trans.setRequest(req);

		trans.execute();

		ReadCoilsResponse res = ((ReadCoilsResponse) trans.getResponse());

		if(res.getCoils().getBit(0)){
			data = 1;
		}

		con.close();

	} catch (Exception ex) {
		ex.printStackTrace();
	}

	return data;
}
 
开发者ID:Dn9x,项目名称:dn-modbus,代码行数:35,代码来源:ModbusUtil.java


示例5: connect

import net.wimpi.modbus.io.ModbusTCPTransaction; //导入依赖的package包/类
/**
 * Connects this <tt>ModbusTCPMaster</tt> with the slave.
 *
 * @throws Exception if the connection cannot be established.
 */
public void connect()
    throws Exception {
  if (m_Connection != null && !m_Connection.isConnected()) {
    m_Connection.connect();
    m_Transaction = new ModbusTCPTransaction(m_Connection);
    m_Transaction.setReconnecting(m_Reconnecting);
  }
}
 
开发者ID:andrey-desman,项目名称:openhab-hdl,代码行数:14,代码来源:ModbusTCPMaster.java


示例6: readRegister

import net.wimpi.modbus.io.ModbusTCPTransaction; //导入依赖的package包/类
public static int readRegister(String ip, int port, int address,
		int slaveId) {
	int data = 0;
	try {
		InetAddress addr = InetAddress.getByName(ip);

		TCPMasterConnection con = new TCPMasterConnection(addr);

		con.setPort(port);
		con.connect();
		ReadMultipleRegistersRequest req = new ReadMultipleRegistersRequest(address, 1);
		req.setUnitID(slaveId);

		ModbusTCPTransaction trans = new ModbusTCPTransaction(con);

		trans.setRequest(req);

		trans.execute();

		ReadMultipleRegistersResponse res = (ReadMultipleRegistersResponse) trans.getResponse();
		
		data = res.getRegisterValue(0);

		con.close();
	} catch (Exception e) {
		e.printStackTrace();
	}

	return data;
}
 
开发者ID:Dn9x,项目名称:dn-modbus,代码行数:31,代码来源:ModbusUtil.java


示例7: writeRegister

import net.wimpi.modbus.io.ModbusTCPTransaction; //导入依赖的package包/类
/**
 * 写入数据到真机,数据类型是RE
 * 
 * @param ip
 * @param port
 * @param slaveId
 * @param address
 * @param value
 */
public static void writeRegister(String ip, int port, int slaveId,
		int address, int value) {

	try {
		InetAddress addr = InetAddress.getByName(ip);

		TCPMasterConnection connection = new TCPMasterConnection(addr);
		connection.setPort(port);
		connection.connect();

		ModbusTCPTransaction trans = new ModbusTCPTransaction(connection);

		UnityRegister register = new UnityRegister(value);

		WriteSingleRegisterRequest req = new WriteSingleRegisterRequest(
				address, register);

		req.setUnitID(slaveId);
		trans.setRequest(req);

		System.out.println("ModbusSlave: FC" + req.getFunctionCode()
				+ " ref=" + req.getReference() + " value="
				+ register.getValue());
		trans.execute();

		connection.close();
	} catch (Exception ex) {
		System.out.println("Error in code");
		ex.printStackTrace();
	}
}
 
开发者ID:Dn9x,项目名称:dn-modbus,代码行数:41,代码来源:ModbusUtil.java


示例8: writeDigitalOutput

import net.wimpi.modbus.io.ModbusTCPTransaction; //导入依赖的package包/类
/**
 * 写入数据到真机的DO类型的寄存器上面
 * 
 * @param ip
 * @param port
 * @param slaveId
 * @param address
 * @param value
 */
public static void writeDigitalOutput(String ip, int port, int slaveId,
		int address, int value) {

	try {
		InetAddress addr = InetAddress.getByName(ip);

		TCPMasterConnection connection = new TCPMasterConnection(addr);
		connection.setPort(port);
		connection.connect();

		ModbusTCPTransaction trans = new ModbusTCPTransaction(connection);

		boolean val = true;

		if (value == 0) {
			val = false;
		}

		WriteCoilRequest req = new WriteCoilRequest(address, val);

		req.setUnitID(slaveId);
		trans.setRequest(req);

		trans.execute();
		connection.close();
	} catch (Exception ex) {
		System.out.println("writeDigitalOutput Error in code");
		ex.printStackTrace();
	}
}
 
开发者ID:Dn9x,项目名称:dn-modbus,代码行数:40,代码来源:ModbusUtil.java


示例9: connect

import net.wimpi.modbus.io.ModbusTCPTransaction; //导入依赖的package包/类
/**
 * Connects this <tt>ModbusTCPMaster</tt> with the slave.
 * 
 * @throws Exception
 *             if the connection cannot be established.
 */
public void connect() throws Exception {
	if (m_Connection != null && !m_Connection.isConnected()) {
		m_Connection.connect();
		m_Transaction = new ModbusTCPTransaction(m_Connection);
		m_Transaction.setReconnecting(m_Reconnecting);
	}
}
 
开发者ID:jeick,项目名称:jamod,代码行数:14,代码来源:ModbusTCPMaster.java


示例10: main

import net.wimpi.modbus.io.ModbusTCPTransaction; //导入依赖的package包/类
public static void main(String[] args) {
	int port = Modbus.DEFAULT_PORT;
	int unitId = 15; //Same as TCPSlaveTest.java
	try {
		if (args != null && args.length == 1) {
			port = Integer.parseInt(args[0]);
		}
		InetAddress addy = InetAddress.getLocalHost();
		TCPMasterConnection connection = new TCPMasterConnection(addy);
		connection.setTimeout(3000);
		connection.setPort(port);
		System.out.println("Trying to connect to "+addy.getCanonicalHostName()+" on port "+port);
		connection.connect();
		
		ModbusTCPTransaction transaction = new ModbusTCPTransaction(connection);
		
		ModbusRequest request;
		while ((request = getNextRequest()) != null) {
			request.setUnitID(unitId);
			transaction.setRequest(request);
			transaction.execute();
			ModbusResponse response = transaction.getResponse();
			gotResponse(response);
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
开发者ID:jeick,项目名称:jamod,代码行数:29,代码来源:TCPMasterTest.java


示例11: getConnection

import net.wimpi.modbus.io.ModbusTCPTransaction; //导入依赖的package包/类
/**
 * Performs physical write to device when slave type is "holding" using Modbus FC06 function
 *
 * @param command command received from OpenHAB
 * @param readRegister reference to the register that stores current value
 * @param writeRegister register reference to write data to
 */

@Override
protected ModbusSlaveConnection getConnection(ModbusSlaveEndpoint endpoint) {
    ModbusSlaveConnection connection = super.getConnection(endpoint);
    if (connection == null) {
        return null;
    }
    if (!(connection instanceof TCPMasterConnection)) {
        // should not happen
        throw new IllegalStateException("Should not happen: wrong connection type for slave " + name);
    }
    ((ModbusTCPTransaction) transaction).setConnection((TCPMasterConnection) connection);
    return connection;
}
 
开发者ID:openhab,项目名称:openhab1-addons,代码行数:22,代码来源:ModbusTcpSlave.java


示例12: connect

import net.wimpi.modbus.io.ModbusTCPTransaction; //导入依赖的package包/类
/**
 * Connects this <tt>ModbusTCPMaster</tt> with the slave.
 *
 * @throws Exception if the connection cannot be established.
 */
public void connect() throws Exception {
    if (m_Connection != null && !m_Connection.isConnected()) {
        m_Connection.connect();
        m_Transaction = new ModbusTCPTransaction(m_Connection);
        m_Transaction.setReconnecting(m_Reconnecting);
    }
}
 
开发者ID:openhab,项目名称:openhab1-addons,代码行数:13,代码来源:ModbusTCPMaster.java


示例13: ModbusTcpSlave

import net.wimpi.modbus.io.ModbusTCPTransaction; //导入依赖的package包/类
public ModbusTcpSlave(String slave, KeyedObjectPool<ModbusSlaveEndpoint, ModbusSlaveConnection> connectionPool) {
    super(slave, connectionPool);
    transaction = new ModbusTCPTransaction();
    ((ModbusTCPTransaction) transaction).setReconnecting(false);
}
 
开发者ID:openhab,项目名称:openhab1-addons,代码行数:6,代码来源:ModbusTcpSlave.java


示例14: readDigitalInput

import net.wimpi.modbus.io.ModbusTCPTransaction; //导入依赖的package包/类
/**
 * 查询Function 为Input Status的寄存器
 * 
 * @param ip
 * @param address
 * @param count
 * @param slaveId
 * @return
 * @throws ModbusIOException
 * @throws ModbusSlaveException
 * @throws ModbusException
 */
public static int readDigitalInput(String ip, int port, int address, int slaveId) {
	int data = 0;

	try {
		InetAddress addr = InetAddress.getByName(ip);

		// 建立连接
		TCPMasterConnection con = new TCPMasterConnection(addr);

		con.setPort(port);

		con.connect();

		// 第一个参数是寄存器的地址,第二个参数时读取多少个
		ReadInputDiscretesRequest req = new ReadInputDiscretesRequest(address, 1);

		// 这里设置的Slave Id, 读取的时候这个很重要
		req.setUnitID(slaveId);

		ModbusTCPTransaction trans = new ModbusTCPTransaction(con);

		trans.setRequest(req);

		// 执行查询
		trans.execute();

		// 得到结果
		ReadInputDiscretesResponse res = (ReadInputDiscretesResponse) trans.getResponse();

		if(res.getDiscretes().getBit(0)){
			data = 1;
		}

		// 关闭连接
		con.close();

	} catch (Exception e) {
		e.printStackTrace();
	}

	return data;
}
 
开发者ID:Dn9x,项目名称:dn-modbus,代码行数:55,代码来源:ModbusUtil.java



注:本文中的net.wimpi.modbus.io.ModbusTCPTransaction类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java MKAddrInfo类代码示例发布时间:2022-05-23
下一篇:
Java DefaultNodeSettingsPane类代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap