// SlPara.cpp: implementation of the CSlPara class. // CSlPara opens a file and gets information about tha paremeters // that will be sent to StressLogic, and contains also the // parameters for motors and I/O ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "SlPara.h" #include "MainDlg.h" #include #include #include #include #include using namespace std; #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif #define LENGTH 14 const char COMMENT= ';'; /* split-function finds if there is a comment character on the line, and erases all text coming after that. Then it finds first string that does not contain chars given as parameter. After that it finds first one of characters given as paremeters and */ string split(string &line, const string &chars) { size_t p; //p=position p = line.find(COMMENT); if(p != string::npos) line.erase(p,line.length()-p); if (line=="") return ""; p = line.find_first_not_of(chars);line.erase(0,p); p = line.find_first_of(chars); string begin = line.substr(0,p);line.erase(0,p+1); if( p == string::npos) line=""; return begin; } /* PrintToFile prints to ostream, in this case to file */ void printToFile(ostream &os, char motor, map::iterator param){ int l=0; if (motor=='A') { os << "MOTOR_A_"; l=8; } else if (motor=='B') { os << "MOTOR_B_"; l=8; } else if (motor=='C') { os << "MOTOR_C_"; l=8; } os << param->first; if (param->first.length() +l >LENGTH) os << "=\t"; else os << "=\t\t"; os << param->second << endl; } /* Constructor for CSlPara. Opens file named "para.txt" and reads its contents to a map */ CSlPara::CSlPara() { string file = "commands.ini"; ifstream fi(file.c_str()); if (!fi) return; string line, key, value; while(getline(fi, line)) { key = split(line," \t\r\n"); if (key=="") continue; value = split(line," \t\r\n"); m_Commands.insert(map::value_type(key,value)); map::iterator i; i = m_Commands.find(key); cout << i->first << "=" << i->second << endl; } fi.close(); LoadMotorParams("lastmotor.par"); } CSlPara::~CSlPara() { SaveMotorParamsInFile("lastmotor.par"); } /* GetCommand returns a string that corresponds to key parameter, if it doesnt find corresponding value it returns "ERROR" */ string CSlPara::GetCommand(string key) { map::iterator i; i = m_Commands.find(key); if ( i != m_Commands.end() ) return (*i).second; else return "ERROR"; } /* SendMotorParams() gathers a string from the parameters that will be sent to StressLogic. Then it sends the string to its parent(IControl) */ string CSlPara::SendMotorParams(char motor, bool vel) { map motorX; map::iterator it; if (motor=='A') motorX = m_motorAParams; if (motor=='B') motorX = m_motorBParams; if (motor=='C') motorX = m_motorCParams; string result; map::iterator i; i = motorX.find("SCALE"); if ( i != motorX.end() && (*i).second != "") result = result + " X" + (*i).second; if (vel) { i = motorX.find("VELOCITY"); if ( i != motorX.end() && (*i).second != "") result = result + " V" + (*i).second; } else { i = motorX.find("CAL_VELOCITY"); if ( i != motorX.end() && (*i).second != "") result = result + " V" + (*i).second; } i = motorX.find("MAX_VELOCITY"); if ( i != motorX.end() && (*i).second != "") result = result + " M" + (*i).second; i = motorX.find("ACCELERATION"); if ( i != motorX.end() && (*i).second != "") result = result + " A" + (*i).second; i = motorX.find("POS_ERROR"); if ( i != motorX.end() && (*i).second != "") result = result + " E" + (*i).second; i = motorX.find("ERROR_STOP"); if ( i != motorX.end() && (*i).second != "") result = result + " S" + (*i).second; i = motorX.find("CNTL_P"); if ( i != motorX.end() && (*i).second != "") result = result + " P" + (*i).second; i = motorX.find("CNTL_I"); if ( i != motorX.end() && (*i).second != "") result = result + " I" + (*i).second; i = motorX.find("CNTL_D"); if ( i != motorX.end() && (*i).second != "") result = result + " D" + (*i).second; i = motorX.find("CNTL_L"); if ( i != motorX.end() && (*i).second != "") result = result + " L" + (*i).second; i = motorX.find("POWER_ON"); if ( i != motorX.end() && (*i).second != "") result = result + " H" + (*i).second; i = motorX.find("FWD_IN"); if ( i != motorX.end() && (*i).second != "") result = result + " F" + (*i).second; i = motorX.find("BACK_IN"); if ( i != motorX.end() && (*i).second != "") result = result + " B" + (*i).second; i = motorX.find("HIGH_IN"); if ( i != motorX.end() && (*i).second != "") result = result + " Y" + (*i).second; i = motorX.find("ENABLE_IN"); if ( i != motorX.end() && (*i).second != "") result = result + " Z" + (*i).second; i = motorX.find("RUN_IND_OUT"); if ( i != motorX.end() && (*i).second != "") result = result + " R" + (*i).second; return result; } /* SaveMotorParam replaces or inserts pair(key,value) to map. In other words it takes parameter as key, to know which pair it's going to replace and also the (new) value */ void CSlPara::SaveMotorParam(char motor, string key, string value){ if (motor=='A') m_motorAParams[key]=value; if (motor=='B') m_motorBParams[key]=value; if (motor=='C') m_motorCParams[key]=value; } /* SaveMotorParamsInFile saves motor parameters to a file, which name is given as a parameter */ void CSlPara::SaveMotorParamsInFile(string file) { ofstream of(file.c_str()); if (!of) return; map::iterator pos; for (pos = m_motorAParams.begin(); pos != m_motorAParams.end(); ++pos) { printToFile(of,'A',pos); } for (pos = m_motorBParams.begin(); pos != m_motorBParams.end(); ++pos) { printToFile(of,'B',pos); } for (pos = m_motorCParams.begin(); pos != m_motorCParams.end(); ++pos) { printToFile(of,'C',pos); } return; } /* LoadMotorParams reads motor and I/O parameters from a file (name as a parameter) and saves them to a map */ void CSlPara::LoadMotorParams(string file) { ifstream fi(file.c_str()); if (!fi) return; string title = "SerialTester Parameter file - " + file; ((CMainDlg*)AfxGetMainWnd())->SetWindowText(title.c_str()); string line, key, value; while (getline(fi, line)) { key = split(line," =\t\r\n"); if (key=="") continue; value = split(line," =\t\r\n"); if (key.substr(0,8) == "MOTOR_A_") { key = key.substr(8,key.length()); m_motorAParams[key]=value; } if (key.substr(0,8) == "MOTOR_B_") { key = key.substr(8,key.length()); m_motorBParams[key]=value; } if (key.substr(0,8) == "MOTOR_C_") { key = key.substr(8,key.length()); m_motorCParams[key]=value; } } } /* Return parameter value which corresponds to key given as parameter. */ string CSlPara::GetMotorParam(char motor, string key) { map motorX; map::iterator it; if (motor=='A') motorX = m_motorAParams; if (motor=='B') motorX = m_motorBParams; if (motor=='C') motorX = m_motorCParams; it = motorX.find(key); if (it==motorX.end()) return ""; //TO DO: if there is no value for key then do WHAT? return it->second; }