串口实现

This commit is contained in:
zcy 2021-09-22 00:01:02 +08:00
parent d3827b9cdb
commit c4a1a02db5
11 changed files with 197 additions and 22 deletions

View File

@ -1,7 +1,7 @@
#include "base_form.h" #include "base_form.h"
#include "serial_port.h"
const std::wstring BasicForm::kClassName = L"Basic"; const std::wstring BasicForm::kClassName = L"Basic";
#define WM_USER_POS_CHANGED (WM_USER + 2)
BasicForm::BasicForm(): BasicForm::BasicForm():
mMonitorNewSelect(nullptr) mMonitorNewSelect(nullptr)
@ -33,6 +33,39 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{ {
if (uMsg == WM_USER_POS_CHANGED) { if (uMsg == WM_USER_POS_CHANGED) {
}
if (uMsg == WM_ADD_UART_MONITOR) {
printf("add monitor");
UartInfo* p = (UartInfo*)wParam;
wprintf(L"%s", p->name.c_str());
ui::TreeNode* node = new ui::TreeNode;
node->SetText(p->name);
node->SetClass(L"listitem");
node->SetFixedHeight(20);
node->SetMargin({ 20, 0, 0, 0 });
mMonitor->GetRootNode()->GetChildNode(0)->AddChildNode(node);
if (mUartForm.find(p->name) == mUartForm.end()) {
auto form = new UartForm(p->name, p->baurate,
p->data_bits, p->stop_bits, p->verify, p->flow_control);
form->SetChildLayoutXML(L"basic/uart_form.xml");
form->SetVisible(false);
mUartForm[p->name] = form;
}
node->AttachAllEvents([this](ui::EventArgs* ev){
if (ui::EventType::kEventSelect == ev->Type) {
mRightSide->RemoveAll();
wprintf(L"%s\r\n",dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText().c_str());
UartForm* p = mUartForm[dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText()];
if (nullptr != p) {
p->SetVisible(true);
mRightSide->Add(mUartForm[dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText()]);
}
}
return true;
});
} }
return WindowImplBase::HandleMessage(uMsg, wParam, lParam); return WindowImplBase::HandleMessage(uMsg, wParam, lParam);
} }
@ -45,14 +78,14 @@ void BasicForm::InitWindow()
if (nullptr != btn_add_new) { if (nullptr != btn_add_new) {
btn_add_new->AttachClick([this](ui::EventArgs* args) { btn_add_new->AttachClick([this](ui::EventArgs* args) {
if (mMonitorNewSelect == nullptr) { if (mMonitorNewSelect == nullptr) {
mMonitorNewSelect = new NewMonitorForm(); mMonitorNewSelect = new NewMonitorForm(this);
mMonitorNewSelect->Create(this->GetHWND(), NewMonitorForm::kClassName.c_str(), mMonitorNewSelect->Create(this->GetHWND(), NewMonitorForm::kClassName.c_str(),
WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & WS_SIZEBOX, 0); WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & WS_SIZEBOX, 0);
mMonitorNewSelect->CenterWindow(); mMonitorNewSelect->CenterWindow();
mMonitorNewSelect->ShowModalFake(this->GetHWND()); mMonitorNewSelect->ShowModalFake(this->GetHWND());
} }
else { else {
mMonitorNewSelect = new NewMonitorForm(); mMonitorNewSelect = new NewMonitorForm(this);
mMonitorNewSelect->Create(this->GetHWND(), NewMonitorForm::kClassName.c_str(), mMonitorNewSelect->Create(this->GetHWND(), NewMonitorForm::kClassName.c_str(),
WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & WS_SIZEBOX, 0); WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & WS_SIZEBOX, 0);
mMonitorNewSelect->CenterWindow(); mMonitorNewSelect->CenterWindow();
@ -61,21 +94,21 @@ void BasicForm::InitWindow()
return true; return true;
}); });
} }
ui::TreeView* tree = dynamic_cast<ui::TreeView*>(FindControl(L"tree")); mMonitor = dynamic_cast<ui::TreeView*>(FindControl(L"tree"));
if (nullptr != tree) { if (nullptr != mMonitor) {
ui::TreeNode* node = new ui::TreeNode; ui::TreeNode* node = new ui::TreeNode;
node->SetText(nbase::StringPrintf(L"uart")); node->SetText(nbase::StringPrintf(L"uart"));
node->SetClass(L"listitem"); node->SetClass(L"listitem");
node->SetFixedHeight(20); node->SetFixedHeight(20);
node->SetMargin({ 10, 0, 0, 0 }); node->SetMargin({ 10, 0, 0, 0 });
tree->GetRootNode()->AddChildNode(node); mMonitor->GetRootNode()->AddChildNode(node);
node = new ui::TreeNode; node = new ui::TreeNode;
node->SetText(nbase::StringPrintf(L"tcp client")); node->SetText(nbase::StringPrintf(L"tcp client"));
node->SetClass(L"listitem"); node->SetClass(L"listitem");
node->SetFixedHeight(20); node->SetFixedHeight(20);
node->SetMargin({ 10, 0, 0, 0 }); node->SetMargin({ 10, 0, 0, 0 });
tree->GetRootNode()->AddChildNode(node); mMonitor->GetRootNode()->AddChildNode(node);
node = new ui::TreeNode; node = new ui::TreeNode;
@ -83,34 +116,34 @@ void BasicForm::InitWindow()
node->SetClass(L"listitem"); node->SetClass(L"listitem");
node->SetFixedHeight(20); node->SetFixedHeight(20);
node->SetMargin({ 10, 0, 0, 0 }); node->SetMargin({ 10, 0, 0, 0 });
tree->GetRootNode()->AddChildNode(node); mMonitor->GetRootNode()->AddChildNode(node);
node = new ui::TreeNode; node = new ui::TreeNode;
node->SetText(nbase::StringPrintf(L"udp client")); node->SetText(nbase::StringPrintf(L"udp client"));
node->SetClass(L"listitem"); node->SetClass(L"listitem");
node->SetFixedHeight(20); node->SetFixedHeight(20);
node->SetMargin({ 10, 0, 0, 0 }); node->SetMargin({ 10, 0, 0, 0 });
tree->GetRootNode()->AddChildNode(node); mMonitor->GetRootNode()->AddChildNode(node);
node = new ui::TreeNode; node = new ui::TreeNode;
node->SetText(nbase::StringPrintf(L"udp server")); node->SetText(nbase::StringPrintf(L"udp server"));
node->SetClass(L"listitem"); node->SetClass(L"listitem");
node->SetFixedHeight(20); node->SetFixedHeight(20);
node->SetMargin({ 10, 0, 0, 0 }); node->SetMargin({ 10, 0, 0, 0 });
tree->GetRootNode()->AddChildNode(node); mMonitor->GetRootNode()->AddChildNode(node);
node = new ui::TreeNode; node = new ui::TreeNode;
node->SetText(nbase::StringPrintf(L"udp group")); node->SetText(nbase::StringPrintf(L"udp group"));
node->SetClass(L"listitem"); node->SetClass(L"listitem");
node->SetFixedHeight(20); node->SetFixedHeight(20);
node->SetMargin({ 10, 0, 0, 0 }); node->SetMargin({ 10, 0, 0, 0 });
tree->GetRootNode()->AddChildNode(node); mMonitor->GetRootNode()->AddChildNode(node);
} }
ui::Window* ss = dynamic_cast<ui::Window*>(FindControl(L"ss")); mRightSide = dynamic_cast<ui::TabBox*>(FindControl(L"tab_side"));
if (nullptr != ss) {
ss->SetWindowResourcePath(L"basic/newmonitor.xml"); mRightSide->RemoveAll();
}
} }
LRESULT BasicForm::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) LRESULT BasicForm::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)

