PySide6.QtSerialBus.QModbusServer¶
- class QModbusServer¶
The
QModbusServer
class is the interface to receive and process Modbus requests. More…Inherited by:
QModbusTcpServer
,QModbusRtuSerialServer
Synopsis¶
Methods¶
def
__init__()
def
data()
def
serverAddress()
def
setData()
Virtual methods¶
def
processRequest()
def
readData()
def
setMap()
def
setValue()
def
value()
def
writeData()
Signals¶
def
dataWritten()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description¶
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.- class Option¶
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:
Constant
Description
QModbusServer.DiagnosticRegister
The diagnostic register of the server.
quint16
QModbusServer.ExceptionStatusOffset
The exception status byte offset of the server.
quint16
QModbusServer.DeviceBusy
Flag to signal the server is engaged in processing a long-duration program command.
quint16
QModbusServer.AsciiInputDelimiter
The Modbus ASCII end of message delimiter.
char
QModbusServer.ListenOnlyMode
Flag to set listen only mode of the server. This function is typically supported only by Modbus serial devices.
bool
QModbusServer.ServerIdentifier
The identifier of the server, not the server address.
quint8
QModbusServer.RunIndicatorStatus
The run indicator of the server.
quint8
QModbusServer.AdditionalData
The additional data of the server.
QByteArray
QModbusServer.DeviceIdentification
The physical and functional description of the server.
QModbusDeviceIdentification
User options:
Constant
Description
QModbusServer.UserOption
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.
Constructs a Modbus server with the specified
parent
.- data(newData)¶
- Parameters:
newData –
QModbusDataUnit
- Return type:
bool
Returns the values in the register range given by
newData
.newData
must provide a valid register type, start address and valueCount. The returnednewData
will contain the register values associated with the given range.If
newData
contains a valid register type but a negative start address the entire register map is returned andnewData
appropriately sized.See also
- data(table, address, data)
- Parameters:
table –
RegisterType
address – int
data –
quint16
- Return type:
bool
Reads data stored in the Modbus server. A Modbus server has four tables (
table
) and each have a uniqueaddress
field, which is used to readdata
from the desired field. SeeRegisterType
for more information about the different tables. Returnsfalse
if address is outside of the map range or the register type is not even defined.See also
- dataWritten(table, address, size)¶
- Parameters:
table –
RegisterType
address – int
size – int
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:
Register type (
table
) that was written,address
of the first field that was written,and
size
of consecutive fields that were written starting fromaddress
.
The signal is not emitted when the to-be-written fields have not changed due to no change in value.
- processPrivateRequest(request)¶
- Parameters:
request –
QModbusPdu
- Return type:
This function should be implemented by custom Modbus servers. It is called by
processRequest()
if the givenrequest
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.
This default implementation returns a
QModbusExceptionResponse
with therequest
function code and error code set to illegal function.See also
- processRequest(request)¶
- Parameters:
request –
QModbusPdu
- Return type:
Processes a Modbus client
request
and returns a Modbus response. This function returns aQModbusResponse
orQModbusExceptionResponse
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.Note
This function should not be overridden to provide a custom implementation for non-standard Modbus request types.
See also
- processesBroadcast()¶
- Return type:
bool
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; otherwisefalse
. The default implementation returns alwaysfalse
.Note
The return value of this function only makes sense from within
processRequest()
orprocessPrivateRequest()
, otherwise it can only tell that the last request processed was a broadcast request.- readData(newData)¶
- Parameters:
newData –
QModbusDataUnit
- Return type:
bool
Reads the values in the register range given by
newData
and writes the data back tonewData
. Returnstrue
on success orfalse
ifnewData
is0
, thenewData
range is outside of the map range or the registerType() does not exist.Note
Sub-classes that implement reading from a different backing store then default one, also need to implement
setMap()
andwriteData()
.See also
- serverAddress()¶
- Return type:
int
Returns the address of this Modbus server instance.
See also
- setData(unit)¶
- Parameters:
unit –
QModbusDataUnit
- Return type:
bool
Writes
newData
to the Modbus server map. Returnsfalse
if thenewData
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 whennewData
contains exactly the same values as the register already. Nevertheless this function returnstrue
in such cases.See also
- setData(table, address, data)
- Parameters:
table –
RegisterType
address – int
data – int
- Return type:
bool
Writes data to the Modbus server. A Modbus server has four tables (
table
) and each have a uniqueaddress
field, which is used to writedata
to the desired field. Returnsfalse
if address outside of the map range.If the call was successful the
dataWritten()
signal is emitted. Note that the signal is not emitted whendata
has not changed. Nevertheless this function returnstrue
in such cases.See also
- setMap(map)¶
- Parameters:
map – Dictionary with keys of type .QModbusDataUnit.RegisterType and values of type QModbusDataUnit.
- Return type:
bool
Sets the registered map structure for requests from other ModBus clients to
map
. The register values are initialized with zero. Returnstrue
on success; otherwisefalse
.If this function is not called before connecting, a default register with zero entries is setup.
Note
Calling this function discards any register value that was previously set.
- setServerAddress(serverAddress)¶
- Parameters:
serverAddress – int
Sets the address for this Modbus server instance to
serverAddress
.See also
- setValue(option, value)¶
- Parameters:
option – int
value – object
- Return type:
bool
Sets the
newValue
foroption
and returnstrue
on success;false
otherwise.Note
If the option’s associated type is
quint8
orquint16
and the type ofnewValue
is larger, the data will be truncated or conversation will fail.Key
Description
Sets the diagnostic register of the server in a device specific encoding to
newValue
. The default value preset is0x0000
. The bit values of the register need device specific documentation.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 with0x0000h
. The default value preset is0x0000
, using the exception status coils similar to Modicon 984 CPUs (coils 1-8).The function returns
true
if the coils register contains the 8 bits required for storing and retrieving the status coils, otherwisefalse
.Sets a flag that signals that the server is engaged in processing a long-duration program command. Valid values are
0x0000
(not busy) and0xffff
(busy). The default value preset is0x0000
.The
newValue
becomes the end of message delimiter for future Modbus ASCII messages. The default value preset is\n
.Ss the server’s listen only state to
newValue
. If listen only mode is set totrue
, messages are monitored but no response will be sent. The default value preset isfalse
.Sets the server’s manufacturer identifier to
newValue
. Possible values are in the range of0x00
to 0xff. The default value preset is0x0a
.Sets the servers’ run indicator status to
newValue
. This data is used as addendum by theReportServerId
function code. Valid values are0x00
(OFF) and0xff
(ON). The default value preset is0xff
(ON).Sets the server’s additional data to
newValue
. This data is used as addendum by theReportServerId
function code. The maximum data size cannot exceed 249 bytes to match response message size restrictions. The default value preset isQt Modbus Server
.Sets the server’s physical and functional description. By default there is no additional device identification data set.
Sets the value of a user option to
newValue
.Note
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.
See also
- value(option)¶
- Parameters:
option – int
- Return type:
object
Returns the value for
option
or an invalidQVariant
if the option is not set.Option
Description
Returns the diagnostic register value of the server. The diagnostic register contains device specific contents where each bit has a specific meaning.
Returns the offset address of the exception status byte location in the coils register.
Returns a flag that signals if the server is engaged in processing a long-duration program command.
Returns a end of message delimiter of Modbus ASCII messages.
Returns the server’s listen only state. Messages are monitored but no response will be sent.
Returns the server manufacturer’s identifier code. This can be an arbitrary value in the range of
0x00
to 0xff.Returns the server’s run indicator status. This data is used as addendum by the
ReportServerId
function code.Returns the server’s additional data. This data is used as addendum by the
ReportServerId
function code.Returns the server’s physical and functional description.
Returns the value of a user option.
Note
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.
See also
- writeData(unit)¶
- Parameters:
unit –
QModbusDataUnit
- Return type:
bool
Writes
newData
to the Modbus server map. Returnstrue
on success, orfalse
if thenewData
range is outside of the map range or the registerType() does not exist.Note
Sub-classes that implement writing to a different backing store then default one, also need to implement
setMap()
andreadData()
. ThedataWritten()
signal needs to be emitted from within the functions implementation as well.See also