update
This commit is contained in:
parent
d2ed2516aa
commit
b17b9558d3
@ -18,32 +18,48 @@ int LuaDelegate::DoString(std::string scr) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
return:
|
||||||
|
-1: error
|
||||||
|
0: success
|
||||||
|
*/
|
||||||
int LuaDelegate::UpdateScript(std::string scr) {
|
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){
|
if(nullptr != mVM){
|
||||||
lua_close(mVM);
|
lua_close(mVM);
|
||||||
}
|
}
|
||||||
free(mVM);
|
mVM = tmp;
|
||||||
mVM = luaL_newstate(); //打开lua
|
|
||||||
if (nullptr != mVM) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
for (auto x : mFunc) {
|
for (auto x : mFunc) {
|
||||||
lua_register(mVM, x.first.c_str(), x.second);
|
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){
|
mScript = scr;
|
||||||
printf("lua error");
|
ret = luaL_dostring(mVM, scr.c_str());
|
||||||
|
if (ret > 0) {
|
||||||
PrintError(mVM);
|
PrintError(mVM);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
*/
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LuaDelegate::PrintError(lua_State *L) {
|
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)
|
int LuaDelegate::BindFunction(std::string name, lua_CFunction function)
|
||||||
@ -56,13 +72,13 @@ int LuaDelegate::BindFunction(std::string name, lua_CFunction function)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LuaDelegate::OnSerialData(std::string data) {
|
void LuaDelegate::OnSerialData(std::string data) {
|
||||||
int i = lua_getglobal(mVM,"OnUartData");
|
int i = lua_getglobal(mVM, "OnUartData");
|
||||||
if(i == 0){
|
if (i == 0) {
|
||||||
std::cout << "OnSerialData not found\r\n";
|
std::cout << "OnUartData not found\r\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
lua_pushstring(mVM,data.data());
|
lua_pushstring(mVM, data.data());
|
||||||
lua_call(mVM,1,0);
|
lua_call(mVM, 1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LuaDelegate::DumpStack() {
|
void LuaDelegate::DumpStack() {
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
|
|
||||||
UartForm::UartForm(ui::Window* hwnd,std::wstring name,
|
UartForm::UartForm(ui::Window* hwnd,std::wstring name,
|
||||||
uint32_t baurate,
|
uint32_t baurate,
|
||||||
uint8_t data_bits, uint8_t stop_bits,
|
uint8_t data_bits, uint8_t stop_bits,
|
||||||
@ -22,15 +23,14 @@ UartForm::UartForm(ui::Window* hwnd,std::wstring name,
|
|||||||
m_runing = true;
|
m_runing = true;
|
||||||
mLua = new LuaDelegate;
|
mLua = new LuaDelegate;
|
||||||
|
|
||||||
std::ifstream infile;
|
mLuaFile.open("D:\\project\\c++\\nim_duilib\\examples\\x64\\script.lua");
|
||||||
infile.open("D:\\project\\c++\\nim_duilib\\examples\\x64\\script.lua");
|
|
||||||
std::string lua_script;
|
std::string lua_script;
|
||||||
if (infile.is_open()) {
|
if (mLuaFile.is_open()) {
|
||||||
std::string s;
|
std::string s;
|
||||||
while (getline(infile, s)) {
|
while (getline(mLuaFile, s)) {
|
||||||
lua_script += s;
|
lua_script += s + "\r\n";
|
||||||
}
|
}
|
||||||
infile.close();
|
mLuaFile.close();
|
||||||
}
|
}
|
||||||
mLuaScript = lua_script;
|
mLuaScript = lua_script;
|
||||||
mLua->DoString(lua_script);
|
mLua->DoString(lua_script);
|
||||||
@ -84,7 +84,10 @@ void UartForm::Init()
|
|||||||
mEditSend = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_send_edit"));
|
mEditSend = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_send_edit"));
|
||||||
mEditRecv = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_recv_eidt"));
|
mEditRecv = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_recv_eidt"));
|
||||||
mEditLua = dynamic_cast<ui::RichEdit*>(FindSubControl(L"lua_script"));
|
mEditLua = dynamic_cast<ui::RichEdit*>(FindSubControl(L"lua_script"));
|
||||||
|
mEditLua->SetRich(true);
|
||||||
|
mEditLua->SetReturnMsgWantCtrl(true);
|
||||||
|
|
||||||
|
mEditLua->SetText(string2wstring(mLuaScript));
|
||||||
mEditRecv->SetReadOnly(true);
|
mEditRecv->SetReadOnly(true);
|
||||||
mEditRecv->SetRich(false);
|
mEditRecv->SetRich(false);
|
||||||
mEditRecv->SetAttribute(L"autovscroll", L"true");
|
mEditRecv->SetAttribute(L"autovscroll", L"true");
|
||||||
@ -120,6 +123,22 @@ void UartForm::Init()
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto mBtnSaveLua = static_cast<ui::Button*>(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()
|
void UartForm::UpdateRecvEdit()
|
||||||
|
@ -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<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"));
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -7,6 +7,7 @@
|
|||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
#include <tchar.h>
|
#include <tchar.h>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
// base header
|
// base header
|
||||||
#include "base/base.h"
|
#include "base/base.h"
|
||||||
@ -48,6 +49,8 @@ public:
|
|||||||
HWND m_hwnd;
|
HWND m_hwnd;
|
||||||
LuaDelegate* mLua;
|
LuaDelegate* mLua;
|
||||||
std::string mLuaScript;
|
std::string mLuaScript;
|
||||||
|
std::ifstream mLuaFile;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void HandleMessage(ui::EventArgs& msg);
|
virtual void HandleMessage(ui::EventArgs& msg);
|
||||||
};
|
};
|
||||||
|
@ -15,13 +15,13 @@
|
|||||||
<Label text="发送数据" height="25" />
|
<Label text="发送数据" height="25" />
|
||||||
<RichEdit class="simple input" width="stretch" name="uart_send_edit" height="30" margin="5,5,5,5" padding="6,6,6" />
|
<RichEdit class="simple input" width="stretch" name="uart_send_edit" height="30" margin="5,5,5,5" padding="6,6,6" />
|
||||||
<HBox height="40">
|
<HBox height="40">
|
||||||
|
<Button class="btn_global_blue_80x30" name="btn_save_lua" width="100" text="保存lua脚本" bkcolor="lightcolor" margin="25,3,5,3" />
|
||||||
<Button class="btn_global_blue_80x30" name="btn_send_data" width="100" text="发送" bkcolor="lightcolor" margin="25,3,5,3" />
|
<Button class="btn_global_blue_80x30" name="btn_send_data" width="100" text="发送" bkcolor="lightcolor" margin="25,3,5,3" />
|
||||||
<CheckBox class="checkbox_font12" name="check_new_line" text="发送新行" margin="10,12,0,0" selected="true"/>
|
<CheckBox class="checkbox_font12" name="check_new_line" text="发送新行" margin="10,12,0,0" selected="true"/>
|
||||||
<CheckBox class="checkbox_font12" name="check_time_send" text="定时发送" margin="10,12,0,0" selected="true"/>
|
<CheckBox class="checkbox_font12" name="check_time_send" text="定时发送" margin="10,12,0,0" selected="true"/>
|
||||||
<CheckBox class="checkbox_font12" name="check_hex_send" text="hex显示" margin="10,12,0,0" selected="true"/>
|
<CheckBox class="checkbox_font12" name="check_hex_send" text="hex显示" margin="10,12,0,0" selected="true"/>
|
||||||
<CheckBox class="checkbox_font12" name="check_hex_recv" text="hex接受" margin="10,12,0,0" selected="true"/>
|
<CheckBox class="checkbox_font12" name="check_hex_recv" text="hex接受" margin="10,12,0,0" selected="true"/>
|
||||||
<Button class="btn_global_blue_80x30" name="btn_close_uart" width="100" text="关闭" bkcolor="lightcolor" margin="25,3,5,3" />
|
<Button class="btn_global_blue_80x30" name="btn_close_uart" width="100" text="关闭" bkcolor="lightcolor" margin="25,3,5,3" />
|
||||||
|
|
||||||
</HBox>
|
</HBox>
|
||||||
</VBox>
|
</VBox>
|
||||||
</ChildBox>
|
</ChildBox>
|
||||||
|
Loading…
Reference in New Issue
Block a user