⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javaci.htm

📁 串口调试工具 用于串口调 试的很方便
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
<title>Modbus Class Members</title>
</head>

<body bgcolor="#FFFFFF">

<p><font size="5"><strong>Java Modbus Class Members </strong></font></p>

<p>public class Modbus </p>

<p><font size="3">Modbus class implements a subset of modbus protocol functions. </font></p>

<p><font size="3">One important comment about these functions is that <strong>all
functions use a zero based index for address.</strong> For instance, if you want to set
the register 40001 (4x) using PresetSingleRegister function you set the parameter <strong>nRegister
equal to 0. </strong></font></p>

<p><strong>Connection Methods:</strong></p>

<p><strong><a href="#Modbus.openConnection">openConnection</a> - </strong>open<strong> </strong>a
connection with a slave device.</p>

<p><strong><a href="#Modbus.CloseConnection">CloseConnection</a> - </strong>close a
connection with a slave device.</p>

<p>&nbsp;</p>

<p><strong>Connection Properties:</strong></p>

<p><strong><a href="#Modbus.Host">Host</a> </strong>- Host name</p>

<p><a href="#Modbus.Port"><strong>Port</strong></a> - IP Port </p>

<p><a href="#Modbus.ReconnectOnEveryMessage"><strong>ReconnectOnEveryMessage</strong></a>
- when this property is true each call to a modbus function call <strong><a
href="#Modbus.openConnection">openConnection</a> </strong>before sending a query and close
the connection after response was received.</p>

<p>&nbsp;</p>

<p><font size="3"><strong>Modbus</strong></font><b> Methods</b> <font size="3"><strong>:</strong></font></p>

<p><a href="#Modbus.ReadOutputRegisters"><font size="3"><strong>ReadOutputRegisters</strong></font></a>
Reads the holding registers (4<i>x</i> references). </p>

<p><font size="3"><strong><a href="#Modbus.ReadInputRegisters">ReadInputRegisters</a> </strong></font>Reads
the input registers (3X references).</p>

<p><font size="3"><strong><a href="#Modbus.ReadOutputStatus">ReadOutputStatus</a> </strong></font>Reads
the discrete outputs (0<i>x</i> references, coils).</p>

<p><font size="3"><strong><a href="#Modbus.ReadOutputStatus">ReadInputStatus</a> </strong></font>Reads
the discrete inputs (1<i>x</i> references, coils).</p>

<p><font size="3"><strong><a href="#Modbus.PresetSingleRegister">PresetSingleRegister</a> </strong></font>Presets
a single holding register (4<i>x</i> reference)</p>

<p><font size="3"><strong><a href="#Modbus.ForceSingleCoil">ForceSingleCoil</a> </strong></font>Forces
a single coil (0<i>x</i> reference) to either True or False (On/Off).</p>

<p><font size="3"><strong><a href="#Modbus.PresetMultipleRegisters">PresetMultipleRegisters</a>
</strong></font>Presets multiple holding registers (4<i>x</i> reference)</p>

<p><font size="3"><strong><a href="#Modbus.ForceSingleCoil">ForceMultipleCoils</a> </strong></font>Forces
multiple coils(0<i>x</i> reference) to either True or False (On/Off).</p>

<p><strong><a href="#Modbus.ErrorMessage">ErrorMessage</a> </strong>Return string
description of an error code.</p>

<p><font size="3"><strong><a href="#Modbus.TxRxMessage">TxRxMessage</a> </strong>pure
virtual function that must be implemented in derived classes and actually send message to
the network.</font></p>

<p><font size="3"><strong>Properties:</strong></font></p>

<p><a href="#Modbus.Retries"><strong>Retries</strong></a> number of times the message is
sent to a slave before the function return an error code.</p>

<p><font size="3"><strong><a href="#Modbus.ThrowException">ThrowException</a> </strong>When
this property is TRUE, the CModbus class functions throw <a href="ModbuEx.htm">ModbusException</a><strong>
</strong>instead of returning an error code.</font></p>

<p>&nbsp;</p>

<p><a name="Modbus.openConnection"></a><font size="5"><strong>Modbus.openConnection</strong></font></p>

<p><strong>public boolean openConnection(String host, int port, int iTimeOut)</strong></p>

<p><strong>Return Value</strong></p>

<p>true if the open was successful; otherwise false. </p>

<p><font size="3"><strong>Parameters </strong></font></p>

<p>host - Host name, IP (127.0.0.1) or name (pep1.modicon.com) format.</p>

<p>port - IP port. Open Modbus protocol use port 502, but you can use another port if you
wish.</p>

<p>iTimeOut - Time out in ms used for sending modbus messages to a slave.</p>

<p><font size="3"><strong>Remarks</strong></font></p>

<p><font size="3">This function try to connect with a slave using TCP/IP network.<strong> </strong></font>When
ReconnectOnEveryMessage is true this function is called before sending a query to the
slave, otherwise the connection remains opened until is explicit closed by <a
href="#Modbus.CloseConnection">closeconnection</a> or by class destructor. Instead of
using function parameters you can use class properties for seting sHost , nPort and
dwTimeOut.</p>

<p><strong>example</strong>:</p>

<p>&nbsp;&nbsp;&nbsp; String sServer; <br>
&nbsp;&nbsp;&nbsp; Modbus objModbus = new Modbus();<br>
<br>
&nbsp;&nbsp;&nbsp; //Make a connection <br>
&nbsp;&nbsp;&nbsp; sServer = &quot;127.0.0.1&quot;;<br>
&nbsp;&nbsp;&nbsp; System.out.println(&quot;connecting with Server:&quot;+ sServer );<br>
<br>
&nbsp;&nbsp;&nbsp; if (!objModbus.openConnection(sServer,502,5000)){<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
System.out.println(&quot;openConnection Failed&quot;);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<br>
&nbsp;&nbsp;&nbsp; }</p>

<p>&nbsp;</p>

<p><a name="Modbus.CloseConnection"></a><font size="5"><strong>Modbus.CloseConnection</strong></font></p>

<p><strong>public void closeConnection()</strong></p>

<p><strong>Return Value</strong></p>

<p>None.</p>

<p>Remarks:</p>

<p>Closes an the connection associated with this object. If ReconnectOnEveryMessage is
true&nbsp; this function is called after the response from a slave was received.</p>

<p>&nbsp;</p>

<p><a name="Modbus.Port"></a><font size="5"><strong>Modbus.Port</strong></font></p>

<p><strong>public void Port(int iPort)<br>
void Port(WORD wPort);</strong></p>

<p><strong>Remarks:</strong></p>

<p>Get/Set IP port. Open modbus protocol use port 502 as a default.</p>

<p><a name="Modbus.Host"></a><font size="5"><strong>Modbus.Host</strong></font></p>

<p><strong>public String Host()<br>
public void Host(String sHost)</strong></p>

<p><strong>Remarks:</strong></p>

<p>Get/Set Host name. Can be numeric IP format like 198.345.125.30 or a name like
pep1.modicon.com.</p>

<p><a name="Modbus.ReconnectOnEveryMessage"></a><font size="5"><strong>Modbus.ReconnectOnEveryMessage</strong></font></p>

<p><strong>public void ReConnectOnEveryMessage(boolean bReConnectOnEveryMessage)<br>
public boolean ReConnectOnEveryMessage()</strong></p>

<p><strong>Remarks:</strong></p>

<p>When this property is true each call to a modbus function call <strong><a
href="#Modbus.openConnection">openConnection</a> </strong>before sending a query and close
the connection after response was received. Making this property as true can save some
resources from the server or slave because there is limit of connections that a server can
manage simultaneously, but the communication will be slower because opening a connection
can take good amount of time.</p>

<p><strong>See also: <a href="#Modbus.openConnection">openConnection</a>,<a
href="#Modbus.CloseConnection">CloseConnection</a>.</strong></p>

<p>&nbsp;</p>

<p><a name="Modbus.ReadOutputRegisters"></a><font size="5"><strong>Modbus.ReadOutputRegisters</strong></font></p>

<p><strong>public short ReadOutputRegisters(short nAddr, int nDataStart , short nQtd ,
short anRegValues[])</strong></p>

<p><strong>public short ReadOutputRegisters(short nAddr, int nDataStart , short nQtd , int
aiRegValues[])</strong></p>

<p><strong>public short ReadOutputRegisters(short nAddr, int nDataStart , short nQtd ,
float afRegValues[], int nRealType )</strong></p>

<p><strong>public short ReadOutputRegisters(short nAddr, int nDataStart , short nQtd ,
double adRegValues[], int nRealType )</strong></p>

<p><font size="3"><strong>Return Value </strong></font></p>

<p><font size="3">An error code if the function failed or ERR_OK if succeed. Use <a
href="#Modbus.ErrorMessage">ErrorMessage</a> to return a error description.</font></p>

<p><font size="3"><strong>Parameters </strong></font></p>

<p><font size="3">nAddr - Slave Address</font></p>

<p><font size="3">nDataStart - Starting Address</font></p>

<p><font size="3">nQtd - number of registers to read . When reading 1 float number you are
actually reading 2 registers and 1 double means 4 registers.</font></p>

<p>aiRegValues<font size="3"> - array with registers read.</font></p>

<p>nRealType<font size="3"> - inverse the order of registers when reading real number
Modbus.REAL_NORMAL (not reversed) Modbus.REAL_REVERSE (reverse order)</font></p>

<p><font size="3"><strong>Remarks</strong></font></p>

<p>This function reads the holding registers (4<i>x</i> references), the first register
40001 is 0, second 40002 is 1 and so on. The array aiRegValues[]&nbsp; returns the
registers on&nbsp; (unsigned 16 bit integer) format. The second form is&nbsp; intended for
devices that use signed integers. The last two forms of this function are used for
interpret modbus registers as IEEE floating point numbers. <font size="3">Single precision
number (float) has 4 bytes , then we need 2 consecutive registers to form a float number.
Another thing we have consider is that not all slaves split the float number in the same
manner then you can use wRealType parameter as REAL_REVERSE to change the order the 2
consecutive registers to see if the float number is interpreted correctly. The same
explanation is valid for double precision numbers , but now we need 4 registers instead of
2 to form a number.</font></p>

<p><strong>example</strong>:</p>

<p>&nbsp;&nbsp;&nbsp; //Reads 40108 ...40110 registers from slave 17 </p>

<p>&nbsp;&nbsp;&nbsp;&nbsp; int aiRegValues[];<br>
&nbsp;&nbsp;&nbsp;&nbsp; short&nbsp; nQnt;<br>
&nbsp;&nbsp;&nbsp;&nbsp; short nErr;<br>
&nbsp;&nbsp;&nbsp;&nbsp; nQnt = (short)3;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp; aiRegValues = new int[nQnt];<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp; nErr = objModbus.ReadOutputRegisters((short)17,(int)17
,nQnt,(short),aiRegValues);<br>
<br>
&nbsp;&nbsp;&nbsp; if (nErr!=Modbus.ERR_OK) {<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;Error Reading Output
Registers&quot;); <br>
<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (i=0;i&lt;nQnt;i++) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
System.out.println(&quot;[&quot;+(i+17)+&quot;]=&quot;+aiRegValues[i]);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>

<p>&nbsp;</p>

<p><font size="3"><strong>See also: <a href="ModbuEx.htm">ModbusException</a> , <a
href="Errors.htm">Error Codes</a> .</strong></font></p>

<p>&nbsp;</p>

<p><a name="Modbus.ReadInputRegisters"></a><font size="5"><strong>ReadInputRegisters</strong></font></p>

<p><strong>public short ReadInputRegisters(short nAddr, int nDataStart , short nQtd ,
short anRegValues[])</strong></p>

