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

📄 myapp1dlg.cpp

📁 上位机软件 for 自制AT89C51/52编程器
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    DisplayMessage("正在检测芯片... ...");

    com.ClearRecvBuff();  //读编程器标题
    char c='\r',str[60];
    com.Write(&c,1);
    if(com.GetStrWait(str,30)<10)
    {
		DisplayMessage("未发现下载器!");
      return;
    }
    int ei=0;
    while(str[strlen(str)-1]!='>')
    {
      if(!com.GetCharWait(&c) || ei>55)
      {
		  DisplayMessage("未发现下载器!");
        return;
      }
      
      char l=strlen(str);
      str[l]=c;
      str[l+1]='\0';
      ei++;
    }                 //读编程器标题结束
	//DisplayMessage(str);return;
	CString estr=str;
    DWORD CpuType=0,NonBlankLen,SetLen;
	if(estr.Find("2051")!=-1)
	{
		DisplayMessage("发现Atmel 89C2051/4051 下载器!");
		CpuType=21;
	}
    else if(estr.Find("89C51")!=-1)
    { 
	  DisplayMessage("发现Atmel 89C51/52 下载器!");
      com.ClearRecvBuff();
      char c='p',str[60];
      com.Write(&c,1);
      if(com.GetStrWait(str,10)<10)
      {
        return;
      }
      int ei=0;
      while(str[strlen(str)-1]!='>')
      {
        if(!com.GetCharWait(&c) || ei>20)
        {
          return;
        }
        
        char l=strlen(str);
        str[l]=c;
        str[l+1]='\0';
        ei++;
      }
      sscanf(str,"p%u,%u,%u",&CpuType,&NonBlankLen,&SetLen);
      //DisplayMessage(CpuType);
    }
	else DisplayMessage("请检查下载器!"); //此行不会执行
    switch (CpuType)
    {
        case 21:
          StopWait();
          UpdateData();
          m_radio1=0;
          UpdateData(FALSE);
          DisplayMessage("89C4051请手动选择!");
          if(com.IsOpen())
          {
            (CButton*)GetDlgItem(IDC_BUTTON2)->EnableWindow();
            (CButton*)GetDlgItem(IDC_BUTTON4)->EnableWindow();
          }
          break;
        case 51:
          StopWait();
          UpdateData();
          m_radio1=2;
          UpdateData(FALSE);
          DisplayMessage("发现Atmel 89C51 ");
          if(com.IsOpen())
          {
            (CButton*)GetDlgItem(IDC_BUTTON2)->EnableWindow();
            (CButton*)GetDlgItem(IDC_BUTTON4)->EnableWindow();
          }
          break;
        case 52:
          StopWait();
          UpdateData();
          m_radio1=3;
          UpdateData(FALSE);
          DisplayMessage("发现Atmel 89C52 ");
          if(com.IsOpen())
          {
            (CButton*)GetDlgItem(IDC_BUTTON2)->EnableWindow();
            (CButton*)GetDlgItem(IDC_BUTTON4)->EnableWindow();
          }
          break;
        default:UpdateData();m_radio1=-1;UpdateData(FALSE); 
		  DisplayMessage("请检查芯片!");               
          break;											
    }
  } //end com.IsOpen()
  CDialog::OnTimer(nIDEvent);
}

void CMYAPP1Dlg::StartWait(UINT ms=5000)
{
  WAIT_OVER=FALSE;
  m_nTimer=SetTimer(1,ms,NULL);

}

void CMYAPP1Dlg::StopWait()
{
  KillTimer(1);
}

