📄 testserialport2dlg.cpp
字号:
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTestSerialPort2Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTestSerialPort2Dlg::OnOK()
{
}
void CTestSerialPort2Dlg::OnButtonOpen()
{
UpdateData();
if (m_pSSP->Open(m_comName.GetBuffer(m_comName.GetLength()),
m_uComRate,
m_uComParity,
m_uComDataBits,
m_fComStopBits))
{
m_btnOpen.EnableWindow(FALSE);
m_btnReceive.EnableWindow();
GetDlgItem(IDC_CHECK_KEEP_SEND)->EnableWindow();
GetDlgItem(IDC_CHECK_KEEP_SAVE)->EnableWindow();
GetDlgItem(IDC_CHECK_USE_CAL)->EnableWindow();
OnButtonReceive();
SetTimer(emTIMER_TRANSFER_CAL, 1000, NULL);
}
else // 打开失败,获取错误信息
{
ShowErrorInfo();
}
}
void CTestSerialPort2Dlg::OnButtonClose()
{
m_pSSP->Close();
m_btnOpen.EnableWindow();
m_btnReceive.EnableWindow(FALSE);
GetDlgItem(IDC_CHECK_KEEP_SEND)->EnableWindow(FALSE);
GetDlgItem(IDC_CHECK_KEEP_SAVE)->EnableWindow(FALSE);
GetDlgItem(IDC_CHECK_USE_CAL)->EnableWindow(FALSE);
KillTimer(emTIMER_SEND);
KillTimer(emTIMER_SAVE);
KillTimer(emTIMER_USE_CAL);
KillTimer(emTIMER_TRANSFER_CAL);
}
void CTestSerialPort2Dlg::OnButtonReceive()
{
if (m_pSSP->StartReceive(m_pSSP))
{
m_btnReceive.EnableWindow(FALSE);
}
else
{
ShowErrorInfo();
}
}
void CTestSerialPort2Dlg::OnButtonSend()
{
UpdateData();
PACKAGE pack;
pack.iLen = m_strSend.GetLength();
strcpy((char *)pack.pData,
m_strSend.GetBuffer(m_strSend.GetLength()));
// 发送包
if (m_pSSP->SendData(&pack))
{
pack.Clear();
}
else
{
ShowErrorInfo();
}
}
void CTestSerialPort2Dlg::OnClose()
{
if (m_pSSP->IsOpen())
{
m_pSSP->Close();
}
KillTimer(emTIMER_SEND);
KillTimer(emTIMER_SAVE);
KillTimer(emTIMER_USE_CAL);
CDialog::OnClose();
}
void CTestSerialPort2Dlg::OnRadioHex()
{
m_ctrlEm.bShowAsHex = TRUE;
}
void CTestSerialPort2Dlg::OnRadioText()
{
m_ctrlEm.bShowAsHex = FALSE;
}
void CTestSerialPort2Dlg::OnCheckKeepSend()
{
UpdateData();
m_ctrlEm.bKeepSend = m_bKeepSend;
m_ctrlEm.dwKeepSendTime = m_uKeepSendTime;
if (m_bKeepSend)
{
if (m_uKeepSendTime > 49)
{
SetTimer(emTIMER_SEND, m_ctrlEm.dwKeepSendTime, NULL);
}
else
{
AfxMessageBox(_T("定时发送的时间间隔太小"));
}
}
else
{
KillTimer(emTIMER_SEND);
}
}
void CTestSerialPort2Dlg::OnCheckKeepSave()
{
UpdateData();
m_ctrlEm.bKeepSave = m_bKeepSave;
m_ctrlEm.dwKeepSaveTime = m_uKeepSaveTime;
if (m_bKeepSave)
{
if (m_uKeepSaveTime > 9999)
{
SetTimer(emTIMER_SAVE, m_ctrlEm.dwKeepSaveTime, NULL);
}
else
{
AfxMessageBox(_T("定时保存的时间间隔太小"));
}
}
else
{
KillTimer(emTIMER_SAVE);
}
}
void CTestSerialPort2Dlg::OnCheckUseCal()
{
UpdateData();
m_ctrlEm.bUseCal = m_bUseCal;
if (m_bUseCal)
{
SetTimer(emTIMER_USE_CAL, 500, NULL);
}
else
{
KillTimer(emTIMER_USE_CAL);
}
}
void CTestSerialPort2Dlg::OnTimer(UINT nIDEvent)
{
switch(nIDEvent)
{
case emTIMER_SAVE:
SaveToLog(); // 保存已经接收到的字符到文本
break;
case emTIMER_SEND:
OnButtonSend();
break;
case emTIMER_USE_CAL:
// 为了在刷新请设置值有效
SetDlgItemInt(IDC_STATIC_RECEIVE_BYTES, m_pSSP->GetReadedBytes());
SetDlgItemInt(IDC_STATIC_SEND_BYTES, m_pSSP->GetWritedBytes());
break;
case emTIMER_TRANSFER_CAL:
{
UINT curRecByte = m_pSSP->GetReadedBytes();
UINT iLen = curRecByte - m_preRecByte;
CString transferSpeed;
transferSpeed.Format(_T("%.2f KB/S"), iLen/1024.0f);
SetDlgItemText(IDC_STATIC_TRANSFER_SPEED, transferSpeed);
m_preRecByte = curRecByte;
}
break;
default:
break;
}
CDialog::OnTimer(nIDEvent);
}
void CTestSerialPort2Dlg::OnChangeEditKeepTime()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
//
UpdateData();
if (m_uKeepSendTime < 50)
{
m_uKeepSendTime = 50;
}
UpdateData(false);
}
void CTestSerialPort2Dlg::OnChangeEditSaveTime()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
//
UpdateData();
if (m_uKeepSaveTime < 10000)
{
m_uKeepSaveTime = 10000;
}
UpdateData(false);
}
void CTestSerialPort2Dlg::OnButtonModemState()
{
CRect thisRect;
this->GetWindowRect(thisRect);
CRect mdRect;
m_pModemStatusDlg->GetWindowRect(mdRect);
m_pModemStatusDlg->MoveWindow(thisRect.right,
thisRect.top,
mdRect.Width(),
mdRect.Height(),
true);
m_pModemStatusDlg->ShowWindow(SW_SHOW);
}
void CTestSerialPort2Dlg::OnButtonClearReceive()
{
m_mdcReceive.SetText("");
m_storeText.Empty();
}
void CTestSerialPort2Dlg::OnButtonFileTransfer()
{
if (!m_pSSP->IsOpen())
{
AfxMessageBox("串口没打开,请先打开串口");
return ;
}
// 文件传输
CFileTransferDlg* ftDlg = new CFileTransferDlg();
ftDlg->pSSP = m_pSSP;
ftDlg->IsReceiveDlg = FALSE;
ftDlg->Create(IDD_DIALOG_FILE_TRANSFER, this);
ftDlg->SetWindowText("文件发送窗口");
ftDlg->ShowWindow(SW_SHOW);
}
void CTestSerialPort2Dlg::OnButtonFileReceive()
{
if (!m_pSSP->IsOpen())
{
AfxMessageBox("串口没打开,请先打开串口");
return ;
}
CFileTransferDlg* ftDlg = new CFileTransferDlg();
ftDlg->pSSP = m_pSSP;
ftDlg->IsReceiveDlg = TRUE;
ftDlg->Create(IDD_DIALOG_FILE_TRANSFER, this);
ftDlg->SetWindowText("文件接收窗口");
ftDlg->ShowWindow(SW_SHOW);
}
void CTestSerialPort2Dlg::OnButtonClearSend()
{
m_strSend.Empty();
UpdateData(false);
}
//////////////////////////////////////////////////////////////////////////
// 主菜单函数
void CTestSerialPort2Dlg::OnToolSendfile()
{
OnButtonFileTransfer();
}
void CTestSerialPort2Dlg::OnToolReceivefile()
{
OnButtonFileReceive();
}
void CTestSerialPort2Dlg::OnToolModemcheck()
{
OnButtonModemState();
}
void CTestSerialPort2Dlg::OnHelpVersion()
{
CAboutDlg adlg;
adlg.DoModal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -