proto-debuger/protoDebuger/sharedata.cpp

134 lines
3.3 KiB
C++
Raw Normal View History

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-04-11 03:28:04 +00:00
2021-04-17 17:57:39 +00:00
ShareData::ShareData(QObject *parent) : QObject(parent),
mView(nullptr)
2021-04-12 15:39:06 +00:00
{
mSerialController = 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);
mLuaScript = in.readAll();
qDebug()<< mLuaScript;
2021-04-18 15:16:03 +00:00
int ret = mLua.DoString(mLuaScript);
if(ret < 0){
qDebug()<<"默认lua脚本加载错误";
}
2021-04-17 17:57:39 +00:00
}
int ShareData::SetQuickView(QQuickView *view)
{
QStringList comList;
const auto infos = QSerialPortInfo::availablePorts();
for (const QSerialPortInfo &info : infos) {
comList<<info.portName();
}
this->mView = view;
QObject *qmlObject = view->findChild<QObject*>("SerialSelect",
Qt::FindChildOption::FindChildrenRecursively);
if(nullptr != qmlObject)
qmlObject->setProperty("comlist",comList);
mProtoDebug = view->findChild<QObject*>("ProtoDebug",
Qt::FindChildOption::FindChildrenRecursively);
if(nullptr != mProtoDebug)
mProtoDebug->setProperty("lua_script_text",mLuaScript);
}
int ShareData::OnDataRecv(QByteArray arr)
{
mLua.OnDataRecv(QString(arr));
}
int ShareData::ShowDataInQML(QString x)
{
if(nullptr != mProtoDebug){
QMetaObject::invokeMethod(mProtoDebug, "addString",Q_ARG(QVariant, x));
}
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){
this->mSerialController->SendData((uint8_t *)data,strlen(data));
return 0;
}
return -1;
}
2021-04-12 15:39:06 +00:00
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){
2021-04-17 17:57:39 +00:00
mSerialController->SetListener(this);
2021-04-12 15:39:06 +00:00
return 0;
}
qDebug()<<"openserial failed";
return -1;
}
int ShareData::CloseSerial()
2021-04-11 03:28:04 +00:00
{
2021-04-13 17:28:19 +00:00
if(mSerialController->CloseSerial() == 0){
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-17 17:57:39 +00:00
mLua.OnDataRecv("ss");
2021-04-14 16:52:05 +00:00
}
2021-04-16 18:27:32 +00:00
int ShareData::TestShowData()
{
if(nullptr != mProtoDebug){
QMetaObject::invokeMethod(mProtoDebug, "addString",Q_ARG(QVariant, QString("test\r\n")));
}
return 0;
}
2021-04-19 16:52:06 +00:00
int ShareData::UpdateLuaScript(QString str)
2021-04-18 15:16:03 +00:00
{
qDebug()<<QThread::currentThreadId();
mLuaScript = str;
qDebug()<<str;
int ret = mLua.DoString(mLuaScript);
if(ret < 0){
qDebug()<<"保存qml失败";
return -1;
}
}
2021-04-19 16:52:06 +00:00
int ShareData::SaveLuaScript(QString s)
{
QFile file("Test.lua"); //---打开文件
if (file.open(QIODevice::WriteOnly) )
{
//---待存入文本文件的字符串。
//-----将数据写入文件
qint64 LineLen = file.write(s.toStdString().c_str(), s.size());
//----同上,写入文件失败,将返回 -1
if (-1 != LineLen)
{
//---输出写入文件的内容
return -1;
}
}
//----关闭文件
file.close();
}
2021-04-17 17:57:39 +00:00