66 lines
1.7 KiB
C++
66 lines
1.7 KiB
C++
#include "uart_process.h"
|
|
|
|
|
|
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()
|
|
{
|
|
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;
|
|
});
|
|
}
|
|
|
|
}
|