QT做UDP组播,一行长度为248,全帧长度为248*128,每一包行数不固定,子帧同步码F1F330,副帧同步码0230DF,将接收的每一包都缓存,在判断收到副帧同步码时发出,给出代码及注释
时间: 2024-03-27 22:35:05 浏览: 10
以下是一个简单的 Qt UDP 组播示例,可以接收每一包数据并缓存,同时在接收到相应的同步码时发出通知:
```c++
#include <QUdpSocket>
#include <QByteArray>
#include <QDataStream>
// 每一行的长度为248
const int LINE_LEN = 248;
// 全帧长度为248*128
const int FRAME_LEN = LINE_LEN * 128;
// 子帧同步码
const quint32 SUBFRAME_SYNC = 0xF1F330;
// 副帧同步码
const quint32 FRAME_SYNC = 0x0230DF;
class UDPReceiver : public QObject
{
Q_OBJECT
public:
UDPReceiver(QObject* parent = nullptr)
: QObject(parent)
{
// 创建组播套接字
m_socket = new QUdpSocket(this);
m_socket->bind(QHostAddress::AnyIPv4, 1234, QUdpSocket::ShareAddress);
m_socket->joinMulticastGroup(QHostAddress("239.255.255.250"));
// 连接 readyRead 信号
connect(m_socket, &QUdpSocket::readyRead, this, &UDPReceiver::processPendingDatagrams);
}
private:
QUdpSocket* m_socket;
QByteArray m_buffer;
quint32 m_subframeSyncCount = 0;
quint32 m_frameSyncCount = 0;
void processPendingDatagrams()
{
while (m_socket->hasPendingDatagrams()) {
// 读取数据报
QByteArray datagram;
datagram.resize(m_socket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
m_socket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
// 将数据追加到缓存
m_buffer.append(datagram);
// 查找同步码
while (true) {
// 搜索子帧同步码
int subframeSyncIndex = m_buffer.indexOf(reinterpret_cast<const char*>(&SUBFRAME_SYNC), sizeof(SUBFRAME_SYNC));
if (subframeSyncIndex == -1) {
// 没有找到子帧同步码
break;
}
++m_subframeSyncCount;
// 检查是否收到了一个完整的副帧
if (m_subframeSyncCount == 64) {
// 发送信号,表示收到一个完整的副帧
emit frameReceived(m_buffer.left(FRAME_LEN));
m_buffer.remove(0, FRAME_LEN);
m_subframeSyncCount = 0;
++m_frameSyncCount;
}
// 继续搜索子帧同步码
m_buffer.remove(0, subframeSyncIndex + sizeof(SUBFRAME_SYNC));
}
// 搜索副帧同步码
int frameSyncIndex = m_buffer.indexOf(reinterpret_cast<const char*>(&FRAME_SYNC), sizeof(FRAME_SYNC));
if (frameSyncIndex != -1) {
// 发送信号,表示收到一个完整的副帧
emit frameReceived(m_buffer.left(FRAME_LEN));
m_buffer.remove(0, FRAME_LEN);
m_subframeSyncCount = 0;
m_frameSyncCount = 0;
}
}
}
signals:
// 收到完整的副帧信号
void frameReceived(const QByteArray& frame);
};
```
使用方法:
```c++
UDPReceiver* receiver = new UDPReceiver(this);
connect(receiver, &UDPReceiver::frameReceived, this, &MyClass::onFrameReceived);
void MyClass::onFrameReceived(const QByteArray& frame)
{
// 处理收到的完整的副帧数据
}
```
在 `onFrameReceived` 方法中,你可以处理收到的完整的副帧数据。