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

📄 filesenddlg.cpp

📁 利用串口进行通讯
💻 CPP
📖 第 1 页 / 共 2 页
字号:
						else
						{
							file.Close();
						}
					}
					else
					{
						//头错误,重新收头
						m_nHeadFlag=0;
					}
				}
			}
			else if(m_chrRecBuffer[0]==2)
			{
				/*
				char ss[20];
				wsprintf(ss,"%d",m_nRecSum);
				SetDlgItemText(IDC_STRECSUM,ss);
				*/
				//文件主体
				if(m_nRecSum>=(m_nRecFileLength+1))
				{
					//接收到了文件主体
					for(DWORD i=0;i<m_nRecFileLength;i++)
					{
						m_chrRecBuffer[i]=m_chrRecBuffer[i+1];
					}

					CFile file;
					if(!file.Open((char *)m_szRecFileName,CFile::modeCreate | CFile::modeWrite))
					{
						AfxMessageBox("创建文件错误!");
					}
					else
					{
						file.WriteHuge(m_chrRecBuffer,m_nRecFileLength);
						file.Close();
					}
					m_nHeadFlag=0;
					m_nRecSum=0;
					m_nRecFileParity=0;
					m_nRecFileLength=0;
					m_szRecFileName[0]=0;
				}
			}
		}
	}
}


/*
void CFileSendDlg::OnSendOver(WPARAM ch, LPARAM port) 
{
	// TODO: Add your control notification handler code here
	if(m_nSendFileState==1)
	{
		//发送完头了,现在发送文件内容
		Sleep(1000);
		m_nSendFileState=2;
		m_port.WriteToPort(m_szSendBuffer,m_nSendBufLength);
		return;
	}

	if(m_nSendFileState==2)
	{
		Sleep(2000);
		//发送完文件内容了,看看是否可以发送下一个文件
		m_nSendFileState=0;
		if(m_nSendNo<5)
		{
			m_nSendNo++;
			FullSendBuffer(m_nSendNo);
		}
		return;
	}
}
*/

void CFileSendDlg::OnSendOver(WPARAM ch, LPARAM port) 
{
	// TODO: Add your control notification handler code here
	if(m_nSendFileState==1)
	{
		//发送完了一包
		if(m_nCurSendBlock<m_nCurMaxBlock) 
		{
			m_nCurSendBlock++;	
		}
		else
		{
			if(m_nSendNo<m_nFileSum)
			{
				m_nSendNo++;
				m_nCurSendBlock=0;
				//休息2秒,让接收方写文件
				Sleep(4000);
			}
			else
			{
				m_nSendFileState=0;
				m_nSendNo=1;
				m_nCurSendBlock=0;
				m_nCurMaxBlock=0;

				SetDlgItemText(IDC_BNSENDFILE,"发送文件");
				return;
			}
		}

		FullSendBuffer(m_nSendNo,m_nCurSendBlock);	
	}
}


/*
void CFileSendDlg::FullSendBufferA(int flag)
{	
	char szFilePath[100];
	GetModuleFileName(NULL,szFilePath,100);
    (_tcsrchr(szFilePath, _T('\\')))[1] =0;//删除文件名,只获得路径

	unsigned char *szSendBuffer;
	szSendBuffer=new  unsigned char[BUFFER_SIZE];
	char strname[5][100]=
	{
		"crt带键盘盒.jpg\0",
		"stdafx.obj\0",
		"ledplay81.xls\0",
		"SerialPort.cpp\0",
		"crt资料.doc\0",
	};

	CFile file;
	char szFileName[200];
	wsprintf(szFileName,"%s%s",szFilePath,strname[flag-1]);
	if(!file.Open(szFileName,CFile::modeRead | CFile::typeBinary))
	{
		AfxMessageBox(strname[flag-1]);
		return;
	}
	DWORD nlength=file.GetLength();

	//前导字符
	unsigned char headstr[81]={"$$$$$$$$"};	
	for(int i=8;i<81;i++) headstr[i]=0;

	//包类型标志
	headstr[8]=1;

	//文件名
	for(i=0;i<60;i++) headstr[9+i]=strname[flag-1][i];

	//文件长度
	headstr[69]=(unsigned char)(nlength>>24)&0x000000FF;
	headstr[70]=(unsigned char)(nlength>>16)&0x000000FF;
	headstr[71]=(unsigned char)(nlength>>8)&0x000000FF;
	headstr[72]=(unsigned char)(nlength&0x000000FF);


	m_nSendBufLength=nlength+1+8;

	//读文件
	DWORD tt=file.ReadHuge(szSendBuffer,BUFFER_SIZE);	
	file.Close();

	char ss[20];
	wsprintf(ss,"%d",nlength);
	SetDlgItemText(IDC_STFILELENGTH,ss);
	
	//装配文件主体缓冲区
	for(i=0;i<8;i++)
		m_szSendBuffer[i]='$';
	//主体包类型标志
	m_szSendBuffer[8]=2;
	//完善主体缓冲区
	for(DWORD t=0;t<nlength;t++)
	{
		m_szSendBuffer[9+t]=szSendBuffer[t];
	}

	//文件校验和
	DWORD fileparity=0;
	for(t=0;t<nlength;t++)
		fileparity +=szSendBuffer[t];
	headstr[73]=(unsigned char)(fileparity>>24)&0x000000FF;
	headstr[74]=(unsigned char)(fileparity>>16)&0x000000FF;
	headstr[75]=(unsigned char)(fileparity>>8)&0x000000FF;
	headstr[76]=(unsigned char)(fileparity&0x000000FF);

	//头包校验和
	DWORD headparity=0;
	for(i=8;i<=76;i++)
		headparity +=headstr[i];
	headstr[77]=(unsigned char)(headparity>>24)&0x000000FF;
	headstr[78]=(unsigned char)(headparity>>16)&0x000000FF;
	headstr[79]=(unsigned char)(headparity>>8)&0x000000FF;
	headstr[80]=(unsigned char)(headparity&0x000000FF);

	delete [] szSendBuffer;
	szSendBuffer = NULL;

	//进入发送文件状态,需要处理SendOver消息
	m_nSendFileState=1;
	m_port.WriteToPort(headstr,81);	
}
*/


