modbuimp.htm
来自「串口调试工具 用于串口调 试的很方便」· HTM 代码 · 共 691 行 · 第 1/2 页
HTM
691 行
<!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>CModbus Class Members</title>
</head>
<body bgcolor="#FFFFFF">
<p><font size="5"><strong>CModbus Class Members </strong></font></p>
<p><font size="3"><strong>class CModbus: public CObject</strong></font></p>
<p><font size="3">CModbus implements a subset of modbus protocol functions. This class do
not send messages to any device , serial port or TCP/IP network , this is done by derived
CModbus classes ( <a href="local.htm">CLocalModbus</a>, <a href="remote.htm">CRemoteModbus</a>
or <a href="automod.htm">CAutoModbus</a>). CModbus works as common interface for sending
messages to modbus devices independent the kind of network employed.</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> </p>
<p><font size="3"><strong>Member functions:</strong></font></p>
<p><font size="3"><strong>Modbus Functions:</strong></font></p>
<p><a href="#CModbus::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="#CModbus::ReadInputRegisters">ReadInputRegisters</a> </strong></font>Reads
the input registers (3X references).</p>
<p><font size="3"><strong><a href="#CModbus::ReadOutputStatus">ReadOutputStatus</a> </strong></font>Reads
the discrete outputs (0<i>x</i> references, coils).</p>
<p><font size="3"><strong><a href="#CModbus::ReadOutputStatus">ReadInputStatus</a> </strong></font>Reads
the discrete inputs (1<i>x</i> references, coils).</p>
<p><font size="3"><strong><a href="#CModbus::PresetSingleRegister">PresetSingleRegister</a>
</strong></font>Presets a single holding register (4<i>x</i> reference)</p>
<p><font size="3"><strong><a href="#CModbus::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="#CModbus::PresetMultipleRegisters">PresetMultipleRegisters</a>
</strong></font>Presets multiple holding registers (4<i>x</i> reference)</p>
<p><font size="3"><strong><a href="#CModbus::ForceSingleCoil">ForceMultipleCoils</a> </strong></font>Forces
multiple coils(0<i>x</i> reference) to either True or False (On/Off).</p>
<p> </p>
<p><strong><a href="#CModbus::ErrorMessage">ErrorMessage</a> </strong>Return string
description of an error code.</p>
<p><font size="3"><strong><a href="#CModbus::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="#CModbus::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="#CModbus::ThrowException">ThrowException</a> </strong>When
this property is TRUE, the CModbus class functions throw <a href="excep.htm">CModbusException</a>
instead of returning an error code.</font></p>
<p><a name="CModbus::ReadOutputRegisters"></a><font size="5"><strong>CModbus::ReadOutputRegisters</strong></font></p>
<p><font size="3"><strong>WORD ReadOutputRegisters(WORD nAddr ,WORD nDataStart ,WORD nQtd
, CWordArray& anRegValues); </strong></font></p>
<p><font size="3"><strong>WORD ReadOutputRegisters(WORD nAddr ,WORD nDataStart ,WORD nQtd
, CShorArray& anRegValues); </strong></font></p>
<p><font size="3"><strong>WORD ReadOutputRegisters(WORD nAddr,WORD nDataStart , WORD nQtd
,CFloatArray& anRegValues,WORD wRealType = REAL_NORMAL);</strong></font></p>
<p><font size="3"><strong>WORD ReadOutputRegisters(WORD nAddr,WORD nDataStart ,WORD nQtd ,
CDblArray& anRegValues,WORD wRealType = REAL_NORMAL);</strong></font></p>
<p><font size="3"><strong>Return Value </strong></font></p>
<p><font size="3">An error code if the function failed or CModbus::ERR_OK if succeed. Use <a
href="#CModbus::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 holding registers (4<i>x</i> references), the first register
40001 is 0, second 40002 is 1 and so on. CWordArray return the registers on WORD (unsigned
16 bit integer) format. CShortArray is a CArray class ( CArray<short,short>) and
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. It was defined two new array classes CFloatArray class(</font>CArray<float,float>)
and <font size="3">CDblArray class (</font>CArray<double,double>) using MFC template
class CArray.</p>
<p><strong>example</strong>:</p>
<p>extern CModbus* pModbus; // this pointer is created by a derived class. </p>
<p>WORD wError;<br>
CWordArray anRegs;<font size="3"><br>
CDblArray adRegValues;</font><br>
<br>
anRegs.SetSize(3); <font size="3"><br>
adRegValues.SetSize(1);</font></p>
<p>wError = pModbus-><font size="3">ReadOutputRegisters(17,107,3,</font>anRegs) ;
//Reads 40108 ...40110 registers from slave 17 </p>
<p>wError = pModbus-><font size="3">ReadOutputRegisters(17,107,1,adRegValues,REAL_NORMAL</font>)
; //Reads 40108 ...40111 registers from slave 17 and interpret as one double precision
floating point number</p>
<p> </p>
<p><font size="3"><strong>See also: <a href="excep.htm">CModbusExcetion</a> , <a
href="Errors.htm">Error Codes</a> .</strong></font></p>
<p> </p>
<p><a name="CModbus::ReadInputRegisters"></a><font size="5"><strong>CModbus::ReadInputRegisters</strong></font></p>
<p><font size="3"><strong>WORD ReadInputRegisters( WORD nAddr, WORD nDataStart ,WORD nQtd
,CWordArray& anRegValues);</strong></font></p>
<p><font size="3"><strong>WORD ReadInputRegisters( WORD nAddr, WORD nDataStart ,WORD nQtd
,CShortArray& anRegValues);</strong></font></p>
<p><font size="3"><strong>WORD ReadInputRegisters(WORD nAddr,WORD nDataStart , WORD nQtd
,CFloatArray& anRegValues,WORD wRealType = REAL_NORMAL);</strong></font></p>
<p><font size="3"><strong>WORD ReadInputRegisters(WORD nAddr,WORD nDataStart ,WORD nQtd ,
CDblArray& anRegValues,WORD wRealType = REAL_NORMAL);</strong></font></p>
<p><font size="3"><strong>Return Value </strong></font></p>
<p><font size="3">A error code if the function failed or CModbus::ERR_OK if succeed. Use <a
href="#CModbus::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. CWordArray return the registers on WORD (unsigned
16 bit integer) format. CShortArray is template CArray class ( CArray<short,short>)
and 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 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. It was defined two new array classes CFloatArray class(</font>CArray<float,float>)
and <font size="3">CDblArray class (</font>CArray<double,double>) using MFC template
class CArray.</p>
<p><strong>example</strong>:</p>
<p>extern CModbus* pModbus; // this pointer is created by a derived class. </p>
<p>WORD wError;<br>
CWordArray anRegs;<br>
<font size="3">CDblArray adRegValues;</font><br>
<br>
anRegs.SetSize(1); <br>
<font size="3">adRegValues.SetSize(1);</font></p>
<p>wError = pModbus-><font size="3">ReadInputRegisters(17,8,1,</font>anRegs) ; //Reads
30009 register from slave 17 </p>
<p>wError = pModbus-><font size="3">ReadInputRegisters(17,8,1,adRegValues</font>) ;
//Read 30009...30012 registers from slave 17 and interpret as one double precision
floating point number</p>
<p><font size="3"><strong>See also: <a href="excep.htm">CModbusExcetion</a> , <a
href="Errors.htm">Error Codes</a> .</strong></font></p>
<p> </p>
<p><a name="CModbus::ReadOutputStatus"></a><font size="5"><strong>CModbus::ReadOutputStatus</strong></font></p>
<p><font size="3"><strong>WORD ReadOutputStatus( WORD nAddr, WORD nDataStart ,WORD nQtd
,CByteArray& anRegValues);</strong></font></p>
<p><font size="3"><strong>Return Value </strong></font></p>
<p><font size="3">A error code if the function failed or CModbus::ERR_OK if succeed. Use <a
href="#CModbus::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 </font></p>
<p><font size="3">anRegValues - array with registers read.</font></p>
<p><font size="3"><strong>Remarks</strong></font></p>
<p>Reads the status of discrete outputs (0<i>x</i> references, coils) in the slave, the
first coil 00001 is 0, second 00002 is 1 and so on. CByteArray return the coil on BOOL
format (TRUE - On , FALSE - Off).</p>
<p><strong>example</strong>:</p>
<p>extern CModbus* pModbus; // this pointer is created by a derived class. </p>
<p>WORD wError;<br>
CByteArray abCoils;<br>
<br>
abCoils.SetSize(37); </p>
<p>wError = pModbus-><font size="3">ReadOutputStatus(17,19,37,</font>abCoils) ; //Reads
coils 00020 ... 0056 from slave 17</p>
<p>//abCoils(0) is 00020 , abCoils(0) is 00021 ...</p>
<p><font size="3"><strong>See also: <a href="excep.htm">CModbusExcetion</a> , <a
href="Errors.htm">Error Codes</a> .</strong></font></p>
<p> </p>
<p><a name="CModbus::ReadInputStatus"></a><font size="5"><strong>CModbus::ReadInputStatus</strong></font></p>
<p><font size="3"><strong>WORD ReadOutputStatus( WORD nAddr, WORD nDataStart ,WORD nQtd
,CByteArray& anRegValues);</strong></font></p>
<p> </p>
<p><font size="3"><strong>Return Value </strong></font></p>
<p><font size="3">A error code if the function failed or CModbus::ERR_OK if succeed. Use <a
href="#CModbus::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 </font></p>
<p><font size="3">anRegValues - array with registers read.</font></p>
<p><font size="3"><strong>Remarks</strong></font></p>
<p>Reads the status of discrete inputs (1<i>x</i> references) in the slave, the first
input 10001 is 0, second 10002 is 1 and so on. CByteArray return the input on BOOL format
(TRUE - On , FALSE - Off).</p>
<p><strong>example</strong>:</p>
<p>extern CModbus* pModbus; // this pointer is created by a derived class. </p>
<p>WORD wError;<br>
CByteArray abCoils;<br>
<br>
abCoils.SetSize(22); </p>
<p>wError = pModbus-><font size="3">ReadIntputStatus(17,196,22,</font>abCoils) ;
//Reads inputs 10197 ... 10218 from slave 17</p>
<p>//abCoils(0) is 10197 , abCoils(0) is 10198 ...</p>
<p><font size="3"><strong>See also: <a href="excep.htm">CModbusExcetion</a> , <a
href="Errors.htm">Error Codes</a> .</strong></font></p>
<p><a name="CModbus::PresetSingleRegister"></a><font size="5"><strong>CModbus::PresetSingleRegister</strong></font></p>
<p><font size="3"><strong>WORD PresetSingleRegister(WORD nAddr, WORD nRegister , WORD
nRegValue);</strong></font></p>
<p><strong>WORD PresetSingleRegister(WORD nAddr,WORD nRegister , short int nRegValue);</strong></p>
<p><strong>WORD PresetSingleRegister(WORD nAddr,WORD nRegister ,float fRegValue,WORD
wRealType = REAL_NORMAL);</strong></p>
<p><strong>WORD PresetSingleRegister(WORD nAddr,WORD nRegister , double dRegValue,WORD
wRealType = REAL_NORMAL);</strong></p>
<p><font size="3"><strong>Return Value </strong></font></p>
<p><font size="3">A error code if the function failed or CModbus::ERR_OK if succeed. Use <a
href="#CModbus::ErrorMessage">ErrorMessage</a> to return an 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">nRegister - Register Address</font></p>
<p><font size="3">nRegValue - Preset Data</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>Presets a single holding register (4<i>x</i> reference). , the first register 40001 is
0, second 40002 is 1 and so on. When the data is a float number you are actually setting 2
registers and with a double number 4 registers are set.</p>
<p><strong>example</strong>:</p>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?