473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Output using ostreams on embedded hardware

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.
Apr 17 '07 #1
2 2344
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::overflow( int_type )

and output your characters to where ever you please.

--
OU
Apr 17 '07 #2
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::overflow( 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::off_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>::overflow(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
Apr 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sizer | last post by:
We make embedded devices that are basically wimpy linux boxes with small custom display/touchscreen heads on them. They're not running X or any other windowing system. All development is done with...
12
by: Matt Garman | last post by:
I'd like to create a "custom output facility". In other words, I want an object whose use is similar to std::cout/std::cerr, but offers more flexibility. Instead of simply writing the parameter...
12
by: Bill Hanna | last post by:
C is inadequate for embedded systems for the following reasons: 1) Direct addressing of memory mapped I/O is not supported simply and easily. You have to find work-arounds that are compiler...
3
by: Erik Cato | last post by:
Hi group! I'm trying to create at strategy for writing software modules. I'm writing code for embedded systems but this is not significant for the solution. What I'm trying to do is construct...
20
by: mohd hisham | last post by:
Write a C program to construct a queue of integers and to perform the following operations on it: a. insert b. delete c. display The program should print appropriate messages for stack overflow...
6
by: agnivesh | last post by:
Hi there, I'm a beginner in C programming and want to learn how to write C (pure C) code to access hardware ports (serial,parallel,usb etc.) under windows environment ? 1. Please, suggest any...
15
by: may_2005 | last post by:
Hello, I am working on a piece of (open source) hardware, which needs a certain amount of (open source) embedded C code developed for the hardware to perform a useful task. I am looking for...
9
by: Paulo Matos | last post by:
Hi all, Given a list of int, it's rather easy to output them line by line: std::copy(mylist.begin(), mylist.end(), std::ostream_iterator<int>(cout, "\n"); Now, there's two ways in which I...
6
by: drhilbert | last post by:
Dear all, in these days, we have found a problem we can't solve even after long long googling, so we are here asking your precious help. In our program instead of using cout to print messages...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.