2021-04-11 03:28:04 +00:00
|
|
|
#include "sharedata.h"
|
2021-04-17 17:57:39 +00:00
|
|
|
#include <QtSerialPort/QSerialPort>
|
|
|
|
#include <QtSerialPort/QSerialPortInfo>
|
|
|
|
#include <QFile>
|
2021-04-18 15:16:03 +00:00
|
|
|
#include <QThread>
|
2021-05-01 06:09:18 +00:00
|
|
|
#include <QQuickItem>
|
|
|
|
#include <QQmlContext>
|
2021-04-11 03:28:04 +00:00
|
|
|
|
2021-04-17 17:57:39 +00:00
|
|
|
ShareData::ShareData(QObject *parent) : QObject(parent),
|
2021-04-29 14:26:45 +00:00
|
|
|
m_qml_view_(nullptr),
|
|
|
|
m_luavm_status_(false)
|
2021-04-12 15:39:06 +00:00
|
|
|
{
|
2021-04-29 14:26:45 +00:00
|
|
|
m_serial_controller_ = new SerialController(nullptr);
|
2021-04-17 17:57:39 +00:00
|
|
|
QFile file("Test.lua");
|
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
|
|
|
|
return;
|
|
|
|
|
|
|
|
QTextStream in(&file);
|
2021-04-29 14:26:45 +00:00
|
|
|
m_lua_string = in.readAll();
|
|
|
|
qDebug()<< m_lua_string;
|
|
|
|
int ret = m_lua_.DoString(m_lua_string);
|
2021-04-18 15:16:03 +00:00
|
|
|
if(ret < 0){
|
|
|
|
qDebug()<<"默认lua脚本加载错误";
|
2021-04-20 15:35:18 +00:00
|
|
|
}else{
|
2021-04-29 14:26:45 +00:00
|
|
|
m_luavm_status_ = true;
|
2021-04-18 15:16:03 +00:00
|
|
|
}
|
2021-04-17 17:57:39 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 06:09:18 +00:00
|
|
|
int ShareData::SetQuickView(QQuickWidget *view)
|
2021-04-17 17:57:39 +00:00
|
|
|
{
|
|
|
|
QStringList comList;
|
|
|
|
const auto infos = QSerialPortInfo::availablePorts();
|
|
|
|
for (const QSerialPortInfo &info : infos) {
|
|
|
|
comList<<info.portName();
|
|
|
|
}
|
2021-05-01 06:09:18 +00:00
|
|
|
// 设置qml基本属性
|
2021-04-29 14:26:45 +00:00
|
|
|
this->m_qml_view_ = view;
|
2021-05-01 06:09:18 +00:00
|
|
|
QObject *qmlObject = view->rootObject()->findChild<QObject*>("SerialSelect",
|
2021-04-17 17:57:39 +00:00
|
|
|
Qt::FindChildOption::FindChildrenRecursively);
|
|
|
|
if(nullptr != qmlObject)
|
|
|
|
qmlObject->setProperty("comlist",comList);
|
2021-05-01 06:09:18 +00:00
|
|
|
m_qml_protodebug_ = view->rootObject()->findChild<QObject*>("ProtoDebug",
|
2021-04-17 17:57:39 +00:00
|
|
|
Qt::FindChildOption::FindChildrenRecursively);
|
2021-05-01 06:09:18 +00:00
|
|
|
|
2021-04-29 14:26:45 +00:00
|
|
|
if(nullptr != m_qml_protodebug_)
|
|
|
|
m_qml_protodebug_->setProperty("lua_script_text",m_lua_string);
|
2021-04-17 17:57:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ShareData::OnDataRecv(QByteArray arr)
|
|
|
|
{
|
2021-04-29 14:26:45 +00:00
|
|
|
if(m_luavm_status_)
|
|
|
|
m_lua_.OnDataRecv(QString(arr));
|
2021-04-17 17:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ShareData::ShowDataInQML(QString x)
|
|
|
|
{
|
2021-04-29 14:26:45 +00:00
|
|
|
if(nullptr != m_qml_protodebug_){
|
|
|
|
QMetaObject::invokeMethod(m_qml_protodebug_, "addString",Q_ARG(QVariant, x));
|
2021-04-17 17:57:39 +00:00
|
|
|
}
|
2021-04-12 15:39:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 15:16:03 +00:00
|
|
|
int ShareData::SendUartData(const char *data)
|
|
|
|
{
|
|
|
|
if(nullptr != data){
|
2021-04-29 14:26:45 +00:00
|
|
|
this->m_serial_controller_->SendData((uint8_t *)data,strlen(data));
|
2021-04-18 15:16:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-04-24 17:21:31 +00:00
|
|
|
int ShareData::openUart(QString port, QString baudRate, QString dataBits, QString stopBits, QString flow)
|
2021-04-12 15:39:06 +00:00
|
|
|
{
|
|
|
|
qDebug()<<port<<baudRate<<dataBits<<stopBits<<flow;
|
2021-04-29 14:26:45 +00:00
|
|
|
if(m_serial_controller_->OpenSerial(port,baudRate,dataBits,stopBits,flow) == 0){
|
|
|
|
m_serial_controller_->SetListener(this);
|
2021-04-12 15:39:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
qDebug()<<"openserial failed";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-04-29 14:26:45 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-24 17:21:31 +00:00
|
|
|
int ShareData::closeSerial()
|
2021-04-11 03:28:04 +00:00
|
|
|
{
|
2021-04-29 14:26:45 +00:00
|
|
|
if(m_serial_controller_->CloseSerial() == 0){
|
2021-04-13 17:28:19 +00:00
|
|
|
qDebug()<<"close serial ok";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
qDebug()<<"cloase serial failed";
|
|
|
|
return -1;
|
2021-04-11 03:28:04 +00:00
|
|
|
}
|
2021-04-14 16:52:05 +00:00
|
|
|
|
|
|
|
int ShareData::TestLua()
|
|
|
|
{
|
2021-04-29 14:26:45 +00:00
|
|
|
m_lua_.OnDataRecv("ss");
|
2021-04-14 16:52:05 +00:00
|
|
|
}
|
2021-04-16 18:27:32 +00:00
|
|
|
|
|
|
|
int ShareData::TestShowData()
|
|
|
|
{
|
2021-04-29 14:26:45 +00:00
|
|
|
if(nullptr != m_qml_protodebug_){
|
|
|
|
QMetaObject::invokeMethod(m_qml_protodebug_, "addString",Q_ARG(QVariant, QString("test\r\n")));
|
2021-04-16 18:27:32 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-24 17:21:31 +00:00
|
|
|
int ShareData::updateLuaScript(QString str)
|
2021-04-18 15:16:03 +00:00
|
|
|
{
|
|
|
|
qDebug()<<QThread::currentThreadId();
|
2021-04-29 14:26:45 +00:00
|
|
|
m_lua_string = str;
|
2021-04-18 15:16:03 +00:00
|
|
|
qDebug()<<str;
|
2021-04-29 14:26:45 +00:00
|
|
|
int ret = m_lua_.DoString(m_lua_string);
|
2021-04-18 15:16:03 +00:00
|
|
|
if(ret < 0){
|
2021-04-20 15:35:18 +00:00
|
|
|
qDebug()<<"更新lua脚本失败";
|
2021-04-29 14:26:45 +00:00
|
|
|
m_luavm_status_ = false;
|
2021-04-18 15:16:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2021-04-29 14:26:45 +00:00
|
|
|
m_luavm_status_ = true;
|
2021-04-27 13:02:32 +00:00
|
|
|
return 0;
|
2021-04-18 15:16:03 +00:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:21:31 +00:00
|
|
|
int ShareData::saveLuaScript(QString s)
|
2021-04-19 16:52:06 +00:00
|
|
|
{
|
|
|
|
QFile file("Test.lua"); //---打开文件
|
|
|
|
if (file.open(QIODevice::WriteOnly) )
|
|
|
|
{
|
|
|
|
//-----将数据写入文件
|
|
|
|
qint64 LineLen = file.write(s.toStdString().c_str(), s.size());
|
2021-04-27 13:02:32 +00:00
|
|
|
if (LineLen <= 0)
|
2021-04-19 16:52:06 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
file.close();
|
2021-04-20 15:35:18 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-24 17:21:31 +00:00
|
|
|
bool ShareData::luaStatus()
|
2021-04-20 15:35:18 +00:00
|
|
|
{
|
2021-04-29 14:26:45 +00:00
|
|
|
return m_luavm_status_;
|
2021-04-19 16:52:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 17:57:39 +00:00
|
|
|
|