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

📄 downloadtooldlg.cpp

📁 ti的数字电视芯片 tvp9000的源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	dcb.Parity = NOPARITY;

	dcb.fOutX  = FALSE;
	//dcb.XonChar = ;
	//dcb.XoffChar = ;
	dcb.fInX   = FALSE;
	
	if(::SetCommState( hFile_com, &dcb )==0)
	{
		msg.Format("\r\nSet %s State failed! error=%d\r\n.", Order_to_COMName[nSelCOM], GetLastError());
		
		DisplayMsg( msg );
		g_bDownloading = 0;
		CloseHandle(hFile_com);
		return -1;
		
	}

	if(file_data.Open(m_strSelFileName, CFile::modeReadWrite,NULL) == FALSE)
	{		
		msg.Format("fail to open file:%s!",m_strSelFileName);
		AfxMessageBox(msg);
		g_bDownloading = 0;
		CloseHandle(hFile_com);
		return -1;
	}
	
	nFileSize = file_data.GetLength();
	usTotalBlock = nFileSize / nBlockSize;
	if((nFileSize % nBlockSize) != 0)
	{
		usTotalBlock +=1;
	}
	
	g_pProgressCtrl->SetRange(0, usTotalBlock);


	msg.Format("Total %d blocks \r\n", usTotalBlock);
	DisplayMsg(msg);

	usCurBlock = 1;
	
	g_bNeedToStop = 0;

	g_pProgressCtrl->SetPos(0);

	usTotalBlock_tmp = htons(usTotalBlock);
	while(nFileSize > 0)
	{
		unsigned short usTmp;

		if(g_bNeedToStop == 1)
		{
			msg.Format("Stop\r\n ");
			DisplayMsg(msg);
			CloseHandle(hFile_com);
			file_data.Close();
			
			g_bDownloading = 0;

			AfxBeginThread(IdleThread, NULL);
			g_pProgressCtrl->SetPos(0);
			return -1;
		}

		msg.Format("Sending the %d block [total %d blocks]... ", usCurBlock, usTotalBlock);
		DisplayMsg(msg);

		memset(buf, 0x00, sizeof(buf));
		nReadSize = file_data.Read(buf + 6, nBlockSize);
		if(nReadSize < 0)
		{
			msg.Format("Read the %d block ERROR! ", usCurBlock);
			DisplayMsg(msg);
			CloseHandle(hFile_com);
			file_data.Close();
			
			g_bDownloading = 0;

			AfxBeginThread(IdleThread, NULL);
			g_pProgressCtrl->SetPos(0);
			return -1;
		}
		else if(nReadSize == 0)
		{
			CloseHandle(hFile_com);
			file_data.Close();
			g_bDownloading = 0;
			
			msg = "Download finish(reach the end of this file)!\r\n";
			DisplayMsg( msg );
			
			AfxBeginThread(IdleThread, NULL);
			g_pProgressCtrl->SetPos(0);
			return 0;
		}
		
		buf[0] = 'F';
		buf[1] = 'P';
		
		usTmp = htons(usCurBlock);		
		memcpy(buf + 2, &usTmp, 2);
		
		memcpy(buf + 4, &usTotalBlock_tmp, 2);
		
		/*clean*/
		PurgeComm(hFile_com,PURGE_RXCLEAR);
		PurgeComm(hFile_com,PURGE_TXCLEAR);

		if(WriteFile(hFile_com, buf, /*nReadSize*/nBlockSize + 6, (unsigned long *) &nWriteSize,NULL) == 0)
		{
			//error
			// deal with the error code 
			switch (dwError = GetLastError()) 
			{ 
			case ERROR_HANDLE_EOF: 
				{ 
					// we have reached the end of the file 
					// during the call to ReadFile 
					
					// code to handle that 
					msg.Format("Failed \r\n");
					DisplayMsg(msg);

					msg.Format("Fail to download the %d block!\r\n",usCurBlock);
					
					DisplayMsg( msg );

					AfxMessageBox(msg);
					CloseHandle(hFile_com);
					file_data.Close();
					
					g_bDownloading = 0;
					g_pProgressCtrl->SetPos(0);
					return -1;
				} 
				break;
			case ERROR_IO_PENDING: 
				{ 
					// asynchronous i/o is still in progress 
					msg.Format("PENDING \r\n ");
					DisplayMsg(msg);
					
				}
				break;
			default:
				break;
			}
		}
		else
		{
			FlushFileBuffers(hFile_com);
			msg.Format("OK \r\n ");
			DisplayMsg(msg);
		}
		
		/*
		BOOL   bReadStatus;
		bReadStatus = ReadFile( m_hIDComDev, buffer, 
								dwBytesRead, &dwBytesRead,    &m_OverlappedRead );
		if(!bReadStatus)
		{
			if(GetLastError()==ERROR_IO_PENDING)
			{
				WaitForSingleObject(m_OverlappedRead.hEvent,1000);
				return ((int)dwBytesRead);
			}
			return(0);
		}
		return ((int)dwBytesRead);		
		*/
		
		/*Read ACK*/
		for(unsigned int i =0; i < ((nTimeOut*1000)/(CHECK_TIME)); i++)
		{
			if(g_bNeedToStop == 1)
			{
				msg.Format("Stop\r\n ");
				DisplayMsg(msg);
				CloseHandle(hFile_com);
				file_data.Close();
				
				g_bDownloading = 0;

				AfxBeginThread(IdleThread, NULL);
				g_pProgressCtrl->SetPos(0);
				return -1;
			}

			::ClearCommError(hFile_com, &error,&ComStat);
			if(ComStat.cbInQue>=6)
			{
				memset(buf_com, 0x00, sizeof(buf_com));
				if(ReadFile(hFile_com, buf_com, ComStat.cbInQue, (unsigned long *)&nReadSize,NULL) == 0)
				{
					//error
					// deal with the error code 
					switch (dwError = GetLastError()) 
					{ 
					case ERROR_HANDLE_EOF: 
						{ 
							// we have reached the end of the file 
							// during the call to ReadFile 
							
							// code to handle that 
							
							msg.Format("Fail to download the %d block(read ACK failed)\r\n!",usCurBlock);
							
							DisplayMsg( msg );

							AfxMessageBox(msg);
							CloseHandle(hFile_com);
							file_data.Close();
							
							g_bDownloading = 0;
							return -1;
						} 
						break;
					case ERROR_IO_PENDING: 
						{ 
							// asynchronous i/o is still in progress 
							
							Sleep(CHECK_TIME);
							msg.Format("%c", g_DisBuf[i%4]);
							DisplayMsgEx( msg );
						}
						break;
					default:
						Sleep(CHECK_TIME);
						msg.Format("%c", g_DisBuf[i%4]);
						DisplayMsgEx( msg );
						break;
					}
				}
				else
				{
					
					msg.Format("Receive ACK : 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x .", 
						(unsigned char)buf_com[0],(unsigned char)buf_com[1],(unsigned char)buf_com[2],(unsigned char)buf_com[3],(unsigned char)buf_com[4],(unsigned char)buf_com[5]);
					
					DisplayMsg( msg );
					
					if(buf_com[0] == 'A' && buf_com[1]=='K' && memcmp(buf_com+2, buf+2, 4)==0)
					{
						//succ
						msg = "...correct!\r\n";
						DisplayMsg( msg );
						break;
					}
					else
					{
						msg = "...NOT correct!\r\n";
						DisplayMsg( msg );
					}					
				}
			}
			else
			{
				Sleep(CHECK_TIME);
				/*re send*/
				//if(i>0)
				//{
				//	WriteFile(hFile_com, buf, /*nReadSize*/1024 + 6, (unsigned long *) &nWriteSize,NULL);
				//}	
				
				msg.Format("%c", g_DisBuf[i%5]);
				DisplayMsgEx( msg );
			}			
		}
		if(i==((nTimeOut*1000)/(CHECK_TIME)))
		{
			
			msg.Format("Fail to download the %d block!(read ACK timeout)\r\n",usCurBlock);
			
			DisplayMsg( msg );
			
			AfxMessageBox(msg);

			CloseHandle(hFile_com);
			file_data.Close();

			g_bDownloading = 0;

			AfxBeginThread(IdleThread, NULL);

			g_pProgressCtrl->SetPos(0);
			return -1;
		}
		
		g_pProgressCtrl->SetPos(usCurBlock);

		nFileSize -= nBlockSize;
		usCurBlock ++;
	}
	CloseHandle(hFile_com);
	file_data.Close();
	g_bDownloading = 0;

	msg = "Download finish!\r\n";
	DisplayMsg( msg );

	AfxMessageBox("Download finish!");

	AfxBeginThread(IdleThread, NULL);
	
	return 0;
}
UINT CDownloadToolDlg::IdleThread(LPVOID)
{
	HANDLE hFile_com;
	CString msg;
	char buf[10];
	int nReadSize;
	DWORD error;
	COMSTAT ComStat;

	int		nBitPerSec	=	m_nBitPerSec;
	int		nSelCOM		=	m_nSelCOM	;
	//int		nDataBits	=	m_nDataBits	;
	//int		nFlowCtrl	=	m_nFlowCtrl	;
	//int		nParity		=	m_nParity	;
	//int		nBlockSize	=	m_nBlockSize;
	//UINT	nTimeOut	=	m_nTimeOut	;


	if(g_bDownloading == 1)
	{
		return -1;
	}

	hFile_com = CreateFile(
		/*"COM5"*/Order_to_COMName[nSelCOM],
		GENERIC_READ | GENERIC_WRITE,
		0,
		NULL,
		OPEN_EXISTING,
		0,
		NULL
		);
	if(hFile_com == NULL)
	{
		msg.Format("Fail to open %s, error=%d.\r\n",Order_to_COMName[nSelCOM],GetLastError());
		
		DisplayMsg( msg );
		
		return -1;
	}
	
	DCB dcb;
	if(::GetCommState( hFile_com, &dcb )==0)
	{
		msg.Format("\r\nGet %s State failed! error=%d.\r\n",Order_to_COMName[nSelCOM],GetLastError());
		
		DisplayMsg( msg );	
		
		CloseHandle(hFile_com);
		return -1;
	}
	dcb.BaudRate = Order_to_BitPerSec[nBitPerSec]/*CBR_115200*/;
	dcb.ByteSize = 8;
	dcb.StopBits = ONESTOPBIT;
	dcb.Parity = NOPARITY;
	
	if(::SetCommState( hFile_com, &dcb )==0)
	{
		msg.Format("\r\nSet %s State failed! error=%d\r\n.",Order_to_COMName[nSelCOM],GetLastError());
		
		DisplayMsg( msg );
				
		CloseHandle(hFile_com);
		return -1;
		
	}

	
	while(g_bDownloading == 0)
	{
		::ClearCommError(hFile_com, &error,&ComStat);
        if(ComStat.cbInQue!=0)
		{			
			/*Read a char*/
			if(ReadFile(hFile_com, buf, 1, (unsigned long *)&nReadSize,NULL) <= 0)
			{		
				Sleep(CHECK_TIME);				
			}
			else
			{
				msg.Format("%c",buf[0]);
				
				DisplayMsg( msg );
			}
		}
		else
		{
			Sleep(50);
		}		
	}
	CloseHandle(hFile_com);
	return 0;
}
		

void CDownloadToolDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent == 1)
	{	
		if(g_bDownloading == 0)
		{
			GetDlgItem(IDSTART)->EnableWindow(TRUE);
			GetDlgItem(IDSTOP)->EnableWindow(FALSE);
		}
		else
		{
			GetDlgItem(IDSTART)->EnableWindow(FALSE);
			GetDlgItem(IDSTOP)->EnableWindow(TRUE);
		}
	}
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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