完善lua功能实现串口发送

master
zcy 2021-04-18 23:16:03 +08:00
parent dcc5b389dd
commit ffe1fd9bb8
8 changed files with 86 additions and 20 deletions

View File

@ -9,6 +9,14 @@
- udp - udp
#### 软件功能 #### 软件功能
不仅仅能够显示收到的数据,还能基于自己编写脚本实现简单的协议。</br> 功能示意图如下:</br>
提供lua脚本功能钩子</br>
![image.png](https://www.testingcloud.club/sapi/api/image_download/000884f1-a057-11eb-a166-525400dc6cec.png)
</br>
串口配置:</br>
![image.png](https://www.testingcloud.club/sapi/api/image_download/2184a3b1-a057-11eb-a166-525400dc6cec.png)
lua脚本调试</br>
![image.png](https://www.testingcloud.club/sapi/api/image_download/36748542-a057-11eb-a166-525400dc6cec.png)

View File

@ -96,7 +96,8 @@ Item {
Layout.bottomMargin: 10 Layout.bottomMargin: 10
onClicked: { onClicked: {
lua_script_text = lua_script.text
DataWrap.SaveLuaScript(lua_script_text)
} }
} }
} }

View File

@ -1,6 +1,7 @@
#include "lua_bind.h" #include "lua_bind.h"
#include "sharedata.h" #include "sharedata.h"
// 在qml界面中显示lua层处理过的数据
int LuaShowData(lua_State *vm) int LuaShowData(lua_State *vm)
{ {
// 获取函数参数:从栈底取一个参数 // 获取函数参数:从栈底取一个参数
@ -12,3 +13,16 @@ int LuaShowData(lua_State *vm)
return 0; return 0;
} }
//
int LuaWriteUart(lua_State *vm)
{
// 获取函数参数:从栈底取一个参数
const char *k = lua_tostring(vm, 1);
if (nullptr != k){
//todo qml显示添加
}
gGlobal.SendUartData(k);
qDebug()<<"send data "<< QString(k);
return 0;
}

View File

@ -9,5 +9,6 @@ extern "C" {
// 在界面中显示数据 // 在界面中显示数据
int LuaShowData(lua_State *); int LuaShowData(lua_State *);
int LuaWriteUart(lua_State *vm);
#endif // LUA_BIND_H #endif // LUA_BIND_H

View File

@ -24,6 +24,34 @@ void LuaDelegate::OnDataRecv(QString data){
lua_call(mVM,2,0); lua_call(mVM,2,0);
} }
void LuaDelegate::DumpStack()
{
static int count = 0;
printf("begin dump lua stack:%d\n", count);
int top = lua_gettop(mVM);
for (int i = top; i > 0; --i)
{
int t = lua_type(mVM, i);
switch (t)
{
case LUA_TSTRING:
qDebug()<<("%s\n", lua_tostring(mVM, i));
break;
case LUA_TBOOLEAN:
qDebug()<<(lua_toboolean(mVM, i) ? "true\n" : "false\n");
break;
case LUA_TNUMBER:
qDebug()<<("%g\n", lua_tonumber(mVM, i));
break;
default:
qDebug()<<("%s\n", lua_typename(mVM, t));
break;
}
}
++count;
}
LuaDelegate::~LuaDelegate() LuaDelegate::~LuaDelegate()
{ {
if(nullptr != mVM){ if(nullptr != mVM){
@ -40,6 +68,8 @@ LuaDelegate::LuaDelegate():
} }
luaL_openlibs(mVM); //打开标准库 luaL_openlibs(mVM); //打开标准库
lua_register(mVM, "showbuffer", LuaShowData); lua_register(mVM, "showbuffer", LuaShowData);
lua_register(mVM, "resp", LuaWriteUart);
} }
int LuaDelegate::DoFile(QString path) int LuaDelegate::DoFile(QString path)
@ -55,11 +85,3 @@ int LuaDelegate::DoFile(QString path)
return 0; return 0;
} }
void TestLua()
{
LuaDelegate v;
qDebug()<<typeid (int).name();
v.DoFile("Test.lua");
v.CallFuntion<int,void*,bool>("test",13,(void *)"sds",false);
}

View File

