实现modbus功能
This commit is contained in:
parent
0842a7738c
commit
8af531ddc6
@ -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;
|
||||
|
@ -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<ui::Label*> (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<ui::RichEdit*>(FindSubControl(L"read_single_addr"));
|
||||
m_button_1 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_send_modbus"));
|
||||
m_rich_edit_2 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"read_batch_addr"));
|
||||
m_rich_edit_3 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"read_batch_cnt"));
|
||||
|
||||
auto mBtnSend = static_cast<ui::Button*>(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<ui::RichEdit*>(FindSubControl(L"uart_send_edit"));
|
||||
mEditLua->SetReturnMsgWantCtrl(true);
|
||||
|
@ -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);
|
||||
};
|
||||
|
@ -63,16 +63,14 @@ vector<wstring> 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;
|
||||
|
Loading…
Reference in New Issue
Block a user