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'!
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);
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.
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.
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.