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

📄 crjscommdlg.cpp

📁 用于串口通讯
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		}
		i++;
		if(i>=len)  break;
		l=str[i];
		t=HexChar(h);
		t1=HexChar(l);
		if((t==16)||(t1==16))
			break;
		else
			t=t*16+t1;
		i++;
		data[rlen]=(char)t;
		rlen++;
	}
	return rlen;
}

char CCrjSCOMMDlg::HexChar(char c)
{
	if((c>='0')&&(c<='9'))
		return c-0x30;
	else if((c>='A')&&(c<='F'))
		return c-'A'+10;
	else if((c>='a')&&(c<='f'))
		return c-'a'+10;
	else 
		return 0x10;
}

long TX_count=0;
void CCrjSCOMMDlg::OnButtonManualsend() 
{
	// TODO: Add your control notification handler code here
	if(m_Port.m_hComm==NULL)//发动时要检测串口是否发开,否则会出错
	{
		m_ctrlAutoSend.SetCheck(0);
		AfxMessageBox("串口没有打开,请打开串口");
		return;
	}
	else
	{
		UpdateData(TRUE);
		if(m_ctrlHexSend.GetCheck())//发送十六进制数据
		{
			char data[512];
			int len=Str2Hex(m_strSendData,data);
			m_Port.WriteToPort(data,len);
			TX_count+=(long)((m_strSendData.GetLength()+1)/3);
			//计算发送的十六进制数据,
			//严格按规则输入
		}
		else//发送ASCII文本
		{
			m_Port.WriteToPort((LPCTSTR)m_strSendData);//发送数据
			TX_count+=m_strSendData.GetLength();//发送计数
		}
		CString strTemp;
		strTemp.Format("TX:%d",TX_count);
		m_ctrlTXCOUNT.SetWindowText(strTemp);//显示计数
	}
}

void CCrjSCOMMDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CString strStatus;

	switch(nIDEvent)
	{
	case 1://定时器ID=1为自动发送时间到
		OnButtonManualsend();//周期到后,只调用OnButtonManualsend
		break;
	case 2://其他定时器
		m_ctrlSavePath.SetWindowText(m_strCurPath);//重新显示路径
		KillTimer(2);//关闭定时器
		break;
	case 3:
		m_ctrlManualSend.EnableWindow(TRUE);
		m_ctrlAutoSend.EnableWindow(TRUE);
		m_ctrlSendFile.EnableWindow(TRUE);
		m_strSendFilePathName=m_strTempSendFilePathName;
		m_ctrlEditSendFile.SetWindowText(m_strSendFilePathName);
		//m_strSendFilePathName
		KillTimer(3);
		if(!(m_ctrlAutoSend.GetCheck()))
		{
			if(m_Port.InitPort(this,m_nCom,m_nBaud,m_cParity,m_nDatabits,m_nStopbits,m_dwCommEvents,512))
			{
				m_Port.StartMonitoring();
				strStatus.Format("STATUS:COM%d OPENDED,%d,%c,%d,%d",m_nCom,m_nBaud,m_cParity,m_nDatabits,m_nStopbits);
				m_ctrlIconOpenoff.SetIcon(m_hIconRed);
			}
			else
			{
				AfxMessageBox("Failed to reset send buffer size!");
				m_ctrlIconOpenoff.SetIcon(m_hIconOff);
			}
			m_ctrlPortStatus.SetWindowText(strStatus);
		}
		break;
	case 4:
		m_animIcon.ShowNextImage();//显示下一幅图像
		break;


	default:
		break;
	}
	CDialog::OnTimer(nIDEvent);
}

void CCrjSCOMMDlg::OnCheckAutosend() 
{
	// TODO: Add your control notification handler code here
	m_bAutoSend=!m_bAutoSend;//标志是否打开自动发送
	if(m_bAutoSend)
	{
		if(m_Port.m_hComm==NULL)
		{
			m_bAutoSend=!m_bAutoSend;
			m_ctrlAutoSend.SetCheck(0);
			AfxMessageBox("串口没有打开,请打开串口");
			return;
		}
		else
			SetTimer(1,m_nCycleTime,NULL);//设置定时器1
	}
	else
	{
		KillTimer(1);//杀掉定时期1
	}
}

void CCrjSCOMMDlg::OnChangeEditSend() 
{
	// 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.
	
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
}


void CCrjSCOMMDlg::OnChangeEditCycletime() 
{
	// 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.
	
	// TODO: Add your control notification handler code here
	CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT_CYCLETIME);
	CString strText;
	pEdit->GetWindowText(strText);
	m_nCycleTime=atoi(strText);
}

void CCrjSCOMMDlg::OnButtonClearReciArea() 
{
	// TODO: Add your control notification handler code here
	m_ReceiveData.Empty();
	UpdateData(FALSE);
}

void CCrjSCOMMDlg::OnButtonStopdisp() 
{
	// TODO: Add your control notification handler code here
	m_bStopDispRXData=!m_bStopDispRXData;
	if(m_bStopDispRXData)
		m_ctrlStopDisp.SetWindowText("继续显示");
	else
		m_ctrlStopDisp.SetWindowText("停止显示");
}

