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