Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Output using ostreams on embedded hardware

Reply
Thread Tools

Output using ostreams on embedded hardware

 
 
Thormod Johansen
Guest
Posts: n/a
 
      04-17-2007
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.


 
Reply With Quote
 
 
 
 
Obnoxious User
Guest
Posts: n/a
 
      04-17-2007
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:verflow( int_type )

and output your characters to where ever you please.

--
OU
 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      04-19-2007
Obnoxious User a écrit :
> 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: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:ff_type off_type;
typedef typename traits_type:os_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>: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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to display images embedded in e-mail as embedded, not attachments Jim Firefox 4 12-11-2004 05:36 AM
ostreams and vector<vector>s Russ Ford C++ 2 09-06-2004 06:16 PM
ternary operator and ostreams Roger Leigh C++ 6 01-19-2004 07:02 PM
Embedded TCL in JSP produces no output using BSF Phil Powell Java 1 11-26-2003 02:21 AM
Re: ostreams, ios::binary, endian, mixed binary-ascii Marc Schellens C++ 8 07-15-2003 12:27 PM



Advertisments