void CMYAPP1Dlg::OnCheck3() 
{
  // TODO: Add your control notification handler code here
  int i;
  CButton* pr[5];
  UpdateData();
  m_radio1=-1;
  UpdateData(FALSE);
  pr[0]=(CButton*)GetDlgItem(IDC_RADIO1);
  pr[1]=(CButton*)GetDlgItem(IDC_RADIO2);
  pr[2]=(CButton*)GetDlgItem(IDC_RADIO3);
  pr[3]=(CButton*)GetDlgItem(IDC_RADIO4);
  pr[4]=(CButton*)GetDlgItem(IDC_RADIO5);
  CButton*  ReadButton=(CButton*)GetDlgItem(IDC_BUTTON2);
  CButton* WriteButton=(CButton*)GetDlgItem(IDC_BUTTON4);
  if (m_check3==TRUE)
  { 
    for (i=0;i<5;i++) pr[i]->EnableWindow(FALSE);
    ReadButton->EnableWindow(FALSE);
    WriteButton->EnableWindow(FALSE);
    DisplayMessage("自动检测芯片");
    StartWait();
    CMYAPP1Dlg::OnTimer(1);
  }
  else
  { 
    for (i=0;i<5;i++)pr[i]->EnableWindow();
    if(0<=m_radio1 && m_radio1<5)
    {
      ReadButton->EnableWindow();
      WriteButton->EnableWindow();
    }
    DisplayMessage("手动设定芯片");
    StopWait();
  }
  
}

void CMYAPP1Dlg::OnClickList3(NMHDR* pNMHDR, LRESULT* pResult) 
{
  // TODO: Add your control notification handler code here
  NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
    CListCtrl* pList =
        (CListCtrl*) GetDlgItem(IDC_LIST3);
    int nSelected = pNMListView->iItem;
    if (nSelected >= 0) {
        CString strItem = pList->GetItemText(nSelected, 0);
        SetDlgItemText(IDC_STATIC_LIST3, strItem);
    }
  
  *pResult = 0;
}
void CMYAPP1Dlg::OnRadio1() 
{
  // TODO: Add your control notification handler code here
  UpdateData(TRUE);
  UpdateData(FALSE);
  CButton*  ReadButton=(CButton*)GetDlgItem(IDC_BUTTON2);
  CButton* WriteButton=(CButton*)GetDlgItem(IDC_BUTTON4);
  if(com.IsOpen())
  {
    ReadButton->EnableWindow();
    WriteButton->EnableWindow();
  }
  
}

void CMYAPP1Dlg::OnRadio2() 
{
  UpdateData(TRUE);
  UpdateData(FALSE);
  CButton*  ReadButton=(CButton*)GetDlgItem(IDC_BUTTON2);
  CButton* WriteButton=(CButton*)GetDlgItem(IDC_BUTTON4);
  if(com.IsOpen())
  {
    ReadButton->EnableWindow();
    WriteButton->EnableWindow();
  }
}

void CMYAPP1Dlg::OnRadio3() 
{
  UpdateData(TRUE);
  UpdateData(FALSE);
  CButton*  ReadButton=(CButton*)GetDlgItem(IDC_BUTTON2);
  CButton* WriteButton=(CButton*)GetDlgItem(IDC_BUTTON4);
  if(com.IsOpen())
  {
    ReadButton->EnableWindow();
    WriteButton->EnableWindow();
  }
}

void CMYAPP1Dlg::OnRadio4() 
{
  UpdateData(TRUE);
  UpdateData(FALSE);
  CButton*  ReadButton=(CButton*)GetDlgItem(IDC_BUTTON2);
  CButton* WriteButton=(CButton*)GetDlgItem(IDC_BUTTON4);
  if(com.IsOpen())
  {
    ReadButton->EnableWindow();
    WriteButton->EnableWindow();
  }
}

void CMYAPP1Dlg::OnRadio5() 
{
  UpdateData(TRUE);
  UpdateData(FALSE);

  CButton*  ReadButton=(CButton*)GetDlgItem(IDC_BUTTON2);
  CButton* WriteButton=(CButton*)GetDlgItem(IDC_BUTTON4);
  if(com.IsOpen())
  {
    ReadButton->EnableWindow();
    WriteButton->EnableWindow();
  }
}

