传感器
传感器示例展示了两个应用程序如何通过使用 UDP 套接字发送 protobuf 消息进行通信。
传感器示例由以下组件组成:
protobuf_sensors
使用qt_add_protobuf调用从 protobuf 模式生成的库。protobuf_sensor_emulator
模拟简单传感器行为的应用程序。protobuf_sensors_client
显示来自 UDP 套接字的传感器数据的应用程序。
客户端应用程序绑定localhost
UDP 端口65500
并等待来自模拟器应用程序的数据。
使用模拟器应用程序更改传感器数据值,并将数据发送到客户端的 UDP 端口。
应用程序使用protobuf_sensors
库生成的信息进行通信。该库由 .proto 文件中描述的 protobuf 模式生成。
qt_add_protobuf(protobuf_sensors PROTO_FILES sensors.proto tlv.proto )
第一个文件描述了封装传感器数据的类型-长度-值报文:
package qt.examples.sensors.tlv; enum MessageType { Coordinates = 0; Temperature = 1; WarningNotification = 2; } // Protobuf messages imply inline data size. message TlvMessage { MessageType type = 1; bytes value = 2; }
第二个 .proto 文件包含传感器信息的描述:
package qt.examples.sensors; message Coordinates { double longitude = 1; double latitude = 2; double altitude = 3; } message Temperature { enum Unit { Farenheit = 0; Celsius = 1; } sint32 value = 1; Unit units = 2; } message WarningNotification { string text = 1; }
信息使用QProtobufSerializer 进行序列化,您可以在客户端中对其进行实例化:
class SensorClient : public QObject { Q_OBJECT ... private: QUdpSocket m_client; QProtobufSerializer m_serializer; };
以及模拟器:
class SensorEmulator : public QObject { Q_OBJECT ... QUdpSocket m_socket; QProtobufSerializer m_serializer; };
在模拟器应用程序中单击Send 按钮后,QLineEdit 字段的数据将从字符串格式转换为特定报文字段的格式,例如,坐标报文字段的格式为双倍:
QObject::connect(ui->sendCoordinates, &QPushButton::clicked, this, [this]() { qt::examples::sensors::Coordinates coord; coord.setLatitude(ui->latitudeValue->text().toDouble()); coord.setLongitude(ui->longitudeValue->text().toDouble()); coord.setAltitude(ui->altitudeValue->text().toDouble()); emit coordinatesUpdated(coord); });
然后将包含所有字段的报文序列化,并用 Type-Length-Value 报文进行包装:
Q_ASSERT(serializer != nullptr); tlv::TlvMessage msg; msg.setType(type); msg.setValue(data); return msg.serialize(serializer);
客户端对接收到的数据报进行反向操作。首先从数据报数据反序列化 Type-Length-Value 报文:
const autodatagram=m_client.receiveDatagram(); qt::examples::sensors::tlv::TlvMessage msg; msg.deserialize(&m_serializer,datagram.data());if(m_serializer.lastError() != = Qt::Sensors.QAbstractProtobufSerializer::Error::None) { qWarning().nospace() << "Unable to deserialize datagram (" <<qToUnderlying(m_serializer.lastError())<< ")"<<m_serializer.lastErrorString();continue; }
然后将类型-长度-值报文反序列化为传感器报文:
qt::examples::sensors::Coordinates coord; coord.deserialize(&m_serializer, msg.value()); emit coordinatesUpdated(coord); break;
最后进行转换并显示给用户:
ui->latitudeValue->setText(QString::number(coord.latitude(), 'f', 7)); ui->longitudeValue->setText(QString::number(coord.longitude(), 'f', 7)); ui->altitudeValue->setText(QString::number(coord.altitude(), 'f', 7));
注意: 运行示例之前,请确保您的操作系统允许绑定 UDP 端口65500
并通过 UDP 发送数据。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.