⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scommdlg.cpp

📁 CSerialPort类做的串口通信编程
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	strTemp.Format("%ld",rxdatacount);
	strTemp="接收:"+strTemp;
	m_ctrlRXCOUNT.SetWindowText(strTemp);
	TX_count=0;
	strTemp.Format("%ld",TX_count);
	strTemp="发送:"+strTemp;
	m_ctrlTXCount.SetWindowText(strTemp);
}


void CSCOMMDlg::OnButtonClose() 
{
	// TODO: Add your control notification handler code here

	//CString str;
	//str.Format("RoaringWindSoft You can use all the functions of the SComAssistant,But if you registered:\r\nNo popup Message and No marks of RoaringWindStudio\r\nFree Updates(Send to you Automaticly by Email)\r\n Thank You");
	//AfxMessageBox(str);

	CSCOMMDlg::OnOK();
}


//选择要发送的文件 (文件打开对话框)
void CSCOMMDlg::OnButtonFilebrowser()
{
	// TODO: Add your control notification handler code here
	CString lpszPath;

	//UpdateData(TRUE);   //将控件的值传给对应的成员变量

	lpszPath.Empty();
	CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT_SENDFILE);
	pEdit->GetWindowText(lpszPath);
	
#ifdef Debug1
	MessageBox(lpszPath, "信息提示", MB_OK);
#endif
	//return;

	//if (m_strSendFilePathName == "还没有选择文件")
	if (lpszPath == "还没有选择文件")
	{
		//lpszPath = "c:\\comdata";
		lpszPath.Empty();
		lpszPath = "c:\\";

		//改变应用程序的当前路径
		SetCurrentDirectory(lpszPath);
	}

	//文件打开对话框
	//static char BASED_CODE szFilter[] = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*||";
	//设置文件对话框过滤器
	CString StrFilter = "文本文档(*.txt)|*.txt|所有文件(*.*)|*.*||";

	CFileDialog FileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, StrFilter, this);
	
	//设置文件对话框打开时的目录位置
	FileDlg.m_pOFN->lpstrInitialDir = lpszPath;

	if (FileDlg.DoModal() == IDOK)
	{

		//CString strFileName = FileDlg.GetFileName();
		//CString strFileExt = FileDlg.GetFileExt();
		//清除 接收编辑框 清屏
		CEdit *pEdit1 = (CEdit *)GetDlgItem(IDC_EDIT_RECIVE);
		pEdit1->SetSel(0, -1);   //选择所有文本
		pEdit1->ReplaceSel("");
		//m_ReceiveData.Empty();
		//UpdateData(FALSE);

		m_strSendFilePathName = FileDlg.GetPathName();   //获得选中文件的完整路径
		CString str;
#ifdef Debug2
		int nStartChar, nEndChar, row, column;
		while (1)
		{
			pEdit1->GetSel(nStartChar, nEndChar);
			row = pEdit1->LineFromChar(nStartChar);
			column = nStartChar - pEdit1->LineIndex(row);
			str.Empty();
			str.Format("nStartChar=%d, nEndChar=%d\nrow=%d, column=%d", nStartChar, nEndChar, row, column);
			MessageBox(str, "信息提示", MB_OKCANCEL);
		    
			//pEdit1->SetSel(5, 5);
			pEdit1->ReplaceSel("1\x0d\x0a\x32\x0d\x0a");
			pEdit1->ReplaceSel("3\x0d\x0a");

			//pEdit1->SetSel(0, -1);   //选取所有文本, 为的是替换
			//pEdit1->SetSel(6, 7);
			//pEdit1->ReplaceSel("abcdefg");
		}
		pEdit1->SetWindowText("");
		MessageBox(m_strSendFilePathName, "信息提示", MB_OK);
#endif
		
		CFile MyFile;
		BOOL bret;

		//打开文件
		bret = MyFile.Open((LPCTSTR)m_strSendFilePathName, CFile::modeRead, NULL);
		if (bret == 0)   //打开失败
		{
			AfxMessageBox("Open file failed!");
			return;
		}

		//获取文件总长度
		ULONGLONG fplength = MyFile.GetLength();

#ifdef Debug3
		str.Empty();
		str.Format("fplength=%lu", fplength);
		MessageBox(str, "信息提示", MB_OK);
#endif

		char *fpBuff;
		fpBuff = new char[fplength+1];   //创建(fplength+1) 这么多的空间
		//char fpBuff[1024];
		//memset(fpBuff, 0, sizeof(fpBuff));

		//定位文件指针到文件开始
		MyFile.SeekToBegin();
		
		//读文件中的内容, 到fpBuff数组中
		UINT ret;
		ret = MyFile.Read(fpBuff, fplength);
		
#ifdef Debug4
		str.Empty();
		str.Format("ret=%u", ret);
		MessageBox(str, "信息提示", MB_OK);
#endif

		if (ret != fplength)
		{
			MyFile.Close();
			return;
		}
		MyFile.Close();
		
		//将从文件中读取到的内容, 显示到接收编辑框中, 并组织一些数据
		//m_ReceiveData.Empty();
		//memcpy(m_ReceiveData.GetBuffer(fplength), fpBuff, fplength);
		//m_ReceiveData.ReleaseBuffer();
		//UpdateData(FALSE);   //将成员变量的值传给对应控件, 并显示出来
		str.Empty();
		str.Format("文件路径: %s\x0d\x0a", (LPCTSTR)m_strSendFilePathName);
		pEdit1->ReplaceSel(str);
		str.Empty();
		str.Format("文件大小: %lu字节\x0d\x0a\x0d\x0a", fplength);
		pEdit1->ReplaceSel(str);
		pEdit1->ReplaceSel("文件内容预览: \x0d\x0a\x0d\x0a");
		
		pEdit1->ReplaceSel(/*m_ReceiveData*/fpBuff);

		//获取发送编辑框的句柄, 并显示选中的文件完整路径
		CEdit *pEdit2 = (CEdit *)GetDlgItem(IDC_EDIT_SENDFILE);
		pEdit2->SetSel(0, -1);    //选中所有文本
		pEdit2->ReplaceSel(m_strSendFilePathName);
		
		delete fpBuff;
	}
}

