Capture data from function(&stream)

I haven't worked much with streams, but I have a new function that works fine... writing out to Serial...Serial3

Passing a 'device' handle to a function(&stream), and it works fine - feeding the data out to 'stream'.

Now I want to pass a char array to the function, and have the streamed data put back into that array... function(&myArray)

Any help how to create the myArray stream object?

This is the source code in the Stream class to read into a String object

String Stream::readString()
{
  String ret;
  int c = timedRead();
  while (c >= 0)
  {
    ret += (char)c;
    c = timedRead();
  }
  return ret;
}

First line instantiates the new String object and then characters are added as you read

You could have a similar method (totally untested, just typed into my cell phone)

// =====================================================
// returns number of characters read. Need to pass a buffer that has bufferSize bytes available.
// functions will put the null char at the end of the buffer for a well formed c string
// for this to be meaningful, the bufferSize needs to be at least 2 (one char read and null termination)
// =====================================================
unsigned int myStreamClass::readcStringIntoCharArray(char * ret, unsigned int  bufferSize)
{
  int c;
  unsigned int nbChar = 0;

  If (ret && (bufferSize>1)) {
     c = timedRead();
     while (c >= 0)  {
        if (nbChar > bufferSize-2) nbChar = bufferSize-2;
        ret[nbChar++] = (char)c;
        c = timedRead();
     }
  }
  if(ret) ret[nbChar] = '\0';
  return nbChar;
}

Many thanks - will give that a kick in the morning... 9pm here, and I've been clicking on other stuff all day !
Easier than I expected! Hope it works like 'so'!

to make it supper simple you could add that as a public method of the Stream library if you want it. source code for this class is here

on my computer that file is at /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Stream.cpp

J-M-L:
to make it supper simple you could add that as a public method of the Stream library

Stream has a function readBytes

size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer

pYro_65:
Stream has a function readBytes

indeed - it just does not add the '\0' at the end - which of course is not a big deal and you could do that yourself after calling readBytes without changing anything in the library.

As I said up the top - my experience coding with streams is minimal...
I played a bit, and didn't destroy anything, but did get some type conversion errors...

ASSUMING
I have created function(&myStream) {} - works fine.

And I call it with function(&Serial);, and it works fine...
Now I want to push that same data stream to a buffer char msgbuf[64];

QUESTION
How to construct the call - declaring msgbuf (inline?) as a stream sink?
e.g. function(&msgbuf);

Many thanks

You can use my PrintEx library for this. It has tools for attaching a Print interface to a

I have already answered pretty much the same issue here.

The linked answer uses PrintAdapter to convert a Print into a Stream, however if your function can be changed to accept a Print object, then you can simply pass the GString object as it implements Print.

This sounds like a job for sprintf() and its friends. There are lots of string handling functions that come in the standard C libraries. The Arduino reference doesn’t mention them because they are not necessary to make an LED blink. But they are still there inside Arduino and you don’t need to #include them.

See http://en.cppreference.com/w/c/string/byte to get started.

lastchancename:
I haven't worked much with streams, but I have a new function that works fine... writing out to Serial...Serial3

Passing a 'device' handle to a function(&stream), and it works fine - feeding the data out to 'stream'.

Now I want to pass a char array to the function, and have the streamed data put back into that array... function(&myArray)

Any help how to create the myArray stream object?

Are you ultimately wanting something like this: GitHub - krupski/Stdinout: Standard input/output/error support for Arduino

This little library lets you "connect" anything to the standard in/out/error streams.

For example, you can do this:

Serial.begin (9600);
STDIO.open (Serial);
fprintf (stdout, "Yippee!!! No more Serial.print tomfoolery!\n");

If so, feel free to grab a copy - and check out the other Arduino goodies in there too.

Thanks team...
I use C string functions a lot, but the magic here is that I want to call the same function from different places - one (existing) call simply pushes the output to Serial.

I modified the function to accept the stream destination as a param - no problem, now I was hoping to use the exact same function to populate a char array from another calling location without reworking the old code 'too much'

I'll have a look at the GitHub reference - thanks.
Edit: the GitHub example still uses stdio (serial) as the output. I'm trying to redirect the output from the 'call' to a buffer, not the buffer to some other existing stream destination.

lastchancename:
I modified the function to accept the stream destination as a param - no problem, now I was hoping to use the exact same function to populate a char array from another calling location without reworking the old code ‘too much’

Did you have a look at the example I linked in my post above, it’ll allow you to do what you need and all it requires is a few extra lines.

pYro_65:
Did you have a look at the example I linked in my post above, it'll allow you to do what you need and all it requires is a few extra lines.

OK - thanks _ I looked at PrintEx, which solves the problem...
I may look at your code and once everything else is neaat and tidy - extract & embed the important bits directly - rather than carry another library around.

Thanks for that.

lastchancename:
OK - thanks _ I looked at PrintEx, which solves the problem...
I may look at your code and once everything else is neaat and tidy - extract & embed the important bits directly - rather than carry another library around.

Thanks for that.

Remember it is a GPL licence, so you can take what you want, however you need to ensure there is appropriate comments/recognition of where it came from. Also my library is updated regularly, so you will end up missing out on bug fixes and improvements (IDE will inform you when a new version is available). If you look at the code on GitHub, there are 30+ commits in the develop branch which will soon become a part of the release version.

understood - thanks.
It may not get into the final project, but redirecting the monitoring data can be useful in the prototype... cheers.