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

📄 ifsusbdlg.cpp

📁 D12和单片机通讯的电脑部分通讯程序.用于测试是否连接通畅.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	HANDLE hSram = INVALID_HANDLE_VALUE;
	ULONG nBytesWrite;
	BOOL bSuccess = FALSE;

	ULONG nByteNumber = pDeviceControl->wValue;

	//send SRAM address through IO CONTROL
	bSuccess = device_ctl(pDeviceControl,false);

	if (!bSuccess)
	{
		MessageBox("Set SRAM address failed!","Error",MB_OK | MB_ICONWARNING);		
		return bSuccess;
	}

	//if set sram address is successful
	hSram = open_file( SRAM_PIPE_OUT);

	if (hSram == INVALID_HANDLE_VALUE)
		MessageBox("Can not open handle,write SRAM failed!","Error",MB_OK | MB_ICONWARNING);
	else
	{
		bSuccess = WriteFile(hSram,
					  pBuff,
	                  nByteNumber,			
	                  &nBytesWrite,
	                  NULL);		 

		if (!bSuccess)
		{
			reset_pipe(hSram);
			MessageBox("Write SRAM failed!","Error",MB_OK | MB_ICONWARNING);		
		}		
				
		CloseHandle(hSram);		
	}
	
	return bSuccess;	
}

void CIfsUsbDlg::SetSwitchStatus()
{
	//set switch checkbox status
	if (m_SwitchStatus & 0x01)	m_Sw1.SetCheck(1);
	else						m_Sw1.SetCheck(0);
	if (m_SwitchStatus & 0x02)	m_Sw2.SetCheck(1);
	else						m_Sw2.SetCheck(0);
	if (m_SwitchStatus & 0x04)	m_Sw3.SetCheck(1);
	else						m_Sw3.SetCheck(0);
	if (m_SwitchStatus & 0x08)	m_Sw4.SetCheck(1);
	else						m_Sw4.SetCheck(0);
	if (m_SwitchStatus & 0x10)	m_Sw5.SetCheck(1);
	else						m_Sw5.SetCheck(0);
	if (m_SwitchStatus & 0x20)	m_Sw6.SetCheck(1);
	else						m_Sw6.SetCheck(0);
	if (m_SwitchStatus & 0x40)	m_Sw7.SetCheck(1);
	else						m_Sw7.SetCheck(0);
	if (m_SwitchStatus & 0x80)	m_Sw8.SetCheck(1);
	else						m_Sw8.SetCheck(0);
	UpdateData(false);

}

LONG CIfsUsbDlg::OnSwChange(WPARAM wParam, LPARAM lParam)
{

	SetSwitchStatus();
	return 0;
}