//发送文件
void CSCOMMDlg::OnButtonSendfile() 
{
	// TODO: Add your control notification handler code here
	CFile MyFile;
	BOOL bret;
	CString str;

	//打开文件
	CString strSend;
	CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT_SENDFILE);
	pEdit->GetWindowText(strSend);

#ifdef Debug5
	MessageBox("strSend="+strSend, "信息提示", MB_OK);
#endif

	//bret = MyFile.Open((LPCTSTR)m_strSendFilePathName, CFile::modeRead);
	bret = MyFile.Open((LPCTSTR)strSend, CFile::modeRead);
	
	/*str.Empty();
	str.Format("%s bret=%d", m_strSendFilePathName, bret);
	MessageBox(str, "信息提示", MB_OK);*/

	if (bret == 0)   //打开失败
	{
		AfxMessageBox("Open file failed!");
		return;
	}

	//fp.SeekToEnd();
	//获取文件总长度
	unsigned long fplength = MyFile.GetLength();
	m_nFileLength = fplength;
#ifdef Debug6
	str.Empty();
	str.Format("fplength=%lu", fplength);
	MessageBox(str, "信息提示", MB_OK);
#endif

	char *fpBuff;
	fpBuff = new char[fplength+1];   //创建(fplength+1) 这么多的空间
	//char fpBuff[1024];
	//memset(fpBuff, 0, sizeof(fpBuff));

	//定位文件指针到文件开始
	MyFile.SeekToBegin();
	
	//读文件中的内容, 到fpBuff数组中
	UINT ret;
	ret = MyFile.Read(fpBuff, fplength);
#ifdef Debug7
	str.Empty();
	str.Format("fplength=%lu, ret=%u", fplength, ret);
	MessageBox(str, "信息提示", MB_OK);
