免费不良正能量网站链接,管理系统项目,自定义wordpress页面模板,营销策划公司简介需求#xff1a;
使用Qt/C 调用 lua 脚本 扩展原有功能。
步骤#xff1a;
1#xff0c;工程中引入 头文件#xff0c;库文件。lua二进制下载地址#xff08;Lua Binaries#xff09; 2#xff0c; 调用脚本内函数。
这里调用lua 脚本中的process函数#xff0c;并…需求
使用Qt/C 调用 lua 脚本 扩展原有功能。
步骤
1工程中引入 头文件库文件。lua二进制下载地址Lua Binaries 2 调用脚本内函数。
这里调用lua 脚本中的process函数并传入16进制假设为 温度 湿度数据。并打印函数返回的字符串。
#include QCoreApplication
#include lua.hpp
#include ostream
#include iostreamint main(int argc, char *argv[])
{QCoreApplication a(argc, argv);//初始化lua_State *L luaL_newstate();luaL_openlibs(L);//lua脚本const char* luaFileName my.lua;if(luaL_dofile(L,luaFileName)0){const char* fun process;// 获取全局函数lua_getglobal(L, fun);if(lua_isfunction(L,-1)){//调用函数const char* data 1245;lua_pushstring(L,data);//传入1个参数期望一个返回值lua_pcall(L,1,1,0);//获取返回值const char* result lua_tostring(L,-1);std::coutResult from lua:resultstd::endl;}else{std::cerrError: funnot foundstd::endl;}}else{std::cerrError loading lua script: lua_tostring(L,-1)std::endl;}lua_close(L);return a.exec();
}3编写my.lua脚本。
解析成两个uint8整型假设为 温度 和湿度。
function process(data)local temByte string.sub(data, 1, 2)local temValue tonumber(temByte, 16)local humByte string.sub(data, 2, 3)local humValue tonumber(humByte, 16)return temperature: .. tostring(temValue) .. hum: .. tostring(humValue)
end4效果