gccDppConsole Test C++ SDK
20170920
DPP C++ Console Demonstration
|
00001 00003 // gccDppConsole.cpp : Defines the entry point for the console application. 00004 #include <iostream> 00005 using namespace std; 00006 #include "ConsoleHelper.h" 00007 #include "stringex.h" 00008 00009 #ifdef _WIN32 00010 #include <windows.h> 00011 #include <conio.h> 00012 #define CLEAR_TERM "cls" 00013 #else 00014 #include <unistd.h> 00015 #define Sleep(x) usleep((x)*1000) 00016 #define _getch getchar 00017 #define CLEAR_TERM "clear" 00018 #endif 00019 00020 CConsoleHelper chdpp; // DPP communications functions 00021 bool bRunSpectrumTest = false; // run spectrum test 00022 bool bRunConfigurationTest = false; // run configuration test 00023 bool bHaveStatusResponse = false; // have status response 00024 bool bHaveConfigFromHW = false; // have configuration from hardware 00025 00026 // connect to default dpp 00027 // CConsoleHelper::LibUsb_Connect_Default_DPP // LibUsb connect to default DPP 00028 void ConnectToDefaultDPP() 00029 { 00030 cout << endl; 00031 cout << "Running DPP LibUsb tests from console..." << endl; 00032 cout << endl; 00033 cout << "\tConnecting to default LibUsb device..." << endl; 00034 if (chdpp.LibUsb_Connect_Default_DPP()) { 00035 cout << "\t\tLibUsb DPP device connected." << endl; 00036 cout << "\t\tLibUsb DPP devices present: " << chdpp.LibUsb_NumDevices << endl; 00037 } else { 00038 cout << "\t\tLibUsb DPP device not connected." << endl; 00039 cout << "\t\tNo LibUsb DPP device present." << endl; 00040 } 00041 } 00042 00043 // Get DPP Status 00044 // CConsoleHelper::LibUsb_isConnected // check if DPP is connected 00045 // CConsoleHelper::LibUsb_SendCommand(XMTPT_SEND_STATUS) // request status 00046 // CConsoleHelper::LibUsb_ReceiveData() // parse the status 00047 // CConsoleHelper::DppStatusString // display status string 00048 void GetDppStatus() 00049 { 00050 if (chdpp.LibUsb_isConnected) { // send and receive status 00051 cout << endl; 00052 cout << "\tRequesting Status..." << endl; 00053 if (chdpp.LibUsb_SendCommand(XMTPT_SEND_STATUS)) { // request status 00054 cout << "\t\tStatus sent." << endl; 00055 cout << "\t\tReceiving status..." << endl; 00056 if (chdpp.LibUsb_ReceiveData()) { 00057 cout << "\t\t\tStatus received..." << endl; 00058 cout << chdpp.DppStatusString << endl; 00059 bRunSpectrumTest = true; 00060 bHaveStatusResponse = true; 00061 bRunConfigurationTest = true; 00062 } else { 00063 cout << "\t\tError receiving status." << endl; 00064 } 00065 } else { 00066 cout << "\t\tError sending status." << endl; 00067 } 00068 } 00069 } 00070 00071 // Read Full DPP Configuration From Hardware // request status before sending/receiving configurations 00072 // CONFIG_OPTIONS // holds configuration command options 00073 // CConsoleHelper::CreateConfigOptions // creates configuration options from last status read 00074 // CConsoleHelper::ClearConfigReadFormatFlags(); // clear configuration format flags, for cfg readback 00075 // CConsoleHelper::CfgReadBack = true; // requesting general readback format 00076 // CConsoleHelper::LibUsb_SendCommand_Config // send command with options 00077 // CConsoleHelper::LibUsb_ReceiveData() // parse the configuration 00078 // CConsoleHelper::HwCfgReady // config is ready 00079 void ReadDppConfigurationFromHardware(bool bDisplayCfg) 00080 { 00081 CONFIG_OPTIONS CfgOptions; 00082 if (bHaveStatusResponse && bRunConfigurationTest) { 00083 //test configuration functions 00084 // Set options for XMTPT_FULL_READ_CONFIG_PACKET 00085 chdpp.CreateConfigOptions(&CfgOptions, "", chdpp.DP5Stat, false); 00086 cout << endl; 00087 cout << "\tRequesting Full Configuration..." << endl; 00088 chdpp.ClearConfigReadFormatFlags(); // clear all flags, set flags only for specific readback properties 00089 //chdpp.DisplayCfg = false; // DisplayCfg format overrides general readback format 00090 chdpp.CfgReadBack = true; // requesting general readback format 00091 if (chdpp.LibUsb_SendCommand_Config(XMTPT_FULL_READ_CONFIG_PACKET, CfgOptions)) { // request full configuration 00092 if (chdpp.LibUsb_ReceiveData()) { 00093 if (chdpp.HwCfgReady) { // config is ready 00094 bHaveConfigFromHW = true; 00095 if (bDisplayCfg) { 00096 cout << "\t\t\tConfiguration Length: " << (unsigned int)chdpp.HwCfgDP5.length() << endl; 00097 cout << "\t================================================================" << endl; 00098 cout << chdpp.HwCfgDP5 << endl; 00099 cout << "\t================================================================" << endl; 00100 cout << "\t\t\tScroll up to see configuration settings." << endl; 00101 cout << "\t================================================================" << endl; 00102 } else { 00103 cout << "\t\tFull configuration received." << endl; 00104 } 00105 } 00106 } 00107 } 00108 } 00109 } 00110 00111 // Display Preset Settings 00112 // CConsoleHelper::strPresetCmd // preset mode 00113 // CConsoleHelper::strPresetVal // preset setting 00114 void DisplayPresets() 00115 { 00116 if (bHaveConfigFromHW) { 00117 cout << "\t\t\tPreset Mode: " << chdpp.strPresetCmd << endl; 00118 cout << "\t\t\tPreset Settings: " << chdpp.strPresetVal << endl; 00119 } 00120 } 00121 00122 // Display Preset Settings 00123 // CONFIG_OPTIONS // holds configuration command options 00124 // CConsoleHelper::CreateConfigOptions // creates configuration options from last status read 00125 // CConsoleHelper::HwCfgDP5Out // preset setting 00126 // CConsoleHelper::LibUsb_SendCommand_Config // send command with options 00127 void SendPresetAcquisitionTime(string strPRET) 00128 { 00129 CONFIG_OPTIONS CfgOptions; 00130 cout << "\tSetting Preset Acquisition Time..." << strPRET << endl; 00131 chdpp.CreateConfigOptions(&CfgOptions, "", chdpp.DP5Stat, false); 00132 CfgOptions.HwCfgDP5Out = strPRET; 00133 // send PresetAcquisitionTime string, bypass any filters, read back the mode and settings 00134 if (chdpp.LibUsb_SendCommand_Config(XMTPT_SEND_CONFIG_PACKET_EX, CfgOptions)) { 00135 ReadDppConfigurationFromHardware(false); // read setting back 00136 DisplayPresets(); // display new presets 00137 } else { 00138 cout << "\t\tPreset Acquisition Time NOT SET" << strPRET << endl; 00139 } 00140 } 00141 00142 // Acquire Spectrum 00143 // CConsoleHelper::LibUsb_SendCommand(XMTPT_DISABLE_MCA_MCS) //disable for data/status clear 00144 // CConsoleHelper::LibUsb_SendCommand(XMTPT_SEND_CLEAR_SPECTRUM_STATUS) //clear spectrum/status 00145 // CConsoleHelper::LibUsb_SendCommand(XMTPT_ENABLE_MCA_MCS); // enabling MCA for spectrum acquisition 00146 // CConsoleHelper::LibUsb_SendCommand(XMTPT_SEND_SPECTRUM_STATUS)) // request spectrum+status 00147 // CConsoleHelper::LibUsb_ReceiveData() // process spectrum and data 00148 // CConsoleHelper::ConsoleGraph() (low resolution display) // graph data on console with status 00149 // CConsoleHelper::LibUsb_SendCommand(XMTPT_DISABLE_MCA_MCS) // disable mca after acquisition 00150 void AcquireSpectrum() 00151 { 00152 int MaxMCA = 11; 00153 bool bDisableMCA; 00154 00155 //bRunSpectrumTest = false; // disable test 00156 if (bRunSpectrumTest) { 00157 cout << "\tRunning spectrum test..." << endl; 00158 cout << "\t\tDisabling MCA for spectrum data/status clear." << endl; 00159 chdpp.LibUsb_SendCommand(XMTPT_DISABLE_MCA_MCS); 00160 Sleep(1000); 00161 cout << "\t\tClearing spectrum data/status." << endl; 00162 chdpp.LibUsb_SendCommand(XMTPT_SEND_CLEAR_SPECTRUM_STATUS); 00163 Sleep(1000); 00164 cout << "\t\tEnabling MCA for spectrum data acquisition with status ." << endl; 00165 chdpp.LibUsb_SendCommand(XMTPT_ENABLE_MCA_MCS); 00166 Sleep(1000); 00167 for(int idxSpectrum=0;idxSpectrum<MaxMCA;idxSpectrum++) { 00168 //cout << "\t\tAcquiring spectrum data set " << (idxSpectrum+1) << " of " << MaxMCA << endl; 00169 if (chdpp.LibUsb_SendCommand(XMTPT_SEND_SPECTRUM_STATUS)) { // request spectrum+status 00170 if (chdpp.LibUsb_ReceiveData()) { 00171 bDisableMCA = true; // we are aquiring data, disable mca when done 00172 system(CLEAR_TERM); 00173 chdpp.ConsoleGraph(chdpp.DP5Proto.SPECTRUM.DATA,chdpp.DP5Proto.SPECTRUM.CHANNELS,true,chdpp.DppStatusString); 00174 Sleep(2000); 00175 } 00176 } else { 00177 cout << "\t\tProblem acquiring spectrum." << endl; 00178 break; 00179 } 00180 } 00181 if (bDisableMCA) { 00182 //system("Pause"); 00183 //cout << "\t\tSpectrum acquisition with status done. Disabling MCA." << endl; 00184 chdpp.LibUsb_SendCommand(XMTPT_DISABLE_MCA_MCS); 00185 Sleep(1000); 00186 } 00187 } 00188 } 00189 00190 // Read Configuration File 00191 // CConsoleHelper::SndCmd.GetDP5CfgStr("PX5_Console_Test.txt"); 00192 void ReadConfigFile() 00193 { 00194 std::string strCfg; 00195 strCfg = chdpp.SndCmd.AsciiCmdUtil.GetDP5CfgStr("PX5_Console_Test.txt"); 00196 cout << "\t\t\tConfiguration Length: " << (unsigned int)strCfg.length() << endl; 00197 cout << "\t================================================================" << endl; 00198 cout << strCfg << endl; 00199 cout << "\t================================================================" << endl; 00200 } 00201 00202 //Following is an example of loading a configuration from file 00203 //then sending the configuration to the DPP device. 00204 // SendConfigFileToDpp("NaI_detector_cfg.txt"); // calls SendCommandString 00205 // AcquireSpectrum(); 00206 // 00207 bool SendCommandString(string strCMD) { 00208 CONFIG_OPTIONS CfgOptions; 00209 chdpp.CreateConfigOptions(&CfgOptions, "", chdpp.DP5Stat, false); 00210 CfgOptions.HwCfgDP5Out = strCMD; 00211 // send ASCII command string, bypass any filters, read back the mode and settings 00212 if (chdpp.LibUsb_SendCommand_Config(XMTPT_SEND_CONFIG_PACKET_EX, CfgOptions)) { 00213 // command sent 00214 } else { 00215 cout << "\t\tASCII Command String NOT SENT" << strCMD << endl; 00216 return false; 00217 } 00218 return true; 00219 } 00220 00221 std::string ShortenCfgCmds(std::string strCfgIn) { 00222 std::string strCfg(""); 00223 strCfg = strCfgIn; 00224 long lCfgLen=0; //ASCII Configuration Command String Length 00225 lCfgLen = (long)strCfg.length(); 00226 if (lCfgLen > 0) { 00227 strCfg = chdpp.SndCmd.AsciiCmdUtil.ReplaceCmdText(strCfg, "US;", ";"); 00228 strCfg = chdpp.SndCmd.AsciiCmdUtil.ReplaceCmdText(strCfg, "OFF;", "OF;"); 00229 strCfg = chdpp.SndCmd.AsciiCmdUtil.ReplaceCmdText(strCfg, "RISING;", "RI;"); 00230 strCfg = chdpp.SndCmd.AsciiCmdUtil.ReplaceCmdText(strCfg, "FALLING;", "FA;"); 00231 } 00232 return strCfg; 00233 } 00234 00235 // run GetDppStatus(); first to get PC5_PRESENT, DppType 00236 // Includes Configuration Oversize Fix 20141224 00237 bool SendConfigFileToDpp(string strFilename){ 00238 std::string strCfg; 00239 long lCfgLen=0; //ASCII Configuration Command String Length 00240 bool bCommandSent=false; 00241 bool isPC5Present=false; 00242 int DppType=0; 00243 int idxSplitCfg=0; //Configuration split position, only if necessary 00244 bool bSplitCfg=false; //Configuration split flag 00245 std::string strSplitCfg(""); //Configuration split string second buffer 00246 bool isDP5_RevDxGains; 00247 unsigned char DPP_ECO; 00248 00249 isPC5Present = chdpp.DP5Stat.m_DP5_Status.PC5_PRESENT; 00250 DppType = chdpp.DP5Stat.m_DP5_Status.DEVICE_ID; 00251 isDP5_RevDxGains = chdpp.DP5Stat.m_DP5_Status.isDP5_RevDxGains; 00252 DPP_ECO = chdpp.DP5Stat.m_DP5_Status.DPP_ECO; 00253 00254 strCfg = chdpp.SndCmd.AsciiCmdUtil.GetDP5CfgStr(strFilename); 00255 strCfg = chdpp.SndCmd.AsciiCmdUtil.RemoveCmdByDeviceType(strCfg,isPC5Present,DppType,isDP5_RevDxGains,DPP_ECO); 00256 lCfgLen = (long)strCfg.length(); 00257 if ((lCfgLen > 0) && (lCfgLen <= 512)) { // command length ok 00258 cout << "\t\t\tConfiguration Length: " << lCfgLen << endl; 00259 } else if (lCfgLen > 512) { // configuration too large, needs fix 00260 cout << "\t\t\tConfiguration Length (Will Shorten): " << lCfgLen << endl; 00261 strCfg = ShortenCfgCmds(strCfg); 00262 lCfgLen = (long)strCfg.length(); 00263 if (lCfgLen > 512) { // configuration still too large, split config 00264 cout << "\t\t\tConfiguration Length (Will Split): " << lCfgLen << endl; 00265 bSplitCfg = true; 00266 idxSplitCfg = chdpp.SndCmd.AsciiCmdUtil.GetCmdChunk(strCfg); 00267 cout << "\t\t\tConfiguration Split at: " << idxSplitCfg << endl; 00268 strSplitCfg = strCfg.substr(idxSplitCfg); 00269 strCfg = strCfg.substr(0, idxSplitCfg); 00270 } 00271 } else { 00272 cout << "\t\t\tConfiguration Length Error: " << lCfgLen << endl; 00273 return false; 00274 } 00275 bCommandSent = SendCommandString(strCfg); 00276 if (bSplitCfg) { 00277 // Sleep(40); // may need delay here 00278 bCommandSent = SendCommandString(strSplitCfg); 00279 } 00280 return bCommandSent; 00281 } 00282 00283 // Close Connection 00284 // CConsoleHelper::LibUsb_isConnected // LibUsb DPP connection indicator 00285 // CConsoleHelper::LibUsb_Close_Connection() // close connection 00286 void CloseConnection() 00287 { 00288 if (chdpp.LibUsb_isConnected) { // send and receive status 00289 cout << endl; 00290 cout << "\tClosing connection to default LibUsb device..." << endl; 00291 chdpp.LibUsb_Close_Connection(); 00292 cout << "\t\tDPP device connection closed." << endl; 00293 } 00294 } 00295 00296 // Helper functions for saving spectrum files 00297 void SaveSpectrumConfig() 00298 { 00299 string strSpectrumConfig; 00300 chdpp.Dp5CmdList = chdpp.MakeDp5CmdList(); // ascii text command list for adding comments 00301 strSpectrumConfig = chdpp.CreateSpectrumConfig(chdpp.HwCfgDP5); // append configuration comments 00302 chdpp.sfInfo.strSpectrumConfig = strSpectrumConfig; 00303 } 00304 00305 // Saving spectrum file 00306 void SaveSpectrumFile() 00307 { 00308 string strSpectrum; // holds final spectrum file 00309 chdpp.sfInfo.strSpectrumStatus = chdpp.DppStatusString; // save last status after acquisition 00310 chdpp.sfInfo.m_iNumChan = chdpp.mcaCH; // number channels in spectrum 00311 chdpp.sfInfo.SerialNumber = chdpp.DP5Stat.m_DP5_Status.SerialNumber; // dpp serial number 00312 chdpp.sfInfo.strDescription = "Amptek Spectrum File"; // description 00313 chdpp.sfInfo.strTag = "TestTag"; // tag 00314 // create spectrum file, save file to string 00315 strSpectrum = chdpp.CreateMCAData(chdpp.DP5Proto.SPECTRUM.DATA,chdpp.sfInfo,chdpp.DP5Stat.m_DP5_Status); 00316 chdpp.SaveSpectrumStringToFile(strSpectrum); // save spectrum file string to file 00317 } 00318 00319 int main(int argc, char* argv[]) 00320 { 00321 system(CLEAR_TERM); 00322 ConnectToDefaultDPP(); 00323 cout << "Press the Enter key to continue . . ."; 00324 _getch(); 00325 00326 if(!chdpp.LibUsb_isConnected) { return 1; } 00327 00328 system(CLEAR_TERM); 00329 chdpp.DP5Stat.m_DP5_Status.SerialNumber = 0; 00330 GetDppStatus(); 00331 cout << "Press the Enter key to continue . . ."; 00332 _getch(); 00333 00334 if (chdpp.DP5Stat.m_DP5_Status.SerialNumber == 0) { return 1; } 00335 00339 00340 system(CLEAR_TERM); 00341 ReadDppConfigurationFromHardware(true); 00342 cout << "Press the Enter key to continue . . ."; 00343 _getch(); 00344 00345 system(CLEAR_TERM); 00346 DisplayPresets(); 00347 cout << "Press the Enter key to continue . . ."; 00348 _getch(); 00349 00350 system(CLEAR_TERM); 00351 SendPresetAcquisitionTime("PRET=20;"); 00352 SaveSpectrumConfig(); 00353 cout << "Press the Enter key to continue . . ."; 00354 _getch(); 00355 00356 system(CLEAR_TERM); 00357 AcquireSpectrum(); 00358 SaveSpectrumFile(); 00359 cout << "Press the Enter key to continue . . ."; 00360 _getch(); 00361 00362 system(CLEAR_TERM); 00363 SendPresetAcquisitionTime("PRET=OFF;"); 00364 cout << "Press the Enter key to continue . . ."; 00365 _getch(); 00366 00367 system(CLEAR_TERM); 00368 ReadConfigFile(); 00369 cout << "Press the Enter key to continue . . ."; 00370 _getch(); 00371 00372 system(CLEAR_TERM); 00373 CloseConnection(); 00374 cout << "Press the Enter key to continue . . ."; 00375 _getch(); 00376 00377 return 0; 00378 }