diff --git a/examples/proto_debuger/base_form.cpp b/examples/proto_debuger/base_form.cpp index 2da255e7..324276b6 100644 --- a/examples/proto_debuger/base_form.cpp +++ b/examples/proto_debuger/base_form.cpp @@ -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) { diff --git a/examples/proto_debuger/lua_bind.cpp b/examples/proto_debuger/lua_bind.cpp new file mode 100644 index 00000000..a3edc30e --- /dev/null +++ b/examples/proto_debuger/lua_bind.cpp @@ -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; +} + diff --git a/examples/proto_debuger/lua_bind.h b/examples/proto_debuger/lua_bind.h new file mode 100644 index 00000000..71639373 --- /dev/null +++ b/examples/proto_debuger/lua_bind.h @@ -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 diff --git a/examples/proto_debuger/lua_wraper.cpp b/examples/proto_debuger/lua_wraper.cpp index 69cd73ec..8b1987bb 100644 --- a/examples/proto_debuger/lua_wraper.cpp +++ b/examples/proto_debuger/lua_wraper.cpp @@ -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) - return; + 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); diff --git a/examples/proto_debuger/lua_wraper.h b/examples/proto_debuger/lua_wraper.h index b7b3b3ef..464f0a02 100644 --- a/examples/proto_debuger/lua_wraper.h +++ b/examples/proto_debuger/lua_wraper.h @@ -25,8 +25,8 @@ public: void Stop(); int DoString(std::string); int UpdateScript(std::string); - template - 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 - void pushstack(T arg1, Types... rest); + template void pushstack(lua_Number arg1, Types... rest) { lua_pushnumber(mVM, arg1); @@ -95,18 +94,15 @@ public: } void PrintError(lua_State *L); - - 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); - + int BindFunction(std::string name, lua_CFunction); + void OnSerialData(std::string); void DumpStack(); ~LuaDelegate(); private: lua_State *mVM; std::string mFile; + std::string mScript; std::map mFunc; }; diff --git a/examples/proto_debuger/proto_debuger.vcxproj b/examples/proto_debuger/proto_debuger.vcxproj index bb0b4245..5f89301e 100644 --- a/examples/proto_debuger/proto_debuger.vcxproj +++ b/examples/proto_debuger/proto_debuger.vcxproj @@ -172,11 +172,13 @@ - + + + @@ -194,6 +196,7 @@ + diff --git a/examples/proto_debuger/proto_debuger.vcxproj.filters b/examples/proto_debuger/proto_debuger.vcxproj.filters index 77e601cd..1dc72dc0 100644 --- a/examples/proto_debuger/proto_debuger.vcxproj.filters +++ b/examples/proto_debuger/proto_debuger.vcxproj.filters @@ -24,7 +24,7 @@ 婧愭枃浠 - + 婧愭枃浠 @@ -51,6 +51,9 @@ 婧愭枃浠 + + 婧愭枃浠 + @@ -86,6 +89,9 @@ 澶存枃浠 + + 澶存枃浠 + @@ -106,6 +112,9 @@ 璧勬簮鏂囦欢 + + 璧勬簮鏂囦欢 + diff --git a/examples/proto_debuger/tcp_client_form.cpp b/examples/proto_debuger/tcp_client_form.cpp index eb30db2a..cf30e135 100644 --- a/examples/proto_debuger/tcp_client_form.cpp +++ b/examples/proto_debuger/tcp_client_form.cpp @@ -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() diff --git a/examples/proto_debuger/tcp_client_form.h b/examples/proto_debuger/tcp_client_form.h index 1f790787..3ca1f15a 100644 --- a/examples/proto_debuger/tcp_client_form.h +++ b/examples/proto_debuger/tcp_client_form.h @@ -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; }; \ No newline at end of file diff --git a/examples/proto_debuger/tcp_server_form.cpp b/examples/proto_debuger/tcp_server_form.cpp index b49534cf..7d741fb0 100644 --- a/examples/proto_debuger/tcp_server_form.cpp +++ b/examples/proto_debuger/tcp_server_form.cpp @@ -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; diff --git a/examples/proto_debuger/tcp_server_form.h b/examples/proto_debuger/tcp_server_form.h index 8c3ea45f..e1f08128 100644 --- a/examples/proto_debuger/tcp_server_form.h +++ b/examples/proto_debuger/tcp_server_form.h @@ -7,7 +7,7 @@ #include "duilib/UIlib.h" #include "tcp_server_libevent.h" #include - +#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 mClients; bool mFlagSelectClient; + LuaDelegate *mLua; }; diff --git a/examples/proto_debuger/uart_form.cpp b/examples/proto_debuger/uart_form.cpp new file mode 100644 index 00000000..784d2842 --- /dev/null +++ b/examples/proto_debuger/uart_form.cpp @@ -0,0 +1,138 @@ +#include "uart_process.h" +#include "utils.h" +#include "msgdef.h" +#include "utils.h" +#include + +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 (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(FindSubControl(L"uart_send_edit")); + mEditRecv = dynamic_cast(FindSubControl(L"uart_recv_eidt")); + mEditLua = dynamic_cast(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(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(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"); + } + +} + diff --git a/examples/proto_debuger/uart_process.h b/examples/proto_debuger/uart_process.h index a3af89cd..aa677050 100644 --- a/examples/proto_debuger/uart_process.h +++ b/examples/proto_debuger/uart_process.h @@ -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); - }; diff --git a/examples/x64/Debug/resources/themes/default/global.xml b/examples/x64/Debug/resources/themes/default/global.xml index 2f6d25e4..647ac9a7 100644 --- a/examples/x64/Debug/resources/themes/default/global.xml +++ b/examples/x64/Debug/resources/themes/default/global.xml @@ -135,7 +135,9 @@ - +