网络部分功能实现

master
zcy 2021-04-29 22:26:45 +08:00
parent 236a1e17c0
commit 6f2e4cec39
5 changed files with 93 additions and 55 deletions

View File

@ -49,7 +49,7 @@ Item {
id: textEdit id: textEdit
Layout.leftMargin: 20 Layout.leftMargin: 20
Layout.topMargin: 20 Layout.topMargin: 20
width: 233 width: 80
height: 40 height: 40
text: qsTr("9001") text: qsTr("9001")
font.italic: false font.italic: false
@ -61,6 +61,26 @@ Item {
cursorVisible: true cursorVisible: true
font.pixelSize: 20 font.pixelSize: 20
} }
RadioButton {
id: radioButton
Layout.leftMargin: 20
Layout.topMargin: 20
width: 138
height: 41
text: qsTr("服务端")
checkable: true
checked: true
onClicked:{
if(radioButton.checked == true){
radioButton.checked = false
}else{
radioButton.checked = true
}
}
}
} }
RowLayout{ RowLayout{
@ -91,7 +111,7 @@ Item {
] ]
} }
RadioButton { RadioButton {
id: radioButton id: radioSelectWebsocket
Layout.leftMargin: 20 Layout.leftMargin: 20
Layout.topMargin: 20 Layout.topMargin: 20
width: 138 width: 138
@ -100,16 +120,11 @@ Item {
checkable: true checkable: true
checked: true checked: true
onCheckableChanged: { onCheckableChanged: {
console.log(radioButton.checkable) checked = ~checked
} }
onClicked:{ onClicked:{
if(radioButton.checked == true){ radioSelectWebsocket.checked = ~radioSelectWebsocket.checked
radioButton.checked = false
}else{
radioButton.checked = true
}
} }
} }
} }
@ -121,7 +136,7 @@ Item {
id: label8 id: label8
Layout.leftMargin: 20 Layout.leftMargin: 20
Layout.topMargin: 20 Layout.topMargin: 20
text: qsTr("客户端/服务端地址: ") text: qsTr("地址: ")
Layout.preferredHeight: 40 Layout.preferredHeight: 40
renderType: Text.QtRendering renderType: Text.QtRendering
wrapMode: Text.NoWrap wrapMode: Text.NoWrap
@ -155,7 +170,12 @@ Item {
height: 52 height: 52
text: qsTr("打开网口") text: qsTr("打开网口")
onClicked: {
let ret = DataWrap.openNetwork()
}
} }
} }

View File

