Output using ostreams on embedded hardware

T

Thormod Johansen

Hi,

I am doing some embedded programming in Paradigm C++, and I would like to be
able to output debugging information using the << operator and ostreams. I
have a serial link for which I have low level code to output one character
at a time. I hope to be able to write code the following way:

sout << "Debugging info: x = " << x << '\r\n';

Where "sout" is an ostream object somehow using the serial connection.

How do one do this? I have an idea about assigning the ostream to an
streambuf object. Is it the streambuf object which is in charge of doing low
level outputting or how does it work?

Thanks in advance.
 
O

Obnoxious User

Thormod Johansen skrev:
Hi,

I am doing some embedded programming in Paradigm C++, and I would like to be
able to output debugging information using the << operator and ostreams. I
have a serial link for which I have low level code to output one character
at a time. I hope to be able to write code the following way:

sout << "Debugging info: x = " << x << '\r\n';

Where "sout" is an ostream object somehow using the serial connection.

How do one do this? I have an idea about assigning the ostream to an
streambuf object. Is it the streambuf object which is in charge of doing low
level outputting or how does it work?

Derive your own stream buffer from std::streambuf and override

virtual int_type std::streambuf::eek:verflow( int_type )

and output your characters to where ever you please.
 
M

Michael DOUBEZ

Obnoxious User a écrit :
Thormod Johansen skrev:

Derive your own stream buffer from std::streambuf and override

virtual int_type std::streambuf::eek:verflow( int_type )

and output your characters to where ever you please.

The class inheriting from stream buffer is:
//! Class template for stream buffer.
template <typename CharT, typename Traits = std::char_traits<CharT> >
class basic_serialstreambuf :
public std::basic_streambuf<CharT, Traits>
{
public:
// traits
typedef CharT char_type;
typedef Traits traits_type;
typedef typename traits_type::int_type int_type;
typedef typename traits_type::eek:ff_type off_type;
typedef typename traits_type::pos_type pos_type;
//..
protected:
//! Transfer character buffer overflows
int_type overflow(int_type c);
//...
};

Your function will look something like the following

template <typename C, typename T>
typename basic_serialstreambuf<C,T>::int_type
basic_serialstreambuf<C,T>::eek:verflow(int_type c)
{ //There is data to write into UART
const std::streamsize count = this->pptr() - this->pbase();
//Write data - as much as you can
const std::streamsize written = ...;
if( written == 0 )
{ //cannot send and no room
return traits_type::eof();
}
//pop written data
this->pbump(-written);
//there is now room for new char
if (!traits_type::eq_int_type(c, traits_type::eof()))
{ //enqueue char for buffering
//(you can also directly send it)
return this->sputc(c);
}
else
{
return traits_type::not_eof(c);
}
}

If your UART device has asynchrounous send (though rotating buffer), you
can also override the sync() member to tell him to send data.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top