#include "sharedata.h" #include #include #include #include #include #include ShareData::ShareData(QObject *parent) : QObject(parent), m_qml_view_(nullptr), m_luavm_status_(false) { 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 ShareData::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 ShareData::OnDataRecv(QByteArray arr) { if(m_luavm_status_) m_lua_.OnDataRecv(QString(arr)); } int ShareData::ShowDataInQML(QString 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->m_serial_controller_->SendData((uint8_t *)data,strlen(data)); return 0; } return -1; } int ShareData::openUart(QString port, QString baudRate, QString dataBits, QString stopBits, QString flow) { qDebug()<OpenSerial(port,baudRate,dataBits,stopBits,flow) == 0){ m_serial_controller_->SetListener(this); return 0; } qDebug()<<"openserial failed"; return -1; } int ShareData::openNetwork(QString ip, unsigned int port, bool is_ws,int type) { if(type == NetworkController::TYPE_TCP_CLIENT){ m_network_ = new NetworkController(NetworkController::TYPE_TCP_CLIENT,ip,port); return 0; } if(type == NetworkController::TYPE_UDP_CLIENT){ } if(type == NetworkController::TYPE_TCP_SERVER){ m_network_ = new NetworkController(NetworkController::TYPE_TCP_SERVER,ip,port); return 0; } return -1; } int ShareData::closeSerial() { if(m_serial_controller_->CloseSerial() == 0){ qDebug()<<"close serial ok"; return 0; } qDebug()<<"cloase serial failed"; return -1; } int ShareData::TestLua() { m_lua_.OnDataRecv("ss"); } int ShareData::TestShowData() { if(nullptr != m_qml_protodebug_){ QMetaObject::invokeMethod(m_qml_protodebug_, "addString",Q_ARG(QVariant, QString("test\r\n"))); } return 0; } int ShareData::updateLuaScript(QString str) { qDebug()<