OpcUaCanOpen
CANopen OPC-UA server
CanModuleUtils.h
Go to the documentation of this file.
1 
23 #ifndef CanModuleUTILS_H_
24 #define CanModuleUTILS_H_
25 
26 #ifdef _WIN32
27 #include <Winsock2.h>
28 #else
29 #include <sys/time.h>
30 #endif
31 #include <sstream>
32 #include <stdexcept>
33 #include <string>
34 #include <chrono>
35 
36 namespace CanModule
37 {
39  {
40  public:
41  template<typename T>
42  static std::string toString(const T t)
43  {
44  std::ostringstream oss;
45  oss << t;
46  return oss.str();
47  }
48 
49  template<typename T>
50  static std::string toHexString(const T t, unsigned int width = 0, char zeropad = ' ')
51  {
52  std::ostringstream oss;
53  oss << std::hex;
54  if (width > 0)
55  {
56  oss.width(width);
57  oss.fill(zeropad);
58  }
59  oss << (unsigned long)t << std::dec;
60  return oss.str();
61  }
62 
63  static unsigned int fromHexString(const std::string &s)
64  {
65  unsigned int x;
66  std::istringstream iss(s);
67  iss >> std::hex >> x;
68  if (iss.bad())
69  throw std::runtime_error("Given string '" + s + "' not convertible from hex to uint");
70  return x;
71  }
72 
73  };
74 
75  timeval convertTimepointToTimeval(const std::chrono::system_clock::time_point &t1);
76  std::chrono::system_clock::time_point convertTimevalToTimepoint(const timeval &t1);
77  double CanModulesubtractTimeval(const std::chrono::system_clock::time_point &t1, const std::chrono::system_clock::time_point &t2);
78  std::chrono::system_clock::time_point currentTimeTimeval();
79  //UaString bytesToUaString( const unsigned char* data, unsigned int len );
80 
82 }
83 #endif /* UTILS_H_ */