void CMYAPP1Dlg::OnButton1() 
{
  // TODO: Add your control notification handler code here
  // CFileDialog( BOOL bOpenFileDialog,            false=保存,true=打开
  //           LPCTSTR lpszDefExt = NULL,          缺省扩展名
  //           LPCTSTR lpszFileName = NULL,        文件名
  //           DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,  读写方式 
  //           LPCTSTR lpszFilter = NULL,          文件流
  //           CWnd* pParentWnd = NULL );          

  CString m_edit2temp=m_edit2;
  CString sFilter="Binfile(*.bin)|*.bin|Hexfile(*.hex)|*.hex|All Files (*.*)|*.*|";
  CFileDialog dlg(true,"bin",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,(LPCTSTR)sFilter,this);
  
  UpdateData(TRUE);
  if(dlg.DoModal()==IDOK)
      m_edit2=dlg.GetPathName();
  else
    m_edit2=m_edit2temp;
  UpdateData(FALSE);

  
}

void CMYAPP1Dlg::OnButton3() 
{
  // TODO: Add your control notification handler code here
  CString sFilter="Binfile(*.bin)|*.bin|Hexfile(*.hex)|*.hex|All Files (*.*)|*.*|";
  CFileDialog dlg(false,"bin",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,(LPCTSTR)sFilter,this);
  CString m_edit3temp=m_edit3;
  UpdateData(TRUE);
  if(dlg.DoModal()==IDOK)
      m_edit3=dlg.GetPathName();
  else
    m_edit3=m_edit3temp;
  UpdateData(FALSE);
  
}
void CMYAPP1Dlg::OnChangeEdit3() 
{
  // 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);
  UpdateData(FALSE);

  
}

void CMYAPP1Dlg::OnChangeEdit2() 
{
  // 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);
  UpdateData(FALSE);
  
}



bool CMYAPP1Dlg::GetOneChar(char *c)
{
  char readstr[4];
  DWORD i=0;
  while (!com.Read(readstr, 1))
  {  
    Sleep(10);
    if(i++>200)return FALSE;
  };
  *c=readstr[0];
  return TRUE;
}
BOOL CMYAPP1Dlg::SetLength(DWORD len)
{
  com.ClearRecvBuff();
  char lenstr[10]="s",readstr[4];
  ltoa(len,lenstr+1,10);
  strcat(lenstr,"\r");
  int n=strlen(lenstr);
  for (int i=0;i<n;i++)
  {
    com.Write(lenstr+i,1);
    readstr[0]=XOFF;
    do
    {
      if(!GetOneChar(readstr))return FALSE;
    }
    while(readstr[0]==XON);
  }
  return TRUE;
}

void CMYAPP1Dlg::OnButton4() //读按钮
{
  // TODO: Add your control notification handler code here
  CButton*  ReadButton=(CButton*)GetDlgItem(IDC_BUTTON4);
  CButton*  WButton=(CButton*)GetDlgItem(IDC_BUTTON2);
  WButton->EnableWindow(FALSE);
  ReadButton->EnableWindow(FALSE);
  //AfxGetMainWnd()->EnableWindow(FALSE);
  char str[60];
  com.ClearRecvBuff();
  DWORD plen;
  switch(m_radio1)
  {
  case 0: plen=2048;  break;
  case 1: plen=4096;  break;
  case 2: plen=4096*1;break;
  case 3: plen=4096*2;break;
  case 4: plen=4096*5;break;
  default:plen=4096*1;break;
  }
    if(!SetLength(plen))
  {
    DisplayMessage("设定程序长度超时!");
    ReadButton->EnableWindow();
    WButton->EnableWindow();
    return;
  };
  //
  if(m_radio1>1)
  {
    com.ClearRecvBuff();
    DisplayMessage("检查程序长度!");
    char c='p';
    com.Write(&c,1);
    if(com.GetStrWait(str,10,6000)<10)
    {
      DisplayMessage("检查程序长度超时!");
      ReadButton->EnableWindow();
      WButton->EnableWindow();
      return;
    }
    int ei=0;
    while(str[strlen(str)-1]!='>')
    {
      if(!com.GetCharWait(&c) || ei>20)
      {
        DisplayMessage("检查程序长度超时!");
        ReadButton->EnableWindow();
        WButton->EnableWindow();
        return;
      }
      
      char l=strlen(str);
      str[l]=c;
      str[l+1]='\0';

⌨️ 快捷键说明

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