comtestdlg.cpp
来自「刚学RS232时,仿串口测试工具界面,做的一个串口测试小软件.」· C++ 代码 · 共 661 行 · 第 1/2 页
CPP
661 行
DWORD byetSize[] = { 8, 7, 6, };
DWORD parity[] = { NOPARITY, ODDPARITY, EVENPARITY, };
TCHAR* szParity[] = { _T("N"), _T("O"), _T("E"), };
DWORD stopBits[] = { ONESTOPBIT, TWOSTOPBITS, };
// 设置串口.
if ( !m_com.SetState(baudRate[m_baudRate.GetCurSel()],
byetSize[m_byetSize.GetCurSel()],
parity[m_parity.GetCurSel()],
stopBits[m_stopBits.GetCurSel()]) )
{
::AfxMessageBox( _T(" 串口设置失败") );
return;
}
m_portSet.SetWindowText( CLOSEPORT ); // 改变按纽控件名.
// 重置状态栏.
CString str = _T("STATUS: ");
str += s[m_port.GetCurSel()];
str += _T(" OPENED, ");
CString strTemp;
strTemp.Format( _T("%d, "), baudRate[m_baudRate.GetCurSel()]);
str += strTemp;
str += szParity[m_parity.GetCurSel()];
strTemp.Format( _T(", %d, "), byetSize[m_byetSize.GetCurSel()]);
str += strTemp;
strTemp.Format( _T("%d"), stopBits[m_stopBits.GetCurSel()]);
str += strTemp; // make "STATUS: COM1 OPENED, 9600, N, 8, 1"
m_StatusBar.SetText(str, 0, 0);
m_StatusBar.SetText(_T("RX: 0"), 1, 0);
m_StatusBar.SetText(_T("TX: 0"), 2, 0);
}
}
void CComTestDlg::OnButtonClear()
{
// TODO: Add your control notification handler code here
m_in.SetSel(0, -1);
m_in.Clear();
// UpdateData(FALSE);
}
void CComTestDlg::OnButtonSave()
{
// TODO: Add your control notification handler code here
CString saveText;
m_in.GetWindowText(saveText);
// 保存文件.
HANDLE hFile; // 查找第一个文件.
WIN32_FIND_DATA findFileData;
CString path = _T("");
if ( (hFile = ::FindFirstFile(m_savePath + SUFFIX, &findFileData)) == INVALID_HANDLE_VALUE )
{ // 空文件夹.
path = m_savePath + _T("\\Rec00.txt");
}
else
{
do
{ // 得到文件名.
path = findFileData.cFileName;
}while (::FindNextFile(hFile, &findFileData)); // 查找下一个文件.
}
::FindClose(hFile);
try
{ // 生成文件并写入数据.
path.ReleaseBuffer(path.GetLength()-4); // Delete .txt
int nCount = ::atoi(path.Mid(3));
path.Format("\\Rec%d.txt", ++nCount);
path = m_savePath + path;
CFile mFile(path, CFile::modeCreate | CFile::modeWrite);
CArchive ar(&mFile, CArchive::store);
CTime time = CTime::GetCurrentTime();
ar.WriteString(time.Format("%A, %B %d, %Y") + _T("\r\n\r\n") + saveText);
ar.Close();
mFile.Close();
}
catch(CFileException *e)
{
if (e->m_cause == CFileException::badPath)
::AfxMessageBox( _T("全部或部分路径无效.\n") );
else if (e->m_cause == CFileException::invalidFile)
::AfxMessageBox( _T("想使用一个无效的文件句柄.\n") );
else if (e->m_cause == CFileException::hardIO)
::AfxMessageBox( _T("硬件错误.\n") );
else if (e->m_cause == CFileException::diskFull)
::AfxMessageBox( _T("磁盘已满.\n") );
else
::AfxMessageBox( _T("发生未指定错误.\n") );
e->Delete();
return;
}
::AfxMessageBox( _T(path + "已保存!\n") );
}
void CComTestDlg::OnButtonSavepath()
{
// TODO: Add your control notification handler code here
HWND hParent = GetActiveWindow()->GetSafeHwnd(); // 得到一个句柄.
SelectFolder(hParent, m_savePath); // 打开文件夹选择对话框.
if ( m_savePath.IsEmpty() ) // 检查是否为空字串.
{
::AfxMessageBox( _T("请选择输出文件夹.\n") );
return;
}
UpdateData(FALSE);
}
BOOL CComTestDlg::SelectFolder(HWND hParent, CString &strFolder)
{
ASSERT(hParent != NULL);
LPTSTR pStrFolder = strFolder.GetBuffer(_MAX_PATH);
BROWSEINFO browseInfo;
// 初始化 browseInfo.
browseInfo.hwndOwner = hParent; // 父窗口句柄.
browseInfo.pidlRoot = NULL; // 设置初始路径为桌面.
browseInfo.pszDisplayName = NULL; // 当前用户选择的文件夹.
browseInfo.lpszTitle = _T("选择文件夹"); // 对话框的标题.
// 对话框的风格.
browseInfo.ulFlags = BIF_RETURNFSANCESTORS|BIF_RETURNONLYFSDIRS;
browseInfo.lpfn = NULL; // 回调函数为空.
browseInfo.lParam = 0; // 此项用于文件夹图标
LPITEMIDLIST lpItemIDList;
if ((lpItemIDList = ::SHBrowseForFolder(&browseInfo)) != NULL)
{
// 从 lpItemIDList 中取得文件夹路径.
if (::SHGetPathFromIDList(lpItemIDList, pStrFolder))
{
// 函数成功返回, 测试 pStrFolder 是否为空字串.
if (pStrFolder[0] == _T('\0'))
return FALSE;
// 释放多余的内存,并返回.
strFolder.ReleaseBuffer(-1);
return TRUE;
}
}
return FALSE;
}
void CComTestDlg::OnButtonClearout()
{
// TODO: Add your control notification handler code here
m_out.Empty();
UpdateData();
}
void CComTestDlg::OnCheckAutosend()
{
// TODO: Add your control notification handler code here
UpdateData();
if (m_autoSend)
{
// 启动线程.
if (::ResumeThread(m_hThread) == -1)
{
::AfxMessageBox( _T("线程启动失败.\n") );
return;
}
}
else
{ // 挂起线程.
if (::SuspendThread(m_hThread) == -1)
{
::AfxMessageBox( _T("线程挂起失败.\n") );
return;
}
}
}
void ThreadFunc(LPVOID lpParam)
{
CComTestDlg* pDlg = (CComTestDlg *)lpParam;
while(pDlg->m_autoSend)
{
Sleep(pDlg->m_cycle); // 延时time.
if (pDlg->m_out.IsEmpty()) // 判断是发送内容是否为空.
continue;
pDlg->m_com.Write(pDlg->m_out);
if (pDlg->m_16out)
{
int len = pDlg->m_out.GetLength()/2;
UCHAR* pstr = new UCHAR[len+1];
pDlg->HexStr2CharStr(pDlg->m_out, pstr, len);
pstr[len] = '\0';
pDlg->m_com.Write(pstr, len);
pDlg->m_TxCount += len;
delete[] pstr;
}
else
{
pDlg->m_TxCount += pDlg->m_out.GetLength();
pDlg->m_com.Write(pDlg->m_out); // 发送数据.
}
CString str;
str.Format("%d", pDlg->m_TxCount);
pDlg->m_StatusBar.SetText( _T("TX: ") + str, 2, 0);
}
}
void CComTestDlg::OnChangeEditOut()
{
// TODO: Add your control notification handler code here
UpdateData(); // 实现同步更新.
}
void CComTestDlg::OnChangeEditCycle()
{
// TODO: Add your control notification handler code here
UpdateData(); // 实现同步更新.
}
void CComTestDlg::OnButtonSend()
{
// TODO: Add your control notification handler code here
if (m_out.IsEmpty()) // 判断是发送内容是否为空.
return;
if (m_16out)
{
int len = m_out.GetLength()/2;
UCHAR* pstr = new UCHAR[len+1];
HexStr2CharStr(m_out, pstr, len);
pstr[len] = '\0';
m_com.Write(pstr, len);
m_TxCount += len;
delete[] pstr;
}
else
{
m_TxCount += m_out.GetLength();
m_com.Write(m_out); // 发送数据.
}
CString str;
str.Format("%d", m_TxCount);
m_StatusBar.SetText( _T("TX: ") + str, 2, 0);
}
void CComTestDlg::OnButtonSelect()
{
// TODO: Add your control notification handler code here
CFileDialog dlg (TRUE, // 创建一个打开文件对话框.
_T("txt"), // 默认方件的后缀名
NULL, // 默认文件名
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, // // 对话框的风格.
_T("文本文档 (*.txt)|*.txt|所有文件 (*.*)|*.*||") );//文件过滤器.
if (dlg.DoModal() == IDOK) // 生成一个打开文件对话框.
{
m_sendPath.SetWindowText( dlg.GetPathName() ); // 使文件路径名在编辑框中显示.
}
}
void CComTestDlg::OnButtonSendfile()
{
// TODO: Add your control notification handler code here
try
{
CString path;
m_sendPath.GetWindowText(path);
CFile mFile(path, CFile::modeRead);
DWORD size = mFile.GetLength(); // 取得文件长度.
TCHAR* buffer = new TCHAR[size+1]; // 在堆中申请字串空间.
size = mFile.ReadHuge(buffer, size); // 读取文件.
if (!size)
{
::AfxMessageBox( _T("文件为空文件.\n") );
return;
}
mFile.Close();
buffer[size + 1] = _T('\0'); // 返回文件字数.
m_com.Write(buffer, size);
delete[] buffer;
::AfxMessageBox( _T("文件发送成功.\n") );
}
catch(CFileException *e)
{
if (e->m_cause == CFileException::badPath)
::AfxMessageBox( _T("全部或部分路径无效.\n") );
else if (e->m_cause == CFileException::invalidFile)
::AfxMessageBox( _T("想使用一个无效的文件句柄.\n") );
else if (e->m_cause == CFileException::hardIO)
::AfxMessageBox( _T("硬件错误.\n") );
else
::AfxMessageBox( _T("发生未指定错误.\n") );
e->Delete();
}
}
void CComTestDlg::OnButtonReset()
{
// TODO: Add your control notification handler code here
// 重置状态栏.
m_RxCount = 0;
m_TxCount = 0;
m_StatusBar.SetText("RX: 0", 1, 0);
m_StatusBar.SetText("TX: 0", 2, 0);
}
void CComTestDlg::Hex2Char(char const* szHex, UCHAR& rch)
{
rch = 0;
for(int i=0; i<2; i++)
{
if(*(szHex + i) >='0' && *(szHex + i) <= '9')
rch = (rch << 4) + (*(szHex + i) - '0');
else if(*(szHex + i) >='A' && *(szHex + i) <= 'F')
rch = (rch << 4) + (*(szHex + i) - 'A' + 10);
else
break;
}
}
void CComTestDlg::HexStr2CharStr(char const* pszHexStr, UCHAR* pucCharStr, int iSize)
{
int i;
UCHAR ch;
for(i=0; i<iSize; i++)
{
Hex2Char(pszHexStr+2*i, ch);
pucCharStr[i] = ch;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?