QModbusDevice 类是基类针对 Modbus 类 QModbusServer and QModbusClient . 更多...
| 头: | #include <QModbusDevice> |
| qmake: | QT += serialbus |
| Since: | Qt 5.8 |
| 继承: | QObject |
| 继承者: |
该类在 Qt 5.8 引入。
| enum | ConnectionParameter { SerialPortNameParameter, SerialParityParameter, SerialBaudRateParameter, SerialDataBitsParameter, SerialStopBitsParameter, …, UserParameter } |
| enum | Error { NoError, ReadError, WriteError, ConnectionError, ConfigurationError, …, UnknownError } |
| enum | State { UnconnectedState, ConnectingState, ConnectedState, ClosingState } |
| QModbusDevice (QObject * parent = nullptr) | |
| virtual | ~QModbusDevice () |
| bool | connectDevice () |
| QVariant | connectionParameter (int parameter ) const |
| QIODevice * | device () const |
| void | disconnectDevice () |
| QModbusDevice::Error | error () const |
| QString | errorString () const |
| void | setConnectionParameter (int parameter , const QVariant & value ) |
| QModbusDevice::State | state () const |
| void | errorOccurred (QModbusDevice::Error error ) |
| void | stateChanged (QModbusDevice::State state ) |
| virtual void | close () = 0 |
| virtual bool | open () = 0 |
| void | setError (const QString & errorText , QModbusDevice::Error error ) |
| void | setState (QModbusDevice::State newState ) |
This enum describes the possible values that can be set for a Modbus device connection.
The general purpose value (and the associated types) are:
| 常量 | 值 | 描述 |
|---|---|---|
QModbusDevice::SerialPortNameParameter
|
0
|
This parameter holds the serial port used for device communication, e.g. COM1.
QString
|
QModbusDevice::SerialParityParameter
|
1
|
此参数保持奇偶校验模式。
QSerialPort::Parity
|
QModbusDevice::SerialBaudRateParameter
|
2
|
This parameter holds the data baud rate for the communication.
QSerialPort::BaudRate
|
QModbusDevice::SerialDataBitsParameter
|
3
|
此参数保持帧中的数据位。
QSerialPort::DataBits
|
QModbusDevice::SerialStopBitsParameter
|
4
|
This parameter holds the number of stop bits in a frame.
QSerialPort::StopBits
|
QModbusDevice::NetworkPortParameter
|
5
|
此参数保持网络端口。
int
|
QModbusDevice::NetworkAddressParameter
|
6
|
This parameter holds the host address for network communication.
QString
|
用户选项:
| 常量 | 值 | 描述 |
|---|---|---|
QModbusDevice::UserParameter
|
0x100
|
This enum value has been deprecated. There will be no replacement. |
此枚举描述所有可能的错误条件。
| 常量 | 值 | 描述 |
|---|---|---|
QModbusDevice::NoError
|
0
|
没有出现错误。 |
QModbusDevice::ReadError
|
1
|
在读取操作期间出现错误。 |
QModbusDevice::WriteError
|
2
|
在写入操作期间出现错误。 |
QModbusDevice::ConnectionError
|
3
|
An error occurred when attempting to open the backend. |
QModbusDevice::ConfigurationError
|
4
|
An error occurred when attempting to set a configuration parameter. |
QModbusDevice::TimeoutError
|
5
|
A timeout occurred during I/O. An I/O operation did not finish within a given time frame. |
QModbusDevice::ProtocolError
|
6
|
出现 Modbus 特定协议错误。 |
QModbusDevice::ReplyAbortedError
|
7
|
The reply was aborted due to a disconnection of the device. |
QModbusDevice::UnknownError
|
8
|
出现未知错误。 |
注意: An UnknownError can also indicate that the received FunctionCode is not supported in the current implementation. In this case custom Modbus client implementations need to override the processResponse() and processPrivateResponse() methods to provide support for needed functions.
此枚举描述所有可能的设备状态。
| 常量 | 值 | 描述 |
|---|---|---|
QModbusDevice::UnconnectedState
|
0
|
设备已断开连接。 |
QModbusDevice::ConnectingState
|
1
|
设备正在连接。 |
QModbusDevice::ConnectedState
|
2
|
设备已连接到 Modbus 网络。 |
QModbusDevice::ClosingState
|
3
|
设备正在关闭。 |
构造 Modbus 设备采用指定 parent .
[signal]
void
QModbusDevice::
errorOccurred
(
QModbusDevice::Error
error
)
This signal is emitted when an error of the type, error ,出现。
[signal]
void
QModbusDevice::
stateChanged
(
QModbusDevice::State
state
)
This signal is emitted every time the state of the device changes. The new state is represented by state .
[虚拟]
QModbusDevice::
~QModbusDevice
()
销毁 QModbusDevice 实例
[pure virtual protected]
void
QModbusDevice::
close
()
This function is responsible for closing the Modbus connection. The implementation must ensure that the instance's state () 被设为 QModbusDevice::UnconnectedState .
另请参阅 disconnectDevice ().
把设备连接到 Modbus 网络。返回
true
若成功初启连接进程;否则
false
。最终连接成功,确认要求
state
() 改变成
QModbusDevice::ConnectedState
.
此函数调用 open () 作为其实现的一部分。
另请参阅 open ().
返回的值关联给定连接 parameter 。返回值可以为空。
默认情况下
QModbusDevice
is initialized with some common values. The serial port settings are even parity, a baud rate of 19200 bits per second, eight data bits and one stop bit. The network settings for the host address is set to local host and port to 502.
注意: 要成功连接串行, SerialPortNameParameter 需要被设为有效通信端口。有效串口的有关信息可以获得从 QSerialPortInfo .
注意: 若设备已连接,在重新连接设备后考虑设置。
另请参阅 setConnectionParameter () 和 ConnectionParameter .
返回的底层
QIODevice
用于 ModBus 通信或
nullptr
若设备尚未完全初始化。
注意: Do not store a pointer to the underlying device, because it can be invalidated at any point in time.
该函数在 Qt 5.14 引入。
断开设备连接。
此函数调用 close () 作为其实现的一部分。
返回设备的错误状态。
另请参阅 setError () 和 QModbusDevice::Error .
返回用于设备错误的描述性错误文本。
另请参阅 QModbusDevice::Error .
[pure virtual protected]
bool
QModbusDevice::
open
()
此函数被调用通过
connectDevice
(). Subclasses must provide an implementation that returns
true
on successful Modbus connection or connection initiation; otherwise returns
false
.
实现必须确保实例的 state () 被设为 QModbusDevice::ConnectingState or QModbusDevice::ConnectedState upon success; otherwise QModbusDevice::UnconnectedState 。通常, QModbusDevice::ConnectingState is used when the connection process reports back asynchronously and QModbusDevice::ConnectedState in case of synchronous connect behavior.
另请参阅 connectDevice ().
设置值为 parameter to value 。若 parameter already exists, the previous value is overwritten. A active or running connection is not affected by such parameter changes.
另请参阅 ConnectionParameter and connectionParameter ().
[protected]
void
QModbusDevice::
setError
(const
QString
&
errorText
,
QModbusDevice::Error
error
)
Sets the error state of the device. ModBus device implementations must use this function in case of an error to set the error type and a descriptive errorText .
另请参阅 error () 和 QModbusDevice::Error .
[protected]
void
QModbusDevice::
setState
(
QModbusDevice::State
newState
)
把设备的状态设为 newState . Modbus device implementations must use this function to update the device state.
另请参阅 state ().
返回设备的当前状态。
另请参阅 setState () 和 stateChanged ().