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

📄 scommdlg.cpp

📁 串口助手源程序。计算机端的界面程序。串口的上位机程序。
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		m_ctrlPortStatus.SetWindowText(strStatus);

	}
	else
	{
		AfxMessageBox("没有成功,请重试");
		m_ctrlIconOpenoff.SetIcon(m_hIconOff);
	}

	
}

void CSCOMMDlg::OnSelendokComboDatabits() 
{
	// TODO: Add your control notification handler code here
	//char temp;
	int i=m_DataBits.GetCurSel();
	switch(i)
	{
	case 0:
		i=8;
		break;
	case 1:
		i=7;
		break;
	case 2:
		i=6;
		break;
	}
	m_nDatabits=i;
	CString strStatus;
	if (m_Port.InitPort(this, m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits,m_dwCommEvents,512))
	{
		if(!m_bOpenPort)
		{
			m_Port.StartMonitoring();
			m_ctrlIconOpenoff.SetIcon(m_hIconRed);
			strStatus.Format("STATUS:COM%d OPENED,%d,%c,%d,%d",m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits);
		}
		else
		{
			m_ctrlIconOpenoff.SetIcon(m_hIconOff);
			strStatus.Format("STATUS:COM%d CLOSED,%d,%c,%d,%d",m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits);
		}
		m_ctrlPortStatus.SetWindowText(strStatus);

	}
	else
	{
		AfxMessageBox("没有成功,请重试");
		m_ctrlIconOpenoff.SetIcon(m_hIconOff);
	}
	
	
}

void CSCOMMDlg::OnSelendokComboStopbits() 
{
	// TODO: Add your control notification handler code here
	int i=m_StopBits.GetCurSel();
	switch(i)
	{
	case 0:
		i=1;
		break;
	case 1:
		i=2;
		break;
	}
	m_nStopbits=i;
	CString strStatus;
	if (m_Port.InitPort(this, m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits,m_dwCommEvents,512))
	{
		if(!m_bOpenPort)
		{
			m_Port.StartMonitoring();
			m_ctrlIconOpenoff.SetIcon(m_hIconRed);
			strStatus.Format("STATUS:COM%d OPENED,%d,%c,%d,%d",m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits);
		}
		else
		{
			m_ctrlIconOpenoff.SetIcon(m_hIconOff);
			strStatus.Format("STATUS:COM%d CLOSED,%d,%c,%d,%d",m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits);
		}
		m_ctrlPortStatus.SetWindowText(strStatus);

	}
	else
	{
		AfxMessageBox("没有成功,请重试");
		m_ctrlIconOpenoff.SetIcon(m_hIconOff);
	}
	
}

//保存显示数据
void CSCOMMDlg::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,以下代码自动检测文件名是否存在,若存在,则后面序号自动递增
	//如Rec00.txt Rec01.txt,程序自动为正要保存的文件命名为Rec02.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 CSCOMMDlg::OnButtonDirbrowser() 
{
	// TODO: Add your control notification handler code here
	static char displayname[MAX_PATH];
	static char path[MAX_PATH];
    LPITEMIDLIST pidlBrowse;    // PIDL selected by user 
	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 CSCOMMDlg::OnButtonCountreset() 
{
	// TODO: Add your control notification handler code here
	rxdatacount=0;
	CString strTemp;
	strTemp.Format("%ld",rxdatacount);
	strTemp="RX:"+strTemp;
	m_ctrlRXCOUNT.SetWindowText(strTemp);
	TX_count=0;
	strTemp.Format("%ld",TX_count);
	strTemp="TX:"+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
	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 CSCOMMDlg::OnButtonSendfile() 
{
	// TODO: Add your control notification handler code here
	
 //  char* sRead; 
//   sRead=new char[2];
 //  fp.Read(sRead,2); 
   CFile fp;
    unsigned long Counter=0;

   if(!(fp.Open((LPCTSTR)m_strSendFilePathName,CFile::modeRead))) 
	{
		AfxMessageBox("Open file failed!");
		return;
	}
     
	fp.SeekToEnd();
    unsigned long fplength=fp.GetLength();
//	 fplength=sRead;
	m_nFileLength=fplength;
	
	
    fp.SeekToBegin();
   char *fpBuff,*data;
      unsigned long i;
	  long j,k;
//	  double j,len=0;
   fpBuff=new char[fplength];
   data=new char[fplength];

for(j=0;j<1500;j++)
{
 for(i=0;i<32;i++)
 {
  fp.Read(fpBuff+2*i+j*64,2);
  Str2Hex(fpBuff+2*i+j*64,data+i+j*32);
 }
 m_Port.WriteToPort(data+j*32,32);
Counter=Counter+32;
 Sleep(60);


if(Counter>=fplength/2)
{ 
  m_ctrlEditSendFile.SetWindowText("发送完毕!");//m_strSendFilePathName	
fp.Close();
return;
}
}
	CString strStatus;

	if (m_Port.InitPort(this, m_nCom, m_nBaud, m_cParity, m_nDatabits, m_nStopbits, m_dwCommEvents, 2))
	{
		m_Port.StartMonitoring();
		strStatus.Format("STATUS:COM%d OPENED,%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,2);
	}
	else
	{
		AfxMessageBox("Failed to send file!");
		m_ctrlIconOpenoff.SetIcon(m_hIconOff);
	}
	delete fpBuff;
//   OnFileSendingEnded(WPARAM wParam,LPARAM port);
}


//检测文件是否发送完毕
LONG CSCOMMDlg::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 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, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOREDRAW);
		BringWindowToTop();
	}
}

void CSCOMMDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	m_ctrlAutoSend.SetCheck(0);  //强行关闭自动发送
	KillTimer(1);   //关闭定时器
	KillTimer(4);
	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);
}



void CSCOMMDlg::OnButtonYuchili() 
{
	// TODO: Add your control notification handler code here
FILE *fp,*fp1;
 char data[1000000];
 unsigned long len=0;
/* if((fp=fopen("m_strSendFilePathName","rb"))==NULL)
 {
	 printf("cannot open this file\n");
     
}
if((fp1=fopen("a.txt","wb"))==NULL)
 {
	 printf("cannot open this file\n");
     
//   exit(0);
 }*/
fp=fopen("g.txt","rb");
fp1=fopen("a.txt","wb");
 while(!feof(fp))
 { 

	fscanf(fp,"%d",&(data[len]));
	fprintf(fp1,"%d",data[len]);
	len++;
 }
 fclose(fp);
 fclose(fp1);
	
}

⌨️ 快捷键说明

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