📄 dialog.cpp
字号:
// TODO: Add your command handler code here
CString filename;
CFileDialog opendlg(true,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"IML文件(*.IML)|*.IML|All Files(*.*)|*.*||",NULL);
if(opendlg.DoModal()==IDOK)
filename=opendlg.GetPathName();
else
return;
CStdioFile file;
file.Open(filename,CFile::modeRead);
CString szLine("");
if (!file.ReadString(szLine)){
AfxMessageBox("打开错误!");
return;
}
szLine.TrimLeft();
szLine.TrimRight();
if(szLine.Compare("MCC I2C Message List File V2")!=0){
AfxMessageBox("文件格式不支持");
return;
}
m_list.m_Data.ClearAll();
while(file.ReadString(szLine)){
if(szLine.GetLength()<17) continue;
if(szLine.GetAt(0)!='$') continue;
szLine.Delete(3,10);
szLine.Replace("$"," ");
szLine.Replace(","," ");
szLine.TrimLeft();
szLine.TrimRight();
m_list.m_Data.AddRow(szLine);
}
m_list.Show();
m_iScrollRow = 0;
ShowValue();
m_FileName = "";
m_list.SetFocus();
}
void C$$Safe_root$$Dlg::OnMenuFileSave()
{
// TODO: Add your command handler code here
if(m_FileName.GetLength() == 0){
OnMenuFileSaveAs();
return;
}
CStdioFile file;
file.Open(m_FileName,CFile::modeCreate|CFile::modeWrite);
/* CString szLine("");
szLine.Format("%02X\n",m_iIICAddr);
file.WriteString(szLine);*/ // ver 1.0
WriteFileHead(file);
for(int i=0;i<m_list.m_Data.GetSize();i++){
file.WriteString(m_list.m_Data.GetRow(i));
}
}
void C$$Safe_root$$Dlg::OnMenuFileSaveAs()
{
// TODO: Add your command handler code here
CString filename;
CFileDialog opendlg(false,"txt","2546",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"文本文件(*.txt)|*.txt|All Files(*.*)|*.*||",NULL);
if(opendlg.DoModal()==IDOK)
filename=opendlg.GetPathName();
else
return;
CStdioFile file;
file.Open(filename,CFile::modeCreate|CFile::modeWrite);
/* CString szLine("");
szLine.Format("%02X\n",m_iIICAddr);
file.WriteString(szLine);*/
WriteFileHead(file);
for(int i=0;i<m_list.m_Data.GetSize();i++){
file.WriteString(m_list.m_Data.GetRow(i));
}
m_FileName = filename;
}
void C$$Safe_root$$Dlg::SelectRow(int row)
{
m_iScrollRow = row;
ShowValue();
}
void C$$Safe_root$$Dlg::SaveRegValue(BYTE val)
{
m_list.SaveScroll(m_iScrollRow,val);
ShowValue();
return;
}
void C$$Safe_root$$Dlg::OnEndlabeleditList4(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_DISPINFO *plvDispInfo = (LV_DISPINFO *)pNMHDR;
LV_ITEM *plvItem = &plvDispInfo->item;
CString str(plvItem->pszText);
int len = str.GetLength();
BYTE val=0;
if (plvItem->pszText != NULL)
{
if(plvItem->iSubItem==0)
m_list.SetItemText (plvItem->iItem, plvItem->iSubItem, plvItem->pszText);
if(StrToVal(str,val)){
if(m_bHex)
str.Format("%02X",val);
else
str.Format("%02d",val);
m_list.SetItemText (plvItem->iItem, plvItem->iSubItem, str);
}
}
*pResult = FALSE;
}
/*
gxListCtrl::OnEndLabelEdit (NMHDR* pNMHDR, LRESULT* pResult)
{
LV_DISPINFO *plvDispInfo = (LV_DISPINFO *)pNMHDR;
LV_ITEM *plvItem = &plvDispInfo->item;
if (plvItem->pszText != NULL)
{
SetItemText (plvItem->iItem, plvItem->iSubItem, plvItem->pszText);
}
*pResult = FALSE;
}*/
BOOL C$$Safe_root$$Dlg::StrToVal(CString str,BYTE& val)
{
int i;
char* p;
if(m_bHex)
i = strtoul(str,&p,16);
else
i = strtoul(str,&p,10);
if((i <= 0xFF)&&(i>=0)){
val = i;
return true;
}else return false;
}
void C$$Safe_root$$Dlg::OnChangeEditIicAddr()
{
UpdateData();
StrToVal(m_strIICAddr,m_iIICAddr);
// m_list.SaveIICAddr(m_iScrollRow,m_iIICAddr);
}
void C$$Safe_root$$Dlg::OnChangeEditNote()
{
UpdateData();
m_list.SaveNote(m_iScrollRow,m_strNote);
}
void C$$Safe_root$$Dlg::OnChangeEditRegAddr()
{
UpdateData();
StrToVal(m_strRegAddr,m_iRegAddr);
m_list.SaveRegAddr(m_iScrollRow,m_iRegAddr);
}
void C$$Safe_root$$Dlg::OnChangeEditRegName()
{
UpdateData();
m_list.SaveRegName(m_iScrollRow,m_strRegName);
}
void C$$Safe_root$$Dlg::OnChangeEditRegValue()
{
UpdateData();
StrToVal(m_strRegValue,m_iRegValue);
m_list.SaveScroll(m_iScrollRow,m_iRegValue);
m_scroll.SetScrollPos(m_iRegValue);
}
BOOL C$$Safe_root$$Dlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message == WM_KEYDOWN)
{
switch(pMsg->wParam)
{
// case VK_RETURN://屏蔽回车
// return TRUE;
case VK_ESCAPE://屏蔽Esc
return TRUE;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
void C$$Safe_root$$Dlg::OnOK()
{
// TODO: Add extra validation here
if(GetFocus() == &m_tmpLine){
OnButtonTx();
return;
}
SendRowData(m_iScrollRow);
}
void C$$Safe_root$$Dlg::SendRowData(int row)
{
BYTE val;
CString tmp;
CString str;
m_WriteLog.GetWindowText(str);
tmp.Format("\r\n#%d: ",++m_iWriteLines);
str += tmp;
int ack = 0,sum =0;
#if 1
if(pPort == NULL) return;
pPort->SendStart();
// pPort->SendByte(m_iIICAddr);
for(int i=0;i<m_list.m_Data.GetRowLength(row);i++){
val = m_list.m_Data.GetValue(row,i);
ack += pPort->SendByte(val);
sum ++;
tmp.Format("%02X ",val);
str += tmp;
}
pPort->SendStop();
tmp.Format("\r\nSend %d bytes,%d ack received.",sum,ack);
str += tmp;
if(str.GetLength()>4094){
str = str.Right(4094);
int pos = str.GetLength() - str.Find("\r\n");
str = str.Right(pos-2);
}
m_WriteLog.SetWindowText(str);
// m_WriteLog.GetLineCount();
m_WriteLog.LineScroll(m_WriteLog.GetLineCount()-1);
#endif
}
void C$$Safe_root$$Dlg::OnMenuHelp()
{
// TODO: Add your command handler code here
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
void C$$Safe_root$$Dlg::OnMenuExit()
{
// TODO: Add your command handler code here
::PostQuitMessage(0);
}
void C$$Safe_root$$Dlg::OnBit0()
{
// TODO: Add your control notification handler code here
BYTE val = 0;
for(int i = 7;i>=0;i--){
val <<= 1;
val+= pBit[i]->GetBit();
}
m_iRegValue = val;
m_scroll.SetScrollPos(val);
OnHScroll(8, 0, &m_scroll);
}
void C$$Safe_root$$Dlg::OnButton2()
{
// TODO: Write Line
if(m_bSendAll) return;
m_bSendAll = true;
SetTimer(1,1000,NULL);
int size = m_list.m_Data.GetSize();
for(int i=0;i<size;i++)
SendRowData(i);
}
void C$$Safe_root$$Dlg::OnClearWriteLog()
{
// TODO: Add your control notification handler code here
m_WriteLog.SetWindowText("Start:");
m_iWriteLines = 0;
}
void C$$Safe_root$$Dlg::OnClearReadLog()
{
// TODO: Add your control notification handler code here
m_ReadLog.SetWindowText("Start:");
m_iReadLines = 0;
}
void C$$Safe_root$$Dlg::WriteFileHead(CStdioFile &file)
{
file.WriteString("##\n");
file.WriteString("#############################\n");
file.WriteString("## I2c data file ver 2.0 ##\n");
file.WriteString("## 注意:第一行不要更改! ##\n");
file.WriteString("## 以#开头的行将当作注释 ##\n");
file.WriteString("#############################\n");
}
void C$$Safe_root$$Dlg::OnButtonAdd()
{
// TODO: Add your control notification handler code here
m_list.m_Data.AddRow();
m_list.Show();
ShowValue();
}
void C$$Safe_root$$Dlg::OnButtonDelete()
{
// TODO: Add your control notification handler code here
if(m_list.m_Data.GetSize()<2) return;
int i = m_list.GetSelectionMark();
if(i<0) return;
if(m_iScrollRow = m_list.m_Data.GetSize() -1)
m_iScrollRow--;
m_list.m_Data.DeleteRow(i);
m_list.Show();
ShowValue();
}
void C$$Safe_root$$Dlg::OnButtonTx()
{
// TODO: Add your control notification handler code here
CString str,str2,tmp;
BYTE val;
m_WriteLog.GetWindowText(str2);
tmp.Format("\r\n#%d: ",++m_iWriteLines);
str2 += tmp;
int ack = 0,sum =0;
this->m_tmpLine.GetWindowText(str);
if(str.GetLength()<2) return;
if(pPort == NULL) return;
pPort->SendStart();
str.Replace(" ","");
for(int i=0;i<str.GetLength();i++){
if(i%2==0) continue;
StrToVal(str.Mid(i-1,2),val);
ack += pPort->SendByte(val);
sum ++;
tmp.Format("%02X ",val);
str2 += tmp;
}
pPort->SendStop();
tmp.Format("\r\nSend %d bytes,%d ack received.",sum,ack);
str2 += tmp;
if(str2.GetLength()>4094){
str2 = str2.Right(4094);
int pos = str2.GetLength() - str2.Find("\r\n");
str2 = str2.Right(pos-2);
}
m_WriteLog.SetWindowText(str2);
// m_WriteLog.GetLineCount();
m_WriteLog.LineScroll(m_WriteLog.GetLineCount()-1);
}
void C$$Safe_root$$Dlg::OnButtonSave()
{
// TODO: Add your control notification handler code here
CString str;
m_tmpLine.GetWindowText(str);
m_list.m_Data.AddRow(str);
m_list.Show();
m_list.EnsureVisible(m_list.m_Data.GetSize(),0);
ShowValue();
}
void C$$Safe_root$$Dlg::OnButtonTxRx()
{
// TODO: Add your control notification handler code here
CString str,str2,tmp,tmp2;
BYTE val,ReadBytes;
m_ReadLog.GetWindowText(str2);
tmp.Format("\r\n#%d:Read ",++m_iReadLines);
str2 += tmp;
int ack = 0,sum = 0,read = 0;
BYTE ReadAddr = 0,RegAddr = 0;
this->m_tmpLine.GetWindowText(str);
if(str.GetLength()<5) return;
if(pPort == NULL) return;
this->m_ReadBytes.GetWindowText(tmp2);
pPort->SendStart();
str.Replace(" ","");
for(int i=0;i<5;i++){
if(i%2==0) continue;
StrToVal(str.Mid(i-1,2),val);
if(i==1)
{
ReadAddr = val+1;
}
else
{
RegAddr = val;
}
ack += pPort->SendByte(val);
sum ++;
tmp.Format("%02X ",val);
str2 += tmp;
}
pPort->SendStart();
pPort->SendByte(ReadAddr);
// pPort->SendByte(RegAddr);
BOOL btmp = m_bHex;
m_bHex = false;
StrToVal(tmp2,val);
m_bHex = btmp;
ReadBytes = val;
tmp.Format(",%d bytes:\r\n",val);
str2 += tmp;
for(int j=0;j<ReadBytes;j++){
if(j == ReadBytes-1)
val = pPort->ReceiveByteNoAck();
else
val = pPort->ReceiveByte();
tmp.Format("%02X ",val);
str2 += tmp;
}
str2 += "\r\n";
for(int k=0;k<ReadBytes;k++){
tmp.Format("%02X ",BYTE(RegAddr+k));
str2 += tmp;
}
if(str2.GetLength()>4094){
str2 = str2.Right(4094);
int pos = str2.GetLength() - str2.Find("\r\n");
str2 = str2.Right(pos-2);
}
m_ReadLog.SetWindowText(str2);
m_ReadLog.LineScroll(m_ReadLog.GetLineCount()-1);
pPort->SendStop();
}
void C$$Safe_root$$Dlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
bool flag;
switch(nIDEvent){
case 1:
KillTimer(1);
m_bSendAll = false;
CDialog::OnTimer(nIDEvent);
break;
case 2:
flag=pPort->CheckBus();
if(flag!=m_bBusReady){
m_bBusReady=flag;
ShowBusState();
}
break;
default:
break;
}
}
void C$$Safe_root$$Dlg::OnTrackList4(NMHDR* pNMHDR, LRESULT* pResult)
{
HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR;
// TODO: Add your control notification handler code here
*pResult = 0;
}
void C$$Safe_root$$Dlg::ShowBusState()
{
if(m_bBusReady){
this->SetWindowText("Bus ready now");
}
else{
this->SetWindowText("!!Bus busy or power off!!");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -