2021-09-20 22:37:32 +08:00
|
|
|
|
#include "uart_process.h"
|
|
|
|
|
|
2021-09-23 00:54:09 +08:00
|
|
|
|
wstring string2wstring(string str)
|
|
|
|
|
{
|
|
|
|
|
wstring result;
|
|
|
|
|
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ռ䣬<D5BC><E4A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
|
|
|
|
|
TCHAR* buffer = new TCHAR[len + 1];
|
|
|
|
|
//<2F><><EFBFBD>ֽڱ<D6BD><DAB1><EFBFBD>ת<EFBFBD><D7AA><EFBFBD>ɿ<EFBFBD><C9BF>ֽڱ<D6BD><DAB1><EFBFBD>
|
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
|
|
|
|
|
buffer[len] = '\0'; //<2F><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>β
|
|
|
|
|
//ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
result.append(buffer);
|
|
|
|
|
delete[] buffer;
|
|
|
|
|
return result;
|
2021-09-22 00:01:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-23 00:54:09 +08:00
|
|
|
|
std::string wstring2string(std::wstring wstr)
|
2021-09-20 22:37:32 +08:00
|
|
|
|
{
|
2021-09-22 00:01:02 +08:00
|
|
|
|
std::string result;
|
|
|
|
|
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ռ䣬<D5BC><E4A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>°<EFBFBD><C2B0>ֽڼ<D6BD><DABC><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
|
|
|
|
|
char* buffer = new char[len + 1];
|
|
|
|
|
//<2F><><EFBFBD>ֽڱ<D6BD><DAB1><EFBFBD>ת<EFBFBD><D7AA><EFBFBD>ɶ<EFBFBD><C9B6>ֽڱ<D6BD><DAB1><EFBFBD>
|
|
|
|
|
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
|
|
|
|
|
buffer[len] = '\0';
|
|
|
|
|
//ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
result.append(buffer);
|
|
|
|
|
delete[] buffer;
|
|
|
|
|
return result;
|
2021-09-20 22:37:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-24 00:38:05 +08:00
|
|
|
|
UartForm::UartForm(ui::Window* hwnd,std::wstring name,
|
2021-09-23 00:54:09 +08:00
|
|
|
|
uint32_t baurate,
|
|
|
|
|
uint8_t data_bits, uint8_t stop_bits,
|
|
|
|
|
uint8_t verify, uint8_t flow_control):
|
2021-09-24 00:38:05 +08:00
|
|
|
|
m_thread_recv(nullptr),
|
|
|
|
|
mEditRecv(nullptr)
|
2021-09-23 00:54:09 +08:00
|
|
|
|
{
|
|
|
|
|
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++) //ת<><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
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);
|
2021-09-24 00:38:05 +08:00
|
|
|
|
this->m_show_recv += string2wstring(recv);
|
2021-09-26 00:43:37 +08:00
|
|
|
|
if (this->mEditRecv != nullptr) {
|
|
|
|
|
this->mEditRecv->AppendText(string2wstring(recv), false);
|
|
|
|
|
::PostMessage(this->GetWindow()->GetHWND(), WM_ADD_UART_RECVDATA,0, 0);
|
|
|
|
|
}
|
2021-09-23 00:54:09 +08:00
|
|
|
|
}
|
|
|
|
|
else {
|
2021-09-26 00:43:37 +08:00
|
|
|
|
Sleep(300);
|
2021-09-23 00:54:09 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-09-24 00:38:05 +08:00
|
|
|
|
if (nullptr != hwnd) {
|
|
|
|
|
this->SetWindow(hwnd, nullptr, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UartForm::OnUpdateUart()
|
|
|
|
|
{
|
2021-09-26 00:43:37 +08:00
|
|
|
|
//if(mEditRecv != nullptr)
|
|
|
|
|
// this->mEditRecv->SetText(this->m_show_recv );
|
2021-09-24 00:38:05 +08:00
|
|
|
|
|
2021-09-23 00:54:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-20 22:37:32 +08:00
|
|
|
|
void UartForm::Init()
|
|
|
|
|
{
|
2021-09-22 00:01:02 +08:00
|
|
|
|
ui::ChildBox::Init();
|
|
|
|
|
auto mRightSide = dynamic_cast<ui::Label*> (FindSubControl(L"uart_info_label"));
|
|
|
|
|
wchar_t p[100] = { 0 };
|
|
|
|
|
wsprintf(p,L"<EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD>: %s <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>%d <20><><EFBFBD><EFBFBD>λ: %d ֹͣλ: %d ",
|
|
|
|
|
m_name.c_str(),m_baurate,
|
|
|
|
|
m_data_bits,m_stop_bits);
|
|
|
|
|
mRightSide->SetText(std::wstring(p));
|
|
|
|
|
|
2021-09-23 00:54:09 +08:00
|
|
|
|
mEditSend = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_send_edit"));
|
|
|
|
|
mEditRecv = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_recv_eidt"));
|
|
|
|
|
mEditRecv->SetReadOnly(true);
|
2021-09-26 00:43:37 +08:00
|
|
|
|
mEditRecv->SetRich(false);
|
|
|
|
|
mEditRecv->SetAttribute(L"autovscroll", L"true");
|
|
|
|
|
mEditRecv->SetAttribute(L"multiline", L"true");
|
|
|
|
|
mEditRecv->SetReturnMsgWantCtrl(true);
|
|
|
|
|
mEditRecv->SetNeedReturnMsg(true);
|
|
|
|
|
mEditRecv->SetWordWrap(true);
|
|
|
|
|
|
2021-09-22 00:01:02 +08:00
|
|
|
|
auto mBtnSend = static_cast<ui::Button*>(FindSubControl(L"btn_send_data"));
|
|
|
|
|
if (mBtnSend != nullptr) {
|
2021-09-23 00:54:09 +08:00
|
|
|
|
mBtnSend->AttachClick([this](ui::EventArgs*) {
|
2021-09-22 00:01:02 +08:00
|
|
|
|
UINT PortNum = 0;
|
|
|
|
|
for (int i = 3; m_name[i] != '\0'; i++) //ת<><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-09-26 00:43:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-09-23 00:54:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
2021-09-20 22:37:32 +08:00
|
|
|
|
|
|
|
|
|
}
|
2021-09-26 00:43:37 +08:00
|
|
|
|
|