QModbusServer 类是接收和处理 Modbus 请求的接口。 更多...
头: | #include <QModbusServer> |
CMake: |
find_package(Qt6 REQUIRED COMPONENTS SerialBus)
target_link_libraries(mytarget PRIVATE Qt6::SerialBus) |
qmake: | QT += serialbus |
继承: | QModbusDevice |
继承者: |
enum | Option { DiagnosticRegister, ExceptionStatusOffset, DeviceBusy, AsciiInputDelimiter, ListenOnlyMode, …, UserOption } |
QModbusServer (QObject * parent = nullptr) | |
bool | data (QModbusDataUnit * newData ) const |
bool | data (QModbusDataUnit::RegisterType table , quint16 address , quint16 * data ) const |
virtual bool | processesBroadcast () const |
int | serverAddress () const |
bool | setData (const QModbusDataUnit & newData ) |
bool | setData (QModbusDataUnit::RegisterType table , quint16 address , quint16 data ) |
virtual bool | setMap (const QModbusDataUnitMap & map ) |
void | setServerAddress (int serverAddress ) |
virtual bool | setValue (int option , const QVariant & newValue ) |
virtual QVariant | value (int option ) const |
void | dataWritten (QModbusDataUnit::RegisterType table , int address , int size ) |
virtual QModbusResponse | processPrivateRequest (const QModbusPdu & request ) |
virtual QModbusResponse | processRequest (const QModbusPdu & request ) |
virtual bool | readData (QModbusDataUnit * newData ) const |
virtual bool | writeData (const QModbusDataUnit & newData ) |
Modbus networks can have multiple Modbus servers. Modbus Servers are read/written by a Modbus client represented by QModbusClient . QModbusServer communicates with a Modbus backend, providing users with a convenient API.
Each Modbus server has a set of values associated with it, each with its own option.
The general purpose options (and the associated types) are:
常量 | 值 | 描述 |
---|---|---|
QModbusServer::DiagnosticRegister
|
0
|
The diagnostic register of the server.
quint16
|
QModbusServer::ExceptionStatusOffset
|
1
|
The exception status byte offset of the server.
quint16
|
QModbusServer::DeviceBusy
|
2
|
Flag to signal the server is engaged in processing a long-duration program command.
quint16
|
QModbusServer::AsciiInputDelimiter
|
3
|
The Modbus ASCII end of message delimiter.
char
|
QModbusServer::ListenOnlyMode
|
4
|
Flag to set listen only mode of the server. This function is typically supported only by Modbus serial devices.
bool
|
QModbusServer::ServerIdentifier
|
5
|
The identifier of the server,
not
the server address.
quint8
|
QModbusServer::RunIndicatorStatus
|
6
|
The run indicator of the server.
quint8
|
QModbusServer::AdditionalData
|
7
|
The additional data of the server.
QByteArray
|
QModbusServer::DeviceIdentification
|
8
|
The physical and functional description of the server.
QModbusDeviceIdentification
|
用户选项:
常量 | 值 | 描述 |
---|---|---|
QModbusServer::UserOption
|
0x100
|
The first option that can be used for user-specific purposes. |
For user options, it is up to the developer to decide which types to use and ensure that components use the correct types when accessing and setting values.
[explicit]
QModbusServer::
QModbusServer
(
QObject
*
parent
= nullptr)
构造 Modbus 服务器采用指定 parent .
Returns the values in the register range given by newData .
newData must provide a valid register type, start address and valueCount. The returned newData will contain the register values associated with the given range.
若 newData contains a valid register type but a negative start address the entire register map is returned and newData appropriately sized.
另请参阅 setData ().
Reads data stored in the Modbus server. A Modbus server has four tables (
table
) and each have a unique
address
field, which is used to read
data
from the desired field. See
QModbusDataUnit::RegisterType
for more information about the different tables. Returns
false
if address is outside of the map range or the register type is not even defined.
另请参阅 QModbusDataUnit::RegisterType and setData ().
[signal]
void
QModbusServer::
dataWritten
(
QModbusDataUnit::RegisterType
table
,
int
address
,
int
size
)
This signal is emitted when a Modbus client has written one or more fields of data to the Modbus server. The signal contains information about the fields that were written:
The signal is not emitted when the to-be-written fields have not changed due to no change in value.
[virtual protected]
QModbusResponse
QModbusServer::
processPrivateRequest
(const
QModbusPdu
&
request
)
This function should be implemented by custom Modbus servers. It is called by processRequest () if the given request is not a standard Modbus request.
Overwriting this function allows handling of additional function codes and subfunction-codes not specified in the Modbus Application Protocol Specification 1.1b. Reimplementations should call this function again to ensure an exception response is returned for all unknown function codes the custom Modbus implementation does not handle.
此默认实现返回
QModbusExceptionResponse
采用
request
function code and error code set to illegal function.
另请参阅 processRequest ().
[virtual protected]
QModbusResponse
QModbusServer::
processRequest
(const
QModbusPdu
&
request
)
Processes a Modbus client request and returns a Modbus response. This function returns a QModbusResponse or QModbusExceptionResponse depending on the nature of the request.
The default implementation of this function handles all standard Modbus function codes as defined by the Modbus Application Protocol Specification 1.1b. All other Modbus function codes not included in the specification are forwarded to processPrivateRequest ().
The default handling of the standard Modbus function code requests can be overwritten by reimplementing this function. The override must handle the request type in question and return the appropriate QModbusResponse . A common reason might be to filter out function code requests for data values to limit read/write access and function codes not desired in particular implementations such as serial line diagnostics on ethernet or Modbus Plus transport layers. Every other request type should be forwarded to this default implementation.
注意: This function should not be overridden to provide a custom implementation for non-standard Modbus request types.
另请参阅 processPrivateRequest ().
[虚拟]
bool
QModbusServer::
processesBroadcast
() const
Subclasses should implement this function if the transport layer shall handle broadcasts. The implementation then should return
true
if the currently processed request is a broadcast request; otherwise
false
. The default implementation returns always
false
.
注意: The return value of this function only makes sense from within processRequest () 或 processPrivateRequest (), otherwise it can only tell that the last request processed was a broadcast request.
[virtual protected]
bool
QModbusServer::
readData
(
QModbusDataUnit
*
newData
) const
Reads the values in the register range given by
newData
and writes the data back to
newData
。返回
true
当成功时或
false
if
newData
is
0
,
newData
range is outside of the map range or the registerType() does not exist.
注意: Sub-classes that implement reading from a different backing store then default one, also need to implement setMap () 和 writeData ().
另请参阅 setMap () 和 writeData ().
Returns the address of this Modbus server instance.
另请参阅 setServerAddress ().
写入
newData
to the Modbus server map. Returns
false
若
newData
range is outside of the map range.
If the call was successful the
dataWritten
() signal is emitted. Note that the signal is not emitted when the addressed register has not changed. This may happen when
newData
contains exactly the same values as the register already. Nevertheless this function returns
true
in such cases.
另请参阅 data ().
Writes data to the Modbus server. A Modbus server has four tables (
table
) and each have a unique
address
field, which is used to write
data
to the desired field. Returns
false
if address outside of the map range.
If the call was successful the
dataWritten
() signal is emitted. Note that the signal is not emitted when
data
has not changed. Nevertheless this function returns
true
in such cases.
另请参阅 QModbusDataUnit::RegisterType , data (),和 dataWritten ().
[虚拟]
bool
QModbusServer::
setMap
(const
QModbusDataUnitMap
&
map
)
Sets the registered map structure for requests from other ModBus clients to
map
. The register values are initialized with zero. Returns
true
当成功时;否则
false
.
If this function is not called before connecting, a default register with zero entries is setup.
注意: Calling this function discards any register value that was previously set.
Sets the address for this Modbus server instance to serverAddress .
另请参阅 serverAddress ().
[虚拟]
bool
QModbusServer::
setValue
(
int
option
, const
QVariant
&
newValue
)
设置
newValue
for
option
并返回
true
当成功时;
false
否则。
注意:
若选项的关联类型为
quint8
or
quint16
and the type of
newValue
is larger, the data will be truncated or conversation will fail.
Key | 描述 |
---|---|
QModbusServer::DiagnosticRegister |
Sets the diagnostic register of the server in a device specific encoding to
newValue
. The default value preset is
0x0000
. The bit values of the register need device specific documentation.
|
QModbusServer::ExceptionStatusOffset |
Sets the exception status byte offset of the server to
newValue
which is the absolute offset address in the coils (0x register). Modbus register table starting with
0x0000h
. The default value preset is
0x0000
, using the exception status coils similar to Modicon 984 CPUs (coils 1-8).
函数返回
|
QModbusServer::DeviceBusy |
Sets a flag that signals that the server is engaged in processing a long-duration program command. Valid values are
0x0000
(not busy) and
0xffff
(busy). The default value preset is
0x0000
.
|
QModbusServer::AsciiInputDelimiter |
The
newValue
becomes the end of message delimiter for future Modbus ASCII messages. The default value preset is
\n
.
|
QModbusServer::ListenOnlyMode |
Ss the server's listen only state to
newValue
. If listen only mode is set to
true
, messages are monitored but no response will be sent. The default value preset is
false
.
|
QModbusServer::ServerIdentifier |
Sets the server's manufacturer identifier to
newValue
. Possible values are in the range of
0x00
to 0xff. The default value preset is
0x0a
.
|
QModbusServer::RunIndicatorStatus |
Sets the servers' run indicator status to
newValue
. This data is used as addendum by the
QModbusPdu::ReportServerId
function code. Valid values are
0x00
(OFF) and
0xff
(ON). The default value preset is
0xff
(ON).
|
QModbusServer::AdditionalData |
Sets the server's additional data to
newValue
. This data is used as addendum by the
QModbusPdu::ReportServerId
function code. The maximum data size cannot exceed 249 bytes to match response message size restrictions. The default value preset is
Qt Modbus Server
.
|
QModbusServer::DeviceIdentification | Sets the server's physical and functional description. By default there is no additional device identification data set. |
QModbusServer::UserOption |
Sets the value of a user option to
newValue
.
注意: For user options, it is up to the developer to decide which types to use and ensure that components use the correct types when accessing and setting values. |
另请参阅 value ().
[虚拟]
QVariant
QModbusServer::
value
(
int
option
) const
Returns the value for
option
or an invalid
QVariant
if the option is not set.
选项 | 描述 |
---|---|
QModbusServer::DiagnosticRegister | Returns the diagnostic register value of the server. The diagnostic register contains device specific contents where each bit has a specific meaning. |
QModbusServer::ExceptionStatusOffset | Returns the offset address of the exception status byte location in the coils register. |
QModbusServer::DeviceBusy | Returns a flag that signals if the server is engaged in processing a long-duration program command. |
QModbusServer::AsciiInputDelimiter | Returns a end of message delimiter of Modbus ASCII messages. |
QModbusServer::ListenOnlyMode | Returns the server's listen only state. Messages are monitored but no response will be sent. |
QModbusServer::ServerIdentifier |
Returns the server manufacturer's identifier code. This can be an arbitrary value in the range of
0x00
to 0xff.
|
QModbusServer::RunIndicatorStatus | Returns the server's run indicator status. This data is used as addendum by the QModbusPdu::ReportServerId function code. |
QModbusServer::AdditionalData | Returns the server's additional data. This data is used as addendum by the QModbusPdu::ReportServerId function code. |
QModbusServer::DeviceIdentification | Returns the server's physical and functional description. |
QModbusServer::UserOption |
Returns the value of a user option. 注意: For user options, it is up to the developer to decide which types to use and ensure that components use the correct types when accessing and setting values. |
另请参阅 setValue ().
[virtual protected]
bool
QModbusServer::
writeData
(const
QModbusDataUnit
&
newData
)
写入
newData
to the Modbus server map. Returns
true
当成功时,或
false
若
newData
range is outside of the map range or the registerType() does not exist.
注意: Sub-classes that implement writing to a different backing store then default one, also need to implement setMap () 和 readData ()。 dataWritten () signal needs to be emitted from within the functions implementation as well.
另请参阅 setMap (), readData (),和 dataWritten ().