Main MRPT website > C++ reference for MRPT 1.5.6
CNTRIPEmitter.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #include "hwdrivers-precomp.h" // Precompiled headers
11 
13 
14 #include <mrpt/system/filesystem.h>
16 
18 
19 using namespace std;
20 using namespace mrpt;
21 using namespace mrpt::utils;
22 using namespace mrpt::obs;
23 using namespace mrpt::hwdrivers;
24 
25 /*-------------------------------------------------------------
26  CNTRIPEmitter
27 -------------------------------------------------------------*/
29  m_client(),
30  m_com_port(""),
31  m_com_bauds(38400),
32  m_transmit_to_server(true),
33  m_rate_count(0)
34 {
35 
36 }
37 
38 /*-------------------------------------------------------------
39  ~CNTRIPEmitter
40 -------------------------------------------------------------*/
41 CNTRIPEmitter::~CNTRIPEmitter()
42 {
43  m_client.close();
46 }
47 
48 /*-------------------------------------------------------------
49  doProcess
50 -------------------------------------------------------------*/
52 {
53  vector_byte buf;
55 
56  if (!buf.empty())
57  {
58  if (m_verbose) {
59  const double At = m_rate_timer.Tac();
60  m_rate_count += buf.size();
61  if (At > 5.0) {
62  const double estim_rate_Bps = m_rate_count / At;
63  cout << format("[NTRIP %s] Rate: %.02f B/s\n", mrpt::system::timeLocalToString(mrpt::system::now()).c_str(), estim_rate_Bps);
64  m_rate_timer.Tic();
65  m_rate_count = 0;
66  }
67 
68  cout << format("[NTRIP %s] RX (%u bytes)\n", mrpt::system::timeLocalToString(mrpt::system::now()).c_str(), (unsigned int)buf.size());
69  }
70  if (m_out_COM.isOpen()) {
71  // Send through the serial port:
72  cout << format("[NTRIP %s] RX: %u bytes\n", mrpt::system::timeLocalToString(mrpt::system::now()).c_str(), (unsigned)buf.size());
73  m_out_COM.WriteBuffer(&buf[0], buf.size());
74  }
75 
76  if (m_raw_output_file_stream.is_open()) {
77  m_raw_output_file_stream.write( reinterpret_cast<const char*>(&buf[0]), buf.size());
78  }
79  }
80 
81  // Try to read a msg from the receiver -> NTRIP caster
83  {
84  char rxbuf[50];
85  const size_t nReadActual = m_out_COM.Read(rxbuf,sizeof(rxbuf)-1);
86  if (nReadActual)
87  {
88  rxbuf[nReadActual] = 0;
89  if (m_verbose) cout << format("[NTRIP %s] TX (%u bytes)\n", mrpt::system::timeLocalToString(mrpt::system::now()).c_str(), (unsigned int)nReadActual);
90  }
91  }
92 
94 }
95 
96 /*-------------------------------------------------------------
97  initialize
98 -------------------------------------------------------------*/
100 {
101  if (m_out_COM.isOpen()) m_out_COM.close();
102 
103  if (!m_com_port.empty())
104  {
105  cout << format("[NTRIP] Opening %s...\n",m_com_port.c_str() );
108  m_out_COM.setTimeouts(0,0,10,0,1);
110  cout << format("[NTRIP] Open %s Ok.\n",m_com_port.c_str() );
111  }
112 
113  if (m_raw_output_file_stream.is_open()) {
114  m_raw_output_file_stream.close();
115  }
116 
117  if (!m_raw_output_file_prefix.empty())
118  {
120  m_raw_output_file_stream.open(fil.c_str(), std::ofstream::out | std::ofstream::binary);
121  if (!m_raw_output_file_stream.is_open())
122  THROW_EXCEPTION_FMT("Error opening output raw file: `%s`",fil.c_str());
123  }
124 
125  string errstr;
126  if (!m_client.open(m_ntrip_args,errstr))
127  THROW_EXCEPTION_FMT("ERROR trying to connect to NTRIP caster: %s",errstr.c_str());
128 }
129 
130 
131 /* -----------------------------------------------------
132  loadConfig_sensorSpecific
133  ----------------------------------------------------- */
136  const std::string &s )
137 {
138 #ifdef MRPT_OS_WINDOWS
139  m_com_port = c.read_string(s, "COM_port_WIN", "");
140 #else
141  m_com_port = c.read_string(s, "COM_port_LIN", "");
142 #endif
143 
144  m_raw_output_file_prefix = c.read_string(s, "raw_output_file_prefix","");
145 
146  ASSERTMSG_(!m_raw_output_file_prefix.empty() || !m_com_port.empty(), "At least one of either raw file output or serial COM file must be specified in configuration file!");
147 
148  if (!m_com_port.empty()) {
149  m_com_bauds = c.read_int(s, "baudRate", m_com_bauds, true);
150  }
151 
152  m_transmit_to_server = c.read_bool( s, "transmit_to_server",m_transmit_to_server);
153 
154  m_ntrip_args.mountpoint = mrpt::system::trim( c.read_string(s, "mountpoint","",true) );
155  m_ntrip_args.server = mrpt::system::trim( c.read_string(s, "server","",true) );
156  m_ntrip_args.port = c.read_int(s, "port",2101,true);
157 
158  m_ntrip_args.user = mrpt::system::trim( c.read_string(s, "user","") );
159  m_ntrip_args.password = mrpt::system::trim( c.read_string(s, "password","") );
160 }
161 
void initialize()
Set up the NTRIP communications, raising an exception on fatal errors.
bool open(const NTRIPArgs &params, std::string &out_errmsg)
Tries to open a given NTRIP stream and, if successful, launches a thread for continuously reading fro...
mrpt::utils::CTicTac m_rate_timer
Definition: CNTRIPEmitter.h:69
std::vector< uint8_t > vector_byte
Definition: types_simple.h:26
void setTimeouts(int ReadIntervalTimeout, int ReadTotalTimeoutMultiplier, int ReadTotalTimeoutConstant, int WriteTotalTimeoutMultiplier, int WriteTotalTimeoutConstant)
Changes the timeouts of the port, in milliseconds.
std::string m_com_port
If set to non-empty, the serial port will be attempted to be opened automatically when this class is ...
Definition: CNTRIPEmitter.h:64
const GLfloat * c
Definition: glew.h:10088
CNTRIPClient m_client
The NTRIP comms object.
Definition: CNTRIPEmitter.h:61
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound=false) const
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
Definition: CStream.cpp:67
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:70
void close()
Closes the connection.
void readAndClear(vector_byte &d)
Definition: MT_buffer.h:58
void Tic()
Starts the stopwatch.
Definition: CTicTac.cpp:77
This class allows loading and storing values and vectors of different types from a configuration text...
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
GLdouble s
Definition: glew.h:1295
void BASE_IMPEXP sleep(int time_ms) MRPT_NO_THROWS
An OS-independent method for sending the current thread to "sleep" for a given period of time...
Definition: threads.cpp:57
void setConfig(int baudRate, int parity=0, int bits=8, int nStopBits=1, bool enableFlowControl=false)
Changes the configuration of the port.
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:21
#define IMPLEMENTS_GENERIC_SENSOR(class_name, NameSpace)
This must be inserted in all CGenericSensor classes implementation files:
void close()
Close the port.
void loadConfig_sensorSpecific(const mrpt::utils::CConfigFileBase &configSource, const std::string &iniSection)
See the class documentation at the top for expected parameters.
std::string BASE_IMPEXP fileNameStripInvalidChars(const std::string &filename, const char replacement_to_invalid_chars= '_')
Replace invalid filename chars by underscores ('_') or any other user-given char. ...
Definition: filesystem.cpp:315
bool isOpen() const
Returns if port has been correctly open.
CNTRIPClient::NTRIPArgs m_ntrip_args
Definition: CNTRIPEmitter.h:59
std::ofstream m_raw_output_file_stream
Definition: CNTRIPEmitter.h:68
void doProcess()
The main loop, which must be called in a timely fashion in order to process the incomming NTRIP data ...
GLsizei const GLcharARB ** string
Definition: glew.h:3293
void purgeBuffers()
Purge tx and rx buffers.
void open()
Open the port.
Definition: CSerialPort.cpp:87
std::string BASE_IMPEXP timeLocalToString(const mrpt::system::TTimeStamp t, unsigned int secondFractionDigits=6)
Convert a timestamp into this textual form (in local time): HH:MM:SS.MMMMMM.
Definition: datetime.cpp:321
double Tac()
Stops the stopwatch.
Definition: CTicTac.cpp:92
std::string BASE_IMPEXP trim(const std::string &str)
Removes leading and trailing spaces.
bool read_bool(const std::string &section, const std::string &name, bool defaultValue, bool failIfNotFound=false) const
mrpt::synch::MT_buffer stream_data
The buffer with all the bytes so-far read from the NTRIP server stream.
Definition: CNTRIPClient.h:141
std::string BASE_IMPEXP dateTimeLocalToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form (in local time): YEAR/MONTH/DAY,HH:MM:SS.MMM.
Definition: datetime.cpp:274
This "virtual driver" encapsulates a NTRIP client (see CNTRIPClient) but adds the functionality of du...
Definition: CNTRIPEmitter.h:54
#define ASSERTMSG_(f, __ERROR_MSG)
CSerialPort m_out_COM
The output serial port.
Definition: CNTRIPEmitter.h:62
size_t Read(void *Buffer, size_t Count)
Implements the virtual method responsible for reading from the stream - Unlike CStream::ReadBuffer, this method will not raise an exception on zero bytes read, as long as there is not any fatal error in the communications.



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018