diff --git a/examples/proto_debuger/lua_wraper.cpp b/examples/proto_debuger/lua_wraper.cpp index 8b1987bb..f74b8ae6 100644 --- a/examples/proto_debuger/lua_wraper.cpp +++ b/examples/proto_debuger/lua_wraper.cpp @@ -18,32 +18,48 @@ int LuaDelegate::DoString(std::string scr) { return 0; } +/* +return: + -1: error + 0: success +*/ int LuaDelegate::UpdateScript(std::string scr) { + auto tmp = luaL_newstate(); //打开lua + if (nullptr == tmp) { + return -1; + } + luaL_openlibs(tmp); //打开标准库 + + int ret = luaL_dostring(tmp, scr.c_str()); + if (ret > 0) { + printf("UpdateScript error\r\n"); + PrintError(tmp); + return -1; + } + if(nullptr != mVM){ lua_close(mVM); } - free(mVM); - mVM = luaL_newstate(); //打开lua - if (nullptr != mVM) { - return -1; - } + mVM = tmp; for (auto x : mFunc) { lua_register(mVM, x.first.c_str(), x.second); } + mScript = scr; + + /* mScript = scr; - if (mVM != nullptr) { - int ret = luaL_dostring(mVM,scr.c_str()); - if (ret > 0){ - printf("lua error"); - PrintError(mVM); - return -1; - } - } + ret = luaL_dostring(mVM, scr.c_str()); + if (ret > 0) { + PrintError(mVM); + return -1; + } + */ return 0; } void LuaDelegate::PrintError(lua_State *L) { - std::cout<<"\nFATAL ERROR:%s\n\n"<< lua_tostring(L, -1); + auto err = std::string("\nFATAL ERROR:%s\n\n") + lua_tostring(L, -1); + std::cout << err; } int LuaDelegate::BindFunction(std::string name, lua_CFunction function) @@ -55,14 +71,14 @@ int LuaDelegate::BindFunction(std::string name, lua_CFunction function) return 0; } -void LuaDelegate::OnSerialData(std::string data) { - int i = lua_getglobal(mVM,"OnUartData"); - if(i == 0){ - std::cout << "OnSerialData not found\r\n"; +void LuaDelegate::OnSerialData(std::string data) { + int i = lua_getglobal(mVM, "OnUartData"); + if (i == 0) { + std::cout << "OnUartData not found\r\n"; return; - } - lua_pushstring(mVM,data.data()); - lua_call(mVM,1,0); + } + lua_pushstring(mVM, data.data()); + lua_call(mVM, 1, 0); } void LuaDelegate::DumpStack() { diff --git a/examples/proto_debuger/uart_form.cpp b/examples/proto_debuger/uart_form.cpp index 784d2842..e6abac9c 100644 --- a/examples/proto_debuger/uart_form.cpp +++ b/examples/proto_debuger/uart_form.cpp @@ -4,6 +4,7 @@ #include "utils.h" #include + UartForm::UartForm(ui::Window* hwnd,std::wstring name, uint32_t baurate, uint8_t data_bits, uint8_t stop_bits, @@ -22,15 +23,14 @@ UartForm::UartForm(ui::Window* hwnd,std::wstring name, m_runing = true; mLua = new LuaDelegate; - std::ifstream infile; - infile.open("D:\\project\\c++\\nim_duilib\\examples\\x64\\script.lua"); + mLuaFile.open("D:\\project\\c++\\nim_duilib\\examples\\x64\\script.lua"); std::string lua_script; - if (infile.is_open()) { + if (mLuaFile.is_open()) { std::string s; - while (getline(infile, s)) { - lua_script += s; + while (getline(mLuaFile, s)) { + lua_script += s + "\r\n"; } - infile.close(); + mLuaFile.close(); } mLuaScript = lua_script; mLua->DoString(lua_script); @@ -84,7 +84,10 @@ void UartForm::Init() mEditSend = dynamic_cast(FindSubControl(L"uart_send_edit")); mEditRecv = dynamic_cast(FindSubControl(L"uart_recv_eidt")); mEditLua = dynamic_cast(FindSubControl(L"lua_script")); + mEditLua->SetRich(true); + mEditLua->SetReturnMsgWantCtrl(true); + mEditLua->SetText(string2wstring(mLuaScript)); mEditRecv->SetReadOnly(true); mEditRecv->SetRich(false); mEditRecv->SetAttribute(L"autovscroll", L"true"); @@ -120,6 +123,22 @@ void UartForm::Init() }); } + auto mBtnSaveLua = static_cast(FindSubControl(L"btn_save_lua")); + if (mBtnSaveLua != nullptr) { + mBtnSaveLua->AttachClick([this](ui::EventArgs*) { + std::cout << "luaű\r\n"; + std::string lua = wstring2string(mEditLua->GetText()); + if (0 == this->mLua->UpdateScript(lua)) { + this->mLuaScript = lua; + } + else { + MessageBox(0, L"luaű", L"", 0); + return; + } + std::cout << lua.c_str() <<"\r\n"; + } + ); + } } void UartForm::UpdateRecvEdit() diff --git a/examples/proto_debuger/uart_process.cpp b/examples/proto_debuger/uart_process.cpp deleted file mode 100644 index 355cf7ee..00000000 --- a/examples/proto_debuger/uart_process.cpp +++ /dev/null @@ -1,116 +0,0 @@ -#include "uart_process.h" -#include "utils.h" -#include "msgdef.h" -#include "utils.h" - -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) -{ - 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; - 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); - 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) { - 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")); - 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 aa677050..dd843714 100644 --- a/examples/proto_debuger/uart_process.h +++ b/examples/proto_debuger/uart_process.h @@ -7,6 +7,7 @@ #include #include #include +#include // base header #include "base/base.h" @@ -48,6 +49,8 @@ public: HWND m_hwnd; LuaDelegate* mLua; std::string mLuaScript; + std::ifstream mLuaFile; + private: virtual void HandleMessage(ui::EventArgs& msg); }; diff --git a/examples/x64/Debug/resources/themes/default/basic/uart_form.xml b/examples/x64/Debug/resources/themes/default/basic/uart_form.xml index 052d9059..b54e2c4c 100644 --- a/examples/x64/Debug/resources/themes/default/basic/uart_form.xml +++ b/examples/x64/Debug/resources/themes/default/basic/uart_form.xml @@ -15,13 +15,13 @@