163 lines
4.1 KiB
C++
163 lines
4.1 KiB
C++
#include "modbus_form.h"
|
|
#include "lua_bind.h"
|
|
#include "msgdef.h"
|
|
#include "modbus.h"
|
|
|
|
|
|
ModbusMasterForm::ModbusMasterForm(ui::Window* hwnd, std::wstring name,
|
|
uint32_t baurate, UINT portnum,
|
|
uint8_t data_bits, uint8_t stop_bits,
|
|
uint8_t verify, uint8_t flow_control) {
|
|
|
|
m_portnum = portnum;
|
|
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;
|
|
|
|
mLuaFile.open(MODBUS_LUA_SCRIPT);
|
|
std::string lua_script;
|
|
if (mLuaFile.is_open()) {
|
|
std::string s;
|
|
while (getline(mLuaFile, s)) {
|
|
lua_script += s + "\r\n";
|
|
}
|
|
mLuaFile.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->CallFuntion<std::string>("OnUartData", std::string(recv));
|
|
if (this->mEditRecv != nullptr) {
|
|
this->mEditRecv->AppendText(string2wstring(recv), false);
|
|
::PostMessage(this->GetWindow()->GetHWND(), WM_ADD_UART_RECVDATA, 0, 0);
|
|
}
|
|
}
|
|
else {
|
|
Sleep(100);
|
|
}
|
|
}
|
|
});
|
|
if (nullptr != hwnd) {
|
|
// set window so we get getwindow by GetWindow funtion
|
|
this->SetWindow(hwnd, nullptr, false);
|
|
}
|
|
this->mLua->BindFunction("showdata", LuaShowData);
|
|
|
|
}
|
|
|
|
ModbusMasterForm::~ModbusMasterForm() {
|
|
|
|
}
|
|
|
|
void ModbusMasterForm::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"));
|
|
mEditLua->SetReturnMsgWantCtrl(true);
|
|
|
|
mEditLua->SetText(string2wstring(mLuaScript));
|
|
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 mBtnSaveLua = static_cast<ui::Button*>(FindSubControl(L"btn_save_lua"));
|
|
if (nullptr != mBtnSaveLua) {
|
|
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;
|
|
mLuaFileEdit = std::ofstream(MODBUS_LUA_SCRIPT, std::ios::out | std::ios::trunc);
|
|
mLuaFileEdit.write(lua.c_str(), lua.size());
|
|
mLuaFileEdit.flush();
|
|
mLuaFileEdit.close();
|
|
}
|
|
else {
|
|
MessageBox(0, L"lua脚本错误", L"", 0);
|
|
return true;
|
|
}
|
|
std::cout << lua.c_str() << "\r\n";
|
|
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 ModbusMasterForm::UpdateRecvEdit()
|
|
{
|
|
}
|
|
|
|
LuaDelegate* ModbusMasterForm::LuaVM() {
|
|
return nullptr;
|
|
}
|
|
|
|
void ModbusMasterForm::ShowDataInEdit(const char*) {
|
|
|
|
}
|
|
|
|
void ModbusMasterForm::OnUpdateUart()
|
|
{
|
|
}
|
|
|
|
void ModbusMasterForm::HandleMessage(ui::EventArgs& msg) {
|
|
|
|
}
|
|
|