void CCrjSCOMMDlg::OnButtonSavedata() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

	int nLength;
	nLength = m_strCurPath.GetLength();

	for (int nCount = 0; nCount < nLength; nCount++)
	{
		if(m_strCurPath.GetAt(nCount) == '\\')
			CreateDirectory(m_strCurPath.Left(nCount + 1),NULL);
	}
	CreateDirectory(m_strCurPath,NULL);

	CFile m_rFile;
	LPCSTR lpszPath = m_strCurPath;//"c:\\comdata";
	SetCurrentDirectory(lpszPath);
	//文件名为Rec**.txt,以下自动检测文件是否存在
	char buf[20];
	for(int j=0;j<100;j++)
	{
		sprintf(buf,"Rec%02d.txt",j);
		if((access(buf,0))==-1)
			break;
	}

	if(!m_rFile.Open(buf,CFile::modeCreate | CFile::modeWrite))
	{
		AfxMessageBox("创建文件失败!");
		return;
	}
	if((access(buf,0))==-1)
	{
		AfxMessageBox("failed");
		return;
	}

	//在文件开始处写保存日期
	CTime t=CTime::GetCurrentTime();
	CString str=t.Format("%y年%m月%d日%h时%m分%s秒\r\n");
	m_rFile.Write((LPCTSTR)str,str.GetLength());
	//保存显示数据
	m_rFile.Write((LPCTSTR)m_ReceiveData,m_ReceiveData.GetLength());
	m_rFile.Flush();
	m_rFile.Close();//关闭文件

	str="OK,";
	for(int i=0;i<5;i++)
		str+=buf[i];
	str+=".txt saved";
	m_ctrlSavePath.SetWindowText(str);
	SetTimer(2,5000,NULL);//在定时器中显示保存文件状态
}


void CCrjSCOMMDlg::OnButtonDirbrowser() 
{
	// TODO: Add your control notification handler code here
	static char displayname[MAX_PATH];
	static char path[MAX_PATH];
	LPITEMIDLIST pidlBrowse;
	BROWSEINFO bi;
	bi.hwndOwner=this->m_hWnd;
	bi.pidlRoot=NULL;
	bi.pszDisplayName=displayname;
	bi.lpszTitle="请选择保存文件夹:";
	bi.ulFlags=BIF_EDITBOX;
	bi.lpfn=NULL;
	pidlBrowse=SHBrowseForFolder(&bi);
	if(pidlBrowse!=NULL)
	{
		SHGetPathFromIDList(pidlBrowse,path);
	}
	CString str=path;//得到路径
	if(str.IsEmpty())
		return;//如果没有选择就返回
	m_strCurPath=str;//接收路径编辑框对应变量
	UpdateData(FALSE);
}

void CCrjSCOMMDlg::OnButtonFilebrowser() 
{
	// TODO: Add your control notification handler code here
	LPCSTR lpszPath="c://comdata";
	SetCurrentDirectory(lpszPath);
	static char BASED_CODE szFilter[]="文本文件(*.txt)|*.txt|所有文件(*.*)|*.*||";

	CFileDialog FileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szFilter);
	FileDlg.m_ofn.lpstrInitialDir=lpszPath;
	if(FileDlg.DoModal()==IDOK)
	{
		CString strFileName=FileDlg.GetFileName();
		CString strFileExt=FileDlg.GetFileExt();
		CString lpstrName=FileDlg.GetPathName();
		m_strSendFilePathName=lpstrName;
		UpdateData(FALSE);
	}
}

void CCrjSCOMMDlg::OnButtonSendfile() 
{
	// TODO: Add your control notification handler code here
	CFile fp;
	if(!(fp.Open((LPCTSTR)m_strSendFilePathName,CFile::modeRead)))
	{
		AfxMessageBox("Open file failed!");
		return;
	}
	fp.SeekToEnd();
	unsigned long fplength=fp.GetLength();
	m_nFileLength=fplength;
	char* fpBuff;
	fpBuff=new char[fplength];
	fp.SeekToBegin();
	if(fp.Read(fpBuff,fplength)<1)
	{
		fp.Close();
		return;
	}
	fp.Close();

	CString strStatus;
	if(m_Port.InitPort(this,m_nCom,m_nBaud,m_cParity,m_nDatabits,m_nStopbits,m_dwCommEvents,fplength))
	{
		m_Port.StartMonitoring();
		strStatus.Format("STATUS:COM%d OPENDED,%d,%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);
		m_Port.WriteToPort((LPCTSTR)fpBuff,fplength);
	}
	else
	{
		AfxMessageBox("Failed to send file!");
		m_ctrlIconOpenoff.SetIcon(m_hIconOff);
	}
	delete fpBuff;
}

//检测文件是否发送完毕
LONG CCrjSCOMMDlg::OnFileSendingEnded(WPARAM wParam, LPARAM port)
{
	if(m_bSendFile)
	{
		m_ctrlEditSendFile.SetWindowText("发送完毕!");//m_strSendFilePathName
		TX_count+=m_nFileLength;
		SetTimer(3,3000,NULL);
		CString strTemp;
		strTemp.Format("TX:%d",TX_count);
		m_ctrlTXCOUNT.SetWindowText(strTemp);
		m_bSendFile=FALSE;
	}
	return 0;
}

void CCrjSCOMMDlg::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,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOREDRAW);
		BringWindowToTop();
	}
}

void CCrjSCOMMDlg::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("串口调试V1.0.exe","");
	ShellExecute(NULL,NULL,_T("help.htm"),NULL,strlpPath,SW_SHOW);
}

⌨️ 快捷键说明

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