📄 f32x_filetransferdlg.cpp
字号:
// F32x_FileTransferDlg.cpp : implementation file
//
#include "stdafx.h"
#include "F32x_FileTransfer.h"
#include "F32x_FileTransferDlg.h"
#include "SiF32xUSB.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CF32x_FileTransferDlg dialog
CF32x_FileTransferDlg::CF32x_FileTransferDlg(CWnd* pParent /*=NULL*/)
: CDialog(CF32x_FileTransferDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CF32x_FileTransferDlg)
m_sTXFileName = _T("");
m_sRXFileName = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CF32x_FileTransferDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CF32x_FileTransferDlg)
DDX_Text(pDX, IDC_TX_FILE_NAME, m_sTXFileName);
DDX_Text(pDX, IDC_RX_FILE_NAME, m_sRXFileName);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CF32x_FileTransferDlg, CDialog)
//{{AFX_MSG_MAP(CF32x_FileTransferDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_TRANSFER_DATA, OnTransferData)
ON_BN_CLICKED(IDC_RECEIVE_DATA, OnReceiveData)
ON_BN_CLICKED(IDC_BROWSE_TX_FILE, OnBrowseTxFile)
ON_BN_CLICKED(IDC_BROWSE_RX_FILE, OnBrowseRxFile)
ON_BN_CLICKED(IDC_UPDATE_DEVICE_LIST, OnUpdateDeviceList)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CF32x_FileTransferDlg message handlers
BOOL CF32x_FileTransferDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// Init. member variables
m_hUSBDevice = INVALID_HANDLE_VALUE;
// Get devices, init. combo box with their names
FillDeviceList();
return TRUE; // return TRUE unless you set the focus to a control
}
void CF32x_FileTransferDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CF32x_FileTransferDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CF32x_FileTransferDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CF32x_FileTransferDlg::OnTransferData()
{
CComboBox* pDevList = (CComboBox*)GetDlgItem(IDC_DEVICE_SELECT);
BeginWaitCursor();
if (pDevList)
{
// Open selected device.
if (pDevList->GetCurSel() >= 0)
{
F32x_STATUS status = F32x_Open(pDevList->GetCurSel(), &m_hUSBDevice);
// Open selected file.
if (status == F32x_SUCCESS)
{
// Write file to device in MAX_PACKET_SIZE_WRITE-byte chunks.
WriteFileData();
// Close device.
F32x_Close(m_hUSBDevice);
m_hUSBDevice = INVALID_HANDLE_VALUE;
}
}
}
EndWaitCursor();
}
void CF32x_FileTransferDlg::OnReceiveData()
{
CComboBox* pDevList = (CComboBox*)GetDlgItem(IDC_DEVICE_SELECT);
BeginWaitCursor();
if (pDevList)
{
// Open selected device.
if (pDevList->GetCurSel() >= 0)
{
F32x_STATUS status = F32x_Open(pDevList->GetCurSel(), &m_hUSBDevice);
// Open temporary file based on file name of input file
if (status == F32x_SUCCESS)
{
// Read file data in MAX_PACKET_SIZE_READ-byte chunks and write to temp file.
// Compare temp. file with original
ReadFileData();
// Close device.
F32x_Close(m_hUSBDevice);
m_hUSBDevice = INVALID_HANDLE_VALUE;
}
}
}
EndWaitCursor();
}
void CF32x_FileTransferDlg::OnBrowseTxFile()
{
CFileDialog fileDlg(TRUE);
if (fileDlg.DoModal() == IDOK)
{
m_sTXFileName = fileDlg.GetPathName();
UpdateData(FALSE);
}
}
void CF32x_FileTransferDlg::OnBrowseRxFile()
{
CFileDialog fileDlg(TRUE);
if (fileDlg.DoModal() == IDOK)
{
m_sRXFileName = fileDlg.GetPathName();
UpdateData(FALSE);
}
}
void CF32x_FileTransferDlg::OnOK()
{
// Close handle if open
if (m_hUSBDevice != INVALID_HANDLE_VALUE)
{
F32x_Close(m_hUSBDevice);
m_hUSBDevice = INVALID_HANDLE_VALUE;
}
CDialog::OnOK();
}
void CF32x_FileTransferDlg::OnUpdateDeviceList()
{
FillDeviceList();
}
void CF32x_FileTransferDlg::FillDeviceList()
{
F32x_DEVICE_STRING devStr;
DWORD dwNumDevices = 0;
CComboBox* pDevList = (CComboBox*)GetDlgItem(IDC_DEVICE_SELECT);
if (pDevList)
{
int numDevs = pDevList->GetCount();
for (int i = 0; i < numDevs; i++)
{
pDevList->DeleteString(0);
}
F32x_STATUS status = F32x_GetNumDevices(&dwNumDevices);
if (status == F32x_SUCCESS)
{
for (DWORD d = 0; d < dwNumDevices; d++)
{
status = F32x_GetProductString(d, devStr, F32x_RETURN_SERIAL_NUMBER);
if (status == F32x_SUCCESS)
{
if (pDevList) // Fill device list
pDevList->AddString(devStr);
}
}
}
pDevList->SetCurSel(0);
}
}
BOOL CF32x_FileTransferDlg::WriteFileData()
{
BOOL success = TRUE;
if (m_sTXFileName.GetLength() > 0)
{
CFile file;
if (file.Open(m_sTXFileName, CFile::modeRead | CFile::shareDenyNone))
{
DWORD size = file.GetLength();
DWORD dwBytesWritten = 0;
DWORD dwBytesRead = 0;
BYTE buf[MAX_PACKET_SIZE_WRITE];
buf[0] = FT_WRITE_MSG;
buf[1] = (char)(size & 0x000000FF);
buf[2] = (char)((size & 0x0000FF00) >> 8);
// Send write file size message
if (DeviceWrite(buf, FT_MSG_SIZE, &dwBytesWritten))
{
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;
if ((size - totalWritten) < MAX_PACKET_SIZE_WRITE)
{
dwWriteLength = size - totalWritten;
}
else
{
dwWriteLength = MAX_PACKET_SIZE_WRITE;
}
memset(buf, 0, dwWriteLength);
file.Read(buf, dwWriteLength);
dwBytesWritten = 0;
success = DeviceWrite(buf, dwWriteLength, &dwBytesWritten);
totalWritten += dwWriteLength;
counterPkts++;
i++;
}
if (success)
{
memset(buf, 0, 1);
// Check for ACK packet after writing 512 bytes or after last packet
while ((buf[0] != 0xFF) && success)
{
success = DeviceRead(buf, 1, &dwBytesRead);
}
}
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;
}
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 CF32x_FileTransferDlg::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];
msg[0] = (BYTE)FT_READ_MSG;
msg[1] = (BYTE)0xFF;
msg[2] = (BYTE)0xFF;
if (DeviceWrite(msg, FT_MSG_SIZE, &dwBytesWritten))
{
DWORD size = 0;
DWORD counterPkts = 0;
DWORD numPkts = 0;
memset(buf, 0, FT_MSG_SIZE);
if (DeviceRead(buf, FT_MSG_SIZE, &dwBytesRead))
{
size = (buf[1] & 0x000000FF) | ((buf[2] << 8) & 0x0000FF00);
numPkts = (size/MAX_PACKET_SIZE_READ) + (((size % MAX_PACKET_SIZE_READ) > 0)? 1 : 0);
// Next line added in from Test Example
DWORD totalRead = 0;
// Now read data from board
while (counterPkts < numPkts && success)
{
DWORD dwReadLength = 0;
dwBytesRead = 0;
if ((size - totalRead) < MAX_PACKET_SIZE_READ)
{
dwReadLength = size - totalRead;
}
else
{
dwReadLength = MAX_PACKET_SIZE_READ;
}
memset(buf, 0, dwReadLength);
if (DeviceRead(buf, dwReadLength, &dwBytesRead))
{
file.Write(buf, dwReadLength);
}
else
{
AfxMessageBox("Failed reading file packet from target device.");
success = FALSE;
}
counterPkts++;
}
}
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 CF32x_FileTransferDlg::DeviceRead(BYTE* buffer, DWORD dwSize, DWORD* lpdwBytesRead, DWORD dwTimeout)
{
DWORD tmpReadTO, tmpWriteTO; // Current timeout values.
F32x_STATUS status = F32x_SUCCESS;
DWORD dwQueueStatus = F32x_RX_NO_OVERRUN;
DWORD dwBytesInQueue = 0;
// Save timeout values.
F32x_GetTimeouts(&tmpReadTO, &tmpWriteTO);
if (dwTimeout == 0)
{
// Wait forever until queue ready
while (status == F32x_SUCCESS && (!(dwQueueStatus & F32x_RX_READY)))
{
status = F32x_CheckRXQueue(m_hUSBDevice, &dwBytesInQueue, &dwQueueStatus);
}
}
else
{
// Set a timeout for the read
F32x_SetTimeouts(dwTimeout, 0);
}
if (status == F32x_SUCCESS)
{
status = F32x_Read(m_hUSBDevice, buffer, dwSize, lpdwBytesRead);
}
// Restore timeouts
F32x_SetTimeouts(tmpReadTO, tmpWriteTO);
return (status == F32x_SUCCESS);
}
BOOL CF32x_FileTransferDlg::DeviceWrite(BYTE* buffer, DWORD dwSize, DWORD* lpdwBytesWritten, DWORD dwTimeout)
{
DWORD tmpReadTO, tmpWriteTO; // Current timeout values.
F32x_STATUS status = F32x_SUCCESS;
// Save timeout values.
F32x_GetTimeouts(&tmpReadTO, &tmpWriteTO);
// Set a timeout for the write
F32x_SetTimeouts(0, dwTimeout);
status = F32x_Write(m_hUSBDevice, buffer, dwSize, lpdwBytesWritten);
// Restore timeouts
F32x_SetTimeouts(tmpReadTO, tmpWriteTO);
return (status == F32x_SUCCESS);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -