📄 cp210xbaudratealiasconfigdlg.cpp
字号:
// 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 CCP210xBaudRateAliasConfigDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCP210xBaudRateAliasConfigDlg::ResetDeviceList()
{
DWORD numDevices;
// Obtain the number of devices connected
CP210x_GetNumDevices(&numDevices);
// Reset the content of our list
m_Devices.ResetContent();
// Loop through each device, and put its product string in the list
for (DWORD i = 0; i < numDevices; i++)
{
CP210x_DEVICE_STRING serialString;
CP210x_GetProductString(i, serialString, CP210x_RETURN_FULL_PATH);
m_Devices.AddString(serialString);
}
// Set our list to select the first device in it
m_Devices.SetCurSel(0);
}
void CCP210xBaudRateAliasConfigDlg::OnButtonGetConfig()
{
// TODO: Add your control notification handler code here
// Check our current device selection
if (m_Devices.GetCurSel() > -1)
{
// If we are selecting a valid device, open it
if (CP210x_Open(m_Devices.GetCurSel(), &m_DeviceHandle) == CP210x_SUCCESS)
{
// Obtain the part num, only CP2102 and up will work
BYTE version;
if (CP210x_GetPartNumber(m_DeviceHandle, &version) == CP210x_SUCCESS)
{
if (version == CP210x_CP2101_VERSION)
{
MessageBox("CP2101 is not supported in this application", "Error", MB_OK | MB_ICONEXCLAMATION);
}
else
{
// If we have a CP2102, then get the baud config and refresh the baud config window
CP210x_GetBaudRateConfig(m_DeviceHandle, m_BaudConfigData);
RefreshBaudConfigList();
}
}
CP210x_Close(m_DeviceHandle);
}
}
}
void CCP210xBaudRateAliasConfigDlg::OnButtonSetConfig()
{
// TODO: Add your control notification handler code here
// Check our current device selection
if (m_Devices.GetCurSel() > -1)
{
// If we are selecting a valid device, open it
if (CP210x_Open(m_Devices.GetCurSel(), &m_DeviceHandle) == CP210x_SUCCESS)
{
// Obtain the part num, only CP2102 and up will work
BYTE version;
if (CP210x_GetPartNumber(m_DeviceHandle, &version) == CP210x_SUCCESS)
{
if (version == CP210x_CP2101_VERSION)
{
MessageBox("CP2101 is not supported in this application", "Error", MB_OK | MB_ICONEXCLAMATION);
}
else
{
if (MessageBox("Are you sure you want to set the current baud rate configuration to the part seleceted?", "Warning", MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
{
// Set the baud config data on the part, then immediately read the data back
// and refresh the baud config window
CP210x_SetBaudRateConfig(m_DeviceHandle, m_BaudConfigData);
CP210x_GetBaudRateConfig(m_DeviceHandle, m_BaudConfigData);
RefreshBaudConfigList();
}
}
}
CP210x_Close(m_DeviceHandle);
}
}
}
void CCP210xBaudRateAliasConfigDlg::OnButtonRestoreConfig()
{
// TODO: Add your control notification handler code here
// Check our current device selection
if (m_Devices.GetCurSel() > -1)
{
// If we are selecting a valid device, open it
if (CP210x_Open(m_Devices.GetCurSel(), &m_DeviceHandle) == CP210x_SUCCESS)
{
// Obtain the part num, only CP2102 and up will work
BYTE version;
if (CP210x_GetPartNumber(m_DeviceHandle, &version) == CP210x_SUCCESS)
{
if (version == CP210x_CP2101_VERSION)
{
MessageBox("CP2101 is not supported in this application", "Error", MB_OK | MB_ICONEXCLAMATION);
}
else
{
// Prompt to see if they want to destroy all current data and return to default
if (MessageBox("Restoring will set each baud rate configuration back to default, are you sure?", "Warning", MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
{
// If so, then set the default data back to the part, and immediatly read
// and update the baud config window
CP210x_SetBaudRateConfig(m_DeviceHandle, m_DefaultBaudConfigData);
CP210x_GetBaudRateConfig(m_DeviceHandle, m_BaudConfigData);
RefreshBaudConfigList();
}
}
}
CP210x_Close(m_DeviceHandle);
}
}
}
void CCP210xBaudRateAliasConfigDlg::OnDblclkListBaudratealias(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
// Get the line selected
int selectedLine = m_BaudRateAliasList.GetNextItem(-1, LVNI_SELECTED);
// Make sure the line is valid
if (selectedLine != -1)
{
CEditConfig editConfig;
// Add 3 since we skipped the first three baud rates
selectedLine += 3;
// Set the "user field" members of our editConfig dlg class, the
// default data will be set automaticall from the desired baud rate
editConfig.m_LineNum.Format("Baud Range: %d to %d", baudRange[selectedLine][1], baudRange[selectedLine][0]);
editConfig.m_BaudGen.Format("%04X", m_BaudConfigData[selectedLine].BaudGen);
editConfig.m_Timer0Reload.Format("%04X", m_BaudConfigData[selectedLine].Timer0Reload);
editConfig.m_Prescaler = m_BaudConfigData[selectedLine].Prescaler;
editConfig.m_BaudRateDBR = m_BaudConfigData[selectedLine].BaudRate;
// Determine if the "desired baud rate" has been overridden with some other data
if (!((CalculateBestBaudGen(CP2102_SYSCLK, m_BaudConfigData[selectedLine].BaudRate) == m_BaudConfigData[selectedLine].BaudGen) &&
(CalculateBestPrescaler(CP2102_SYSCLK, m_BaudConfigData[selectedLine].BaudRate) == m_BaudConfigData[selectedLine].Prescaler) &&
(CalculateTimer0Reload(CP2102_TIMEOUT_CLOCK, CalculateActualBaudRate(CP2102_SYSCLK, m_BaudConfigData[selectedLine].Prescaler, m_BaudConfigData[selectedLine].BaudGen)) == m_BaudConfigData[selectedLine].Timer0Reload)))
{
editConfig.m_Override = true;
}
// Show the window
if (editConfig.DoModal() == IDOK)
{
// If ok is pressed, then store all of the data from the window into our baud config array
m_BaudConfigData[selectedLine].BaudGen = HexStringToValue(editConfig.m_BaudGen, 4);
m_BaudConfigData[selectedLine].Timer0Reload = HexStringToValue(editConfig.m_Timer0Reload, 4);
m_BaudConfigData[selectedLine].Prescaler = editConfig.m_Prescaler;
m_BaudConfigData[selectedLine].BaudRate = editConfig.m_BaudRateDBR;
// Refresh the baud config window
RefreshBaudConfigList();
}
}
*pResult = 0;
}
void CCP210xBaudRateAliasConfigDlg::RefreshBaudConfigList()
{
CString tmp;
// Clear any previous data from the window
m_BaudRateAliasList.DeleteAllItems();
// Loop through each baud config, and display them in the list
// Format: # highrange lowrange desiredbaud actualbaud
// Note that we are starting at 2, this is to clear the top 3 baud rates
// since they normally will not need to be aliased
for (int i = 2; i < NUM_BAUD_CONFIGS; i++)
{
// Config number
tmp.Format("%d", i-3);
m_BaudRateAliasList.InsertItem(i-3, tmp);
// High baud range
tmp.Format("%u", baudRange[i][0]);
m_BaudRateAliasList.SetItemText(i-3, 1, tmp);
// Low baud range
tmp.Format("%u", baudRange[i][1]);
m_BaudRateAliasList.SetItemText(i-3, 2, tmp);
// Desired baud rate
tmp.Format("%u", m_BaudConfigData[i].BaudRate);
m_BaudRateAliasList.SetItemText(i-3, 3, tmp);
// Actual baud rate (calculated)
tmp.Format("%u", CalculateBaudRate(CP2102_SYSCLK, m_BaudConfigData[i].Prescaler, m_BaudConfigData[i].BaudGen));
m_BaudRateAliasList.SetItemText(i-3, 4, tmp);
}
}
void CCP210xBaudRateAliasConfigDlg::OnButtonExport()
{
// TODO: Add your control notification handler code here
// Check our current device selection
if (m_Devices.GetCurSel() > -1)
{
CFileDialog fileDlg(FALSE, "hex", "CP210xBaudConfig.hex", NULL, "Hex Files (*.hex)|*.hex|All Files (*.*)|*.*||", this);
if (fileDlg.DoModal() == IDOK)
{
CString fileName = fileDlg.GetPathName();
// If we are selecting a valid device, open it
if (CP210x_Open(m_Devices.GetCurSel(), &m_DeviceHandle) == CP210x_SUCCESS)
{
if (CP210x_CreateHexFile(m_DeviceHandle, fileName) != CP210x_SUCCESS)
MessageBox("Could not create file");
CP210x_Close(m_DeviceHandle);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -