QT文件的创建此处略过,qt网络通信需在pro文件中添QT += network这个命令,否则找不到网络通信相关的头文件
不会配置FlightGear通信配置的请查看下面章节:
配置xml文件来实现FlightGear通信,接收与发送数据_残梦5314的博客-CSDN博客
下面的代码可以实现FlightGear数据的接收,也可以进行发送数据,需要开两个端口,一个进行接收,一个进行数据的发送,接收数据时为服务器,FlightGear为客户端,发送数据时与接收数据时相反。
开两个端口的原因:
FlightGear通信协议向外提供了三个参数用来指定通信的方向,out(输出),in(输入),bi(双向),bi参数在源码中没做处理,所以我用bi参数试的时候,连接不上,这是一个bug
.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include <QMessageBox>
#include <QTextCodec>//编码转换
#include <QHostInfo>
#include <QString>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void TabWidget();
private slots:
void on_open_pushButton_clicked();
void myslot();
void on_HEX_RadioButton_clicked();
void on_Send_PushButtn_clicked();
void on_ASCII_radioButton_clicked();
void on_clear_pushButton_clicked();
void on_client_pushButton_clicked();
private:
Ui::MainWindow *ui;
QTcpServer *server;
QTextCodec *tc;
QTcpSocket *socket;//通讯套接字
QTcpSocket *sockfd1;
QList<QString> list;
};
#endif // MAINWINDOW_H
.cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QList>
#include <QHostAddress>
#include <QStringList>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//获取本机名称
QHostInfo name =QHostInfo::fromName(QHostInfo::localHostName());
//获取本机可用ip地址
QList<QHostAddress> addr = name.addresses();
for(int i=1;i<addr.count();i++)
{
//将获取到的ip地址添加到QCombox组件中
ui->comboBox->addItem(addr.at(i).toString());
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::TabWidget()
{
//设置列数,与属性个数相关
ui->tableWidget->setColumnCount(2);
//设置表头
ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"名称"<<"数据");
list<<"lon(经度)"<<"lat(纬度))"<<"AOA(攻角)"<<"FSideSlip(侧滑角)"<<"Temp(静温)";
int row = ui->tableWidget->rowCount();
//设置总行数
ui->tableWidget->setRowCount(row+list.count());
for(int i=0;i<list.count();i++){
ui->tableWidget->setItem(i,0, new QTableWidgetItem(list.at(i)));
}
//x先自适应宽度
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
//然后设置要根据内容使用宽度的列
ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
}
void MainWindow::on_open_pushButton_clicked()
{
tc = QTextCodec::codecForName("gbk");
//创建监听套接字
server = new QTcpServer(this);
connect(server,&QTcpServer::newConnection,this,&MainWindow::myslot,Qt::UniqueConnection);
quint16 port =ui->lineEdit ->text().toShort();
server->listen(QHostAddress::Any,port);
if(ui->open_pushButton->text().compare("打开") == 0) {
ui->open_pushButton->setText("关闭");
ui->statusbar->showMessage("服务器已打开",5000);
}
else
ui->open_pushButton->setText("打开");
}
void MainWindow::myslot()
{
//返回一个挂起的通信套接字,没有则为0
socket= server->nextPendingConnection();
ui->statusbar->showMessage("已连接",5000);
TabWidget();
connect(socket,&QTcpSocket::readyRead,[=](){
QStringList list1= tc->toUnicode(socket->readAll()).split("\n");
for(int i=0;i<list1.count();i++){
ui->tableWidget->setItem(i,1,new QTableWidgetItem(list1.at(i)));
}
});
connect(socket,&QTcpSocket::disconnected,[=](){
socket->close(); //关闭连接
socket->deleteLater(); //释放资源
});
}
void MainWindow::on_HEX_RadioButton_clicked()
{
/* 将Ascii字符串转换为Hex字符串 */
char byteBuff;
QString temp;
QByteArray data = ui->textEdit ->toPlainText().toLocal8Bit();
foreach(char byte, data)
{
// 高字节
byteBuff = (byte & 0xF0)>>4;
temp += (byteBuff < 10) ? byteBuff+'0' : byteBuff+'A'-10;
// 低字节
byteBuff = byte&0x0F;
temp += (byteBuff < 10) ? byteBuff+'0' : byteBuff+'A'-10;
temp += ' ';
}
temp += "0A";
ui->textEdit->setText(temp);
}
void MainWindow::on_Send_PushButtn_clicked()
{
QString sendMsg = ui->textEdit->toPlainText();
sockfd1->write(sendMsg.toUtf8());
ui->textBrowser->setText(sendMsg.toUtf8());
}
void MainWindow::on_ASCII_radioButton_clicked()
{
/* 将Hex字符串转换为Ascii字符串 */
QTextCodec *tc = QTextCodec::codecForName("System");
QString temp;
QByteArray ascii_data;
QString text = ui->textEdit->toPlainText().toUtf8(); //获取文本
QStringList temp_list = text.split(' ', QString::SkipEmptyParts);
foreach(QString str, temp_list)
{
ascii_data.append(str);
}
// 将ascii_data中的16进制数据转化为对应的字符串,比如\x31转换为"1"
temp = tc->toUnicode(QByteArray::fromHex(ascii_data));
ui->textEdit->setText(temp); //写入文本框
}
void MainWindow::on_clear_pushButton_clicked()
{
ui->textBrowser->clear();
}
void MainWindow::on_client_pushButton_clicked()
{
sockfd1 = new QTcpSocket(this);
connect(sockfd1,&QTcpSocket::connected,[=](){
QMessageBox::information(this,"info","客户端连接成功",QMessageBox::Ok);
connect(sockfd1,&QTcpSocket::readyRead,[=](){
QByteArray str = sockfd1->readAll();
ui->textBrowser->append(QString("sever:"+str));
});
});
QString ip =ui->ip_lineEdit->text();
QString port =ui->C_port_lineEdit ->text();
sockfd1->connectToHost(ip,port.toShort());
}
下面的图片为接收到的数据并加以显示(能力有限,界面比较low)
此处源码下载:「FG_server」等文件 https://www.aliyundrive.com/s/WkNbkrd5VRt 点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。