proto-debuger/protoDebuger/sharedata.cpp

166 lines
4.2 KiB
C++

#include "sharedata.h"
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QFile>
#include <QThread>
#include <QQuickItem>
#include <QQmlContext>
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<<info.portName();
}
// 设置qml基本属性
this->m_qml_view_ = view;
QObject *qmlObject = view->rootObject()->findChild<QObject*>("SerialSelect",
Qt::FindChildOption::FindChildrenRecursively);
if(nullptr != qmlObject)
qmlObject->setProperty("comlist",comList);
m_qml_protodebug_ = view->rootObject()->findChild<QObject*>("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()<<port<<baudRate<<dataBits<<stopBits<<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 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) ||
(type == NetworkController::TYPE_UDP_SERVER)){
m_network_ = new NetworkController((NetworkController::NetWorkType)type,ip,port);
return 0;
}
if(type == NetworkController::TYPE_TCP_SERVER){
m_network_ = new NetworkController(NetworkController::TYPE_TCP_SERVER,ip,port);
return 0;
}
return -1;
}
int ShareData::closeNetwork()
{
m_network_->Close();
}
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()<<QThread::currentThreadId();
m_lua_string = str;
qDebug()<<str;
int ret = m_lua_.DoString(m_lua_string);
if(ret < 0){
qDebug()<<"更新lua脚本失败";
m_luavm_status_ = false;
return -1;
}
m_luavm_status_ = 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 m_luavm_status_;
}