Skip to content

Commit

Permalink
优化本机IP的获取,优先获取无线网络IP,次优先获取以太网IP
Browse files Browse the repository at this point in the history
  • Loading branch information
changfeng1050 committed May 3, 2021
1 parent da81a79 commit 9641b23
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 68 deletions.
50 changes: 46 additions & 4 deletions global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@
#include <QtWidgets/QWidget>
#include <QtWidgets/QMessageBox>
#include <QtNetwork/QHostInfo>
#include <QtNetwork/QNetworkInterface>
#include "global.h"

QTextCodec *gbk = QTextCodec::codecForName("GB18030");
QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");

QString utf82Gbk(const QString &inStr) {
// QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
// QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");

// gbk->fromUnicode(utf8->toUnicode(inStr.toLatin1()));
// gbk->fromUnicode(utf8->toUnicode(inStr.toLatin1()));

return QString(gbk->fromUnicode(inStr));

// QString utf2gbk = gbk->toUnicode(inStr.toLocal8Bit());
// return utf2gbk;
// QString utf2gbk = gbk->toUnicode(inStr.toLocal8Bit());
// return utf2gbk;
}
//
//QString gbk2Utf8(const QString &inStr) {
Expand Down Expand Up @@ -89,9 +90,23 @@ QString getFileDir(const QString &filePath) {
}

