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

📄 testfirmdlg.cpp

📁 D12 Mcu 凌阳.用BULK传输方式,实现语音播放
💻 CPP
📖 第 1 页 / 共 2 页
字号:
}

void CTestfirmDlg::OnCloseusbButton() 
{
	// TODO: Add your control notification handler code here
	CloseIfOpen();
}

void CTestfirmDlg::CloseIfOpen(void)
{
	if (hDevice1 != INVALID_HANDLE_VALUE)
	{

		if (!CloseHandle(hDevice1))
		{
			AfxMessageBox("ERROR: CloseHandle1 returns: %ld \n", GetLastError());
		}

	 		hDevice1 = INVALID_HANDLE_VALUE;
	}
	
	if (hDevice2 != INVALID_HANDLE_VALUE)
	{

		if (!CloseHandle(hDevice2))
		{
			AfxMessageBox("ERROR: CloseHandle1 returns: %ld \n", GetLastError());
		}
	 		hDevice2 = INVALID_HANDLE_VALUE;
	}
	AfxMessageBox("CloseHandle success\n");

}





void CTestfirmDlg::OnConnecttestButton() 
{
	// TODO: Add your control notification handler code here
	CString show,tmp;
	char  Recievedata[1];
	char  Senddata[64];
	int i;
	ULONG nRead,nWrite;

    Senddata[0]=ID0;
    WriteFile(hDevice2, Senddata, 64, &nWrite, NULL);
    for(i=0;i<50000;i++)
	{

	};
	Recievedata[0]=0x0;
	ReadFile(hDevice1, Recievedata, 1, &nRead, NULL);
	if(Recievedata[0]==ID0)
    {
		
		AfxMessageBox("USB外设连接成功\n");
	}
}






void CTestfirmDlg::OnUploadButton() 
{
	// TODO: Add your control notification handler code here
	BOOL Test=0;
	//CHAR *filename;
	char filename[0x100];
    m_Up_Download_FileName.GetWindowText(filename,0x100);
    Test=FunUpLoad();
	if(Test==0)
	{
		MessageBox("数据上载出错!");

	}
	else
	{
		MessageBox("数据上载完毕!");
		WriteDataToFile(filename);	
	
	}                                                                                                                                             
}
	
BOOL CTestfirmDlg::FunUpLoad()
{
    //int UpLoad_Id;
	ULONG nWrite=0;

	ULONG nRead=0;

	BOOL nRet=0;
	ULONG timeout=0;
	unsigned long i=0,j=0;
    BYTE WriteId[64];
	BYTE ReadId[5];
	BYTE UpLoad_Data_Tmp[64];
	unsigned int block = 64;
	unsigned long P=0,wsize=0,wtsize=0;
	unsigned int Int_Counter=0;
    WriteId[0]=5;//ID4=5
    WriteId[1]=1;
	ReadId[0]=0;
	ReadId[1]=0;
	ReadId[2]=0;
	ReadId[3]=0;
	ReadId[4]=0;
	WriteFile(hDevice2,WriteId,64,&nWrite,NULL);//write  UpLoadID to 061
    timeout=0;
	while(ReadId[0]!=5)
	{
		ReadFile(hDevice1,ReadId,5,&nRead,NULL);   //Read Apply Id
		timeout++;
		if(timeout==0x00ffffff)
		{
			MessageBox("Receive ID Fail");
			return 0;
		}
	}
	if(ReadId[0]==5)
	{
		Len=ReadId[4];
		Len<<=8;
		Len+=ReadId[3];
		Len<<=8;
		Len+=ReadId[2];
		Len<<=8;
		Len+=ReadId[1];
	}
	else
	{
		MessageBox("读ID失败!");
		return 0;
	}
	ReadId[0]=0;
	ReadId[1]=0;
   	ReadId[2]=0;
	ReadId[3]=0;
	ReadId[4]=0;

	if (UpLoad_Data_Buffer!=NULL)
	{	
		free(UpLoad_Data_Buffer);
		UpLoad_Data_Buffer = NULL;
	}
	UpLoad_Data_Buffer=(char*)malloc(Len+0x0100);
	//	UpLoad_Data_Buffer=(char*)malloc(Len);
	if(UpLoad_Data_Buffer==NULL)
	{
		MessageBox("内存溢出!");
		return 0;
	}

	timeout=0;
	Int_Counter=0;
	while (wtsize<Len) 
	{
		nRead=0;       // 
		wsize=0;
		wsize = ((Len-wtsize) > block) ? block : (Len-wtsize);
		nRet=FALSE;
		while(nRet==FALSE)
		{

			nRet=ReadFile(hDevice1,UpLoad_Data_Tmp,wsize,&nRead,NULL);
			if(nRet==FALSE)
			{
				MessageBox("Readfile操作失败!");
				return 0;
			}
			timeout++;
			if(timeout==0x00fff)
			{
				MessageBox("Readfile操作失败!");
				return 0;
			}
		}

		if(nRead==wsize)
		{
			memcpy(&UpLoad_Data_Buffer[P],UpLoad_Data_Tmp,wsize);
			wtsize += wsize;
			P += wsize;
			timeout=0;
			Int_Counter++;
		}
	
		if(Int_Counter==0x002)
		{
			timeout=0;
			timeout=0;
		}
	
	}
	if(wtsize<Len)
	{
		MessageBox("读 second ID失败!");
		return 0;
	}
   	return 1;
}