void CFileSendDlg::FullSendBuffer(int FileNo,int Block)
{	
	if(Block==0)
	{
		//第0块,要读一个文件
/*
		char szFilePath[100];
		GetModuleFileName(NULL,szFilePath,100);
		(_tcsrchr(szFilePath, _T('\\')))[1] =0;//删除文件名,只获得路径

		//传送的是当前目录下的几个文件

		char strname[5][100]=
		{
			"SerialPort.cpp\0",
			"crt带键盘盒.jpg\0",
			"5E控制器关0.jpg\0",
			"ledplay81.xls\0",			
			"crt资料.doc\0",
		};

		char szFileName[200];
		wsprintf(szFileName,"%s%s",szFilePath,strname[FileNo-1]);
*/

		unsigned char *szSendBuffer;
		szSendBuffer=new  unsigned char[BUFFER_SIZE];

		char szFileName[200];
		wsprintf(szFileName,"%s",m_szFileList[FileNo-1]);
		SetDlgItemText(IDC_STMESSAGE,szFileName);

		CString str;
		char strname[100];
		str=szFileName;
		int pos=str.ReverseFind('\\');
		wsprintf(strname,"%s",str.Mid(pos+1));		

		if(!m_file.Open(szFileName,CFile::modeRead | CFile::typeBinary))
		{
			AfxMessageBox(szFileName);
			return;
		}

		DWORD nlength=m_file.GetLength();
		
		char ss[20];
		wsprintf(ss,"%d",nlength);
		SetDlgItemText(IDC_STFILELENGTH,ss);

		//计算需要多少块才能发完
		m_nCurMaxBlock=(nlength-1)/BLOCK_SIZE+1;

		//前导字符
		unsigned char headstr[81]={"$$$$$$$$"};	
		for(int i=8;i<81;i++) headstr[i]=0;

		//包类型标志
		headstr[8]=1;

		//文件名
		for(i=0;i<60;i++) headstr[9+i]=strname[i];

		//文件长度
		headstr[69]=(unsigned char)(nlength>>24)&0x000000FF;
		headstr[70]=(unsigned char)(nlength>>16)&0x000000FF;
		headstr[71]=(unsigned char)(nlength>>8)&0x000000FF;
		headstr[72]=(unsigned char)(nlength&0x000000FF);
				
		//读文件
		m_file.ReadHuge(szSendBuffer,BUFFER_SIZE);	
	
		//计算文件校验和
		DWORD t,fileparity=0;
		for(t=0;t<nlength;t++)
			fileparity +=szSendBuffer[t];

		headstr[73]=(unsigned char)(fileparity>>24)&0x000000FF;
		headstr[74]=(unsigned char)(fileparity>>16)&0x000000FF;
		headstr[75]=(unsigned char)(fileparity>>8)&0x000000FF;
		headstr[76]=(unsigned char)(fileparity&0x000000FF);

		//头包校验和
		DWORD headparity=0;
		for(i=8;i<=76;i++)
			headparity +=headstr[i];
		headstr[77]=(unsigned char)(headparity>>24)&0x000000FF;
		headstr[78]=(unsigned char)(headparity>>16)&0x000000FF;
		headstr[79]=(unsigned char)(headparity>>8)&0x000000FF;
		headstr[80]=(unsigned char)(headparity&0x000000FF);

		delete [] szSendBuffer;
		szSendBuffer = NULL;

		//进入发送文件状态,需要处理SendOver消息
		m_nSendFileState=1;
		m_port.WriteToPort(headstr,81);	

	}
	else
	{		
		unsigned char *tempbuffer;
		tempbuffer=new  unsigned char[BLOCK_SIZE+20];
			
		for(DWORD i=0;i<8;i++)
			tempbuffer[i]='$';
		tempbuffer[8]=2;
		
		m_file.Seek((Block-1)*BLOCK_SIZE,CFile::begin);
		DWORD tt=m_file.ReadHuge(tempbuffer,BLOCK_SIZE);
		DWORD xx=tt;

		if(Block==m_nCurMaxBlock) 
		{
			m_file.Close();
		}

		if(Block==1)
		{
			//需要加一个前导串
			for(DWORD i=tt-1;i>=0;i--)
			{
				tempbuffer[i+9]=tempbuffer[i];
				if(i==0) break;
			}

			for(i=0;i<8;i++)
				tempbuffer[i]='$';
			tempbuffer[8]=2;
			tt +=9;

		}
		m_port.WriteToPort(tempbuffer,tt);
		
		delete [] tempbuffer;
		tempbuffer = NULL;

		char ss[10];
		wsprintf(ss,"%d",(Block-1)*BLOCK_SIZE+xx);
		SetDlgItemText(IDC_STCURSENDSUM,ss);
	}
}


