LONG CMyCommView::OnCommunication(WPARAM ch, LPARAM port) { if (m_IsViewReceiveData) { CString str; if (m_ctrlReceiveHex.GetCheck()) { str.Format("%02X ",ch); //m_EditLogger.AddText(str); } else { str.Format("%c",ch); //m_EditLogger.AddText(str); } CEdit *edit = (CEdit*)GetDlgItem(IDC_EDRECDATA); int nLength=edit->SendMessage(WM_GETTEXTLENGTH); edit->SetSel(nLength, nLength); edit->ReplaceSel(str); UpdateData(FALSE); } GetDocument()->m_RXCount++; CMyCommApp * myApp = (CMyCommApp *)AfxGetApp(); CString str; str.Format("RX:%d",GetDocument()->m_RXCount); myApp->DoSetStautsBarText(SBSRX,str); m_ctrlReciveData.LineScroll(m_ctrlReciveData.GetLineCount()); uart_on_recv_char((unsigned char)ch); return 0; }
void CMyCommView::OnBtSend() { // TODO: Add your control notification handler code here if (!GetDocument()->m_ComAction) { return; } UpdateData(TRUE); int i; if(m_ctrlSendScript.GetCheck()) { CString mycommand; CString myscript; char myc; mycommand.Empty(); myscript = this->m_strSendData; for(i=0;i<myscript.GetLength();i++) { myc = myscript[i]; if (myc==';') { DoRunScript(mycommand); mycommand.Empty(); } else{ if ((myc != '\n') && (myc != '\r')) {mycommand += myc;} } } } else if (m_ctrlSendHex.GetCheck()) { char data[512]; int len=DoStr2Hex(m_strSendData,data); GetDocument()->m_Comm.WriteToPort(data,len); GetDocument()->m_RXCount+=(long)((m_strSendData.GetLength()+1)/3); CMyCommApp * myApp = (CMyCommApp *)AfxGetApp(); CString str; str.Format("TX:%d",GetDocument()->m_RXCount); myApp->DoSetStautsBarText(SBSTX,str); } else { GetDocument()->m_Comm.WriteToPort((LPCTSTR)m_strSendData); //发送数据 GetDocument()->m_RXCount+=m_strSendData.GetLength(); CMyCommApp * myApp = (CMyCommApp *)AfxGetApp(); CString str; str.Format("TX:%d",GetDocument()->m_RXCount); myApp->DoSetStautsBarText(SBSTX,str); } CommCommand myCommand; myCommand.m_IsHex = m_ctrlSendHex.GetCheck(); myCommand.m_isScript = m_ctrlSendScript.GetCheck(); myCommand.m_strCommand = m_strSendData; m_CurrenthistoryIndex = GetDocument()->AddHistory(myCommand); }
void CMyCommView::OnSendkeyShiftenter() { // TODO: Add your command handler code here CMyCommApp * myApp = (CMyCommApp *)AfxGetApp(); myApp->m_SendkeyType = SKSHIFTENTER; CButton * mybt = (CButton *) GetDlgItem(IDC_BTSEND); mybt->SetWindowText(_T("发送 S")); myApp->DoSetStautsBarText(SBSSENDKEY,"S"); }
void CMyCommView::OnBtclearrxtx() { // TODO: Add your control notification handler code here GetDocument()->m_RXCount = 0; GetDocument()->m_TXCount = 0; CMyCommApp * myApp = (CMyCommApp *)AfxGetApp(); myApp->DoSetStautsBarText(SBSRX,"RX:"); myApp->DoSetStautsBarText(SBSTX,"TX:"); }
static void fxSend(unsigned char c) { currDoc->m_Comm.WriteToPort((BYTE*)&c, 1); currDoc->m_RXCount += 1; CMyCommApp * myApp = (CMyCommApp *)AfxGetApp(); CString str; str.Format("TX:%d",currDoc->m_RXCount); myApp->DoSetStautsBarText(SBSTX,str); appendSentHex(&c, 1); }
void ThreadCheckVersion() { //app dir CString myAppDir; CMyCommApp *myApp = (CMyCommApp *)AfxGetApp(); CMainFrame * myMain = (CMainFrame *) myApp->GetMainWnd(); CStdioFile myfile; myAppDir = myApp->m_AppDir; CString vertext = myAppDir + "\\version"; if(!myfile.Open(vertext,CFile::modeRead)) { CString myver; char s[256]; _tcscpy(s,vertext.GetBuffer(vertext.GetLength())) ; if(myMain->DonwLoadFile("https://github.com/liquanhai/LSDComm/version/ver.txt", s)) myMain->m_IsNewVertxt = TRUE; return; } else{ CString myverion; myfile.ReadString(myverion); //line:1 myverion.TrimLeft(); myverion.TrimRight(); if (atof(myApp->m_AppVersion)<atof(myverion)) { //new version CMyCommView * myview = (CMyCommView *) myMain->GetActiveView(); //myview->m_EditLogger.AddText(vertext); //myview->m_EditLogger.AddText(myApp->m_AppVersion+"\r\n"); myview->m_EditLogger.AddText(">>最新版本:"+myverion+"\r\n"); CString strLine; myfile.ReadString(strLine); // downfilename line:2 myApp->m_downfileexefilename = strLine; myfile.ReadString(strLine); //line:3 myview->m_EditLogger.AddText(">>"+strLine +"\r\n"); myview->m_EditLogger.AddText(">>升级点击主菜单的[帮助]->[在线升级]\r\n"); myfile.Close(); myMain->m_IsNewVertxt = TRUE; } else{ char s[256]; _tcscpy(s,vertext.GetBuffer(vertext.GetLength())) ; if(myMain->DonwLoadFile("https://github.com/liquanhai/LSDComm/version/ver.txt", s)) myMain->m_IsNewVertxt = TRUE; myfile.Close(); return; } } }
void ThreadCheckVersion() { #if 1 //app dir CString myAppDir; CMyCommApp *myApp = (CMyCommApp *)AfxGetApp(); CMainFrame * myMain = (CMainFrame *) myApp->GetMainWnd(); CStdioFile myfile; myAppDir = myApp->m_AppDir; CString vertext = myAppDir + "\\version"; if(!myfile.Open(vertext,CFile::modeRead)) { CString myver; char s[256]; _tcscpy(s,vertext.GetBuffer(vertext.GetLength())) ; if(myMain->DonwLoadFile("http://git.oschina.net/yihui/lsdcomm/raw/master/release/version.txt", s)) myMain->m_IsNewVertxt = TRUE; return; } else{ CString myverion; myfile.ReadString(myverion); //line:1 myverion.TrimLeft(); myverion.TrimRight(); if (atof(myApp->m_AppVersion)<atof(myverion)) { //new version CMyCommView * myview = (CMyCommView *) myMain->GetActiveView(); //myview->m_EditLogger.AddText(vertext); //myview->m_EditLogger.AddText(myApp->m_AppVersion+"\r\n"); myview->m_EditLogger.AddText(">>Latest Version:"+myverion+"\r\n"); CString strLine; myfile.ReadString(strLine); // downfilename line:2 myApp->m_downfileexefilename = strLine; myfile.ReadString(strLine); //line:3 myview->m_EditLogger.AddText(">>"+strLine +"\r\n"); myview->m_EditLogger.AddText(">>Click menu[Help]->[Upgrade]\r\n"); myfile.Close(); myMain->m_IsNewVertxt = TRUE; } else{ char s[256]; _tcscpy(s,vertext.GetBuffer(vertext.GetLength())) ; if(myMain->DonwLoadFile("http://git.oschina.net/yihui/lsdcomm/raw/master/release/version.txt", s)) myMain->m_IsNewVertxt = TRUE; myfile.Close(); return; } } #endif }
void CMyCommView::DoRunScript(const CString str) { CString Commandstr; CString Paramsstr,SendStr; char data[512]; int myindex; int commandindex; if (str.GetLength()==0){return;} if (str[0]=='#'){return;} //注释行不处理。 myindex = str.Find('=',0); if (myindex == -1) { //有可能是无参数的命令 Commandstr = str; Commandstr.MakeUpper(); if ((Commandstr=="CLEAR") || (Commandstr=="DATE") || (Commandstr=="HELP")) { Paramsstr.Empty(); } else { CString ErrorStr; ErrorStr.Format("\n\r语法出错1 %s\n\r",str); m_EditLogger.AddText(_T(ErrorStr)); return; } } else{ Commandstr = str.Left(myindex); Paramsstr = str.Right(str.GetLength()-myindex-1); Commandstr.MakeUpper(); } if (Commandstr=="SEND") { //内容可能有#1的情况 if ((Paramsstr.GetLength()>0) && (Paramsstr[0]=='%')) { commandindex = atoi(Paramsstr.Right(Paramsstr.GetLength()-1))-1; if (GetDocument()->m_Command[commandindex].m_isScript) { CString ErrorStr; ErrorStr.Format("\n\r语法出错,脚本不能做SEND的参数。 %s\n\r",str); m_EditLogger.AddText(_T(ErrorStr)); return; } SendStr=GetDocument()->m_Command[commandindex].m_strCommand; } else{ SendStr = Paramsstr; } if(m_ctrlSendHex.GetCheck()) { int len=DoStr2Hex(SendStr,data); GetDocument()->m_Comm.WriteToPort(data,len); GetDocument()->m_RXCount+=(long)((SendStr.GetLength()+1)/3); CMyCommApp * myApp = (CMyCommApp *)AfxGetApp(); CString str; str.Format("TX:%d",GetDocument()->m_RXCount); myApp->DoSetStautsBarText(SBSTX,str); } else{ GetDocument()->m_Comm.WriteToPort((LPCTSTR)SendStr); //发送数据 GetDocument()->m_RXCount+=SendStr.GetLength(); CMyCommApp * myApp = (CMyCommApp *)AfxGetApp(); CString str; str.Format("TX:%d",GetDocument()->m_RXCount); myApp->DoSetStautsBarText(SBSTX,str); } CEdit *edit = (CEdit*)GetDlgItem(IDC_EDRECDATAVALUE); int nLength=edit->SendMessage(WM_GETTEXTLENGTH); edit->SetSel(nLength, nLength); edit->ReplaceSel(SendStr); } else if (Commandstr=="SLEEP") { Sleep(atoi(Paramsstr)); } else if (Commandstr=="OUT") { //char re[3] = {VK_RETURN, 0x0a}; SendStr = Paramsstr; //SendStr.Replace("\n\r","abc"); m_EditLogger.AddText(_T(SendStr)); } else if (Commandstr =="DATE") { CTime time=CTime::GetCurrentTime(); CString s; if (Paramsstr.IsEmpty()) s = time.Format("%Y-%m-%d %H:%M:%S"); else s = time.Format(Paramsstr); m_EditLogger.AddText(s); } else if (Commandstr == "CLEAR") { OnBtclearreceivedata(); } else if (Commandstr == "HELP") { CMyCommApp * myApp = (CMyCommApp *)AfxGetApp(); m_EditLogger.AddText(myApp->m_ScriptHelp); } else{ CString ErrorStr; ErrorStr.Format("\n\r语法出错2 %s\n\r",str); m_EditLogger.AddText(_T(ErrorStr)); } }
void CMyCommView::OnBtopencomm() { // TODO: Add your control notification handler code here if (GetDocument()->m_ComAction) { GetDocument()->CloseComm(); m_ctrlOpenComm.SetWindowText(_T(" 打开串口")); GetDocument()->m_ComAction = FALSE; CMyCommApp * myApp = (CMyCommApp *)AfxGetApp(); myApp->DoSetStautsBarText(SBSCOMM,_T("串口:×")); } else{ CString mystr; char myc; if (m_ctrlCOM.GetCurSel()<0) { AfxMessageBox(_T("请选择串口")); return ; } GetDocument()->m_intPort = m_ctrlCOM.GetCurSel()+1; m_ctrlBaudRate.GetWindowText(mystr); if (!DoIsNumeric(mystr)) { AfxMessageBox(_T("请选择波特率")); return; } GetDocument()->m_intBaudRate = atoi(mystr); m_ctrlDataBits.GetWindowText(mystr); if (!DoIsNumeric(mystr)) { AfxMessageBox(_T("请选择数据位")); return; } GetDocument()->m_intDataBits = atoi(mystr); myc = 'N'; int i=m_ctrlParity.GetCurSel(); switch(i) { case 0: myc='N'; break; case 1: myc='O'; break; case 2: myc='E'; break; case 3: myc='M'; break; case 4: myc='S'; break; } GetDocument()->m_cParity=myc; if (m_ctrlStopBits.GetCurSel()<0) { AfxMessageBox(_T("请选择停止位")); return; } GetDocument()->m_intStopBits = m_ctrlStopBits.GetCurSel(); //index 表示 if (GetDocument()->OpenComm(this)) { CBitmap myBitmap; myBitmap.LoadBitmap(IDB_BITMAPOPENCOM); m_ctrlOpenComm.SetBitmap(myBitmap); m_ctrlOpenComm.SetWindowText(_T("关闭串口")); GetDocument()->m_ComAction = TRUE; CEdit * myedit = (CEdit *)GetDlgItem(IDC_EDSENDDATA); myedit->SetFocus(); CMyCommApp * myApp = (CMyCommApp *)AfxGetApp(); myApp->DoSetStautsBarText(SBSCOMM,_T("串口:√")); } else AfxMessageBox(_T("串口被占用!")); } // send botton BOOL myAction = GetDocument()->m_ComAction; GetDlgItem(IDC_BTSEND)->EnableWindow(myAction); // com param GetDlgItem(IDC_CBCOM)->EnableWindow(!myAction); GetDlgItem(IDC_CBBANDRATE)->EnableWindow(!myAction); GetDlgItem(IDC_CBDATABITS)->EnableWindow(!myAction); GetDlgItem(IDC_CBPARITY)->EnableWindow(!myAction); GetDlgItem(IDC_CBSTOPBITS)->EnableWindow(!myAction); GetDlgItem(IDC_BTADVANCED)->EnableWindow(!myAction); }
void CMainFrame::OnAppUpgrade() { // TODO: Add your command handler code here CString myStr; BOOL myhasUpgrade = FALSE; CMyCommApp *myApp = (CMyCommApp *)AfxGetApp(); CMainFrame * myMain = (CMainFrame *) myApp->GetMainWnd(); CStdioFile myfile; CString vertext = myApp->m_AppDir + "\\version"; if(!myMain->m_IsNewVertxt || !IsFileExist(vertext)) { char s[256]; _tcscpy(s,vertext.GetBuffer(vertext.GetLength())) ; if(!DonwLoadFile("https://github.com/liquanhai/LSDComm/version/ver.txt",s)) { AfxMessageBox(_T("无法连接服务器。")); return; } myMain->m_IsNewVertxt = TRUE; } if(!myfile.Open(vertext,CFile::modeRead)) { AfxMessageBox(_T("打开升级文件出错。")); return; } else{ CString myverion; myfile.ReadString(myverion); //line:1 myverion.TrimLeft(); myverion.TrimRight(); if (atof(myApp->m_AppVersion)<atof(myverion)) { //new version myhasUpgrade = TRUE; CMyCommView * myview = (CMyCommView *) myMain->GetActiveView(); myStr = "最新版本:"+myverion+"\r\n"; CString strLine; myfile.ReadString(strLine); // downfilename line:2 myApp->m_downfileexefilename = strLine; while(myfile.ReadString(strLine)) { if(strLine.GetLength()>2) if ((strLine[0] == '>') && (strLine[1]=='>')) myStr += strLine+"\r\n"; } } else{ myStr = myApp->m_AppVersion + "版本已是最新了"; } } myfile.Close(); CUpgradeDlg dlg; dlg.m_strData = myStr; dlg.m_strdownfile = myApp->m_downfileexefilename; dlg.m_StrLocalfile = myApp->m_AppDir + "\\exe"; dlg.m_IsUpgrade = myhasUpgrade; if((dlg.DoModal()==IDOK) && myhasUpgrade) { if(!IsFileExist(myApp->m_AppDir + "\\exe")) return; CStdioFile batfile; CString mybatFile; CString myAppDir; mybatFile = myApp->m_AppDir+"\\update.bat"; if(!batfile.Open(mybatFile,CStdioFile::modeReadWrite|CStdioFile::modeCreate)) return; myAppDir = DoGetShortFileName(myApp->m_AppDir); batfile.WriteString("@echo off\n\r"); CString myexename; myexename = myAppDir + "\\LSDComm.exe"; batfile.WriteString("del "+ myexename + "\n\r"); batfile.WriteString("if exist "+ myexename +" goto loop" + "\n\r"); batfile.WriteString("copy "+ myAppDir + "\\exe" + " " + myexename + "\n\r"); batfile.WriteString("del "+ myAppDir + "\\exe" + "\n\r"); batfile.WriteString(myexename+"\n\r"); batfile.WriteString("del "+mybatFile+"\n\r"); batfile.Close(); ShellExecute(NULL,NULL,mybatFile,NULL,NULL,SW_HIDE); CFrameWnd::OnClose(); }; }