Before I re-invent the wheel, does anyone know of an existing library that does this?
It is not a library, but the serial input basics tutorial (Serial Input Basics - updated - Introductory Tutorials - Arduino Forum) will show how to read serial data.
No you misunderstand what I mean.
What I want to do is more advanced than just reading from existing hardware serial etc.
I want to use a 'c' string as a source of serial data, with the same functions that hardware serial has.
I thought there might have been a library for it but I have searched and searched and there does not seem to be.
So I guess I will have to implement a new serial class myself.
just a reminder of some basic computer science basics.....
once something is sent through a connection such as serial or wifi any standard connection can ONLY send numbers from 0 to 255.
words can get across a connection because both the sender and receiver both encode and decode the numbers into characters the same way.
once characters are sent, what is important when reading is that they are both sent using ASCII encoding.
its irrelevant weather they came from a string variable or character array when the receiver reads the string.
If you have a cstring such as
char myString = "hello world";
then you can iterate over it with code like this
for (n = 0; n < len(myString); n++) {
nextChar = myString[n];
}
Is that what you want to do?
If not please provide a clearer description.
...R
I have CStringBuilder using Print class. You want enhance the Stream class functions. you must implement the pure virtual functions of Stream.
virtual size_t write(uint8_t) = 0;
virtual int available() = 0;
virtual int read() = 0;
virtual int peek() = 0;
Something like this?
class CharStream : Stream
{
public:
CharStream(const char *contents)
{
cp = (char *)contents;
};
void begin(const char *contents)
{
cp = (char *)contents;
};
int available()
{
return (cp) ? strlen(cp) : 0;
};
int read()
{
if (cp && *cp) // Pointer is not NULL and doesn't point to a NULL
return *cp++;
else
return -1;
};
int peek()
{
if (cp && *cp) // Pointer is not NULL and doesn't point to a NULL
return *cp;
else
return -1;
};
size_t write(uint8_t)
{
return 0;
};
private:
char *cp;
};
CharStream CStream("This is the contents value.");
void setup()
{
Serial.begin(115200);
while (CStream.available())
Serial.print((char)CStream.read());
Serial.println();
CStream.begin("Here we replace the value with a new value.");
while (CStream.available())
Serial.print((char)CStream.read());
Serial.println();
}
void loop() {}
johnwasser:
Something like this?class CharStream : Stream
{
public:
CharStream(const char *contents)
{
cp = (char *)contents;
};
void begin(const char *contents)
{
cp = (char *)contents;
};
int available()
{
return (cp) ? strlen(cp) : 0;
};
int read()
{
if (cp && *cp) // Pointer is not NULL and doesn't point to a NULL
return *cp++;
else
return -1;
};
int peek()
{
if (cp && *cp) // Pointer is not NULL and doesn't point to a NULL
return *cp;
else
return -1;
};
size_t write(uint8_t)
{
return 0;
};
private:
char *cp;
};
CharStream CStream("This is the contents value.");
void setup()
{
Serial.begin(115200);
while (CStream.available())
Serial.print((char)CStream.read());
Serial.println();
CStream.begin("Here we replace the value with a new value.");
while (CStream.available())
Serial.print((char)CStream.read());
Serial.println();
}
void loop() {}
try it with find(), readBytesUntil() or parseInt()
It's still unclear to me how this will be useful.
John's code answers the OP's question nicely but I don't see the reason for the original request either.
It seems like just using a char pointer and walking through the string until a control character or NULL is seen can be done without having to emulate the Serial lib.
Blackfin:
John's code answers the OP's question nicely but I don't see the reason for the original request either.It seems like just using a char pointer and walking through the string until a control character or NULL is seen can be done without having to emulate the Serial lib.
to somehow simulate input for a function which takes Stream as parameter?
Juraj:
try it with find(), readBytesUntil() or parseInt()
Sorry, I guess I should have written:
class CharStream : public Stream
Juraj:
try it with find(), readBytesUntil() or parseInt()
I guess this is where it's appropriate to use String class.
arduino_new:
I guess this is where it's appropriate to use String class.
Nope. The "String" class has nothing to do with it. I just failed to make Stream a public parent class so some of the features of Stream (not String) could not be used.