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

时间:2012-05-31 08:48:39

标签: java modbus

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

2 个答案:

答案 0 :(得分:2)

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

这取决于您是通过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();  

答案 1 :(得分:1)

首先,当您使用Modbus / TCP时,“地址”是不明确的,因为存在从站的IP地址,您正在与之交谈的设备的单元号(对于Modbus / TCP通常为0),以及任何寄存器的地址。

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