@ -23,7 +23,6 @@ public:
int DoFile(QString); int DoFile(QString);
void Stop(); void Stop();
int DoString(QString); int DoString(QString);
void pushstack() { void pushstack() {
// const std::type_info &t1 = std::bad_typeid(arg1); // const std::type_info &t1 = std::bad_typeid(arg1);
// std::cout << t1.name() << std::endl; // std::cout << t1.name() << std::endl;
@ -45,7 +44,7 @@ public:
} }
/* 收到数据发送给lua层进行处理*/ /* 收到数据发送给lua层进行处理*/
void OnDataRecv(QString); void OnDataRecv(QString);
void DumpStack();
~LuaDelegate(); ~LuaDelegate();
private: private:

View File

@ -2,6 +2,7 @@
#include <QtSerialPort/QSerialPort> #include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo> #include <QtSerialPort/QSerialPortInfo>
#include <QFile> #include <QFile>
#include <QThread>
ShareData::ShareData(QObject *parent) : QObject(parent), ShareData::ShareData(QObject *parent) : QObject(parent),
mView(nullptr) mView(nullptr)
@ -14,7 +15,10 @@ ShareData::ShareData(QObject *parent) : QObject(parent),
QTextStream in(&file); QTextStream in(&file);
mLuaScript = in.readAll(); mLuaScript = in.readAll();
qDebug()<< mLuaScript; qDebug()<< mLuaScript;
mLua.DoString(mLuaScript); int ret = mLua.DoString(mLuaScript);
if(ret < 0){
qDebug()<<"默认lua脚本加载错误";
}
} }
int ShareData::SetQuickView(QQuickView *view) int ShareData::SetQuickView(QQuickView *view)
@ -41,10 +45,6 @@ int ShareData::SetQuickView(QQuickView *view)
int ShareData::OnDataRecv(QByteArray arr) int ShareData::OnDataRecv(QByteArray arr)
{ {
// qDebug()<<QString(arr)<<"data";
// if(nullptr != mProtoDebug){
// QMetaObject::invokeMethod(mProtoDebug, "addString",Q_ARG(QVariant, QString(arr)));
// }
mLua.OnDataRecv(QString(arr)); mLua.OnDataRecv(QString(arr));
} }
@ -55,6 +55,15 @@ int ShareData::ShowDataInQML(QString 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) int ShareData::OpenUart(QString port, QString baudRate, QString dataBits, QString stopBits, QString flow)
{ {
qDebug()<<port<<baudRate<<dataBits<<stopBits<<flow; qDebug()<<port<<baudRate<<dataBits<<stopBits<<flow;
@ -90,4 +99,16 @@ int ShareData::TestShowData()
return 0; return 0;
} }
int ShareData::SaveLuaScript(QString str)
{
qDebug()<<QThread::currentThreadId();
mLuaScript = str;
qDebug()<<str;
int ret = mLua.DoString(mLuaScript);
if(ret < 0){
qDebug()<<"保存qml失败";
return -1;
}
}

View File

@ -17,9 +17,7 @@ extern ShareData gGlobal;
class ShareData : public QObject ,SerialController::SerialListener class ShareData : public QObject ,SerialController::SerialListener
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString _txt READ rtxt WRITE settxt NOTIFY txtchanged) Q_PROPERTY(QString _txt READ rtxt WRITE settxt NOTIFY txtchanged)
public: public:
explicit ShareData(QObject *parent = nullptr); explicit ShareData(QObject *parent = nullptr);
int SetQuickView(QQuickView *); int SetQuickView(QQuickView *);
@ -32,6 +30,7 @@ public:
} }
int OnDataRecv(QByteArray); int OnDataRecv(QByteArray);
int ShowDataInQML(QString x); int ShowDataInQML(QString x);
int SendUartData(const char *);
Q_INVOKABLE void getValFromQml(int v) { Q_INVOKABLE void getValFromQml(int v) {
qDebug() << "value from qml is :" << v; qDebug() << "value from qml is :" << v;
@ -43,6 +42,7 @@ public:
Q_INVOKABLE int CloseSerial(); Q_INVOKABLE int CloseSerial();
Q_INVOKABLE int TestLua(); Q_INVOKABLE int TestLua();
Q_INVOKABLE int TestShowData(); Q_INVOKABLE int TestShowData();
Q_INVOKABLE int SaveLuaScript(QString);
QString _txt = "hello world\r\n"; QString _txt = "hello world\r\n";