void CFileSendDlg::OnBnopencom() 
{
	// TODO: Add your control notification handler code here
	
	if(m_bComState==FALSE)
	{
		UpdateData(TRUE);
		int nport=GetDlgItemInt(IDC_CBCOMPORT);
		int nbaud=GetDlgItemInt(IDC_CBCOMBAUD);
		
		if(m_port.InitPort(this,nport,nbaud,'n',8,2))
		{
			m_port.StartMonitoring();
		}
		else
		{
			AfxMessageBox("打开通讯口失败!");
			return;
		}	
		m_nHeadFlag=0;
		m_nRecFileParity=0;
		m_nRecFileLength=0;
		m_nRecSum=0;
	
		m_bComState=TRUE;
		SetDlgItemText(IDC_BNOPENCOM,"关闭串口");
	}
	else
	{
		m_port.StopMonitoring();
		m_port.ClosePort();
		m_bComState=FALSE;
		SetDlgItemText(IDC_BNOPENCOM,"打开串口");
	}
}

void CFileSendDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	delete [] m_szSendBuffer;
	delete [] m_chrRecBuffer;

	m_szSendBuffer = NULL;
	m_chrRecBuffer=NULL;
	
	CDialog::OnClose();
}



void CFileSendDlg::OnBnclear() 
{
	// TODO: Add your control notification handler code here
	m_list1.ResetContent();
	m_nFileSum=0;	
}

void CFileSendDlg::OnBncleara() 
{
	// TODO: Add your control notification handler code here
	m_list2.ResetContent();
}

void CFileSendDlg::OnBnbrowse() 
{
	// TODO: Add your control notification handler code here
	// TODO: Add your control notification handler code here
	if(m_nFileSum>=10) 
	{
		AfxMessageBox("每次最多10个!");
		return;
	}

	char szFilter[] = "all file (*.*)|*.*||";

	CFileDialog fd(TRUE,"*",NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, NULL);

	if(fd.DoModal()==IDOK)
	{
		CString st1;
		st1=fd.GetPathName();		
		m_list1.AddString(st1);
		wsprintf((char *)m_szFileList[m_nFileSum++],"%s",st1);
	}
}

void CFileSendDlg::OnBnsendfile() 
{
	// TODO: Add your command handler code here
	if(m_nSendFileState==1)
	{
		SetDlgItemText(IDC_BNSENDFILE,"发送文件");
		m_nSendFileState=0;
	}
	else
	{
		if(m_nFileSum==0)
		{
			AfxMessageBox("没有文件可以传送!");
			return;
		}
		m_nHeadFlag=0;

		m_nSendFileState=1;
		m_nSendNo=1;
		m_nCurSendBlock=0;
		FullSendBuffer(m_nSendNo,m_nCurSendBlock);
		SetDlgItemText(IDC_BNSENDFILE,"中止发送");
		//CFile::Rename(("F:\\"+st+".txt"),"F:\\文件.txt");
	}
}

⌨️ 快捷键说明

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