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

📄 filetransferdlg.cpp

📁 上位机与下位机的USB通讯
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	if (m_hUSBDevice != INVALID_HANDLE_VALUE)
	{
		// Close the USBXpress device
		SI_Close(m_hUSBDevice);
		m_hUSBDevice = INVALID_HANDLE_VALUE;
	}
	
	// Unregister for device notification
	UnregisterNotification();

	CDialog::OnOK();
}

void CFileTransferDlg::OnUpdateDeviceList() 
{
	FillDeviceList();
}

void CFileTransferDlg::FillDeviceList()
{
	SI_DEVICE_STRING	devStr;
	DWORD				dwNumDevices	= 0;
	CComboBox*			pDevList		= (CComboBox*)GetDlgItem(IDC_DEVICE_SELECT);

	// Make sure that pDevList is not NULL
	if (pDevList)
	{
		// Delete all the strings in the list
		for (int i = 0; i < pDevList->GetCount(); i++)
		{
			pDevList->DeleteString(0);
		}

		// Get the number of USBXpress devices connected
		SI_STATUS status = SI_GetNumDevices(&dwNumDevices);

		if (status == SI_SUCCESS)
		{
			// Go through each device and add their serial numbers to the list
			for (DWORD d = 0; d < dwNumDevices; d++)
			{
				status = SI_GetProductString(d, devStr, SI_RETURN_SERIAL_NUMBER);

				if (status == SI_SUCCESS)
				{
					if (pDevList)
						pDevList->AddString(devStr);
				}
			}
		}

		//Set the current selection to the first item
		pDevList->SetCurSel(0);
	}
}

BOOL CFileTransferDlg::WriteFileData()
{
	BOOL success = TRUE;

	// Make sure a file is specified in the dialog
	if (m_sTXFileName.GetLength() > 0)
	{
		CFile file;

		// Open the file for reading
		if (file.Open(m_sTXFileName, CFile::modeRead | CFile::shareDenyNone))
		{
			// Make sure the file size is 4KB or less
			if (1)//(file.GetLength() && (file.GetLength() <= MAX_PACKET_SIZE_READ))
			{
				DWORD		size			= file.GetLength();
				DWORD		dwBytesWritten	= 0;
				DWORD		dwBytesRead		= 0;
				BYTE		buf[MAX_PACKET_SIZE_WRITE];

				// Set up the write message command with the file size
				buf[0] = FT_WRITE_MSG;
				buf[1] = (BYTE)(size & 0x000000FF);
				buf[2] = (BYTE)((size & 0x0000FF00) >> 8);

				// Send write file size message with a 1 second timeout
				if (DeviceWrite(buf, FT_MSG_SIZE, &dwBytesWritten, 1000))
				{
					if (dwBytesWritten == FT_MSG_SIZE)
					{
						DWORD numPkts		= (size / MAX_PACKET_SIZE_WRITE) + (((size % MAX_PACKET_SIZE_WRITE) > 0)? 1 : 0);
						DWORD numLoops		= (numPkts / MAX_WRITE_PKTS) + (((numPkts % MAX_WRITE_PKTS) > 0)? 1 : 0);
						DWORD counterPkts	= 0;
						DWORD counterLoops	= 0;
						DWORD totalWritten	= 0;

						// Now write data to board
						// After each 512-byte packet, the device will send an 0xFF ACK signal
						while ((counterLoops < numLoops) && success)
						{
							int	i = 0;

							while ((i < MAX_WRITE_PKTS) && (counterPkts < numPkts) && success)
							{
								DWORD dwWriteLength	= 0;

								// Determine if the size is the maximum, or a partial packet
								if ((size - totalWritten) < MAX_PACKET_SIZE_WRITE)
								{
									dwWriteLength = size - totalWritten;
								}
								else
								{
									dwWriteLength = MAX_PACKET_SIZE_WRITE;
								}

								// Read in the data for the packet from the file
								memset(buf, 0, dwWriteLength);
								file.Read(buf, dwWriteLength);
								dwBytesWritten = 0;

								//Write to the device with a 1 second timeout
								success = DeviceWrite(buf, dwWriteLength, &dwBytesWritten, 1000);
								totalWritten += dwWriteLength;
								counterPkts++;
								i++;
							}

							if (success)
							{
								memset(buf, 0, 1);

								// Check for ACK packet after writing 512 bytes or after last packet
								// with a 3 second timeout
								while ((buf[0] != 0xFF) && success)
								{
									success = DeviceRead(buf, 1, &dwBytesRead, 3000);
								}
							}

							counterLoops++;
						}
						
						if (!success)
						{
							AfxMessageBox("Target device failure while sending file data.\nCheck file size.");
							success = FALSE;
						}
					}
					else
					{
						AfxMessageBox("Incomplete write file size message sent to device.");
						success = FALSE;
					}
				}
				else
				{
					AfxMessageBox("Target device failure while sending file size information.");
					success = FALSE;
				}
			}
			else
			{
				AfxMessageBox("Limit for file size is 4KB");
				success = FALSE;
			}

			file.Close();
		}
		else
		{
			CString err;
			err.Format("Failed opening file:\n%s", m_sTXFileName);
			AfxMessageBox(err);
			success = FALSE;
		}
	}
	else
	{
		AfxMessageBox("Error:  No file selected.");
		success = FALSE;
	}

	return success;
}