QString getIp() {
auto interfaces = QNetworkInterface::allInterfaces();

for (auto interface:interfaces){
qDebug() << "interface name:" << interface.name();
qDebug() << "interface address:" << interface.hardwareAddress();
qDebug() << "interface type:" << interface.type();
auto entries = interface.addressEntries();

for (auto entry:entries){
qDebug() << "entry ip address:" << entry.ip().toString();
}
}

auto localHostName = QHostInfo::localHostName();
qDebug() << "local host name:" << localHostName;
auto ipAddress = QHostInfo::fromName(localHostName).addresses();
qDebug() << "ip address count:" << ipAddress.length();
qDebug() << "ip address:" << ipAddress;

for (auto address:ipAddress) {
Expand Down Expand Up @@ -127,3 +142,30 @@ QByteArray dataFromHex(const QString &hex) {
auto result = QByteArray::fromHex(line);
return result;
}

QString getIpAddress(const QNetworkInterface &interface){
for (auto address:interface.addressEntries()) {
if (address.ip().protocol() == QAbstractSocket::IPv4Protocol && !address.ip().toString().startsWith("169.")){
return address.ip().toString();
}
}
return "";
}

QList<QNetworkInterface> getNetworkInterfaces() {
QList<QNetworkInterface> list;
auto interfaces = QNetworkInterface::allInterfaces();
for (auto interface:interfaces){
if (interface.isValid() && (interface.type() == QNetworkInterface::Wifi|| interface.type() == QNetworkInterface::Ethernet)) {
qDebug() << "interface " <<interface.type() << interface.name() << interface.hardwareAddress();
auto entries = interface.addressEntries();
for (auto entry:entries){
if (entry.ip().protocol()== QAbstractSocket::IPv4Protocol) {
qDebug() << "interface ip:" << entry.ip().toString();
}
}
list.append(interface);
}
}
return list;
}
5 changes: 5 additions & 0 deletions global.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <QString>
#include <QWidget>
#include <QtNetwork/QNetworkInterface>

extern QString utf82Gbk(const QString &inStr);

Expand Down Expand Up @@ -37,6 +38,10 @@ extern QString getFileDir(const QString &filePath);

extern QString getIp();

extern QString getIpAddress(const QNetworkInterface &interface);

extern QList<QNetworkInterface> getNetworkInterfaces();

extern QByteArray dataToHex(const QByteArray &data);

extern QByteArray dataFromHex(const QString &data);
Expand Down
165 changes: 101 additions & 64 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,17 @@ void MainWindow::initUi() {
auto *serialPortBaudRateLabel = new QLabel(tr("波特率"), this);
serialPortBaudRateComboBox = new QComboBox(this);
serialPortBaudRateComboBox->addItems(QStringList()
<< "1200"
<< "2400"
<< "4800"
<< "9600"
<< "19200"
<< "38400"
<< "25600"
<< "57600"
<< "115200"
<< "256000"
);
<< "1200"
<< "2400"
<< "4800"
<< "9600"
<< "19200"
<< "38400"
<< "25600"
<< "57600"
<< "115200"
<< "256000"
);
serialPortBaudRateLabel->setBuddy(serialPortBaudRateComboBox);

auto serialPortDataBitsLabel = new QLabel(tr("数据位"), this);
Expand Down Expand Up @@ -159,17 +159,17 @@ void MainWindow::initUi() {
auto *secondSerialPortBaudRateLabel = new QLabel(tr("波特率"), this);
secondSerialPortBaudRateComboBox = new QComboBox(this);
secondSerialPortBaudRateComboBox->addItems(QStringList()
<< "1200"
<< "2400"
<< "4800"
<< "9600"
<< "19200"
<< "38400"
<< "25600"
<< "57600"
<< "115200"
<< "256000"
);
<< "1200"
<< "2400"
<< "4800"
<< "9600"
<< "19200"
<< "38400"
<< "25600"
<< "57600"
<< "115200"
<< "256000"
);
secondSerialPortBaudRateLabel->setBuddy(secondSerialPortBaudRateComboBox);

auto secondSerialPortDataBitsLabel = new QLabel(tr("数据位"), this);
Expand Down Expand Up @@ -627,25 +627,25 @@ void MainWindow::createConnect() {

connect(readWriterButtonGroup, QOverload<QAbstractButton *, bool>::of(&QButtonGroup::buttonToggled),
[=](QAbstractButton *button, bool checked) {
if (checked && isReadWriterOpen()) {
SerialType serialType;
if (button == tcpServerRadioButton) {
serialType = SerialType::TcpServer;
} else if (button == tcpClientRadioButton) {
serialType = SerialType::TcpClient;
} else if (button == bridgeRadioButton) {
serialType = SerialType::Bridge;
} else {
serialType = SerialType::Normal;
}
if (checked && isReadWriterOpen()) {
SerialType serialType;
if (button == tcpServerRadioButton) {
serialType = SerialType::TcpServer;
} else if (button == tcpClientRadioButton) {
serialType = SerialType::TcpClient;
} else if (button == bridgeRadioButton) {
serialType = SerialType::Bridge;
} else {
serialType = SerialType::Normal;
}

if (serialType != _serialType) {
if (showWarning("", tr("串口配置已经改变,是否重新打开串口?"))) {
openReadWriter();
}
}
if (serialType != _serialType) {
if (showWarning("", tr("串口配置已经改变,是否重新打开串口?"))) {
openReadWriter();
}
});
}
}
});

connect(this, &MainWindow::serialStateChanged, [this](bool isOpen) {
setOpenButtonText(isOpen);
Expand Down Expand Up @@ -734,21 +734,21 @@ void MainWindow::createConnect() {

connect(lineReturnButtonGroup, QOverload<QAbstractButton *, bool>::of(&QButtonGroup::buttonToggled),
[=](QAbstractButton *button, bool checked) {
if (checked) {
if (button == sendRReturnLineButton) {
lineReturn = QByteArray("\r");
} else if (button == sendNReturnLineButton) {
lineReturn = QByteArray("\n");
} else {
lineReturn = QByteArray("\r\n");
}
}
});
if (checked) {
if (button == sendRReturnLineButton) {
lineReturn = QByteArray("\r");
} else if (button == sendNReturnLineButton) {
lineReturn = QByteArray("\n");
} else {
lineReturn = QByteArray("\r\n");
}
}
});

connect(autoSendTimer, &QTimer::timeout,
[this] {
sendNextData();
});
sendNextData();
});
connect(hexCheckBox, &QCheckBox::stateChanged, [this] {
this->_dirty = true;
});
Expand Down Expand Up @@ -844,10 +844,10 @@ void MainWindow::displayReceiveData(const QByteArray &data) {
s.append("[").append(getTimestamp()).append("] ");
}

if (!s.isEmpty()) {
s.append(" ");
}
if (displayReceiveDataAsHexCheckBox->isChecked()) {
if (!s.isEmpty()) {
s.append(" ");
}
s.append(dataToHex(data));
} else {
s.append(QString::fromLocal8Bit(data));
Expand Down Expand Up @@ -1031,7 +1031,7 @@ void MainWindow::readSettings() {
sendLineReturnCheckBox->setChecked(sendLineReturn);

auto sendLineReturnType = LineReturn(
settings.value("send_line_return_type", static_cast<int >(LineReturn::RN)).toInt());
settings.value("send_line_return_type", static_cast<int >(LineReturn::RN)).toInt());
if (sendLineReturnType == LineReturn::R) {
sendRReturnLineButton->setChecked(true);
} else if (sendLineReturnType == LineReturn::N) {
Expand All @@ -1041,13 +1041,56 @@ void MainWindow::readSettings() {
}

settings.beginGroup("TcpSettings");
auto ipList = getNetworkInterfaces();
auto ipAddress = settings.value("tcp_address","").toString();
QString selectAddress ="";
if (!ipAddress.isEmpty() && !ipList.isEmpty()){
auto found = false;
for (auto ip:ipList) {
if (getIpAddress(ip) == ipAddress) {
selectAddress = ipAddress;
found=true;
break;
}
}
if (!found) {
selectAddress = getIpAddress(ipList.first());
}
}
if (selectAddress.isEmpty()) {
if (!ipList.isEmpty()) {
do {
for (auto ip:ipList){
if (ip.type() == QNetworkInterface::Wifi && !getIpAddress(ip).isEmpty()){
selectAddress = getIpAddress(ip);
break;
}
}
if (!selectAddress.isEmpty()) {
break;
}
for (auto ip:ipList) {
if (ip.type() == QNetworkInterface::Ethernet && !getIpAddress(ip).isEmpty()){
selectAddress = getIpAddress(ip);
}
}
if (!selectAddress.isEmpty()){
break;
}

selectAddress = getIpAddress(ipList.first());

}while(false);
}
}

tcpAddressLineEdit->setText(selectAddress);

auto tcpPort = settings.value("tcp_port").toInt();
tcpPortLineEdit->setText(QString::number(tcpPort));

sendTextEdit->setText(sendText);

tcpAddressLineEdit->setText(getIp());

settings.beginGroup("RunConfig");
auto lastDir = settings.value("last_dir", "").toString();
auto lastFilePath = settings.value("last_file_path", "").toString();
Expand Down Expand Up @@ -1130,6 +1173,7 @@ void MainWindow::writeSettings() {
settings.setValue("send_line_return_type", static_cast<int >(sendLineReturn));

settings.beginGroup("TcpSettings");
settings.setValue("tcp_address", tcpAddressLineEdit->text());
settings.setValue("tcp_port", tcpPortLineEdit->text().toInt());

settings.beginGroup("RunConfig");
Expand Down Expand Up @@ -1346,10 +1390,3 @@ void MainWindow::updateSendType() {
}
serialController = newController;
}







0 comments on commit 9641b23

Please sign in to comment.