<p><strong>public short ReadInputRegisters(short nAddr, int nDataStart , short nQtd , int
aiRegValues[])</strong></p>

<p><strong>public short ReadInputRegisters(short nAddr, int nDataStart , short nQtd ,
float afRegValues[], int nRealType )</strong></p>

<p><strong>public short ReadInputRegisters(short nAddr, int nDataStart , short nQtd ,
double adRegValues[], int nRealType )</strong></p>

<p>&nbsp;</p>

<p><font size="3"><strong>Return Value </strong></font></p>

<p><font size="3">A error code if the function failed or Modbus.ERR_OK if succeed. Use <a
href="#Modbus.ErrorMessage">ErrorMessage</a> to return a error description.</font></p>

<p><font size="3"><strong>Parameters </strong></font></p>

<p><font size="3">nAddr - Slave Address</font></p>

<p><font size="3">nDataStart - Starting Address</font></p>

<p><font size="3">nQtd - number of registers to read.When reading 1 float number you are
actually reading 2 registers and 1 double means 4 registers.</font></p>

<p><font size="3">anRegValues - array with registers read.</font></p>

<p><font size="3">wRealType - inverse the order of registers when reading real number
REAL_NORMAL (not reversed) REAL_REVERSE (reverse order).</font></p>

<p><font size="3"><strong>Remarks</strong></font></p>

<p>This function reads the input registers (3<i>x</i> references), the first register
30001 is 0, second 30002 is 1 and so on.The array aiRegValues[] return the registers
&nbsp; as 16 bit unsigned&nbsp;&nbsp; integer numbers. The array&nbsp; anRegValues[] is
&nbsp; intended for devices that use signed integers. The last two forms of this function
are used to interpret modbus registers as IEEE floating point numbers. <font size="3">Single
precision number (float) has 4 bytes , then we need 2 consecutive registers to form a
float number. Another thing we have consider is that not all slaves split the float number
in the same manner then you can use wRealType parameter as Modbus.REAL_REVERSE to change
the order the 2 consecutive registers to see if the float number is interpreted correctly.
The same explanation is valid for double precision numbers , but now we need 4 registers
instead of 2 to form a number.</font></p>

<p><strong>example</strong>:</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp; ///Reads 30009 register from slave 17</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp; int aiRegValues[];<br>
&nbsp;&nbsp;&nbsp;&nbsp; short&nbsp; nQnt;<br>
&nbsp;&nbsp;&nbsp;&nbsp; short nErr;<br>
&nbsp;&nbsp;&nbsp;&nbsp; nQnt = (short)1;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp; aiRegValues = new int[nQnt];<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp; nErr = objModbus.ReadInputRegisters((short)17,(int)8
,nQnt,(short),aiRegValues);<br>
<br>
&nbsp;&nbsp;&nbsp; if (nErr!=Modbus.ERR_OK) {<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;Error Reading Output
Registers&quot;); <br>
<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp; for (i=0;i&lt;nQnt;i++) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
System.out.println(&quot;[&quot;+(i+17)+&quot;]=&quot;+aiRegValues[i]);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>

<p><font size="3"><strong>See also: <a href="ModbuEx.htm">ModbusException</a> , <a
href="Errors.htm">Error Codes</a> .</strong></font></p>

<p>&nbsp;</p>

<p><a name="Modbus.ReadOutputStatus"></a><font size="5"><strong>ReadOutputStatus</strong></font></p>

<p><strong>public short ReadOutputStatus(short nAddr, int nDataStart , short nQtd ,
boolean abCoilValues[])</strong></p>

<p><font size="3"><strong>Return Value </strong></font></p>

<p><font size="3">A error code if the function failed or Modbus.ERR_OK if succeed. Use <a
href="#Modbus.ErrorMessage">ErrorMessage</a> to return a error description.</font></p>

<p><font size="3"><strong>Parameters </strong></font></p>

<p><font size="3">nAddr - Slave Address</font></p>

<p><font size="3">nDataStart - Starting Address</font></p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -