2012-05-31 46 views
0

我的客戶想要使用自定義解決方案來控制安裝在其站點中的HVAC系統。 HVAC設備提供MODBUS TCP/IP連接。我是這個領域的新手,對MODBUS沒有任何瞭解。我搜索了互聯網,並發現jamod作爲MODBUS的Java庫。現在我想用jamod編寫一個程序。但我的困惑是如何獲得我想連接的設備的地址。而我的第二個問題是即使我設法連接設備,如何從MODBUS獲得所需的數據(以工程單位,如溫度)。我的問題聽起來很糟糕,但請原諒我,因爲我是這個領域的新手。如何使用jamod連接到設備並解釋數據

回答

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

首先,「地址」是不明確的,當你與Modbus/TCP協議的工作,因爲在從機的IP地址,你是事物的單元號與Modbus/TCP通信(通常爲0)以及任何寄存器的地址。

對於「工程單位」問題,您要的是Modbus寄存器映射,包含任何單位或轉換因子。您可能還需要了解數據類型,因爲所有Modbus寄存器都是16位。

相關問題