277 lines
7.8 KiB
C++
277 lines
7.8 KiB
C++
#include "new_monitor_form.h"
|
||
|
||
#include <atlstr.h>
|
||
#include <setupapi.h>
|
||
#include <vector>
|
||
#include <windows.h>
|
||
#include <iostream>
|
||
#include <winspool.h>
|
||
#include "serial_port.h"
|
||
#include "base_form.h"
|
||
|
||
using namespace std;
|
||
|
||
const std::wstring NewMonitorForm::kClassName = L"Basic";
|
||
|
||
|
||
vector<wstring> EnumPortsWdm()
|
||
{
|
||
std::vector<std::wstring> ret;
|
||
LPCTSTR Reg_Path = _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"); //串口注册表路径
|
||
//TCHAR pnum[8];
|
||
HKEY H_Key;
|
||
static UINT portlist[255]; //一般情况下255个数组够用了。
|
||
long Status;
|
||
UINT PortNum;
|
||
DWORD count = 0;//遍历键值计数。
|
||
DWORD Com_Length, Type_Length, Type;
|
||
TCHAR Com_Name[256];//存储串口名字
|
||
TCHAR Type_Name[256];//存储串口类型
|
||
Status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, Reg_Path, 0, KEY_READ, &H_Key); //打开注册表表键,返回long
|
||
memset(portlist, 0, sizeof(portlist)); // 清0
|
||
|
||
if (Status) //返回值为0表示打开成功,不为0则表示打开失败
|
||
{
|
||
return ret;
|
||
}
|
||
|
||
while (true)
|
||
{
|
||
Com_Length = 256; //存储两个长度,每次都要赋一个大一点的值,不然下次可能会失败,Com_length值翻倍
|
||
Type_Length = 256;
|
||
PortNum = 0;
|
||
Status = RegEnumValue(H_Key, count++, Type_Name, &Type_Length
|
||
, 0, &Type, PUCHAR(Com_Name), &Com_Length);
|
||
if (Status) //查询失败说明遍历结束,跳出循环。
|
||
{
|
||
break;
|
||
}
|
||
ret.push_back(Com_Name);
|
||
for (int i = 3; Com_Name[i]; i++) //转换为数字
|
||
{
|
||
PortNum = PortNum * 10 + (Com_Name[i] - '0');
|
||
}
|
||
|
||
portlist[count - 1] = PortNum;
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
|
||
NewMonitorForm::NewMonitorForm(ui::Window* parent)
|
||
{
|
||
m_parent = parent;
|
||
}
|
||
|
||
NewMonitorForm::~NewMonitorForm()
|
||
{
|
||
}
|
||
|
||
std::wstring NewMonitorForm::GetSkinFolder()
|
||
{
|
||
return L"basic";
|
||
}
|
||
|
||
std::wstring NewMonitorForm::GetSkinFile()
|
||
{
|
||
return L"newmonitor.xml";
|
||
}
|
||
|
||
std::wstring NewMonitorForm::GetWindowClassName() const
|
||
{
|
||
return kClassName;
|
||
}
|
||
|
||
LRESULT NewMonitorForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||
{
|
||
return WindowImplBase::HandleMessage(uMsg, wParam, lParam);
|
||
}
|
||
|
||
void NewMonitorForm::InitWindow()
|
||
{
|
||
ui::Button* btnclose = dynamic_cast<ui::Button*>(FindControl(L"exit_button"));
|
||
if (nullptr != btnclose) {
|
||
btnclose->SetText(L"退出");
|
||
btnclose->AttachClick([this](ui::EventArgs*) {
|
||
this->Close();
|
||
return true;
|
||
});
|
||
}
|
||
ui::Button* btn_add_new = dynamic_cast<ui::Button*>(FindControl(L"open_button"));
|
||
if (nullptr != btn_add_new) {
|
||
btn_add_new->AttachClick([this](ui::EventArgs*) {
|
||
if(m_combo_type->GetText() == L"uart"){
|
||
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 {
|
||
|
||
}
|
||
}
|
||
if (m_combo_type->GetText() == L"tcp_client") {
|
||
|
||
}
|
||
|
||
return true;
|
||
});
|
||
}
|
||
|
||
m_combo_type = dynamic_cast<ui::Combo*>(FindControl(L"combo_type"));
|
||
if (NULL != m_combo_type) {
|
||
ui::ListContainerElement* element = new ui::ListContainerElement;
|
||
element->SetClass(L"listitem");
|
||
element->SetFixedHeight(30);
|
||
element->SetBkColor(L"white");
|
||
element->SetTextPadding({ 6,0,6,0 });
|
||
element->SetText(nbase::StringPrintf(L""));
|
||
|
||
m_combo_type->Add(element);
|
||
|
||
element = new ui::ListContainerElement;
|
||
element->SetClass(L"listitem");
|
||
element->SetFixedHeight(30);
|
||
element->SetBkColor(L"white");
|
||
element->SetTextPadding({ 6,0,6,0 });
|
||
element->SetText(nbase::StringPrintf(L"uart"));
|
||
|
||
m_combo_type->Add(element);
|
||
|
||
element = new ui::ListContainerElement;
|
||
element->SetClass(L"listitem");
|
||
element->SetFixedHeight(30);
|
||
element->SetBkColor(L"white");
|
||
element->SetTextPadding({ 6,0,6,0 });
|
||
element->SetText(nbase::StringPrintf(L"tcp client"));
|
||
m_combo_type->Add(element);
|
||
|
||
element = new ui::ListContainerElement;
|
||
element->SetClass(L"listitem");
|
||
element->SetFixedHeight(30);
|
||
element->SetBkColor(L"white");
|
||
element->SetTextPadding({ 6,0,6,0 });
|
||
element->SetText(nbase::StringPrintf(L"tcp server"));
|
||
m_combo_type->Add(element);
|
||
|
||
element = new ui::ListContainerElement;
|
||
element->SetClass(L"listitem");
|
||
element->SetFixedHeight(30);
|
||
element->SetBkColor(L"white");
|
||
element->SetTextPadding({ 6,0,6,0 });
|
||
element->SetText(nbase::StringPrintf(L"udp client"));
|
||
m_combo_type->Add(element);
|
||
|
||
element = new ui::ListContainerElement;
|
||
element->SetClass(L"listitem");
|
||
element->SetFixedHeight(30);
|
||
element->SetBkColor(L"white");
|
||
element->SetTextPadding({ 6,0,6,0 });
|
||
element->SetText(nbase::StringPrintf(L"udp server"));
|
||
m_combo_type->Add(element);
|
||
}
|
||
m_ip_config_vbox = dynamic_cast<ui::VBox*>(FindControl(L"ip_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->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_ip_select = dynamic_cast<ui::RichEdit*>(FindControl(L"ip_edit"));
|
||
m_port_select= dynamic_cast<ui::RichEdit*>(FindControl(L"port_input"));
|
||
|
||
m_combo_type->AttachAllEvents([this](ui::EventArgs* ev) {
|
||
if (nullptr != ev) {
|
||
auto p = static_cast<ui::Combo*>(ev->pSender);
|
||
if (ev->Type == 42) {
|
||
auto text = p->GetText();
|
||
wprintf(L"%s\r\n", text.c_str());
|
||
|
||
if (text == L"uart") {
|
||
this->m_ip_config_vbox->SetVisible(false);
|
||
this->m_uart_config_vbox->SetVisible(true);
|
||
auto ports = EnumPortsWdm();
|
||
this->m_uart_select_combo->RemoveAll();
|
||
for (auto x : ports) {
|
||
ui::ListContainerElement* element = new ui::ListContainerElement;
|
||
element->SetClass(L"listitem");
|
||
element->SetFixedHeight(30);
|
||
element->SetBkColor(L"white");
|
||
element->SetTextPadding({ 6,0,6,0 });
|
||
element->SetText(nbase::StringPrintf(L"%s", &x.c_str()[x.find(L"COM")]));
|
||
element->SetDataID(nbase::StringPrintf(L"%s", x.c_str()));
|
||
this->m_uart_select_combo->Add(element);
|
||
}
|
||
vector<wstring> baurate;
|
||
baurate.push_back(L"110");
|
||
baurate.push_back(L"300");
|
||
baurate.push_back(L"600");
|
||
baurate.push_back(L"1200");
|
||
baurate.push_back(L"2400");
|
||
baurate.push_back(L"4800");
|
||
baurate.push_back(L"9600");
|
||
baurate.push_back(L"14400");
|
||
baurate.push_back(L"19200");
|
||
baurate.push_back(L"38400");
|
||
baurate.push_back(L"56000");
|
||
baurate.push_back(L"57600");
|
||
baurate.push_back(L"115200");
|
||
|
||
baurate.push_back(L"128000");
|
||
baurate.push_back(L"256000");
|
||
|
||
m_uart_baurate_select->RemoveAll();
|
||
for (auto baus : baurate) {
|
||
ui::ListContainerElement* element = new ui::ListContainerElement;
|
||
element->SetClass(L"listitem");
|
||
element->SetFixedHeight(30);
|
||
element->SetBkColor(L"white");
|
||
element->SetTextPadding({ 6,0,6,0 });
|
||
element->SetText(nbase::StringPrintf(L"%s", baus.c_str()));
|
||
this->m_uart_baurate_select->Add(element);
|
||
}
|
||
|
||
}
|
||
if ((text == L"tcp client")|| (text == L"tcp server") ||
|
||
(text == L"udp client") || (text == L"udp server")) {
|
||
this->m_ip_config_vbox->SetVisible(true);
|
||
this->m_uart_config_vbox->SetVisible(false);
|
||
|
||
m_ip_select->SetText(L"127.0.0.1");
|
||
m_port_select->SetText(L"9001");
|
||
}
|
||
}
|
||
}
|
||
return true;
|
||
});
|
||
}
|
||
|
||
LRESULT NewMonitorForm::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
|
||
{
|
||
return __super::OnClose(uMsg, wParam, lParam, bHandled);
|
||
}
|