View File

@ -16,6 +16,12 @@
#include "ui_components/ui_components.h" #include "ui_components/ui_components.h"
#include "new_monitor_form.h" #include "new_monitor_form.h"
#include "uart_process.h" #include "uart_process.h"
#include <vector>
#include <map>
#define WM_USER_POS_CHANGED (WM_USER + 2)
#define WM_ADD_UART_MONITOR (WM_USER + 3)
class BasicForm : public ui::WindowImplBase class BasicForm : public ui::WindowImplBase
{ {
@ -46,5 +52,10 @@ public:
static const std::wstring kClassName; static const std::wstring kClassName;
private: private:
NewMonitorForm* mMonitorNewSelect; NewMonitorForm* mMonitorNewSelect;
ui::TabBox *mRightSide;
ui::TreeView *mMonitor;
std::map<std::wstring,UartForm*> mUartForm;
}; };

View File

@ -52,6 +52,8 @@ void MainThread::Init()
ui::GlobalManager::Startup(L"resources\\", ui::CreateControlCallback(), false); ui::GlobalManager::Startup(L"resources\\", ui::CreateControlCallback(), false);
#endif #endif
auto dpiManager = ui::DpiManager::GetInstance();
// 创建一个默认带有阴影的居中窗口 // 创建一个默认带有阴影的居中窗口
BasicForm* window = new BasicForm(); BasicForm* window = new BasicForm();
window->Create(NULL, BasicForm::kClassName.c_str(), WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & WS_SIZEBOX, 0); window->Create(NULL, BasicForm::kClassName.c_str(), WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & WS_SIZEBOX, 0);

