废话没有,上关键代码

头文件

#include <QUdpSocket>
#include <qtcpsocket.h>
#ifndef vrUDP
#define vrUDP #define PLANE_DATA_NUM 30 struct PlanePoseStruct{
double lon;
double lat;
double height;
double heading;
double pitch;
double rotate;
}; class JSBSimSocket : public QObject
{
Q_OBJECT
public:
JSBSimSocket();
~JSBSimSocket();
void initNet();
void sendPlaneControlData(char * data, int length);
private slots:
void recvPlanePose();
void recvTcpBack();
private:
QTcpSocket *m_control_TcpSocket;
QUdpSocket *m_get_UdpSocket;
QHostAddress m_get_udphost;
quint16 m_get_UdpPort; double m_JSBSimData[PLANE_DATA_NUM]; ·······
};

源文件

#include "vrUDP.h"

JSBSimSocket::JSBSimSocket()
{
} JSBSimSocket::~JSBSimSocket()
{
} void JSBSimSocket::initNet()
{
//用于控制端,基于QT_TCP客服端:飞机控制发送,信息反馈
QHostAddress tcp_host = QHostAddress::LocalHost;//这里请用xml数据
quint16 tcp_port = ;//这里请用xml数据
m_control_TcpSocket = new QTcpSocket;
m_control_TcpSocket->connectToHost(tcp_host, tcp_port);
m_control_TcpSocket->waitForConnected();
connect(m_control_TcpSocket, SIGNAL(readyRead()), this, SLOT(recvTcpBack()));
m_control_TcpSocket->waitForReadyRead(); //用于获取飞行姿态,基于QT_UDP的服务器。
m_get_udphost = QHostAddress::Any;//这里请用xml数据
m_get_UdpPort = ;//这里请用xml数据
m_get_UdpSocket = new QUdpSocket;
m_get_UdpSocket->bind(m_get_udphost,m_get_UdpPort);
connect(m_get_UdpSocket,SIGNAL(readyRead()),this,SLOT(recvPlanePose()));
} void JSBSimSocket::sendPlaneControlData(char * data, int length)
{
//发送控制信息
//通信的关键就在于这个data的组织
//具体的通讯内容要参考JSBSim的数据结构
int re = m_control_TcpSocket->write(data);
if (re == - )
{
printf("send error");
}
} void JSBSimSocket::recvTcpBack()
{
char buffer[];
qint64 size = m_control_TcpSocket->read(buffer, );
if (size > )
{
//这里获取TCP的反馈,可以进行后期交互操作
buffer[size] = '\0';
printf("recvTcpBack:%s", buffer);
}
} void JSBSimSocket::recvPlanePose()
{
char buffer[];
qint64 size = m_get_UdpSocket->readDatagram(buffer, sizeof(PlanePoseStruct));
if (size > )
{
buffer[size] = '\0';
memcpy(&m_JSBSimData[], buffer, size); m_PlanePose.lon = m_JSBSimData[];
m_PlanePose.lat = m_JSBSimData[];
m_PlanePose.height = m_JSBSimData[] / 3.2808;
m_PlanePose.rotate = m_JSBSimData[] / 3.14 * ;
m_PlanePose.pitch = m_JSBSimData[] / 3.14 * ;
m_PlanePose.heading = m_JSBSimData[] / 3.14 * ; ···············
}
} ···············

简单说一下:

我的功能是控制飞机的驾驶并用TCP告诉JSBSim。

JSBSim算当前飞机姿态,然后通过UDP告诉我飞机姿态

至于,为什么不用TCP一次性搞定,因为。。。我只是想试下TCP|UDP可以么。。。

关于JSBSim端怎么实现的,这个看看它源码自己研究下就可以喽!

04-13 21:35