BOOL CFileTransferDlg::ReadFileData()
{
	BOOL success = TRUE;
    DWORD	length;
	if ((length = m_sRXFileName.GetLength()) > 0)
	{
		CFile	file;

		// Open the temporary file for writing
		if (file.Open(m_sRXFileName, CFile::modeWrite | CFile::modeCreate | CFile::shareDenyNone))
		{
			DWORD		dwBytesRead		= 0;
			DWORD		dwBytesWritten	= 0;
			BYTE		buf[MAX_PACKET_SIZE_READ];
			BYTE		msg[FT_MSG_SIZE];

			// Setup the read command buffer
			msg[0] = FT_READ_MSG;
			msg[1] = (BYTE)0xFF;
			msg[2] = (BYTE)0xFF;

			// Send the read command buffer to the device with a 1 second timeout
			if (DeviceWrite(msg, FT_MSG_SIZE, &dwBytesWritten, 1000))
			{
				DWORD size			= 0;
				DWORD counterPkts	= 0;
				DWORD numPkts		= 0;
				DWORD totalRead		= 0;

				// Reset the message buffer
				memset(msg, 0, FT_MSG_SIZE);

				// Read the response containing the size with a 3 second timeout
				if (DeviceRead(buf, FT_MSG_SIZE, &dwBytesRead, 3000))
				{
					size	= (buf[1] & 0x000000FF) | ((buf[2] << 8) & 0x0000FF00);
					numPkts	= (size/MAX_PACKET_SIZE_READ) + (((size % MAX_PACKET_SIZE_READ) > 0)? 1 : 0);

					// Now read data from board
					while ((counterPkts < numPkts) && success)
					{
						DWORD dwReadLength = 0;
						dwBytesRead = 0;

						// Determine if the size is the max or a partial packet
						if ((size - totalRead) < MAX_PACKET_SIZE_READ)
						{
							dwReadLength = size - totalRead;
						}
						else
						{
							dwReadLength = MAX_PACKET_SIZE_READ;
						}
						
						memset(buf, 0, dwReadLength);
						
						//Read the packet from the device with a 3 second timeout
						if (DeviceRead(buf, dwReadLength, &dwBytesRead, 3000))
						{
							totalRead += dwBytesRead;
							file.Write(buf, dwBytesRead);
							if (dwBytesRead == dwReadLength)
							{
								counterPkts++;
							}
						}
						else
						{
							AfxMessageBox("Failed reading file packet from target device.");
							success = FALSE;
						}


					}
					
				}
				else
				{
					AfxMessageBox("Failed reading file size message from target device.");
					success = FALSE;
				}
			}
			else
			{
				AfxMessageBox("Failed sending read file message to target device.");
				success = FALSE;
			}

			if (file.GetLength() == 0)
			{
				CString err;
				err.Format("File has 0 length:\n%s", m_sRXFileName);
				AfxMessageBox(err);
			}

			file.Close();
		}
		else
		{
			CString err;
			err.Format("Failed opening file:\n%s", m_sRXFileName);
			AfxMessageBox(err);
			success = FALSE;
		}
	}
	else
	{
		AfxMessageBox("Error:  No file selected.");
		success = FALSE;
	}

	return success;
}

BOOL CFileTransferDlg::DeviceRead(BYTE* buffer, DWORD dwSize, DWORD* lpdwBytesRead, DWORD dwTimeout)
{
	DWORD		tmpReadTO, tmpWriteTO;
	SI_STATUS	status			= SI_SUCCESS;

	// Save timeout values.
	SI_GetTimeouts(&tmpReadTO, &tmpWriteTO);

	// Set a timeout for the read
	SI_SetTimeouts(dwTimeout, 0);

	// Read the data
	status = SI_Read(m_hUSBDevice, buffer, dwSize, lpdwBytesRead);
	
	// Restore timeouts
	SI_SetTimeouts(tmpReadTO, tmpWriteTO);

	return (status == SI_SUCCESS);
}

BOOL CFileTransferDlg::DeviceWrite(BYTE* buffer, DWORD dwSize, DWORD* lpdwBytesWritten, DWORD dwTimeout)
{
	DWORD		tmpReadTO, tmpWriteTO;
	SI_STATUS	status	= SI_SUCCESS;

	// Save timeout values.
	SI_GetTimeouts(&tmpReadTO, &tmpWriteTO);

	// Set a timeout for the write
	SI_SetTimeouts(0, dwTimeout);

	// Write the data
	status = SI_Write(m_hUSBDevice, buffer, dwSize, lpdwBytesWritten);

	// Restore timeouts
	SI_SetTimeouts(tmpReadTO, tmpWriteTO);

	return (status == SI_SUCCESS);
}

⌨️ 快捷键说明

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