View File

@ -6,6 +6,8 @@
#include <windows.h> #include <windows.h>
#include <iostream> #include <iostream>
#include <winspool.h> #include <winspool.h>
#include "serial_port.h"
#include "base_form.h"
using namespace std; using namespace std;
@ -55,9 +57,10 @@ vector<wstring> EnumPortsWdm()
return ret; return ret;
} }
NewMonitorForm::NewMonitorForm()
{
NewMonitorForm::NewMonitorForm(ui::Window* parent)
{
m_parent = parent;
} }
NewMonitorForm::~NewMonitorForm() NewMonitorForm::~NewMonitorForm()
@ -96,10 +99,35 @@ void NewMonitorForm::InitWindow()
} }
ui::Button* btn_add_new = dynamic_cast<ui::Button*>(FindControl(L"open_button")); ui::Button* btn_add_new = dynamic_cast<ui::Button*>(FindControl(L"open_button"));
if (nullptr != btn_add_new) { if (nullptr != btn_add_new) {
btn_add_new->SetText(L"退出");
btn_add_new->AttachClick([this](ui::EventArgs*) { btn_add_new->AttachClick([this](ui::EventArgs*) {
if(m_uart_select_combo->GetText() != L""){
UINT PortNum = 0;
for (int i = 3; m_uart_select_combo->GetText()[i] != '\0'; i++) //转换为数字
{
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->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_MONITOR, (WPARAM)p, 0);
if (!succ) {
printf("postmessage error :%d\r\n", GetLastError());
}
this->Close();
return true;
}
else {
}
}
return true; return true;
}); });
} }
@ -160,6 +188,13 @@ void NewMonitorForm::InitWindow()
m_uart_config_vbox = dynamic_cast<ui::VBox*>(FindControl(L"uart_config")); m_uart_config_vbox = dynamic_cast<ui::VBox*>(FindControl(L"uart_config"));
m_uart_select_combo = dynamic_cast<ui::Combo*>(FindControl(L"uart_type")); m_uart_select_combo = dynamic_cast<ui::Combo*>(FindControl(L"uart_type"));
m_uart_select_combo->AttachAllEvents([this](ui::EventArgs* ev) {
if (ev->Type == 42) {
}
return true;
});
m_uart_baurate_select = dynamic_cast<ui::Combo*>(FindControl(L"uart_baurate_select")); m_uart_baurate_select = dynamic_cast<ui::Combo*>(FindControl(L"uart_baurate_select"));
m_combo_type->AttachAllEvents([this](ui::EventArgs* ev) { m_combo_type->AttachAllEvents([this](ui::EventArgs* ev) {

View File

@ -17,7 +17,7 @@
class NewMonitorForm : public ui::WindowImplBase class NewMonitorForm : public ui::WindowImplBase
{ {
public: public:
NewMonitorForm(); NewMonitorForm(ui::Window *);
~NewMonitorForm(); ~NewMonitorForm();
/** /**
@ -47,6 +47,7 @@ private:
ui::Combo* m_uart_select_combo; ui::Combo* m_uart_select_combo;
ui::Combo* m_uart_baurate_select; ui::Combo* m_uart_baurate_select;
ui::Combo* m_combo_type; ui::Combo* m_combo_type;
ui::Window* m_parent;
}; };

View File

@ -160,6 +160,7 @@
<ClCompile Include="base_form.cpp" /> <ClCompile Include="base_form.cpp" />
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
<ClCompile Include="new_monitor_form.cpp" /> <ClCompile Include="new_monitor_form.cpp" />
<ClCompile Include="serial_port.cpp" />
<ClCompile Include="uart_process.cpp" /> <ClCompile Include="uart_process.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -167,6 +168,7 @@
<ClInclude Include="main.h" /> <ClInclude Include="main.h" />
<ClInclude Include="new_monitor_form.h" /> <ClInclude Include="new_monitor_form.h" />
<ClInclude Include="resource1.h" /> <ClInclude Include="resource1.h" />
<ClInclude Include="serial_port.h" />
<ClInclude Include="uart_process.h" /> <ClInclude Include="uart_process.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -24,6 +24,12 @@
<ClCompile Include="new_monitor_form.cpp"> <ClCompile Include="new_monitor_form.cpp">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="uart_process.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="serial_port.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="main.h"> <ClInclude Include="main.h">
@ -38,6 +44,12 @@
<ClInclude Include="new_monitor_form.h"> <ClInclude Include="new_monitor_form.h">
<Filter>头文件</Filter> <Filter>头文件</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="uart_process.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="serial_port.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Xml Include="..\Debug\resources\themes\default\global.xml"> <Xml Include="..\Debug\resources\themes\default\global.xml">

View File

@ -188,6 +188,7 @@ int SerialPort::InitPort(UINT PortNo, UINT Baud, byte Parity, byte Data, byte St
return 1; return 1;
} }
int SerialPort::PortState(UINT PortNo) int SerialPort::PortState(UINT PortNo)
{ {
map<int, HANDLE>::iterator iter; map<int, HANDLE>::iterator iter;

View File

@ -5,6 +5,16 @@
using namespace std; using namespace std;
typedef struct{
wstring name;
uint32_t baurate;
uint8_t data_bits;
uint8_t stop_bits;
uint8_t verify;
uint8_t flow_control;
}UartInfo;
class SerialPort class SerialPort
{ {

View File

@ -1,10 +1,65 @@
#include "uart_process.h" #include "uart_process.h"
UartForm::UartForm()
UartForm::UartForm(std::wstring name,
uint32_t baurate,
uint8_t data_bits, uint8_t stop_bits,
uint8_t verify, uint8_t flow_control)
{ {
m_name = name;
m_baurate = baurate;
m_data_bits = data_bits;
m_stop_bits = stop_bits;
m_verify = verify;
m_flow_contro = flow_control;
}
std:: string wstring2string(std::wstring wstr)
{
std::string result;
//获取缓冲区大小,并申请空间,缓冲区大小事按字节计算的
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
char* buffer = new char[len + 1];
//宽字节编码转换成多字节编码
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
buffer[len] = '\0';
//删除缓冲区并返回值
result.append(buffer);
delete[] buffer;
return result;
} }
void UartForm::Init() 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));
auto mEditSend = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_send_edit"));
auto mBtnSend = static_cast<ui::Button*>(FindSubControl(L"btn_send_data"));
if (mBtnSend != nullptr) {
mBtnSend->AttachClick([this, mEditSend](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());
printf("%s\r\n", tmp.c_str());
SerialPort::WritePort(PortNum, tmp.c_str(), tmp.size());
return true;
});
}
} }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <string>
// C runtime header // C runtime header
#include <stdlib.h> #include <stdlib.h>
@ -9,15 +10,27 @@
// base header // base header
#include "base/base.h" #include "base/base.h"
#include "serial_port.h"
// duilib // duilib
#include "duilib/UIlib.h" #include "duilib/UIlib.h"
class UartForm : public ui::ChildBox class UartForm : public ui::ChildBox
{ {
public: public:
UartForm(); UartForm(std::wstring name, uint32_t baurate,
uint8_t data_bits,
uint8_t stop_bits,
uint8_t verify,
uint8_t flow_control);
/// 重写父类方法,提供个性化功能,请参考父类声明 /// 重写父类方法,提供个性化功能,请参考父类声明
virtual void Init() override; virtual void Init() override;
std::wstring m_name;
uint32_t m_baurate;
uint8_t m_data_bits;
uint8_t m_stop_bits;
uint8_t m_verify;
uint8_t m_flow_contro;
}; };