Output using ostreams on embedded hardware

Discussion in 'C++' started by Thormod Johansen, Apr 17, 2007.

  1. 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.
     
    Thormod Johansen, Apr 17, 2007
    #1
    1. Advertising

  2. 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.

    --
    OU
     
    Obnoxious User, Apr 17, 2007
    #2
    1. Advertising

  3. 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::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
     
    Michael DOUBEZ, Apr 19, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc Schellens
    Replies:
    8
    Views:
    3,040
    John Harrison
    Jul 15, 2003
  2. Roger Leigh

    ternary operator and ostreams

    Roger Leigh, Jan 16, 2004, in forum: C++
    Replies:
    6
    Views:
    670
    Roger Leigh
    Jan 19, 2004
  3. Russ Ford

    ostreams and vector<vector>s

    Russ Ford, Sep 6, 2004, in forum: C++
    Replies:
    2
    Views:
    464
    Jonathan Turkanis
    Sep 6, 2004
  4. Colin Paul Gloster
    Replies:
    48
    Views:
    1,911
    Colin Paul Gloster
    Apr 10, 2007
  5. Juan A. Gomez-Pulido
    Replies:
    0
    Views:
    1,594
    Juan A. Gomez-Pulido
    Aug 24, 2009
Loading...

Share This Page