// SlParaDlg.cpp : implementation file // #include "stdafx.h" #include "SerialTester.h" #include "MainDlg.h" #include #include #include using namespace std; #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CSlParaDlg dialog CSlParaDlg::CSlParaDlg(CDialog* pParent /*=NULL*/, char motorparam) // : CDialog(CSlParaDlg::IDD, pParent) { //{{AFX_DATA_INIT(CSlParaDlg) //}}AFX_DATA_INIT motor = motorparam; } /* For getting rid of the window properly. */ void CSlParaDlg::PostNcDestroy() { CDialog::PostNcDestroy(); delete this; } CSlParaDlg::~CSlParaDlg() { } void CSlParaDlg::OnCancel() { CDialog::OnCancel(); ((CMainDlg*)AfxGetMainWnd())->SetForegroundWindow(); } /* GetValues() method asks m_pPara if there are any values corresponding to the keys. */ void CSlParaDlg::GetValues() { string value; value = m_pPara->GetMotorParam(motor,"VELOCITY"); m_Vel.SetWindowText(value.c_str()); value = m_pPara->GetMotorParam(motor,"MAX_VELOCITY"); m_MaxVel.SetWindowText(value.c_str()); value = m_pPara->GetMotorParam(motor,"ACCELERATION"); m_Acc.SetWindowText(value.c_str()); value = m_pPara->GetMotorParam(motor,"CAL_VELOCITY"); m_CalVel.SetWindowText(value.c_str()); value = m_pPara->GetMotorParam(motor,"POS_ERROR"); m_AllowError.SetWindowText(value.c_str()); value = m_pPara->GetMotorParam(motor,"ERROR_STOP"); m_ErrStop.SetWindowText(value.c_str()); value = m_pPara->GetMotorParam(motor,"CNTL_P"); m_P.SetWindowText(value.c_str()); value = m_pPara->GetMotorParam(motor,"CNTL_I"); m_I.SetWindowText(value.c_str()); value = m_pPara->GetMotorParam(motor,"CNTL_D"); m_D.SetWindowText(value.c_str()); value = m_pPara->GetMotorParam(motor,"CNTL_L"); m_IntLimit.SetWindowText(value.c_str()); value = m_pPara->GetMotorParam(motor,"POWER_ON"); if(value=="1") m_Power.SetCheck(1); else m_Power.SetCheck(0); value = m_pPara->GetMotorParam(motor,"SCALE"); m_Scale.SetWindowText(value.c_str()); value = m_pPara->GetMotorParam(motor,"FWD_IN"); m_FwdIn.SetWindowText(value.c_str()); value = m_pPara->GetMotorParam(motor,"BACK_IN"); m_BackIn.SetWindowText(value.c_str()); value = m_pPara->GetMotorParam(motor,"ENABLE_IN"); m_EnabIn.SetWindowText(value.c_str()); value = m_pPara->GetMotorParam(motor,"RUN_IND_OUT"); m_RunInOut.SetWindowText(value.c_str()); value = m_pPara->GetMotorParam(motor,"HIGH_IN"); m_HighIn.SetWindowText(value.c_str()); } /* SetValues sends all the values to m_pPara. This is done when saving or sending values to controller. */ void CSlParaDlg::SetValues() { CString sValueStr; m_Vel.GetWindowText(sValueStr); m_pPara->SaveMotorParam(motor, "VELOCITY", (LPCTSTR)sValueStr); m_MaxVel.GetWindowText(sValueStr); m_pPara->SaveMotorParam(motor, "MAX_VELOCITY", (LPCTSTR)sValueStr); m_Acc.GetWindowText(sValueStr); m_pPara->SaveMotorParam(motor, "ACCELERATION", (LPCTSTR)sValueStr); m_CalVel.GetWindowText(sValueStr); m_pPara->SaveMotorParam(motor, "CAL_VELOCITY", (LPCTSTR)sValueStr); m_AllowError.GetWindowText(sValueStr); m_pPara->SaveMotorParam(motor, "POS_ERROR", (LPCTSTR)sValueStr); m_ErrStop.GetWindowText(sValueStr); m_pPara->SaveMotorParam(motor, "ERROR_STOP", (LPCTSTR)sValueStr); m_P.GetWindowText(sValueStr); m_pPara->SaveMotorParam(motor, "CNTL_P", (LPCTSTR)sValueStr); m_I.GetWindowText(sValueStr); m_pPara->SaveMotorParam(motor, "CNTL_I", (LPCTSTR)sValueStr); m_D.GetWindowText(sValueStr); m_pPara->SaveMotorParam(motor, "CNTL_D", (LPCTSTR)sValueStr); m_IntLimit.GetWindowText(sValueStr); m_pPara->SaveMotorParam(motor, "CNTL_L", (LPCTSTR)sValueStr); int i = m_Power.GetCheck(); char s[1]; sprintf(s,"%d",i); m_pPara->SaveMotorParam(motor, "POWER_ON", (LPCTSTR)s); m_Scale.GetWindowText(sValueStr); m_pPara->SaveMotorParam(motor, "SCALE", (LPCTSTR)sValueStr); m_FwdIn.GetWindowText(sValueStr); m_pPara->SaveMotorParam(motor, "FWD_IN", (LPCTSTR)sValueStr); m_BackIn.GetWindowText(sValueStr); m_pPara->SaveMotorParam(motor, "BACK_IN", (LPCTSTR)sValueStr); m_EnabIn.GetWindowText(sValueStr); m_pPara->SaveMotorParam(motor, "ENABLE_IN", (LPCTSTR)sValueStr); m_RunInOut.GetWindowText(sValueStr); m_pPara->SaveMotorParam(motor, "RUN_IND_OUT", (LPCTSTR)sValueStr); m_HighIn.GetWindowText(sValueStr); m_pPara->SaveMotorParam(motor, "HIGH_IN", (LPCTSTR)sValueStr); } /* SlControl class tells to use given pointer. */ void CSlParaDlg::UseThis(CSlPara* pPr){ m_pPara = pPr; } void CSlParaDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSlParaDlg) DDX_Control(pDX, IDC_EDITVEL, m_Vel); DDX_Control(pDX, IDC_EDITACC, m_Acc); DDX_Control(pDX, IDC_EDITINDOUT, m_RunInOut); DDX_Control(pDX, IDC_EDITHSPDIN, m_HighIn); DDX_Control(pDX, IDC_EDITFIN, m_FwdIn); DDX_Control(pDX, IDC_EDITENIN, m_EnabIn); DDX_Control(pDX, IDC_EDITBIN, m_BackIn); DDX_Control(pDX, IDC_CHECKPWRTRGT, m_Power); DDX_Control(pDX, IDC_EDITAERR, m_AllowError); DDX_Control(pDX, IDC_EDITCVEL, m_CalVel); DDX_Control(pDX, IDC_EDITD, m_D); DDX_Control(pDX, IDC_EDITERRSTOP, m_ErrStop); DDX_Control(pDX, IDC_EDITI, m_I); DDX_Control(pDX, IDC_EDITINTLIM, m_IntLimit); DDX_Control(pDX, IDC_EDITMVEL, m_MaxVel); DDX_Control(pDX, IDC_EDITP, m_P); DDX_Control(pDX, IDC_EDITSCALE, m_Scale); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CSlParaDlg, CDialog) //{{AFX_MSG_MAP(CSlParaDlg) ON_BN_CLICKED(IDC_BUTTONLOAD, OnButtonLoad) ON_BN_CLICKED(IDC_BUTTOSAVE, OnButtonSave) ON_BN_CLICKED(IDOK, OnButtonSend) ON_WM_HELPINFO() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSlParaDlg message handlers /* OnButtonLoad opens CFileDialog so that user can load motor parameter files.. */ void CSlParaDlg::OnButtonLoad() { CFileDialog dlg( TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_FILEMUSTEXIST, "Parameter Files (*.par)|*.par||", this); dlg.m_ofn.lpstrTitle = "Open motor parameter file"; if ( dlg.DoModal() == IDOK) { string file = dlg.GetPathName(); m_pPara->LoadMotorParams(file); } GetValues(); } /* OnButtonSave checks first if all the fields are numeric, and if they are it calls SetValues function that sends the values to m_pSlPara. Then it opens Windows FileDialog and asks the user the name for the file. Finally OnButtonSave calls SaveMotorParamsInFile method that saves motor parameters in file. */ void CSlParaDlg::OnButtonSave() { string isOK = CheckValues(); if(isOK!="") { if(isOK.find_first_of(",") != string::npos) MessageBox(("Fields " + isOK +" are not numeric.").c_str()); else MessageBox(("Field " +isOK + " is not numeric.").c_str()); } else { SetValues(); CFileDialog dlg( FALSE, "par", NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "Parameter Files (*.par)|*.par||", this); dlg.m_ofn.lpstrTitle = "Save motor parameter file"; if ( dlg.DoModal() == IDOK) { string file = dlg.GetPathName(); m_pPara->SaveMotorParamsInFile(file); } } } /* OnButtonSend checks first if all the fields are numeric, and if they are it calls SetValues function that sends the values to m_pSlPara. After that it asks m_pController to send the values to Controller. */ void CSlParaDlg::OnButtonSend() { string isOK = CheckValues(); if(isOK!="") { if(isOK.find_first_of(",") != string::npos) MessageBox(("Fields " + isOK +" are not numeric.").c_str()); else MessageBox(("Field " +isOK + " is not numeric.").c_str()); } else { if (m_bScaleError) { MessageBox("Position field * scale must be < 32767."); return; } SetValues(); CMainDlg* p_Main= ((CMainDlg*)AfxGetMainWnd()); p_Main->m_pController->SetParameters(motor,true); p_Main->SetParamsSent(motor, true); } } /* Function isNumeric checks if string given is numeric. */ bool isNumeric(const string &s) { return (s.find_first_not_of("0123456789.-+eE") == string::npos); } /* CheckValues returns a string that contains names of the fields that are not numeric. */ string CSlParaDlg::CheckValues() { CString sValueCStr; string value, returnvalue; double nEtoStop=0; double nAError=0; double nScale=0; m_bScaleError=false; m_Vel.GetWindowText(sValueCStr); value = (LPCTSTR)sValueCStr; if(!isNumeric(value)) { if(returnvalue != "") returnvalue += ", "; returnvalue += "velocity"; } m_MaxVel.GetWindowText(sValueCStr); value = (LPCTSTR)sValueCStr; if(!isNumeric(value)) { if(returnvalue != "") returnvalue += ", "; returnvalue += "Max. velocity"; } m_Acc.GetWindowText(sValueCStr); value = (LPCTSTR)sValueCStr; if(!isNumeric(value)) { if(returnvalue != "") returnvalue += ", "; returnvalue += "Acceleration"; } m_CalVel.GetWindowText(sValueCStr); value = (LPCTSTR)sValueCStr; if(!isNumeric(value)) { if(returnvalue != "") returnvalue += ", "; returnvalue += "Calibr. Vel"; } m_AllowError.GetWindowText(sValueCStr); value = (LPCTSTR)sValueCStr; if(!isNumeric(value)) { if(returnvalue != "") returnvalue += ", "; returnvalue += "Allowed error"; } else { stringstream ss(value.c_str()); ss >> nAError; } m_ErrStop.GetWindowText(sValueCStr); value = (LPCTSTR)sValueCStr; if(!isNumeric(value)) { if(returnvalue != "") returnvalue += ", "; returnvalue += "Error to stop"; } else { stringstream ss(value.c_str()); ss >> nEtoStop; } m_P.GetWindowText(sValueCStr); value = (LPCTSTR)sValueCStr; if(!isNumeric(value)) { if(returnvalue != "") returnvalue += ", "; returnvalue += "P"; } m_I.GetWindowText(sValueCStr); value = (LPCTSTR)sValueCStr; if(!isNumeric(value)) { if(returnvalue != "") returnvalue += ", "; returnvalue += "I"; } m_D.GetWindowText(sValueCStr); value = (LPCTSTR)sValueCStr; if(!isNumeric(value)) { if(returnvalue != "") returnvalue += ", "; returnvalue += "D"; } m_IntLimit.GetWindowText(sValueCStr); value = (LPCTSTR)sValueCStr; if(!isNumeric(value)) { if(returnvalue != "") returnvalue += ", "; returnvalue += "Integr. limit"; } m_FwdIn.GetWindowText(sValueCStr); value = (LPCTSTR)sValueCStr; if(!isNumeric(value)) { if(returnvalue != "") returnvalue += ", "; returnvalue += "Fwd. in"; } m_BackIn.GetWindowText(sValueCStr); value = (LPCTSTR)sValueCStr; if(!isNumeric(value)) { if(returnvalue != "") returnvalue += ", "; returnvalue += "Backw. in"; } m_EnabIn.GetWindowText(sValueCStr); value = (LPCTSTR)sValueCStr; if(!isNumeric(value)) { if(returnvalue != "") returnvalue += ", "; returnvalue += "Enable in"; } m_RunInOut.GetWindowText(sValueCStr); value = (LPCTSTR)sValueCStr; if(!isNumeric(value)) { if(returnvalue != "") returnvalue += ", "; returnvalue += "Run ind. out"; } m_HighIn.GetWindowText(sValueCStr); value = (LPCTSTR)sValueCStr; if(!isNumeric(value)) { if(returnvalue != "") returnvalue += ", "; returnvalue += "High spd. In"; } m_Scale.GetWindowText(sValueCStr); value = (LPCTSTR)sValueCStr; if(!isNumeric(value)) { if(returnvalue != "") returnvalue += ", "; returnvalue += "Scale"; } else { stringstream ss(value.c_str()); ss >> nScale; if (nScale*nAError > MAX_INTEGER || nScale*nEtoStop > MAX_INTEGER) m_bScaleError=true; } return returnvalue; } /* Catch the controls needed for edit fields. If not any of those, do shortcut handling for the paramerer dialog. (same as in the main dialog) */ BOOL CSlParaDlg::PreTranslateMessage(MSG* pMsg) { if (pMsg->message == WM_KEYUP || pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYUP || pMsg->message == WM_SYSKEYDOWN){ if ((pMsg->wParam==VK_DOWN) || (pMsg->wParam==VK_UP) || (pMsg->wParam==VK_NEXT) || (pMsg->wParam==VK_PRIOR) || (pMsg->wParam==VK_LEFT) || (pMsg->wParam==VK_RIGHT) || (pMsg->wParam==VK_TAB)) return CDialog::PreTranslateMessage(pMsg); } return ((CMainDlg*)AfxGetMainWnd())->PreTranslateMessage(pMsg); } /* For overriding the windows F1 help behaviour */ BOOL CSlParaDlg::OnHelpInfo(HELPINFO* pHelpInfo) { return false; }