@ -1,7 +1,7 @@
#include "network_controller.h" #include "network_controller.h"
network_controller::network_controller(network_controller::NetWorkType type, QString ip, uint16_t port) NetworkController::NetworkController(NetworkController::NetWorkType type, QString ip, uint16_t port)
{ {
if(type == NetWorkType::TYPE_TCP_CLIENT){ if(type == NetWorkType::TYPE_TCP_CLIENT){
mTcp = new QTcpSocket(); mTcp = new QTcpSocket();
@ -11,21 +11,25 @@ network_controller::network_controller(network_controller::NetWorkType type, QSt
QObject::connect(mTcp, SIGNAL(disconnected()), this, SLOT(on_disconect())); QObject::connect(mTcp, SIGNAL(disconnected()), this, SLOT(on_disconect()));
mTcp->connectToHost(ip,port,QIODevice::ReadWrite); mTcp->connectToHost(ip,port,QIODevice::ReadWrite);
} }
} }
network_controller::~network_controller() int NetworkController::SendData(int8_t *data, uint32_t len)
{
return mTcp->write((const char *)data,len);
}
NetworkController::~NetworkController()
{ {
} }
void network_controller::on_ready_read() void NetworkController::on_ready_read()
{ {
} }
void network_controller::on_disconect() void NetworkController::on_disconect()
{ {
} }

View File

@ -8,18 +8,20 @@
#include <QThread> #include <QThread>
class network_controller : public QObject class NetworkController : public QObject
{ {
Q_OBJECT Q_OBJECT
public:
typedef enum{ typedef enum{
TYPE_UDP_SERVER, TYPE_UDP_SERVER = 0,
TYPE_TCP_SERVER, TYPE_TCP_SERVER,
TYPE_UDP_CLIENT, TYPE_UDP_CLIENT,
TYPE_TCP_CLIENT, TYPE_TCP_CLIENT,
}NetWorkType; }NetWorkType;
public:
network_controller(NetWorkType type,QString ip,uint16_t port); NetworkController(NetWorkType type,QString ip,uint16_t port);
~network_controller(); int SendData(int8_t *data,uint32_t len);
~NetworkController();
public slots: public slots:
void on_ready_read(); void on_ready_read();

View File

@ -5,22 +5,22 @@
#include <QThread> #include <QThread>
ShareData::ShareData(QObject *parent) : QObject(parent), ShareData::ShareData(QObject *parent) : QObject(parent),
mView(nullptr), m_qml_view_(nullptr),
mLuaStatus(false) m_luavm_status_(false)
{ {
mSerialController = new SerialController(nullptr); m_serial_controller_ = new SerialController(nullptr);
QFile file("Test.lua"); QFile file("Test.lua");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return; return;
QTextStream in(&file); QTextStream in(&file);
mLuaScript = in.readAll(); m_lua_string = in.readAll();
qDebug()<< mLuaScript; qDebug()<< m_lua_string;
int ret = mLua.DoString(mLuaScript); int ret = m_lua_.DoString(m_lua_string);
if(ret < 0){ if(ret < 0){
qDebug()<<"默认lua脚本加载错误"; qDebug()<<"默认lua脚本加载错误";
}else{ }else{
mLuaStatus = true; m_luavm_status_ = true;
} }
} }
@ -33,36 +33,36 @@ int ShareData::SetQuickView(QQuickView *view)
} }
this->mView = view; this->m_qml_view_ = view;
QObject *qmlObject = view->findChild<QObject*>("SerialSelect", QObject *qmlObject = view->findChild<QObject*>("SerialSelect",
Qt::FindChildOption::FindChildrenRecursively); Qt::FindChildOption::FindChildrenRecursively);
if(nullptr != qmlObject) if(nullptr != qmlObject)
qmlObject->setProperty("comlist",comList); qmlObject->setProperty("comlist",comList);
mProtoDebug = view->findChild<QObject*>("ProtoDebug", m_qml_protodebug_ = view->findChild<QObject*>("ProtoDebug",
Qt::FindChildOption::FindChildrenRecursively); Qt::FindChildOption::FindChildrenRecursively);
if(nullptr != mProtoDebug) if(nullptr != m_qml_protodebug_)
mProtoDebug->setProperty("lua_script_text",mLuaScript); m_qml_protodebug_->setProperty("lua_script_text",m_lua_string);
} }
int ShareData::OnDataRecv(QByteArray arr) int ShareData::OnDataRecv(QByteArray arr)
{ {
if(mLuaStatus) if(m_luavm_status_)
mLua.OnDataRecv(QString(arr)); m_lua_.OnDataRecv(QString(arr));
} }
int ShareData::ShowDataInQML(QString x) int ShareData::ShowDataInQML(QString x)
{ {
if(nullptr != mProtoDebug){ if(nullptr != m_qml_protodebug_){
QMetaObject::invokeMethod(mProtoDebug, "addString",Q_ARG(QVariant, x)); QMetaObject::invokeMethod(m_qml_protodebug_, "addString",Q_ARG(QVariant, x));
} }
} }
int ShareData::SendUartData(const char *data) int ShareData::SendUartData(const char *data)
{ {
if(nullptr != data){ if(nullptr != data){
this->mSerialController->SendData((uint8_t *)data,strlen(data)); this->m_serial_controller_->SendData((uint8_t *)data,strlen(data));
return 0; return 0;
} }
return -1; return -1;
@ -71,17 +71,27 @@ int ShareData::SendUartData(const char *data)
int ShareData::openUart(QString port, QString baudRate, QString dataBits, QString stopBits, QString flow) int ShareData::openUart(QString port, QString baudRate, QString dataBits, QString stopBits, QString flow)
{ {
qDebug()<<port<<baudRate<<dataBits<<stopBits<<flow; qDebug()<<port<<baudRate<<dataBits<<stopBits<<flow;
if(mSerialController->OpenSerial(port,baudRate,dataBits,stopBits,flow) == 0){ if(m_serial_controller_->OpenSerial(port,baudRate,dataBits,stopBits,flow) == 0){
mSerialController->SetListener(this); m_serial_controller_->SetListener(this);
return 0; return 0;
} }
qDebug()<<"openserial failed"; qDebug()<<"openserial failed";
return -1; return -1;
} }
int ShareData::openNetwork(QString ip, uint32_t port, bool is_ws,int type)
{
if(type == NetworkController::TYPE_TCP_CLIENT){
}
if(type == NetworkController::TYPE_UDP_CLIENT){
m_network_ = new NetworkController(NetworkController::TYPE_TCP_CLIENT,ip,port);
}
}
int ShareData::closeSerial() int ShareData::closeSerial()
{ {
if(mSerialController->CloseSerial() == 0){ if(m_serial_controller_->CloseSerial() == 0){
qDebug()<<"close serial ok"; qDebug()<<"close serial ok";
return 0; return 0;
} }
@ -91,13 +101,13 @@ int ShareData::closeSerial()
int ShareData::TestLua() int ShareData::TestLua()
{ {
mLua.OnDataRecv("ss"); m_lua_.OnDataRecv("ss");
} }
int ShareData::TestShowData() int ShareData::TestShowData()
{ {
if(nullptr != mProtoDebug){ if(nullptr != m_qml_protodebug_){
QMetaObject::invokeMethod(mProtoDebug, "addString",Q_ARG(QVariant, QString("test\r\n"))); QMetaObject::invokeMethod(m_qml_protodebug_, "addString",Q_ARG(QVariant, QString("test\r\n")));
} }
return 0; return 0;
} }
@ -105,15 +115,15 @@ int ShareData::TestShowData()
int ShareData::updateLuaScript(QString str) int ShareData::updateLuaScript(QString str)
{ {
qDebug()<<QThread::currentThreadId(); qDebug()<<QThread::currentThreadId();
mLuaScript = str; m_lua_string = str;
qDebug()<<str; qDebug()<<str;
int ret = mLua.DoString(mLuaScript); int ret = m_lua_.DoString(m_lua_string);
if(ret < 0){ if(ret < 0){
qDebug()<<"更新lua脚本失败"; qDebug()<<"更新lua脚本失败";
mLuaStatus = false; m_luavm_status_ = false;
return -1; return -1;
} }
mLuaStatus = true; m_luavm_status_ = true;
return 0; return 0;
} }
@ -136,7 +146,7 @@ int ShareData::saveLuaScript(QString s)
bool ShareData::luaStatus() bool ShareData::luaStatus()
{ {
return mLuaStatus; return m_luavm_status_;
} }

View File

@ -3,7 +3,7 @@
#include "serialcontroller.h" #include "serialcontroller.h"
#include "qserialproto.h" #include "qserialproto.h"
#include "network_controller.h"
#include <QObject> #include <QObject>
#include <QDebug> #include <QDebug>
#include "lua_wraper.h" #include "lua_wraper.h"
@ -39,6 +39,7 @@ 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,uint32_t port,bool is_ws,int type);
Q_INVOKABLE int closeSerial(); Q_INVOKABLE int closeSerial();
Q_INVOKABLE int TestLua(); Q_INVOKABLE int TestLua();
Q_INVOKABLE int TestShowData(); Q_INVOKABLE int TestShowData();
@ -53,13 +54,14 @@ signals:
void valueFromCpp(int val); void valueFromCpp(int val);
private: private:
SerialController *mSerialController; NetworkController *m_network_;
LuaDelegate mLua; SerialController *m_serial_controller_;
QObject *mProtoDebug; LuaDelegate m_lua_;
QObject *mRootObj; QObject *m_qml_protodebug_;
bool mLuaStatus; QObject *m_root_obj_;
QString mLuaScript; bool m_luavm_status_;
QQuickView *mView; QString m_lua_string;
QQuickView *m_qml_view_;
}; };