#include "sharedata.h" #include #include #include #include #include #include QmlShareData::QmlShareData(QObject *parent) : QObject(parent), m_qml_view_(nullptr), m_luavm_status_(false), m_network_(nullptr) { m_serial_controller_ = new SerialController(nullptr); QFile file("Test.lua"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; QTextStream in(&file); m_lua_string = in.readAll(); int ret = m_lua_.DoString(m_lua_string); if(ret < 0){ qDebug()<<"默认lua脚本加载错误"; }else{ m_luavm_status_ = true; } } int QmlShareData::SetQuickView(QQuickWidget *view) { QStringList comList; const auto infos = QSerialPortInfo::availablePorts(); for (const QSerialPortInfo &info : infos) { comList<m_qml_view_ = view; QObject *qmlObject = view->rootObject()->findChild("SerialSelect", Qt::FindChildOption::FindChildrenRecursively); if(nullptr != qmlObject) qmlObject->setProperty("comlist",comList); m_qml_protodebug_ = view->rootObject()->findChild("ProtoDebug", Qt::FindChildOption::FindChildrenRecursively); if(nullptr != m_qml_protodebug_) m_qml_protodebug_->setProperty("lua_script_text",m_lua_string); } int QmlShareData::OnDataRecv(QByteArray arr) { if(m_luavm_status_) m_lua_.OnSerialData(QString(arr)); } int QmlShareData::ShowDataInQML(QString x) { if(nullptr != m_qml_protodebug_){ QMetaObject::invokeMethod(m_qml_protodebug_, "addString",Q_ARG(QVariant, x)); } } int QmlShareData::SendUartData(const char *data) { if(nullptr != data){ this->m_serial_controller_->SendData((char *)data,strlen(data)); return 0; } return -1; } int QmlShareData::openUart(QString port, QString baudRate, QString dataBits, QString stopBits, QString flow) { if(m_serial_controller_->OpenSerial(port,baudRate,dataBits,stopBits,flow) == 0){ m_serial_controller_->SetListener(this); return 0; } qDebug()<<"openserial failed"; return -1; } int QmlShareData::openNetwork(QString ip, unsigned int port, bool is_ws,int type) { if(type == NetworkController::TYPE_TCP_CLIENT){ qDebug()<<"connect to server"; if(m_network_ == nullptr) m_network_ = new NetworkController(NetworkController::TYPE_TCP_CLIENT,ip,port); else{ delete m_network_; m_network_ = nullptr; m_network_ = new NetworkController(NetworkController::TYPE_TCP_CLIENT,ip,port); } connect(m_network_,SIGNAL(on_connected()),this,SLOT(on_network_conected())); connect(m_network_,SIGNAL(on_disconnectd()),this,SLOT(on_network_disconected())); connect(m_network_,SIGNAL(on_data_recv()),this,SLOT(on_network_data_recv())); return 0; } if((type == NetworkController::TYPE_UDP_CLIENT) || (type == NetworkController::TYPE_UDP_SERVER)){ m_network_ = new NetworkController((NetworkController::NetworkType)type,ip,port); connect(m_network_,SIGNAL(on_data_recv()),this,SLOT(on_network_data_recv())); return 0; } if(type == NetworkController::TYPE_TCP_SERVER){ m_network_ = new NetworkController(NetworkController::TYPE_TCP_SERVER,ip,port); connect(m_network_,SIGNAL(on_data_recv()),this,SLOT(on_network_data_recv())); return 0; } return -1; } int QmlShareData::closeNetwork() { m_network_->Close(); } int QmlShareData::closeSerial() { if(m_serial_controller_->CloseSerial() == 0){ qDebug()<<"close serial ok"; return 0; } qDebug()<<"cloase serial failed"; return -1; } int QmlShareData::TestLua() { m_lua_.OnSerialData("ceshi"); return 0; } int QmlShareData::TestShowData() { if(nullptr != m_qml_protodebug_){ QMetaObject::invokeMethod(m_qml_protodebug_, "addString",Q_ARG(QVariant, QString("test\r\n"))); } return 0; } int QmlShareData::updateLuaScript(QString str) { qDebug()<m_network_->ReadData((int8_t * )dat); qDebug()< 0){ m_lua_.OnNetworkData((char*)ret.addr.toStdString().data(),dat,ret.port.toUInt()); } } void QmlShareData::on_network_conected() { emit(sendToQml(11)); } void QmlShareData::on_network_disconected() { emit(disConnected()); }