no message
parent
3e514bb131
commit
6d2b0c7309
|
@ -16,7 +16,7 @@ set(CMAKE_AUTORCC FALSE)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 11)
|
set(CMAKE_CXX_STANDARD 11)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -Wall -g ")
|
||||||
find_package(Qt5 COMPONENTS Core Quick Widgets REQUIRED)
|
find_package(Qt5 COMPONENTS Core Quick Widgets REQUIRED)
|
||||||
|
|
||||||
set(HEADERS # 待预编译的cpp头文件
|
set(HEADERS # 待预编译的cpp头文件
|
||||||
|
@ -81,6 +81,7 @@ add_executable(protoDebuger #最后需要参与链接生成的是
|
||||||
${NO_MOC_SOURCES}
|
${NO_MOC_SOURCES}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
target_compile_definitions(protoDebuger
|
target_compile_definitions(protoDebuger
|
||||||
PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
|
PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
|
||||||
|
|
||||||
|
|
|
@ -159,42 +159,47 @@ Item {
|
||||||
RowLayout{
|
RowLayout{
|
||||||
spacing: 5
|
spacing: 5
|
||||||
Button {
|
Button {
|
||||||
id: button
|
id: button_network
|
||||||
Layout.leftMargin: 20
|
Layout.leftMargin: 20
|
||||||
Layout.topMargin: 20
|
Layout.topMargin: 20
|
||||||
width: 128
|
width: 128
|
||||||
height: 52
|
height: 52
|
||||||
text: qsTr("打开网口")
|
text: qsTr("打开网络")
|
||||||
|
|
||||||
onClicked: {
|
onClicked: {
|
||||||
let type_network = -1
|
if(button_network.text == "打开网络"){
|
||||||
if(proto_combox.currentText == "udp" && isServer.checked){
|
let type_network = -1
|
||||||
type_network = 0
|
if(proto_combox.currentText == "udp" && isServer.checked){
|
||||||
|
type_network = 0
|
||||||
|
}
|
||||||
|
if(proto_combox.currentText == "udp" && !isServer.checked){
|
||||||
|
type_network = 2
|
||||||
|
}
|
||||||
|
if(proto_combox.currentText == "tcp" && isServer.checked){
|
||||||
|
type_network = 1
|
||||||
|
}
|
||||||
|
if(proto_combox.currentText == "tcp" && !isServer.checked){
|
||||||
|
type_network = 3
|
||||||
|
}
|
||||||
|
console.log(hostEdit.text,
|
||||||
|
port.text,
|
||||||
|
radioSelectWebsocket.checked,
|
||||||
|
type_network)
|
||||||
|
let ret = DataWrap.openNetwork(hostEdit.text,
|
||||||
|
Number(port.text),
|
||||||
|
radioSelectWebsocket.checked,
|
||||||
|
type_network)
|
||||||
|
if(ret != 0){
|
||||||
|
tip.fail("网络连接失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tip.success("网络连接成功")
|
||||||
|
button_network.text = "关闭网络"
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
DataWrap.closeNetwork();
|
||||||
|
button_network.text = "打开网络"
|
||||||
}
|
}
|
||||||
if(proto_combox.currentText == "udp" && !isServer.checked){
|
|
||||||
type_network = 2
|
|
||||||
}
|
|
||||||
if(proto_combox.currentText == "tcp" && isServer.checked){
|
|
||||||
type_network = 1
|
|
||||||
}
|
|
||||||
if(proto_combox.currentText == "tcp" && !isServer.checked){
|
|
||||||
type_network = 3
|
|
||||||
}
|
|
||||||
console.log(hostEdit.text,
|
|
||||||
port.text,
|
|
||||||
radioSelectWebsocket.checked,
|
|
||||||
type_network)
|
|
||||||
let ret = DataWrap.openNetwork(hostEdit.text,
|
|
||||||
Number(port.text),
|
|
||||||
radioSelectWebsocket.checked,
|
|
||||||
type_network)
|
|
||||||
if(ret != 0){
|
|
||||||
tip.fail("网络连接失败")
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
tip.success("网络连接成功")
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,8 +26,10 @@ Rectangle {
|
||||||
width: parent.width
|
width: parent.width
|
||||||
height: parent.height
|
height: parent.height
|
||||||
Button{
|
Button{
|
||||||
|
id: btn_serial
|
||||||
width: parent.width
|
width: parent.width
|
||||||
height:60
|
height:60
|
||||||
|
anchors.top: parent.top
|
||||||
onClicked: {
|
onClicked: {
|
||||||
uart_choose.visible = true
|
uart_choose.visible = true
|
||||||
net_choose.visible = false
|
net_choose.visible = false
|
||||||
|
@ -37,19 +39,20 @@ Rectangle {
|
||||||
anchors.fill: parent
|
anchors.fill: parent
|
||||||
source: "qrc:///res/serial.svg"
|
source: "qrc:///res/serial.svg"
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Text {
|
Text {
|
||||||
|
id: label_serial
|
||||||
text: qsTr("串口设置")
|
text: qsTr("串口设置")
|
||||||
width: parent.width
|
width: parent.width
|
||||||
anchors.centerIn: parent.Center
|
|
||||||
color: "white"
|
color: "white"
|
||||||
font.pointSize: 10
|
font.pointSize: 10
|
||||||
|
anchors.top: btn_serial.bottom
|
||||||
}
|
}
|
||||||
Button{
|
Button{
|
||||||
|
id: button_net
|
||||||
width: parent.width
|
width: parent.width
|
||||||
height:60
|
height:60
|
||||||
|
anchors.top: label_serial.bottom
|
||||||
onClicked: {
|
onClicked: {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -64,16 +67,19 @@ Rectangle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Text {
|
Text {
|
||||||
|
id: label_net
|
||||||
text: qsTr("网口设置")
|
text: qsTr("网口设置")
|
||||||
width: parent.width
|
width: parent.width
|
||||||
anchors.centerIn: parent.Center
|
|
||||||
color: "white"
|
color: "white"
|
||||||
font.pointSize: 10
|
font.pointSize: 10
|
||||||
|
anchors.top: button_net.bottom
|
||||||
|
|
||||||
}
|
}
|
||||||
Button{
|
Button{
|
||||||
|
id: button_proto
|
||||||
width: parent.width
|
width: parent.width
|
||||||
height:60
|
height:60
|
||||||
|
anchors.top: label_net.bottom
|
||||||
onClicked: {
|
onClicked: {
|
||||||
uart_choose.visible = false
|
uart_choose.visible = false
|
||||||
net_choose.visible = false
|
net_choose.visible = false
|
||||||
|
@ -87,19 +93,22 @@ Rectangle {
|
||||||
|
|
||||||
}
|
}
|
||||||
Text {
|
Text {
|
||||||
|
id: label_proto
|
||||||
text: qsTr("协议调试")
|
text: qsTr("协议调试")
|
||||||
width: parent.width
|
width: parent.width
|
||||||
anchors.centerIn: parent.Center
|
|
||||||
color: "white"
|
color: "white"
|
||||||
font.pointSize: 10
|
font.pointSize: 10
|
||||||
|
anchors.top: button_proto.bottom
|
||||||
}
|
}
|
||||||
|
|
||||||
Text {
|
Text {
|
||||||
text: qsTr("协议调试")
|
text: qsTr("未连接")
|
||||||
width: parent.width
|
width: parent.width
|
||||||
anchors.centerIn: parent.Center
|
|
||||||
color: "white"
|
color: "white"
|
||||||
font.pointSize: 10
|
font.pointSize: 10
|
||||||
|
anchors.bottom: parent.bottom
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SerialSelect{
|
SerialSelect{
|
||||||
|
|
|
@ -1,7 +1,12 @@
|
||||||
#include "network_controller.h"
|
#include "network_controller.h"
|
||||||
|
|
||||||
|
|
||||||
NetworkController::NetworkController(NetworkController::NetWorkType type, QString ip, uint16_t port)
|
NetworkController::NetworkController(NetworkController::NetWorkType type,
|
||||||
|
QString ip,
|
||||||
|
uint16_t port):mTcp(nullptr),
|
||||||
|
mUDP(nullptr),
|
||||||
|
mTcpServer(nullptr),
|
||||||
|
mCnn(nullptr)
|
||||||
{
|
{
|
||||||
mType = type;
|
mType = type;
|
||||||
if(_checkType(type) == TYPE_UNKOWN){
|
if(_checkType(type) == TYPE_UNKOWN){
|
||||||
|
@ -23,13 +28,20 @@ NetworkController::NetworkController(NetworkController::NetWorkType type, QStrin
|
||||||
connect(mTcpServer,SIGNAL(newConnection()),
|
connect(mTcpServer,SIGNAL(newConnection()),
|
||||||
this,SLOT(on_server_accept()));
|
this,SLOT(on_server_accept()));
|
||||||
connect(mTcpServer, SIGNAL(acceptError(QAbstractSocket::SocketError socketError)),
|
connect(mTcpServer, SIGNAL(acceptError(QAbstractSocket::SocketError socketError)),
|
||||||
this, SLOT(displayError()));
|
this, SLOT( on_accept_error(QAbstractSocket::SocketError socketError)));
|
||||||
|
|
||||||
if (!mTcpServer->listen(QHostAddress::Any, port))
|
if (!mTcpServer->listen(QHostAddress::Any, port))
|
||||||
{
|
{
|
||||||
qDebug() << "m_pTcpServer->listen() error";
|
qDebug() << "m_pTcpServer->listen() error";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if((type == NetWorkType::TYPE_UDP_SERVER) || (type == NetWorkType::TYPE_TCP_CLIENT)){
|
||||||
|
mUDP = new QUdpSocket(this);
|
||||||
|
mUDP->bind(QHostAddress::LocalHost, port);
|
||||||
|
mCnn = mUDP;
|
||||||
|
connect(mUDP, SIGNAL(readyRead()),
|
||||||
|
this, SLOT(on_ready_read()));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,15 +60,33 @@ int NetworkController::ReadData(int8_t *data)
|
||||||
memcpy(data,mCnn->readAll().data(),mCnn->size());
|
memcpy(data,mCnn->readAll().data(),mCnn->size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int NetworkController::Close()
|
||||||
|
{
|
||||||
|
if(mType == TYPE_TCP_SERVER){
|
||||||
|
mTcpServer->close();
|
||||||
|
if(nullptr != mCnn)
|
||||||
|
mCnn->close();
|
||||||
|
delete mTcpServer;
|
||||||
|
delete mCnn;
|
||||||
|
}
|
||||||
|
if(mType == TYPE_TCP_CLIENT){
|
||||||
|
mCnn->close();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
NetworkController::~NetworkController()
|
NetworkController::~NetworkController()
|
||||||
{
|
{
|
||||||
delete mTcp;
|
delete mTcp;
|
||||||
|
delete mCnn;
|
||||||
|
delete mTcpServer;
|
||||||
|
delete mUDP;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkController::on_ready_read()
|
void NetworkController::on_ready_read()
|
||||||
{
|
{
|
||||||
qDebug()<<QString::fromStdString(mTcp->readAll().toStdString());
|
qDebug()<<QString::fromStdString(mCnn->readAll().toStdString());
|
||||||
emit(on_data_recv());
|
emit(on_data_recv());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,8 +94,11 @@ void NetworkController::on_disconect()
|
||||||
{
|
{
|
||||||
qDebug()<<"close";
|
qDebug()<<"close";
|
||||||
if(nullptr != mCnn){
|
if(nullptr != mCnn){
|
||||||
emit(this->on_conection_close());
|
|
||||||
mCnn->close();
|
mCnn->close();
|
||||||
|
emit(this->on_conection_close());
|
||||||
|
}
|
||||||
|
if(mType == TYPE_TCP_SERVER){
|
||||||
|
mTcpServer->resumeAccepting();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,9 +112,17 @@ void NetworkController::on_server_accept()
|
||||||
QObject::connect(pClientConnection, SIGNAL(disconnected()), this, SLOT(on_disconect()));
|
QObject::connect(pClientConnection, SIGNAL(disconnected()), this, SLOT(on_disconect()));
|
||||||
}
|
}
|
||||||
mTcp = pClientConnection;
|
mTcp = pClientConnection;
|
||||||
|
mCnn = mTcp;
|
||||||
|
qDebug()<<"on_server_accept";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NetworkController::on_accept_error(QAbstractSocket::SocketError socketError)
|
||||||
|
{
|
||||||
|
qDebug()<<socketError;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
NetworkController::NetWorkType NetworkController::_checkType(NetWorkType type){
|
NetworkController::NetWorkType NetworkController::_checkType(NetWorkType type){
|
||||||
if(type < TYPE_UNKOWN){
|
if(type < TYPE_UNKOWN){
|
||||||
return type;
|
return type;
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
#include <QTcpServer>
|
#include <QTcpServer>
|
||||||
|
|
||||||
|
// this is not a thread-safe class,any interface invoked in multi-thread maybe will cause unkown falut
|
||||||
class NetworkController : public QObject
|
class NetworkController : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
@ -23,13 +24,14 @@ public:
|
||||||
NetworkController(NetWorkType type,QString ip,uint16_t port);
|
NetworkController(NetWorkType type,QString ip,uint16_t port);
|
||||||
int SendData(int8_t *data,uint32_t len);
|
int SendData(int8_t *data,uint32_t len);
|
||||||
int ReadData(int8_t *data);
|
int ReadData(int8_t *data);
|
||||||
|
int Close();
|
||||||
~NetworkController();
|
~NetworkController();
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void on_ready_read();
|
void on_ready_read();
|
||||||
void on_disconect();
|
void on_disconect();
|
||||||
void on_server_accept();
|
void on_server_accept();
|
||||||
|
void on_accept_error(QAbstractSocket::SocketError socketError);
|
||||||
signals:
|
signals:
|
||||||
void on_data_recv();
|
void on_data_recv();
|
||||||
void on_conection_ok();
|
void on_conection_ok();
|
||||||
|
|
|
@ -97,6 +97,11 @@ int ShareData::openNetwork(QString ip, unsigned int port, bool is_ws,int type)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int ShareData::closeNetwork()
|
||||||
|
{
|
||||||
|
m_network_->Close();
|
||||||
|
}
|
||||||
|
|
||||||
int ShareData::closeSerial()
|
int ShareData::closeSerial()
|
||||||
{
|
{
|
||||||
if(m_serial_controller_->CloseSerial() == 0){
|
if(m_serial_controller_->CloseSerial() == 0){
|
||||||
|
|
|
@ -40,6 +40,8 @@ public:
|
||||||
Q_INVOKABLE int openUart(QString port,QString baudRate,QString dataBits,QString stopBits,
|
Q_INVOKABLE int openUart(QString port,QString baudRate,QString dataBits,QString stopBits,
|
||||||
QString flow);
|
QString flow);
|
||||||
Q_INVOKABLE int openNetwork(QString ip,unsigned int port,bool is_ws,int type);
|
Q_INVOKABLE int openNetwork(QString ip,unsigned int port,bool is_ws,int type);
|
||||||
|
Q_INVOKABLE int closeNetwork();
|
||||||
|
|
||||||
Q_INVOKABLE int closeSerial();
|
Q_INVOKABLE int closeSerial();
|
||||||
Q_INVOKABLE int TestLua();
|
Q_INVOKABLE int TestLua();
|
||||||
Q_INVOKABLE int TestShowData();
|
Q_INVOKABLE int TestShowData();
|
||||||
|
|
Loading…
Reference in New Issue