proto-debuger/protoDebuger/sharedata.cpp

143 lines
3.3 KiB
C++

#include "sharedata.h"
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QFile>
#include <QThread>
ShareData::ShareData(QObject *parent) : QObject(parent),
mView(nullptr),
mLuaStatus(false)
{
mSerialController = 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);
if(ret < 0){
qDebug()<<"默认lua脚本加载错误";
}else{
mLuaStatus = true;
}
}
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)
{
if(mLuaStatus)
mLua.OnDataRecv(QString(arr));
}
int ShareData::ShowDataInQML(QString x)
{
if(nullptr != mProtoDebug){
QMetaObject::invokeMethod(mProtoDebug, "addString",Q_ARG(QVariant, x));
}
}
int ShareData::SendUartData(const char *data)
{
if(nullptr != data){
this->mSerialController->SendData((uint8_t *)data,strlen(data));
return 0;
}
return -1;
}
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);
return 0;
}
qDebug()<<"openserial failed";
return -1;
}
int ShareData::closeSerial()
{
if(mSerialController->CloseSerial() == 0){
qDebug()<<"close serial ok";
return 0;
}
qDebug()<<"cloase serial failed";
return -1;
}
int ShareData::TestLua()
{
mLua.OnDataRecv("ss");
}
int ShareData::TestShowData()
{
if(nullptr != mProtoDebug){
QMetaObject::invokeMethod(mProtoDebug, "addString",Q_ARG(QVariant, QString("test\r\n")));
}
return 0;
}
int ShareData::updateLuaScript(QString str)
{
qDebug()<<QThread::currentThreadId();
mLuaScript = str;
qDebug()<<str;
int ret = mLua.DoString(mLuaScript);
if(ret < 0){
qDebug()<<"更新lua脚本失败";
mLuaStatus = false;
return -1;
}
mLuaStatus = true;
return 0;
}
int ShareData::saveLuaScript(QString s)
{
QFile file("Test.lua"); //---打开文件
if (file.open(QIODevice::WriteOnly) )
{
//-----将数据写入文件
qint64 LineLen = file.write(s.toStdString().c_str(), s.size());
if (LineLen <= 0)
{
return -1;
}
}
file.close();
return 0;
}
bool ShareData::luaStatus()
{
return mLuaStatus;
}