网络部分功能实现

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

View File

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

View File

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

View File

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