void CIfsUsbDlg::OnButtonLoopTest() 
{
	// TODO: Add your control notification handler code here
	BOOL bSuccess = FALSE;
	HANDLE hSramWrite = INVALID_HANDLE_VALUE;
	HANDLE hSramRead = INVALID_HANDLE_VALUE;
	BYTE *poutBuf = NULL;
	BYTE *pinBuf = NULL;
	ULONG nBytesWrite;
	ULONG nBytesRead;
	UINT i,j;

	DWORD dLoopStartTime;
	DWORD dLoopEndTime;
	UINT dLoopTimes = 0;
	UINT dMemorySize = 128;


	CString temp;
	CString strTemp,strTemp2;
	m_StaticLoopTestResult = "0";
	//update loop test seconds and times edit variable
	UpdateData(true);	
	if (m_EditLoopTimes == 0)
	{
		MessageBox("LoopBack test times can not be 0!","Warning",MB_OK | MB_ICONWARNING);
		return ;
	}

	m_Progress.SetRange(0,m_EditLoopTimes);
	m_Progress.SetPos(0);
	m_StaticLoopTestResult = "Transfer Rate: 0   KB/S";
	UpdateData(false);


	sDeviceControl.bRequest = RAM_COMMAND_LOOPBACK;
	sDeviceControl.wIndex   = 0;
	sDeviceControl.wValue   = 0;
	sDeviceControl.wLength  = 0;						//read back switch status,one byte
	sDeviceControl.pBuffer  = NULL;
	
	bSuccess = device_ctl(&sDeviceControl,false);
	
	if (!bSuccess)
	{
		MessageBox("Set LOOPBACK command failed!","Error",MB_OK | MB_ICONWARNING);		
		return ;
	}


	hSramWrite = open_file( SRAM_PIPE_OUT);
	poutBuf = (PBYTE)malloc(dMemorySize);

	if ((hSramWrite == INVALID_HANDLE_VALUE) || (poutBuf == NULL))
	{
		if (hSramWrite != INVALID_HANDLE_VALUE)
		{
			CloseHandle(hSramWrite);
		}
		if (poutBuf != NULL)
		{
			free(poutBuf);
		}
	}
	else
	{

		//initialize data that will write to sram
		for (i=0;i<dMemorySize;i++)
		{
			*(poutBuf+i) = 0x55;
		}


		hSramRead = open_file( SRAM_PIPE_IN);
		pinBuf = (PBYTE)malloc(dMemorySize);
		if ((hSramRead == INVALID_HANDLE_VALUE) || 	(pinBuf == NULL))
		{
			if (hSramRead != INVALID_HANDLE_VALUE)
			{
				CloseHandle(hSramRead);
			}
			if (pinBuf != NULL)
			{
				free(pinBuf);
			}
			free(poutBuf);
			CloseHandle(hSramWrite);
		}
		else
		{
			dLoopStartTime = timeGetTime();
			for (;;)
			{
				dLoopTimes++;
				m_Progress.SetPos(dLoopTimes);
			//begin loopback test:
			bSuccess = WriteFile(hSramWrite,
	                  poutBuf,
	                  dMemorySize,			
	                  &nBytesWrite,
	                  NULL);		 
			if (bSuccess)	
			{
				//read back
				bSuccess = ReadFile(hSramRead,
							pinBuf,
							dMemorySize,
							&nBytesRead,
							NULL); 		
	
				if (bSuccess)	
				{
					UCHAR bErrorCount = 0;
					CString strError = "";

					for (i=0;i<dMemorySize;i++)
					{
						if (*(pinBuf+i) != 0x55)	
						{	
							bErrorCount++;
						}
					}
					if (bErrorCount >0)
					{
						strError.Format("%x",bErrorCount);
						MessageBox("Write and read Compare error count is"+strError+" !","Error",MB_OK);
					}
				}
				else
				{
					reset_pipe(hSramRead);
					MessageBox("Read error!","Error",MB_OK);
				}

			}
			else
			{
				reset_pipe(hSramWrite);
				MessageBox("Write error!","Error",MB_OK);
			}

			if (dLoopTimes >=m_EditLoopTimes)
				break;

			}
			free(poutBuf);
			free(pinBuf);
			CloseHandle(hSramWrite);
			CloseHandle(hSramRead);

			dLoopEndTime = timeGetTime();
			CString strTemp;
			strTemp.Format("%d",(dLoopTimes * dMemorySize *2)/(dLoopEndTime - dLoopStartTime));
			m_StaticLoopTestResult = "Transfer Rate: " + strTemp + " KB/S";
			m_Progress.SetPos(0);
			UpdateData(false);
		}
	}



	//After loopback test finished,0x0000H~0x007fH of RAM will be altert,
	//so if current RAM ListBox should be refresh if the content of 
	//0x0000H~0x007FH should be display.
	m_ListTopIndex = m_ListSram.GetTopIndex();
	if (m_ListTopIndex<8)	//0~0x7f
	{
		sDeviceControl.bRequest = RAM_COMMAND_READ;		//read ram
		sDeviceControl.wIndex   = m_ListTopIndex*8;		//start address
		sDeviceControl.wValue   = 128;					//read length
		sDeviceControl.wLength  = 0;					//read ram is a vendor request without data stage 
		sDeviceControl.pBuffer  = NULL;					//wLength is 0,pBuffer will always NULL

		bSuccess = ReadSram(&sDeviceControl,&m_SramPage[0]);

		if (bSuccess)
		{
			for (i=0;i<16;i++)
			{
				m_ListSram.GetText(m_ListTopIndex+i,temp);
				strTemp2 = temp.Mid(0,6);
				for (j=0;j<8;j++)
				{
					strTemp.Format("%X",m_SramPage[i*8+j]);
					if (strTemp.GetLength() == 1)
					{
						strTemp = "0"+strTemp;
					}
					strTemp = " "+strTemp;	//add a space
					strTemp2 += strTemp;	//get data string
				}
				//update List content
				m_ListSram.DeleteString(m_ListTopIndex+i);
				m_ListSram.InsertString(m_ListTopIndex+i,strTemp2);
			}
		}
		else
		{
			MessageBox("Refresh RAM list failed!","Error",MB_OK | MB_ICONWARNING);		
		}

		m_ListSram.SetTopIndex(m_ListTopIndex);
		UpdateData(false);
	}
}

void CIfsUsbDlg::OnButtonFirmwareVersion() 
{
	// TODO: Add your control notification handler code here
	BYTE	bFirmWareVersion;
	BOOL	bSuccess;
	CString strTemp = "";
	CString strTemp2 = "";

	sDeviceControl.bRequest = GET_FIRMWARE_VERSION;
	sDeviceControl.wIndex   = 0;
	sDeviceControl.wValue   = 0;
	sDeviceControl.wLength  = 1;						//read back switch status,one byte
	sDeviceControl.pBuffer  = &bFirmWareVersion;
	
	bSuccess = device_ctl(&sDeviceControl,true);
	
	if (!bSuccess)
	{
		MessageBox("Read firmware version failed!","Error",MB_OK | MB_ICONWARNING);		
	}
	else
	{
		strTemp.Format("%d",((bFirmWareVersion&0xf0)>>4));
		strTemp2.Format("%d",(bFirmWareVersion&0x0f));
		strTemp += "."+strTemp2;
	}
	
	m_FirmWareVersion = strTemp;
	UpdateData(false);
}

void CIfsUsbDlg::OnButtonSetSerialNumber() 
{
	// TODO: Add your control notification handler code here
	BOOL	bSuccess;

	UpdateData(true);
	m_SerialNumber[0] = EditToByte(m_EditSn4);
	m_SerialNumber[1] = EditToByte(m_EditSn3);
	m_SerialNumber[2] = EditToByte(m_EditSn2);
	m_SerialNumber[3] = EditToByte(m_EditSn1);

	sDeviceControl.bRequest = SET_DEVICE_SERIAL_NUMBER;
	sDeviceControl.wIndex   = 0;
	sDeviceControl.wValue   = 0;
	sDeviceControl.wLength  = 4;						//read back switch status,one byte
	sDeviceControl.pBuffer  = (PUCHAR)(&m_SerialNumber[0]);
	
	bSuccess = device_ctl(&sDeviceControl,false);
	
	if (!bSuccess)
	{
		MessageBox("Write serial number failed!","Error",MB_OK | MB_ICONWARNING);		
	}

}

//
BOOL CIfsUsbDlg::EditHexCheck(CString m_sStr)
{
	int i, iLength;
	UCHAR b;
	BOOL bSuccess = true;

	iLength = m_sStr.GetLength();
	if (iLength == 0)
		bSuccess = true;
	else
	{
		for (i=0;i<iLength;i++)
		{
			b = m_sStr.GetAt(i);
			if (!((b>=0x30 && b<=0x39) || (b>=0x41 && b<=0x46)))
			{
				bSuccess = false;
				break;
			}
		}
	}

	return bSuccess;
}

void CIfsUsbDlg::OnChangeEditSn1() 
{
	// 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
	BOOL bSuccess;
	UpdateData(true);
	bSuccess = EditHexCheck(m_EditSn1);
	if (!bSuccess)
	{
		MessageBox("Must input HEX format number!","Error",MB_OK | MB_ICONWARNING);	
		m_EditSn1 = "";
		UpdateData(false);
	}		
}

void CIfsUsbDlg::OnChangeEditSn2() 
{
	// 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
	BOOL bSuccess;
	UpdateData(true);
	bSuccess = EditHexCheck(m_EditSn2);
	if (!bSuccess)
	{
		MessageBox("Must input HEX format number!","Error",MB_OK | MB_ICONWARNING);	
		m_EditSn2 = "";
		UpdateData(false);
	}	
}

void CIfsUsbDlg::OnChangeEditSn3() 
{
	// 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
	BOOL bSuccess;
	UpdateData(true);
	bSuccess = EditHexCheck(m_EditSn3);
	if (!bSuccess)
	{
		MessageBox("Must input HEX format number!","Error",MB_OK | MB_ICONWARNING);	
		m_EditSn3 = "";
		UpdateData(false);
	}		
}

void CIfsUsbDlg::OnChangeEditSn4() 
{
	// 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
	BOOL bSuccess;
	UpdateData(true);
	bSuccess = EditHexCheck(m_EditSn4);
	if (!bSuccess)
	{
		MessageBox("Must input HEX format number!","Error",MB_OK | MB_ICONWARNING);	
		m_EditSn4 = "";
		UpdateData(false);
	}		
}



void CIfsUsbDlg::OnChangeEditLoopTimes() 
{
	// 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);
}

⌨️ 快捷键说明

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