Java 如何使用jamod连接到设备并解释数据

Java 如何使用jamod连接到设备并解释数据,java,modbus,Java,Modbus,我的客户希望通过定制解决方案控制安装在其现场的HVAC系统。HVAC设备提供MODBUS TCP/IP连接。我是这个领域的新手,对MODBUS一无所知。我搜索了互联网,发现jamod是MODBUS的java库。现在我想用jamod编写一个程序。但我的困惑是如何获得我想要连接的设备的地址。我的第二个问题是,即使我成功连接了设备,我如何从MODBUS获得所需的数据(以温度等工程单位)。我的问题可能听起来很糟糕,但请原谅我,因为我是这方面的新手。如何获取我要连接的设备的地址? 这取决于您是通过Modb

我的客户希望通过定制解决方案控制安装在其现场的HVAC系统。HVAC设备提供MODBUS TCP/IP连接。我是这个领域的新手,对MODBUS一无所知。我搜索了互联网,发现jamod是MODBUS的java库。现在我想用jamod编写一个程序。但我的困惑是如何获得我想要连接的设备的地址。我的第二个问题是,即使我成功连接了设备,我如何从MODBUS获得所需的数据(以温度等工程单位)。我的问题可能听起来很糟糕,但请原谅我,因为我是这方面的新手。

如何获取我要连接的设备的地址?

这取决于您是通过Modbus RTU还是Modbus TCP进行连接。RTU(串行)将有一个您将指定的从机id,而tcp更直接,从机id应始终为1

如何从MODBUS获取所需数据(以温度等工程单位表示)?

希望数据已经以工程单位格式化。检查设备手册,应该有一个表或图表将寄存器映射到值

示例:

String portname = "COM1"; //the name of the serial port to be used
int unitid = 1; //the unit identifier we will be talking to, see the first question
int ref = 0; //the reference, where to start reading from
int count = 0; //the count of IR's to read
int repeat = 1; //a loop for repeating the transaction

// setup the modbus master
ModbusCoupler.createModbusCoupler(null);
ModbusCoupler.getReference().setUnitID(1); <-- this is the master id and it doesn't really matter

// setup serial parameters
SerialParameters params = new SerialParameters();
params.setPortName(portname);
params.setBaudRate(9600);
params.setDatabits(8);
params.setParity("None");
params.setStopbits(1);
params.setEncoding("ascii");
params.setEcho(false);

// open the connection
con = new SerialConnection(params);
con.open();

// prepare a request
req = new ReadInputRegistersRequest(ref, count);
req.setUnitID(unitid); // <-- remember, this is the slave id from the first connection
req.setHeadless();

// prepare a transaction
trans = new ModbusSerialTransaction(con);
trans.setRequest(req);

// execute the transaction repeat times because serial connections arn't exactly trustworthy...
int k = 0;
do {
  trans.execute();
  res = (ReadInputRegistersResponse) trans.getResponse();
  for (int n = 0; n < res.getWordCount(); n++) {
    System.out.println("Word " + n + "=" + res.getRegisterValue(n));
  }
  k++;
} while (k < repeat);

// close the connection
con.close();  
String portname=“COM1”//要使用的串行端口的名称
int unitid=1//我们将要讨论的单元标识符,请参见第一个问题
int ref=0//参考,从哪里开始阅读
整数计数=0//要读取的IR的计数
int repeat=1//用于重复事务的循环
//设置modbus主机
ModBus耦合器。CreateModBus耦合器(空);

ModBus耦合器.getReference().setUnitID(1) 如何获取要连接的设备的地址?

这取决于您是通过Modbus RTU还是Modbus TCP进行连接。RTU(串行)将有一个您将指定的从机id,而tcp更直接,从机id应始终为1

如何从MODBUS获取所需数据(以温度等工程单位表示)?

希望数据已经以工程单位格式化。检查设备手册,应该有一个表或图表将寄存器映射到值

示例:

String portname = "COM1"; //the name of the serial port to be used
int unitid = 1; //the unit identifier we will be talking to, see the first question
int ref = 0; //the reference, where to start reading from
int count = 0; //the count of IR's to read
int repeat = 1; //a loop for repeating the transaction

// setup the modbus master
ModbusCoupler.createModbusCoupler(null);
ModbusCoupler.getReference().setUnitID(1); <-- this is the master id and it doesn't really matter

// setup serial parameters
SerialParameters params = new SerialParameters();
params.setPortName(portname);
params.setBaudRate(9600);
params.setDatabits(8);
params.setParity("None");
params.setStopbits(1);
params.setEncoding("ascii");
params.setEcho(false);

// open the connection
con = new SerialConnection(params);
con.open();

// prepare a request
req = new ReadInputRegistersRequest(ref, count);
req.setUnitID(unitid); // <-- remember, this is the slave id from the first connection
req.setHeadless();

// prepare a transaction
trans = new ModbusSerialTransaction(con);
trans.setRequest(req);

// execute the transaction repeat times because serial connections arn't exactly trustworthy...
int k = 0;
do {
  trans.execute();
  res = (ReadInputRegistersResponse) trans.getResponse();
  for (int n = 0; n < res.getWordCount(); n++) {
    System.out.println("Word " + n + "=" + res.getRegisterValue(n));
  }
  k++;
} while (k < repeat);

// close the connection
con.close();  
String portname=“COM1”//要使用的串行端口的名称
int unitid=1//我们将要讨论的单元标识符,请参见第一个问题
int ref=0//参考,从哪里开始阅读
整数计数=0//要读取的IR的计数
int repeat=1//用于重复事务的循环
//设置modbus主机
ModBus耦合器。CreateModBus耦合器(空);
ModBus耦合器.getReference().setUnitID(1) 首先,当您使用Modbus/TCP时,“地址”是不明确的,因为存在从机的IP地址、您正在通话的对象的单元号(对于Modbus/TCP,通常为0)以及任何寄存器的地址

对于“工程单位”问题,您需要的是Modbus寄存器映射,包括任何单位或转换系数。您可能还需要知道数据类型,因为所有Modbus寄存器都是16位。

首先,当您使用Modbus/TCP时,“地址”是不明确的,因为存在从机的IP地址、您正在通话的对象的单元号(对于Modbus/TCP,通常为0)以及任何寄存器的地址


对于“工程单位”问题,您需要的是Modbus寄存器映射,包括任何单位或转换系数。您可能还需要知道数据类型,因为所有Modbus寄存器都是16位。

@GrefBuehler,我已经编写了类似的代码,但它不起作用。你能帮我指出我的错误吗@GrefBuehler,我写过类似的代码,但它不起作用。你能帮我指出我的错误吗。