From 27faa25b5859d88d5fe61c8f5aaaf87ccd6bd7ef Mon Sep 17 00:00:00 2001 From: Karol Krizka <karol.krizka@cern.ch> Date: Thu, 10 Dec 2020 16:02:17 +0000 Subject: [PATCH] SCPIPs: Check only first character of return string to ignore termination with CharDevCom. --- src/libPS/SCPIPs.cpp | 2 ++ src/libUtils/StringUtils.h | 51 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/libUtils/StringUtils.h diff --git a/src/libPS/SCPIPs.cpp b/src/libPS/SCPIPs.cpp index 2e96921..60660ac 100644 --- a/src/libPS/SCPIPs.cpp +++ b/src/libPS/SCPIPs.cpp @@ -4,6 +4,7 @@ #include <thread> #include "Logger.h" +#include "StringUtils.h" //Register power supply #include "PowerSupplyRegistry.h" @@ -110,6 +111,7 @@ void SCPIPs::send(const std::string& cmd) { m_com->send(cmd); std::string opcreply=m_com->sendreceive("*OPC?"); + utils::rtrim(opcreply); if(opcreply!="1") throw std::runtime_error("SCPI command not completed"); } diff --git a/src/libUtils/StringUtils.h b/src/libUtils/StringUtils.h new file mode 100644 index 0000000..5c77109 --- /dev/null +++ b/src/libUtils/StringUtils.h @@ -0,0 +1,51 @@ +#ifndef STRINGUTILS_H +#define STRINGUTILS_H + +#include <string> +#include <algorithm> + +namespace utils +{ + // + // Credit for trim functions: https://stackoverflow.com/a/217605 + + //! \brief Remove whitespaces from start of string + /** + * See std::isspace for definition of whitespace + * + * \param s String object from which to remove white space from + */ + static inline void ltrim(std::string &s) + { + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) + { return !std::isspace(ch); } + )); + } + + //! \brief Remove whitespaces from end of string + /** + * See std::isspace for definition of whitespace + * + * \param s String object from which to remove white space from + */ + static inline void rtrim(std::string &s) + { + s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) + { return !std::isspace(ch); } + ).base(), s.end()); + } + + //! \brief Remove whitespaces from both sides of a string + /** + * See std::isspace for definition of whitespace + * + * \param s String object from which to remove white space from + */ + static inline void trim(std::string &s) + { + ltrim(s); + rtrim(s); + } +}; + +#endif // STRINGUTILS_H -- GitLab