From 8af531ddc6d2eac9bdf6059e7748b3b4221cf14d Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Tue, 24 Jan 2023 23:05:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0modbus=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/libmodbus/src/modbus-rtu.c | 6 +-- examples/proto_debuger/modbus_form.cpp | 48 +++++++++++++++++++-- examples/proto_debuger/modbus_form.h | 13 ++++-- examples/proto_debuger/new_monitor_form.cpp | 46 +++++++++----------- 4 files changed, 77 insertions(+), 36 deletions(-) diff --git a/examples/libmodbus/src/modbus-rtu.c b/examples/libmodbus/src/modbus-rtu.c index 7e6e4a8c..4159483e 100644 --- a/examples/libmodbus/src/modbus-rtu.c +++ b/examples/libmodbus/src/modbus-rtu.c @@ -335,13 +335,13 @@ static int _modbus_rtu_connect(modbus_t *ctx) GENERIC_READ | GENERIC_WRITE, 0, NULL, - OPEN_EXISTING, + CREATE_ALWAYS, 0, NULL); /* Error checking */ if (ctx_rtu->w_ser.fd == INVALID_HANDLE_VALUE) { - fprintf(stderr, "ERROR Can't open the device %s (%s)\n", + printf("ERROR Can't open the device %s (%s)\n", ctx_rtu->device, strerror(errno)); return -1; } @@ -349,7 +349,7 @@ static int _modbus_rtu_connect(modbus_t *ctx) /* Save params */ ctx_rtu->old_dcb.DCBlength = sizeof(DCB); if (!GetCommState(ctx_rtu->w_ser.fd, &ctx_rtu->old_dcb)) { - fprintf(stderr, "ERROR Error getting configuration (LastError %d)\n", + printf(stderr, "ERROR Error getting configuration (LastError %d)\n", (int)GetLastError()); CloseHandle(ctx_rtu->w_ser.fd); ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE; diff --git a/examples/proto_debuger/modbus_form.cpp b/examples/proto_debuger/modbus_form.cpp index 7305f927..761f0119 100644 --- a/examples/proto_debuger/modbus_form.cpp +++ b/examples/proto_debuger/modbus_form.cpp @@ -1,7 +1,7 @@ #include "modbus_form.h" #include "lua_bind.h" #include "msgdef.h" -#include "modbus.h" + ModbusMasterForm::ModbusMasterForm(ui::Window* hwnd, std::wstring name, @@ -33,6 +33,8 @@ ModbusMasterForm::ModbusMasterForm(ui::Window* hwnd, std::wstring name, 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++) @@ -55,12 +57,12 @@ ModbusMasterForm::ModbusMasterForm(ui::Window* hwnd, std::wstring name, } } }); + */ if (nullptr != hwnd) { // set window so we get getwindow by GetWindow funtion this->SetWindow(hwnd, nullptr, false); } this->mLua->BindFunction("showdata", LuaShowData); - } ModbusMasterForm::~ModbusMasterForm() { @@ -70,7 +72,6 @@ ModbusMasterForm::~ModbusMasterForm() { void ModbusMasterForm::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 ", @@ -78,6 +79,47 @@ void ModbusMasterForm::Init() { m_data_bits, m_stop_bits); mRightSide->SetText(std::wstring(p)); + m_rich_edit_1 = dynamic_cast(FindSubControl(L"read_single_addr")); + m_button_1 = dynamic_cast(FindSubControl(L"btn_send_modbus")); + m_rich_edit_2 = dynamic_cast(FindSubControl(L"read_batch_addr")); + m_rich_edit_3 = dynamic_cast(FindSubControl(L"read_batch_cnt")); + + auto mBtnSend = static_cast(FindSubControl(L"btn_send_modbus")); + + if (NULL != mBtnSend) + { + mBtnSend->AttachClick([this](ui::EventArgs*){ + wprintf(L"com is %s m_name", m_name.c_str()); + printf("open ptr is %s %d %d %d ", wstring2string(m_name).c_str(), m_baurate,m_data_bits,m_stop_bits); + mModbus = modbus_new_rtu(wstring2string(wstring(L"\\\\.\\") + m_name), + m_baurate,'N',m_data_bits, m_stop_bits); + + if (mModbus != nullptr) { + modbus_set_slave(mModbus, 9); + modbus_set_debug(mModbus,true); + int ret = modbus_connect(mModbus); + if (ret < 0) { + printf("modbus_connect err %d\r\n", ret); + } + timeval* ptr = new timeval; + ptr->tv_sec = 1; + ptr->tv_usec = 0; + modbus_set_response_timeout(mModbus, ptr); + uint16_t dest[32]; + ret = modbus_read_registers(mModbus, 0x31, 1, dest); + if (ret < 0) { + printf("read %d timeout %d ", ret); + } + + modbus_flush(mModbus); + modbus_close(mModbus); + } + + return false; + + }); + } + /* mEditSend = dynamic_cast(FindSubControl(L"uart_send_edit")); mEditLua->SetReturnMsgWantCtrl(true); diff --git a/examples/proto_debuger/modbus_form.h b/examples/proto_debuger/modbus_form.h index 1dffe670..9182530a 100644 --- a/examples/proto_debuger/modbus_form.h +++ b/examples/proto_debuger/modbus_form.h @@ -14,6 +14,9 @@ #include "lua_wraper.h" #include "global.h" #include "utils.h" +#include "modbus.h" +#include "modbus-rtu.h" +#include "modbus.h" #define MODBUS_LUA_SCRIPT "modbus_script.lua" @@ -48,16 +51,18 @@ public: std::thread* m_thread_recv; wstring m_show_recv; - ui::RichEdit* mEditSend; - ui::RichEdit* mEditRecv; - ui::RichEdit* mEditLua; + ui::Label* m_label_1; + ui::RichEdit* m_rich_edit_1; + ui::Button* m_button_1; + ui::RichEdit* m_rich_edit_2; + ui::RichEdit* m_rich_edit_3; HWND m_hwnd; LuaDelegate* mLua; std::string mLuaScript; std::ifstream mLuaFile; std::ofstream mLuaFileEdit; - + modbus_t* mModbus; private: virtual void HandleMessage(ui::EventArgs& msg); }; diff --git a/examples/proto_debuger/new_monitor_form.cpp b/examples/proto_debuger/new_monitor_form.cpp index 2044eb13..a7263b7f 100644 --- a/examples/proto_debuger/new_monitor_form.cpp +++ b/examples/proto_debuger/new_monitor_form.cpp @@ -63,16 +63,14 @@ vector EnumPortsWdm() } -NewMonitorForm::NewMonitorForm(ui::Window* parent) -{ +NewMonitorForm::NewMonitorForm(ui::Window* parent) { m_parent = parent; m_tcp_client = nullptr; m_tcp_server = nullptr; - } -NewMonitorForm::~NewMonitorForm() -{ +NewMonitorForm::~NewMonitorForm() { + } std::wstring NewMonitorForm::GetSkinFolder() @@ -120,7 +118,7 @@ void NewMonitorForm::InitWindow() UartInfo* p = new UartInfo; p->port_num = PortNum; p->name = m_uart_select_combo->GetText(); - p->baurate = 115200; + p->baurate = atoi( wstring2string(m_uart_baurate_select->GetText()).c_str()); p->data_bits = 8; p->flow_control = 0; p->verify = 0; @@ -259,28 +257,24 @@ void NewMonitorForm::InitWindow() { PortNum = PortNum * 10 + (m_uart_select_combo->GetText()[i] - '0'); } - if (1 == SerialPort::InitPort(PortNum, 115200, 0, 8, 0)) { - // 打开成功 - MessageBox(0, L"打开成功", L"", 0); - UartInfo* p = new UartInfo; - p->port_num = PortNum; - p->name = m_uart_select_combo->GetText(); - p->baurate = 115200; - p->data_bits = 8; - p->flow_control = 0; - p->verify = 0; - p->stop_bits = 1; - - auto succ = ::PostMessage(m_parent->GetHWND(), WM_ADD_UART_MODBUS_MASTER, (WPARAM)p, 0); - if (!succ) { - printf("postmessage error :%d\r\n", GetLastError()); - } - this->Close(); - return true; - } - else { + // 打开成功 + MessageBox(0, L"打开成功", L"", 0); + UartInfo* p = new UartInfo; + p->port_num = PortNum; + p->name = m_uart_select_combo->GetText(); + p->baurate = atoi(wstring2string(m_uart_baurate_select->GetText()).c_str()); + p->data_bits = 8; + p->flow_control = 0; + p->verify = 0; + p->stop_bits = 1; + auto succ = ::PostMessage(m_parent->GetHWND(), WM_ADD_UART_MODBUS_MASTER, (WPARAM)p, 0); + if (!succ) { + printf("postmessage error :%d\r\n", GetLastError()); } + this->Close(); + return true; + } if (m_combo_type->GetText() == L"modbus salve") { UINT PortNum = 0;