void CTestfirmDlg::WriteDataToFile(CHAR *filename)
{
		int ret=1;
		CString msg;
        FILE *FileOut;
        DWORD block = 512;
		DWORD wtsize=0,wsize;
		char *p=UpLoad_Data_Buffer;
        FileOut = fopen(filename,"wb+");

		if (FileOut==NULL) 
		{
			msg.Format("Can not open file : %s",filename);
			AfxMessageBox(msg);
			return;
		}
	
		while (wtsize < Len) 
		{
			wsize = ((Len-wtsize) > block) ? block : (Len-wtsize);
			fwrite(p,1,wsize,FileOut);
			wtsize += wsize;
			p += wsize;
		}
		MessageBox("数据上载保存成功!");
		fclose(FileOut);
}

void CTestfirmDlg::OnDownloadButton() 
{
	// TODO: Add your control notification handler code here
	BOOL nRet=0;
	//CHAR *filename0;
    char filename0[0x100];
    m_Up_Download_FileName.GetWindowText(filename0,0x100);
	nRet=ReadDataFromFile(filename0);
	if(nRet==0)
	{
		MessageBox("读文件失败");
	}
	else
	{
		MessageBox("读文件成功!");
	}
	FunDownLoad();

}

BOOL CTestfirmDlg::ReadDataFromFile(CHAR *filename)
{
	
	DWORD block = 512;
	DWORD count=0;
	CString msg;
	char *Buffer_Pointer=NULL;
    FILE *FileIn=fopen(filename,"rb+");
    if (FileIn==NULL) 
	{
		msg.Format("Can not open file : %s",filename);
		AfxMessageBox(msg);
		return 0;
	}
    fseek(FileIn,0,SEEK_END);
	m_DownLoadFileLen=ftell(FileIn);
    fseek(FileIn,0,SEEK_SET);
	if(Buffer_Pointer!=NULL)
	{
		free(Buffer_Pointer);
		
	}
    DownLoad_Data_Buffer=(char*)malloc(512*1024);
	if(DownLoad_Data_Buffer==NULL)
	{
		MessageBox("内存溢出");
		return 0;
	}
    Buffer_Pointer=DownLoad_Data_Buffer;
	while( !feof( FileIn ) )
	{
		
		count=fread( Buffer_Pointer, 1, block, FileIn );
		Buffer_Pointer += count;
		

	}
	fclose(FileIn);
	return 1;	
}

