2022-01-23 01:02:13 +08:00
|
|
|
|
#include "uart_process.h"
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
#include "msgdef.h"
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
#include <fstream>
|
2022-01-25 23:45:07 +08:00
|
|
|
|
#include "lua_bind.h"
|
2022-01-23 23:47:00 +08:00
|
|
|
|
|
2022-01-23 01:02:13 +08:00
|
|
|
|
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;
|
|
|
|
|
|
2022-01-25 23:45:07 +08:00
|
|
|
|
mLuaFile.open("script.lua");
|
2022-01-23 01:02:13 +08:00
|
|
|
|
std::string lua_script;
|
2022-01-23 23:47:00 +08:00
|
|
|
|
if (mLuaFile.is_open()) {
|
2022-01-23 01:02:13 +08:00
|
|
|
|
std::string s;
|
2022-01-23 23:47:00 +08:00
|
|
|
|
while (getline(mLuaFile, s)) {
|
|
|
|
|
lua_script += s + "\r\n";
|
2022-01-23 01:02:13 +08:00
|
|
|
|
}
|
2022-01-23 23:47:00 +08:00
|
|
|
|
mLuaFile.close();
|
2022-01-23 01:02:13 +08:00
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-01-25 23:45:07 +08:00
|
|
|
|
Sleep(100);
|
2022-01-23 01:02:13 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (nullptr != hwnd) {
|
|
|
|
|
// set window so we get getwindow by GetWindow funtion
|
|
|
|
|
this->SetWindow(hwnd, nullptr, false);
|
|
|
|
|
}
|
2022-01-25 23:45:07 +08:00
|
|
|
|
this->mLua->BindFunction("showdata", LuaShowData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UartForm::ShowDataInEdit(const char*p) {
|
|
|
|
|
if (nullptr == p) {
|
|
|
|
|
std::cout << "ShowDataInEdit " << std::endl;
|
|
|
|
|
}
|
|
|
|
|
this->mEditRecv->AppendText(string2wstring(p), false);
|
2022-01-23 01:02:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UartForm::OnUpdateUart()
|
|
|
|
|
{
|
|
|
|
|
//if(mEditRecv != nullptr)
|
|
|
|
|
// this->mEditRecv->SetText(this->m_show_recv );
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-25 23:45:07 +08:00
|
|
|
|
LuaDelegate* UartForm::LuaVM() {
|
|
|
|
|
return mLua;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-23 01:02:13 +08:00
|
|
|
|
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"<EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD>: %s <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>%d <20><><EFBFBD><EFBFBD>λ: %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"));
|
2022-01-23 23:47:00 +08:00
|
|
|
|
mEditLua->SetRich(true);
|
|
|
|
|
mEditLua->SetReturnMsgWantCtrl(true);
|
2022-01-23 01:02:13 +08:00
|
|
|
|
|
2022-01-23 23:47:00 +08:00
|
|
|
|
mEditLua->SetText(string2wstring(mLuaScript));
|
2022-01-23 01:02:13 +08:00
|
|
|
|
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++) //ת<><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-23 23:47:00 +08:00
|
|
|
|
auto mBtnSaveLua = static_cast<ui::Button*>(FindSubControl(L"btn_save_lua"));
|
|
|
|
|
if (mBtnSaveLua != nullptr) {
|
2022-01-25 23:45:07 +08:00
|
|
|
|
mBtnSaveLua->AttachClick([this](ui::EventArgs*) ->bool {
|
2022-01-23 23:47:00 +08:00
|
|
|
|
std::cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>lua<EFBFBD>ű<EFBFBD>\r\n";
|
|
|
|
|
std::string lua = wstring2string(mEditLua->GetText());
|
|
|
|
|
if (0 == this->mLua->UpdateScript(lua)) {
|
|
|
|
|
this->mLuaScript = lua;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
MessageBox(0, L"lua<EFBFBD>ű<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", L"", 0);
|
2022-01-25 23:45:07 +08:00
|
|
|
|
return true;
|
2022-01-23 23:47:00 +08:00
|
|
|
|
}
|
2022-01-25 23:45:07 +08:00
|
|
|
|
std::cout << lua.c_str() << "\r\n";
|
|
|
|
|
return true;
|
2022-01-23 23:47:00 +08:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-01-23 01:02:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UartForm::UpdateRecvEdit()
|
|
|
|
|
{
|
2022-01-25 23:45:07 +08:00
|
|
|
|
|
2022-01-23 01:02:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UartForm::HandleMessage(ui::EventArgs& msg)
|
|
|
|
|
{
|
|
|
|
|
if (msg.Type == WM_ADD_UART_RECVDATA) {
|
|
|
|
|
printf("hello world\r\n");
|
|
|
|
|
this->mEditRecv->SetText(L"123");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|