繁体   English   中英

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

[英]How do I connect to device using jamod and interpret the data

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

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

这种类型取决于您是通过Modbus RTU还是Modbus TCP进行连接。 当tcp更直接时,RTU(串行)将具有您要指定的从站ID,并且从站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();  

首先,当您使用Modbus / TCP时,“地址”是模棱两可的,因为存在从站的IP地址,要与之通信的单元的编号(对于Modbus / TCP,通常为0)以及任何寄存器。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM