本文整理汇总了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;未经允许,请勿转载。 |
请发表评论