117 lines
2.9 KiB
C++
117 lines
2.9 KiB
C++
#include "uart_process.h"
|
|
#include "utils.h"
|
|
#include "msgdef.h"
|
|
#include "utils.h"
|
|
|
|
UartForm::UartForm(ui::Window* hwnd,std::wstring name,
|
|
uint32_t baurate,
|
|
uint8_t data_bits, uint8_t stop_bits,
|
|
uint8_t verify, uint8_t flow_control):
|
|
m_thread_recv(nullptr),
|
|
mEditRecv(nullptr)
|
|
{
|
|
m_name = name;
|
|
m_baurate = baurate;
|
|
m_data_bits = data_bits;
|
|
m_stop_bits = stop_bits;
|
|
m_verify = verify;
|
|
m_flow_contro = flow_control;
|
|
|
|
m_runing = true;
|
|
m_thread_recv = new std::thread([this]() {
|
|
UINT PortNum = 0;
|
|
for (int i = 3; m_name[i] != '\0'; i++) //转换为数字
|
|
{
|
|
PortNum = PortNum * 10 + (m_name[i] - '0');
|
|
}
|
|
char recv[1024] = {0};
|
|
while (this->m_runing) {
|
|
if (0 < SerialPort::ReadPort(PortNum, recv, 1024)) {
|
|
printf("recv data: %s", recv);
|
|
this->m_show_recv += string2wstring(recv);
|
|
if (this->mEditRecv != nullptr) {
|
|
this->mEditRecv->AppendText(string2wstring(recv), false);
|
|
::PostMessage(this->GetWindow()->GetHWND(), WM_ADD_UART_RECVDATA,0, 0);
|
|
}
|
|
}
|
|
else {
|
|
Sleep(300);
|
|
}
|
|
}
|
|
});
|
|
if (nullptr != hwnd) {
|
|
this->SetWindow(hwnd, nullptr, false);
|
|
}
|
|
}
|
|
|
|
void UartForm::OnUpdateUart()
|
|
{
|
|
//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 ",
|
|
m_name.c_str(),m_baurate,
|
|
m_data_bits,m_stop_bits);
|
|
mRightSide->SetText(std::wstring(p));
|
|
|
|
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*) {
|
|
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());
|
|
SerialPort::WritePort(PortNum, tmp.c_str(), tmp.size());
|
|
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()
|
|
{
|
|
|
|
}
|
|
|
|
void UartForm::HandleMessage(ui::EventArgs& msg)
|
|
{
|
|
if (msg.Type == WM_ADD_UART_RECVDATA) {
|
|
printf("hello world\r\n");
|
|
this->mEditRecv->SetText(L"123");
|
|
}
|
|
|
|
}
|
|
|