串口通信的lua binding 逻辑实现。
This commit is contained in:
parent
ee86da70c6
commit
d2ed2516aa
@ -79,8 +79,6 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
auto key = info->ip + L":" + std::to_wstring(gTcpClientCnt);
|
||||
node->SetText(key);
|
||||
node->SetClass(L"listitem");
|
||||
node->SetFixedHeight(20);
|
||||
node->SetMargin({ 20, 0, 0, 0 });
|
||||
mMonitor->GetRootNode()->GetChildNode(1)->AddChildNode(node);
|
||||
gTcpClientCnt++;
|
||||
if (mTcpClientForm.find(key) == mTcpClientForm.end())
|
||||
@ -101,13 +99,11 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
|
||||
if (p != nullptr) {
|
||||
printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
|
||||
|
||||
p->SetAutoDestroy(true);
|
||||
if (mRightShow != nullptr) {
|
||||
mRightShow->SetVisible(false);
|
||||
p->SetVisible(true);
|
||||
mRightShow = p;
|
||||
wprintf(L"%s",p->GetName());
|
||||
mRightSide->SelectItem(p->GetName());
|
||||
}
|
||||
else {
|
||||
@ -132,8 +128,6 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
auto key = info->ip + L":" + std::to_wstring(info->port);
|
||||
node->SetText(key);
|
||||
node->SetClass(L"listitem");
|
||||
node->SetFixedHeight(20);
|
||||
std::cout << "node heigjt" << node->GetHeight();
|
||||
gTcpServerCnt++;
|
||||
|
||||
printf("WM_ADD_TCPSERVER_MONITOR\r\n");
|
||||
@ -149,7 +143,6 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
printf("error 1");
|
||||
}
|
||||
|
||||
|
||||
node->AttachAllEvents(
|
||||
[this](ui::EventArgs* ev) {
|
||||
if (ui::EventType::kEventSelect == ev->Type) {
|
||||
|
27
examples/proto_debuger/lua_bind.cpp
Normal file
27
examples/proto_debuger/lua_bind.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
#include "lua_bind.h"
|
||||
|
||||
// 在qml界面中显示lua层处理过的数据
|
||||
int LuaShowData(lua_State* vm)
|
||||
{
|
||||
// 获取函数参数:从栈底取一个参数
|
||||
const char* k = lua_tostring(vm, 1);
|
||||
if (nullptr != k) {
|
||||
//todo qml显示添加
|
||||
}
|
||||
// gGlobal.ShowDataInQML(QString(k));
|
||||
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;
|
||||
}
|
||||
|
14
examples/proto_debuger/lua_bind.h
Normal file
14
examples/proto_debuger/lua_bind.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef LUA_BIND_H
|
||||
#define LUA_BIND_H
|
||||
|
||||
extern "C" {
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
}
|
||||
|
||||
// 在界面中显示数据
|
||||
int LuaShowData(lua_State*);
|
||||
int LuaWriteUart(lua_State* vm);
|
||||
|
||||
#endif // LUA_BIND_H
|
@ -14,6 +14,7 @@ int LuaDelegate::DoString(std::string scr) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
this->mScript = scr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -23,12 +24,13 @@ int LuaDelegate::UpdateScript(std::string scr) {
|
||||
}
|
||||
free(mVM);
|
||||
mVM = luaL_newstate(); //打开lua
|
||||
if(nullptr != mVM) {
|
||||
printf("shit is nullptr");
|
||||
if (nullptr != mVM) {
|
||||
return -1;
|
||||
}
|
||||
for (auto x : mFunc) {
|
||||
lua_register(mVM, x.first.c_str(), x.second);
|
||||
}
|
||||
mScript = scr;
|
||||
if (mVM != nullptr) {
|
||||
int ret = luaL_dostring(mVM,scr.c_str());
|
||||
if (ret > 0){
|
||||
@ -44,15 +46,25 @@ void LuaDelegate::PrintError(lua_State *L) {
|
||||
std::cout<<"\nFATAL ERROR:%s\n\n"<< lua_tostring(L, -1);
|
||||
}
|
||||
|
||||
int LuaDelegate::BindFunction(std::string name, lua_CFunction function)
|
||||
{
|
||||
if ((nullptr == function) || (name == ""))
|
||||
return -1;
|
||||
this->mFunc[name] = function;
|
||||
lua_register(mVM, "serial_send", function);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void LuaDelegate::OnSerialData(std::string data) {
|
||||
int i = lua_getglobal(mVM,"OnDataReady");
|
||||
if(i == 0)
|
||||
int i = lua_getglobal(mVM,"OnUartData");
|
||||
if(i == 0){
|
||||
std::cout << "OnSerialData not found\r\n";
|
||||
return;
|
||||
}
|
||||
lua_pushstring(mVM,data.data());
|
||||
lua_call(mVM,1,0);
|
||||
}
|
||||
|
||||
|
||||
void LuaDelegate::DumpStack() {
|
||||
static int count = 0;
|
||||
printf("begin dump lua stack:%d\n", count);
|
||||
@ -79,7 +91,6 @@ void LuaDelegate::DumpStack() {
|
||||
++count;
|
||||
}
|
||||
|
||||
|
||||
LuaDelegate::~LuaDelegate() {
|
||||
if(nullptr != mVM){
|
||||
lua_close(mVM);
|
||||
|
@ -25,8 +25,8 @@ public:
|
||||
void Stop();
|
||||
int DoString(std::string);
|
||||
int UpdateScript(std::string);
|
||||
template <typename T>
|
||||
void pushstack(T arg1);
|
||||
|
||||
|
||||
void pushstack(lua_Number arg) {
|
||||
lua_pushnumber(mVM, arg);
|
||||
}
|
||||
@ -48,8 +48,7 @@ public:
|
||||
void pushstack(lua_CFunction fn, int n) {
|
||||
lua_pushcclosure(mVM, fn, n);
|
||||
}
|
||||
template <typename T, typename ... Types>
|
||||
void pushstack(T arg1, Types... rest);
|
||||
|
||||
template <typename ... Types>
|
||||
void pushstack(lua_Number arg1, Types... rest) {
|
||||
lua_pushnumber(mVM, arg1);
|
||||
@ -95,18 +94,15 @@ public:
|
||||
}
|
||||
|
||||
void PrintError(lua_State *L);
|
||||
|
||||
int BindFunction(std::string name, lua_CFunction);
|
||||
void OnSerialData(std::string);
|
||||
void OnNetworkData(char*,char*,uint32_t port);
|
||||
void OnNewTcpClient(std::string ip,uint32_t port,uint32_t sockptr);
|
||||
void OnClientLeave(std::string ip,uint32_t port,uint32_t sockptr);
|
||||
|
||||
void DumpStack();
|
||||
|
||||
~LuaDelegate();
|
||||
private:
|
||||
lua_State *mVM;
|
||||
std::string mFile;
|
||||
std::string mScript;
|
||||
std::map<std::string, lua_CFunction> mFunc;
|
||||
};
|
||||
|
||||
|
@ -172,11 +172,13 @@
|
||||
<ClCompile Include="tcp_client.cpp" />
|
||||
<ClCompile Include="tcp_client_form.cpp" />
|
||||
<ClCompile Include="tcp_server_libevent.cpp" />
|
||||
<ClCompile Include="uart_process.cpp" />
|
||||
<ClCompile Include="uart_form.cpp" />
|
||||
<ClCompile Include="utils.cpp" />
|
||||
<ClCompile Include="lua_bind.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="base_form.h" />
|
||||
<ClInclude Include="lua_bind.h" />
|
||||
<ClInclude Include="lua_wraper.h" />
|
||||
<ClInclude Include="main.h" />
|
||||
<ClInclude Include="msgdef.h" />
|
||||
@ -194,6 +196,7 @@
|
||||
<Xml Include="..\Debug\resources\themes\default\basic\uart_form.xml" />
|
||||
<Xml Include="..\x64\Debug\resources\themes\default\basic\tcp_form.xml" />
|
||||
<Xml Include="..\x64\Debug\resources\themes\default\basic\tcp_server_form.xml" />
|
||||
<Xml Include="Debug\themes\default\global.xml" />
|
||||
<Xml Include="x64\Debug\themes\default\global.xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
@ -24,7 +24,7 @@
|
||||
<ClCompile Include="new_monitor_form.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="uart_process.cpp">
|
||||
<ClCompile Include="uart_form.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="serial_port.cpp">
|
||||
@ -51,6 +51,9 @@
|
||||
<ClCompile Include="tcp_server_form.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="lua_bind.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="main.h">
|
||||
@ -86,6 +89,9 @@
|
||||
<ClInclude Include="tcp_server_form.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="lua_bind.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Xml Include="..\Debug\resources\themes\default\basic\basic.xml">
|
||||
@ -106,6 +112,9 @@
|
||||
<Xml Include="x64\Debug\themes\default\global.xml">
|
||||
<Filter>资源文件</Filter>
|
||||
</Xml>
|
||||
<Xml Include="Debug\themes\default\global.xml">
|
||||
<Filter>资源文件</Filter>
|
||||
</Xml>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ResourceCompile Include="proto_debuger.rc">
|
||||
|
@ -2,7 +2,8 @@
|
||||
#include "utils.h"
|
||||
|
||||
TcpClientForm::TcpClientForm(ui::Window* hwnd,std::string url, uint32_t port, TcpClientLibevent* p):
|
||||
m_connected(false)
|
||||
m_connected(false),
|
||||
mLua(nullptr)
|
||||
{
|
||||
mClient = p;
|
||||
mClient->SetObserver(this);
|
||||
@ -11,6 +12,7 @@ TcpClientForm::TcpClientForm(ui::Window* hwnd,std::string url, uint32_t port, Tc
|
||||
}
|
||||
m_url = url;
|
||||
m_port = port;
|
||||
mLua = new LuaDelegate();
|
||||
}
|
||||
|
||||
void TcpClientForm::Init()
|
||||
|
@ -14,6 +14,9 @@
|
||||
#include "duilib/UIlib.h"
|
||||
#include "tcp_client.h"
|
||||
|
||||
#include "lua_wraper.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
class TcpClientForm :
|
||||
public ui::ChildBox,
|
||||
@ -48,4 +51,5 @@ private:
|
||||
std::string m_url;
|
||||
int m_port;
|
||||
bool m_connected;
|
||||
LuaDelegate* mLua;
|
||||
};
|
@ -12,7 +12,9 @@ extern "C" {
|
||||
#include "event2/thread.h"
|
||||
};
|
||||
|
||||
TcpServerFrom::TcpServerFrom(ui::Window* hwnd, string url, uint32_t port, TcpServerLibevent* p)
|
||||
TcpServerFrom::TcpServerFrom(ui::Window* hwnd, string url,
|
||||
uint32_t port, TcpServerLibevent* p):
|
||||
mLua(nullptr)
|
||||
{
|
||||
m_server = p;
|
||||
m_url = url;
|
||||
|
@ -7,7 +7,7 @@
|
||||
#include "duilib/UIlib.h"
|
||||
#include "tcp_server_libevent.h"
|
||||
#include <map>
|
||||
|
||||
#include "lua_wraper.h"
|
||||
|
||||
class TcpServerFrom :
|
||||
public ui::ChildBox
|
||||
@ -18,7 +18,6 @@ public:
|
||||
virtual void Init() override;
|
||||
void OnNewConnAccept(ConnectionLibevent*);
|
||||
void OnDisConnected(ConnectionLibevent*);
|
||||
|
||||
protected:
|
||||
|
||||
private:
|
||||
@ -42,6 +41,7 @@ private:
|
||||
ui::Button* m_button_3;
|
||||
std::map<uint32_t, ConnectionLibevent*> mClients;
|
||||
bool mFlagSelectClient;
|
||||
LuaDelegate *mLua;
|
||||
|
||||
};
|
||||
|
||||
|
138
examples/proto_debuger/uart_form.cpp
Normal file
138
examples/proto_debuger/uart_form.cpp
Normal file
@ -0,0 +1,138 @@
|
||||
#include "uart_process.h"
|
||||
#include "utils.h"
|
||||
#include "msgdef.h"
|
||||
#include "utils.h"
|
||||
#include <fstream>
|
||||
|
||||
UartForm::UartForm(ui::Window* hwnd,std::wstring name,
|
||||
uint32_t baurate,
|
||||
uint8_t data_bits, uint8_t stop_bits,
|
||||
uint8_t verify, uint8_t flow_control):
|
||||
m_thread_recv(nullptr),
|
||||
mEditRecv(nullptr),
|
||||
mLua(nullptr)
|
||||
{
|
||||
m_name = name;
|
||||
m_baurate = baurate;
|
||||
m_data_bits = data_bits;
|
||||
m_stop_bits = stop_bits;
|
||||
m_verify = verify;
|
||||
m_flow_contro = flow_control;
|
||||
|
||||
m_runing = true;
|
||||
mLua = new LuaDelegate;
|
||||
|
||||
std::ifstream infile;
|
||||
infile.open("D:\\project\\c++\\nim_duilib\\examples\\x64\\script.lua");
|
||||
std::string lua_script;
|
||||
if (infile.is_open()) {
|
||||
std::string s;
|
||||
while (getline(infile, s)) {
|
||||
lua_script += s;
|
||||
}
|
||||
infile.close();
|
||||
}
|
||||
mLuaScript = lua_script;
|
||||
mLua->DoString(lua_script);
|
||||
std::cout << "lua script is " << lua_script << std::endl;
|
||||
|
||||
m_thread_recv = new std::thread([this]() {
|
||||
UINT PortNum = 0;
|
||||
for (int i = 3; m_name[i] != '\0'; i++)
|
||||
{
|
||||
PortNum = PortNum * 10 + (m_name[i] - '0');
|
||||
}
|
||||
char recv[1024] = {0};
|
||||
while (this->m_runing) {
|
||||
if (0 < SerialPort::ReadPort(PortNum, recv, 1024)) {
|
||||
printf("recv data: %s", recv);
|
||||
this->m_show_recv += string2wstring(recv);
|
||||
this->mLua->OnSerialData(recv);
|
||||
if (this->mEditRecv != nullptr) {
|
||||
this->mEditRecv->AppendText(string2wstring(recv), false);
|
||||
::PostMessage(this->GetWindow()->GetHWND(), WM_ADD_UART_RECVDATA,0, 0);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Sleep(300);
|
||||
}
|
||||
}
|
||||
});
|
||||
if (nullptr != hwnd) {
|
||||
// set window so we get getwindow by GetWindow funtion
|
||||
this->SetWindow(hwnd, nullptr, false);
|
||||
}
|
||||
}
|
||||
|
||||
void UartForm::OnUpdateUart()
|
||||
{
|
||||
//if(mEditRecv != nullptr)
|
||||
// this->mEditRecv->SetText(this->m_show_recv );
|
||||
|
||||
}
|
||||
|
||||
void UartForm::Init()
|
||||
{
|
||||
ui::ChildBox::Init();
|
||||
auto mRightSide = dynamic_cast<ui::Label*> (FindSubControl(L"uart_info_label"));
|
||||
wchar_t p[100] = { 0 };
|
||||
wsprintf(p,L"串口号: %s 波特率%d 数据位: %d 停止位: %d ",
|
||||
m_name.c_str(),m_baurate,
|
||||
m_data_bits,m_stop_bits);
|
||||
mRightSide->SetText(std::wstring(p));
|
||||
|
||||
mEditSend = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_send_edit"));
|
||||
mEditRecv = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_recv_eidt"));
|
||||
mEditLua = dynamic_cast<ui::RichEdit*>(FindSubControl(L"lua_script"));
|
||||
|
||||
mEditRecv->SetReadOnly(true);
|
||||
mEditRecv->SetRich(false);
|
||||
mEditRecv->SetAttribute(L"autovscroll", L"true");
|
||||
mEditRecv->SetAttribute(L"multiline", L"true");
|
||||
mEditRecv->SetReturnMsgWantCtrl(true);
|
||||
mEditRecv->SetNeedReturnMsg(true);
|
||||
mEditRecv->SetWordWrap(true);
|
||||
|
||||
auto mBtnSend = static_cast<ui::Button*>(FindSubControl(L"btn_send_data"));
|
||||
if (mBtnSend != nullptr) {
|
||||
mBtnSend->AttachClick([this](ui::EventArgs*) {
|
||||
UINT PortNum = 0;
|
||||
for (int i = 3; m_name[i] != '\0'; i++) //转换为数字
|
||||
{
|
||||
PortNum = PortNum * 10 + (m_name[i] - '0');
|
||||
}
|
||||
|
||||
auto x = mEditSend->GetText();
|
||||
auto tmp = wstring2string(x);
|
||||
wprintf(L"%s\r\n", x.c_str());
|
||||
SerialPort::WritePort(PortNum, tmp.c_str(), tmp.size());
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
auto mBtnClose = static_cast<ui::Button*>(FindSubControl(L"btn_close_uart"));
|
||||
if (mBtnClose != nullptr) {
|
||||
mBtnClose->AttachClick([this](ui::EventArgs*) {
|
||||
wstring* name = new wstring(this->GetName());
|
||||
::PostMessage(this->GetWindow()->GetHWND(),
|
||||
WM_ADD_UART_CLOSE, (WPARAM)name, 0);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void UartForm::UpdateRecvEdit()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void UartForm::HandleMessage(ui::EventArgs& msg)
|
||||
{
|
||||
if (msg.Type == WM_ADD_UART_RECVDATA) {
|
||||
printf("hello world\r\n");
|
||||
this->mEditRecv->SetText(L"123");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -13,6 +13,7 @@
|
||||
#include "serial_port.h"
|
||||
// duilib
|
||||
#include "duilib/UIlib.h"
|
||||
#include "lua_wraper.h"
|
||||
|
||||
class UartForm : public ui::ChildBox
|
||||
{
|
||||
@ -41,9 +42,13 @@ public:
|
||||
|
||||
ui::RichEdit* mEditSend;
|
||||
ui::RichEdit* mEditRecv;
|
||||
ui::RichEdit* mEditLua;
|
||||
|
||||
|
||||
HWND m_hwnd;
|
||||
LuaDelegate* mLua;
|
||||
std::string mLuaScript;
|
||||
private:
|
||||
virtual void HandleMessage(ui::EventArgs& msg);
|
||||
|
||||
};
|
||||
|
||||
|
@ -135,7 +135,9 @@
|
||||
|
||||
<!--列表通用样式-->
|
||||
<Class name="list" bkcolor="bk_wnd_lightcolor" vscrollbar="true" />
|
||||
<Class name="listitem" hotcolor="bk_listitem_hovered" pushedcolor="bk_listitem_selected" selectednormalcolor="bk_listitem_selected" fadehot="false" />
|
||||
<Class name="listitem" height="25" hotcolor="bk_listitem_hovered"
|
||||
pushedcolor="bk_listitem_selected" selectednormalcolor="bk_listitem_selected"
|
||||
fadehot="false" margin="10,0,0,0" />
|
||||
<Class name="list_topitem" height="30" font="system_14" normalcolor="bk_topitem_normal" hotcolor="bk_topitem_hovered" pushedcolor="bk_topitem_selected" selectednormalcolor="bk_topitem_selected" fadehot="false" />
|
||||
|
||||
<!--进度条通用样式, 注意进度条的值使用`_value`属性-->
|
||||
|
Loading…
Reference in New Issue
Block a user