#endif
	/*str.Empty();
	str.Format("%s\nfplength=%u\n\nbret=%u", fpBuff, fplength, ret);
	MessageBox(str, "信息提示", MB_OK);*/

	if (ret != fplength)
	{
		MyFile.Close();
		return;
	}
	MyFile.Close();
	
	/*//将从文件中读取到的内容, 显示到接收编辑框中, 并组织一些数据
	memcpy(m_ReceiveData.GetBuffer(fplength), fpBuff, fplength);
	m_ReceiveData.ReleaseBuffer();
	UpdateData(FALSE);
	MessageBox("12313", "信息提示", MB_OK);*/

	
	//打开串口, 发送数据
	CString strStatus;
	bret = m_Port.InitPort(this, m_nCom, m_nBaud, m_cParity, m_nDatabits, m_nStopbits, m_dwCommEvents, /*fplength*/1024);
	if (bret)
	{
		m_Port.StartMonitoring();
		//strStatus.Format("STATUS:COM%d OPENED,%d,%c,%d,%d",m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits);
		strStatus.Empty();
		strStatus.Format("当前状态: COM%d已打开,%dbps,%c,%d,%d", m_nCom, m_nBaud, m_cParity, m_nDatabits, m_nStopbits);
		
		//设置串口图标
		m_ctrlIconOpenoff.SetIcon(m_hIconRed);

		m_bSendFile = TRUE;
		m_strTempSendFilePathName = m_strSendFilePathName;
		m_ctrlEditSendFile.SetWindowText("正在发送......");
		
		//发送文件时,以下功能不能使用
		m_ctrlManualSend.EnableWindow(FALSE);   //"手动发送" 禁用
		m_ctrlAutoSend.EnableWindow(FALSE);     //"自动发送" 禁用
		m_ctrlSendFile.EnableWindow(FALSE);     //"发送文件" 禁用
		
		//向串口发送数据
		unsigned long num=fplength/1024, i;
		for (i=0; i<num; i++)
		{
			m_Port.WriteToPort(fpBuff+i*1024, 1024);
		}
	}
	else
	{
		AfxMessageBox("Failed to send file!");
		m_ctrlIconOpenoff.SetIcon(m_hIconOff);
	}

	delete fpBuff;
}


//检测文件是否发送完毕
LONG CSCOMMDlg::OnFileSendingEnded(WPARAM wParam, LPARAM port)
{
	if (m_bSendFile)
	{
		m_bSendFile = FALSE;

		m_ctrlEditSendFile.SetWindowText("发送完毕!");//m_strSendFilePathName
		
		CString strTemp;
		TX_count += m_nFileLength;
		strTemp.Format("发送:%d", TX_count);
		m_ctrlTXCount.SetWindowText(strTemp);

		SetTimer(3, 3000, NULL);
	}

	return 0;
}


void CSCOMMDlg::OnButtonPushpin() 
{
	// TODO: Add your control notification handler code here
	m_ctrlPushPin.ProcessClick();

	m_bVisible=!m_bVisible;

	if (m_bVisible)
	{
		SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
	}
	else
	{
		SetWindowPos(&wndBottom/*wndNoTopMost*/, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOREDRAW);
		BringWindowToTop();
	}
}

//程序关闭时, 自动调用, 释放占用资源
void CSCOMMDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	//强行关闭自动发送
	m_ctrlAutoSend.SetCheck(0);

	KillTimer(1);   //关闭定时器
	KillTimer(4);
	KillTimer(5);
	//m_Port.StopMonitoring();
	m_Port.ClosePort();  //关闭串口
	m_ReceiveData.Empty();  //清空接收数据字符串
}

void CSCOMMDlg::OnButtonHelp() 
{
	// TODO: Add your control notification handler code here
	TCHAR exeFullPath[MAX_PATH]; 
	GetModuleFileName(NULL,exeFullPath,MAX_PATH);
	CString strlpPath;
	strlpPath.Format("%s",exeFullPath);
	strlpPath.MakeUpper();
	strlpPath.Replace("串口调试助手V2.2.EXE","");
	ShellExecute(NULL,NULL,_T("help.htm"),NULL,strlpPath,SW_SHOW);
}



