废话没有,上关键代码
头文件
#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 = 8009;//这里请用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 = 8008;//这里请用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 == -1 ) { printf("send error"); } } void JSBSimSocket::recvTcpBack() { char buffer[4096]; qint64 size = m_control_TcpSocket->read(buffer, 4096); if (size > 0) { //这里获取TCP的反馈,可以进行后期交互操作 buffer[size] = '