no message

This commit is contained in:
zcy 2021-09-26 00:43:37 +08:00
parent 17cff75119
commit e0afe13a45
5 changed files with 78 additions and 29 deletions

View File

@ -4,7 +4,8 @@
const std::wstring BasicForm::kClassName = L"Basic";
BasicForm::BasicForm():
mMonitorNewSelect(nullptr)
mMonitorNewSelect(nullptr),
mRightShow(nullptr)
{
}
@ -31,12 +32,34 @@ std::wstring BasicForm::GetWindowClassName() const
LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_ADD_UART_CLOSE) {
wprintf(L"close %s\r\n", ((wstring*)wParam)->c_str());
mRightShow->SetVisible(false);
mRightSide->SetAutoDestroy(true);
mUartForm[*(wstring*)(wParam)] = nullptr;
int cnt = mMonitor->GetRootNode()->GetChildNode(0)->GetChildNodeCount();
ui::TreeNode* p = nullptr;
for (int i = 0; i < cnt; i++) {
if (mMonitor->GetRootNode()->GetChildNode(0)->GetChildNode(i)->GetText() == *(wstring*)(wParam)) {
p = mMonitor->GetRootNode()->GetChildNode(0)->GetChildNode(i);
break;
}
}
auto comname = *(wstring*)(wParam);
mMonitor->GetRootNode()->GetChildNode(0)->RemoveChildNode(p);
UINT PortNum = 0;
for (int i = 3; comname[i] != L'\0'; i++) //ת»»ÎªÊý×Ö
{
PortNum = PortNum * 10 + (comname[i] - L'0');
}
SerialPort::ClosePort(PortNum);
}
if (uMsg == WM_ADD_UART_RECVDATA) {
printf("WM_ADD_UART_RECVDATA");
this->Paint();
for (auto itr = mUartForm.begin(); itr != mUartForm.end(); itr++) {
itr->second->OnUpdateUart();
}
//this->Paint();
}
if (uMsg == WM_USER_POS_CHANGED) {
@ -57,17 +80,32 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
auto form = new UartForm(this,p->name, p->baurate,
p->data_bits, p->stop_bits, p->verify, p->flow_control);
form->SetChildLayoutXML(L"basic/uart_form.xml");
form->SetName(p->name);
form->SetVisible(false);
mUartForm[p->name] = form;
if (!mRightSide->Add(form))
printf("erroer 1");
}
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());
printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
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()]);
if (p != nullptr) {
p->SetAutoDestroy(true);
if (mRightShow != nullptr) {
mRightShow->SetVisible(false);
p->SetVisible(true);
mRightShow = p;
mRightSide->SelectItem(p->GetName());
}
else {
p->SetVisible(true);
mRightSide->SelectItem(p->GetName());
mRightShow = p;
}
}
}
return true;

View File

@ -55,5 +55,5 @@ private:
ui::TabBox *mRightSide;
ui::TreeView *mMonitor;
std::map<std::wstring,UartForm*> mUartForm;
ui::Control* mRightShow;
};

View File

@ -144,14 +144,8 @@ int SerialPort::InitPort(UINT PortNo, UINT Baud, byte Parity, byte Data, byte St
//int count = 0;
COMMTIMEOUTS ComTimeouts = { 10, 0, 0, 0, 0 };
/*
ComTimeouts.ReadIntervalTimeout = 0;
ComTimeouts.ReadTotalTimeoutConstant = 0;
ComTimeouts.ReadTotalTimeoutMultiplier = 0;
ComTimeouts.WriteTotalTimeoutConstant = 0;
ComTimeouts.WriteTotalTimeoutMultiplier = 0;
*/
COMMTIMEOUTS ComTimeouts = { 0, 0, 10, 0, 10 };
DCB S_dcb;
if (!SetCommTimeouts(H_Com, &ComTimeouts))//设置超时
{
@ -174,7 +168,7 @@ int SerialPort::InitPort(UINT PortNo, UINT Baud, byte Parity, byte Data, byte St
// return 0;
//}
S_dcb.fRtsControl = RTS_CONTROL_ENABLE; //允许Rts信号
S_dcb.fRtsControl = RTS_CONTROL_DISABLE; //允许Rts信号
if (!SetCommState(H_Com, &S_dcb)) //设置DCB
{
int i = GetLastError();
@ -211,7 +205,6 @@ int SerialPort::ReadPort(UINT PortNo, char* P_recved, int length) //
BOOL Result = TRUE;
Result = ReadFile(H_Com, P_recved, length, &Recn, NULL);
if (Result == 0)
{

View File

@ -6,6 +6,7 @@
using namespace std;
#define WM_ADD_UART_RECVDATA (WM_USER + 4)
#define WM_ADD_UART_CLOSE (WM_USER + 5)
typedef struct{
wstring name;

View File

@ -56,10 +56,13 @@ UartForm::UartForm(ui::Window* hwnd,std::wstring name,
if (0 < SerialPort::ReadPort(PortNum, recv, 1024)) {
printf("recv data: %s", recv);
this->m_show_recv += string2wstring(recv);
::PostMessage(this->GetWindow()->GetHWND(), WM_ADD_UART_RECVDATA, 0, 0);
if (this->mEditRecv != nullptr) {
this->mEditRecv->AppendText(string2wstring(recv), false);
::PostMessage(this->GetWindow()->GetHWND(), WM_ADD_UART_RECVDATA,0, 0);
}
}
else {
Sleep(30);
Sleep(300);
}
}
});
@ -70,17 +73,14 @@ UartForm::UartForm(ui::Window* hwnd,std::wstring name,
void UartForm::OnUpdateUart()
{
if(mEditRecv != nullptr)
this->mEditRecv->SetText(this->m_show_recv.c_str());
//if(mEditRecv != nullptr)
// this->mEditRecv->SetText(this->m_show_recv );
}
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 ",
@ -91,6 +91,13 @@ void UartForm::Init()
mEditSend = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_send_edit"));
mEditRecv = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_recv_eidt"));
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*) {
@ -107,6 +114,17 @@ void UartForm::Init()
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 UartForm::UpdateRecvEdit()
@ -116,11 +134,10 @@ void UartForm::UpdateRecvEdit()
void UartForm::HandleMessage(ui::EventArgs& msg)
{
printf("0x%x \r\n", msg.Type);
if (msg.Type == WM_ADD_UART_RECVDATA) {
printf("hello world\r\n");
this->mEditRecv->SetText(L"123");
}
this->mEditRecv->SetText(this->m_show_recv.c_str());
}