//downdata have ben save to  char DownLoad_Data_Buffer[FILE LENGTH]
void CTestfirmDlg::FunDownLoad(void)
{
	unsigned long m_DownLoadFileLen_Tmp;
	ULONG nWrite=0,nRead=0;
	unsigned int i=0;
	unsigned long wsize=0,wtsize=0,nRet=0;
	ULONG Raise=0;
	unsigned long timeout=0;
	unsigned long j=0;
	unsigned long Count_64=0;
	ULONG EraseTime=0;
    char WriteId[64];
	char ReadId[1];
	////////////////
	BOOL WritePipe_Test;
	char DownLoad_Data_Buffer_Array[64];
	
    char * DownLoad_Data_Buffer_Pointer=DownLoad_Data_Buffer;
	m_DownLoadFileLen_Tmp=m_DownLoadFileLen;
    WriteId[0]=6;      //ID6
	WriteId[1]=1;

	WriteId[2]=(char)(m_DownLoadFileLen_Tmp & 0x000000ff);//LOW 8 BYTES
    WriteId[3]=(char)((m_DownLoadFileLen_Tmp & 0x0000ff00)>>8);//low word
    WriteId[4]=(char)((m_DownLoadFileLen_Tmp & 0x00ff0000)>>16);
	WriteId[5]=(char)((m_DownLoadFileLen_Tmp & 0xff000000)>>24);//high word

	//WriteFile(hDevice2,WriteId,4,&nWrite,NULL);   
    WriteFile(hDevice2,WriteId,64,&nWrite,NULL);  
    ReadId[0]=0;
	while(ReadId[0]!=6)
	{
		nRet=ReadFile(hDevice1,ReadId,1,&nRead,NULL);
		
		for(j=0;j<10000;j++)
		{
			EraseTime++;
		};
		if(EraseTime==0x00ffffff)
		{
		    MessageBox(" Read Apply error!");
			break;
		}

	}
    m_DownLoadFileLen_Tmp=m_DownLoadFileLen;

	i=0;
	
	wtsize=0;
	Count_64=0;
	while(wtsize<m_DownLoadFileLen_Tmp)
	 {
		nWrite=0;
		wsize = ((m_DownLoadFileLen-wtsize) > 64) ? 64 : (m_DownLoadFileLen-wtsize);
		memcpy(DownLoad_Data_Buffer_Array,&DownLoad_Data_Buffer_Pointer[Raise],wsize);
		WritePipe_Test=FALSE;
		while(WritePipe_Test==FALSE)
		{
			for(j=0;j<50;j++)
			{
					
			}
		
			WritePipe_Test=WriteFile(hDevice2,DownLoad_Data_Buffer_Array,wsize,&nWrite,NULL);
		
			if(WritePipe_Test!=FALSE)
			{
				wtsize += wsize;
				Raise += wsize;
				timeout=0;
				Count_64++;
				i++;
			}
			else
			{
				
				timeout++;
				if(timeout==0x00ffffff)
				{
					MessageBox("超时!");
					break;
				}
			}
	
			if(timeout==0x0ffff)
			{
				MessageBox("超时!");
				break;
			}
			if(Count_64==5)
			{
				Count_64=5;
				Count_64=5;
			}
		}//while
     }//while
	WriteFile(hDevice2,DownLoad_Data_Buffer_Array,1,&nWrite,NULL);
    MessageBox("文件下载成功!");	
	if(DownLoad_Data_Buffer!=NULL)
	{
		free(DownLoad_Data_Buffer);
		DownLoad_Data_Buffer=NULL;

	}
}


void CTestfirmDlg::OnBinToWaveButton() 
{
	// TODO: Add your control notification handler code here
    char parameter[256];
	char Dsacm_Path[256];
    //	char * P_Dsacm_Path=NULL;
	char * temp=NULL;
	CString path;
	UpdateData(TRUE);
    strcpy(Dsacm_Path,P_Dsacm_Path);
    strcat(Dsacm_Path,"\\dsacm2000.exe"); 
	
	 path="16  "+m_BinFile_Path+"  "+m_Wave_File_Direction;
 	 temp=(LPSTR)(LPCTSTR)(path);
     strcpy(parameter,temp);
      ShellExecute(NULL,"open",Dsacm_Path, parameter, NULL, SW_HIDE);  
	 AfxMessageBox("转WAVE文件成功!");
}

void CTestfirmDlg::OnPlayWaveButton() 
{
	// TODO: Add your control notification handler code here
	//m_Wave_File_Direction
	char * WavePath;
	if(m_Wave_File_Direction=="")
	{
		MessageBox("请选择WAVE文件的路径");
	}
	WavePath=(LPSTR)(LPCTSTR)(m_Wave_File_Direction);
    PlaySound(WavePath,NULL,SND_ASYNC|SND_NODEFAULT );
	
}


void CTestfirmDlg::OnBinFileDirectionButton() 
{
	// TODO: Add your control notification handler code here
	CString temp;
	CFileDialog dlg ( TRUE, NULL, "*.bin", OFN_HIDEREADONLY | OFN_FILEMUSTEXIST,
                  _T("All Files (*.bin)|*.bin|"), this );

    if ( IDOK == dlg.DoModal() )
    {
 		 temp=dlg.GetFileName();
		 BinFileName=(LPSTR)(LPCTSTR)(temp);
		 m_BinFile_Path=dlg.GetPathName();
		 UpdateData(FALSE);
	      	
    }

	
}

void CTestfirmDlg::OnWaveFileDirectionButton() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg ( FALSE, NULL, "*.WAV", OFN_HIDEREADONLY | OFN_FILEMUSTEXIST,
                  _T("All Files (*.WAV)|*.WAV|"), this );

    if ( IDOK == dlg.DoModal() )
        {
          m_Wave_File_Direction=dlg.GetPathName();
		  UpdateData(FALSE);
	      	
        }

}


void CTestfirmDlg::OnStopWaveButton() 
{
	// TODO: Add your control notification handler code here
	PlaySound( NULL, NULL, NULL );
}

⌨️ 快捷键说明

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