Pc端发送:
domain:149
topic:/program/send
Pc端接收:
domain:150
topic:/program/recv
json内容
{
"id":"01",
"type":"00",
"status":"SUCCESS",
"data":""
}
ccdds.h
#ifndef CCDDSWIDGET_H
#define CCDDSWIDGET_H
#include <QWidget>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <fastdds.h>
namespace DDS {
namespace Internal {
class CCDDS : public QWidget
{
Q_OBJECT
public:
explicit CCDDS(QWidget *parent = nullptr);
private:
static void recv_data(void *data, int len);
static void send(QString id, QString type, QString status, QString data);
void dds_init();
private:
fastdds *m_cert_dds_send;
fastdds *m_cert_dds_recv;
};
} //end Internal
} //end DDS
#endif
// CCDDS
ccdds.cpp
void CCDDS::dds_init()
{
// 发送服务
m_dds_send = new fastdds(data_type_buf);
int domain_id_send = 149;
const char *topic_send = "/program/send";
m_dds_send->set_history_qos(recv_real_time);
m_dds_send->reg(domain_id_send, topic_send, nullptr);
// 接受服务
m_dds_recv = new fastdds(data_type_buf);
int domain_id_recv = 150;
const char *topic_recv = "/program/recv";
m_dds_recv->set_history_qos(recv_real_time);
m_dds_recv->reg(domain_id_recv, topic_recv , recv_data);
}
void CCDDS::recv_data(void *data, int len)
{
QByteArray str_data = QString::fromLatin1((char *)data, len).toLatin1();
QJsonParseError json_error;
QJsonDocument jsonDoc(QJsonDocument::fromJson(str_data, &json_error));
if(json_error.error != QJsonParseError::NoError){
return;
}
QJsonObject rootObj = jsonDoc.object();
//TODO:解析json数据
}
void CCDDS::send_data(QString id, QString type, QString status, QString data)
{
QJsonObject json;
json.insert("id", id);
json.insert("type", type);
json.insert("status", status);
json.insert("data", data);
QJsonDocument docment;
docment.setObject(json);
QByteArray data = QString::fromUtf8(docment.toJson(QJsonDocument::Compact)).toLatin1();
delay_msec(1000);
m_dds_send->send(data.data(), data.length());
}