/**********************************************
 * 函数原型: byte abcd_to_asc(byte abyte);
 * 函数功能: 单个字符转换为ASCII
 * 输入参数: abyte  欲转换为ASCII的字符
 * 输出参数: 无
 * 函数返回值: ASCII值
 * 函数调用:
 **********************************************/
unsigned char CSCOMMDlg::abcd_to_asc(unsigned char abyte)
{
		if(abyte <= 9)
				abyte += '0';
		else
				abyte += ('A' - 10);

		return abyte;
}


/**********************************************
 * 函数原型: byte aasc_to_bcd(byte asc);
 * 函数功能: 单个字符转换为BCD
 * 输入参数: asc  欲转换为BCD的字符
 * 输出参数: 无
 * 函数返回值: BCD值
 * 函数调用:
 **********************************************/
unsigned char CSCOMMDlg::aasc_to_bcd(unsigned char asc)
{
		unsigned char bcd;

		if((asc >= '0') && (asc <= '9'))
			bcd = asc - '0';
		else if((asc >= 'A') && (asc <= 'F'))
			bcd = asc - 'A' + 10;
		else if((asc >= 'a') && (asc <= 'f'))
			bcd = asc - 'a' + 10;
		else if((asc > 0x39) && (asc <= 0x3f))
			bcd = asc - '0';
		else if(asc == 0x00) //// zzxzxzx防止字符串结束标志转换成0xff
			bcd = 0;
		else
			bcd = 0x0f;

		return bcd;
}



/****************************************************************************
 * 函数原型: void BCDToASC(byte *asc_buf, byte *bcd_buf, int asclen);
 * 函数功能: 将BCD字符串转换为ASC字符串
 * 输入参数: bcd_buf  BCD字符串
 *           asclen   ASC字符串长度
 * 输出参数: asc_buf  ASC字符串
 * 函数返回值: 无
 * 函数调用: abcd_to_asc()  单个字符转换为ASCII
 ****************************************************************************/
void CSCOMMDlg::BCDToASC(unsigned char *asc_buf, unsigned char *bcd_buf, int asclen)
{
		int i, j;

		j = 0;
		for (i = 0; i < asclen / 2; i++)
		{
				asc_buf[j] = (bcd_buf[i] & 0xf0) >> 4;
				asc_buf[j] = abcd_to_asc(asc_buf[j]);      //将单个字符转换为ASCII
				j++;
				asc_buf[j] = bcd_buf[i] & 0x0f;
				asc_buf[j] = abcd_to_asc(asc_buf[j]);
				j++;
		}

		if(asclen % 2)
			{
					asc_buf[j] = (bcd_buf[i] & 0xf0) >> 4;
					asc_buf[j] = abcd_to_asc(asc_buf[j]);
			}
}


/**********************************************************************
 * 函数原型: void ASCToBCD(byte *bcd_buf, byte *asc_buf, int asclen);
 * 函数功能: 将ASC字符串转换为BCD字符串
 * 输入参数: asc_buf  ASC字符串
 *           asclen   ASC字符串长度
 * 输出参数: bcd_buf  BDC字符串
 * 函数返回值: 无
 * 函数调用: aasc_to_bcd()  单个字符转换为BCD
 **********************************************************************/
void CSCOMMDlg::ASCToBCD(unsigned char *bcd_buf, unsigned char *asc_buf, int asclen)
{
		int i, j;

		j = 0;

		for (i = 0; i < (asclen + 1) / 2; i++)
		{
				bcd_buf[i] = aasc_to_bcd(asc_buf[j++]);
				bcd_buf[i] = ((j >= asclen) ? 0x00 : aasc_to_bcd(asc_buf[j++])) + (bcd_buf[i] << 4);
		}
}


void CSCOMMDlg::OnChangeEditSavepath()
{
	UpdateData(TRUE);
}

void CAboutDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	OnOK();
}

//单击"应用程序右上角的关闭按钮", 重写WM_CLOSE消息
void CSCOMMDlg::OnClose()
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	int m_nIndex;

	m_nIndex = MessageBox("是否需要关闭程序?", "信息提示", MB_YESNO);
	if (m_nIndex == IDYES)
	{
		CDialog::OnClose();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -