https://forum.arduino.cc/index.php?topic=547012.msg3730009#msg3730009
what the difference in this particular case if we will use just name of variable instead of &b
void serial_flush() {
byte b;
while (Serial.readBytes(b, 1));
}
https://forum.arduino.cc/index.php?topic=547012.msg3730009#msg3730009
what the difference in this particular case if we will use just name of variable instead of &b
void serial_flush() {
byte b;
while (Serial.readBytes(b, 1));
}
answer is - because readBytes as input value expecting address of array... and in this particular case we send it address of simple variable.. if we will use just "b" we will send it value of b
correct?
indeed, this is not correct while (Serial.readBytes(b, 1));===> you need a pointer to the memory location, so &b
if will give you the number of bytes read, so here 0 or 1. The while() will exit if it's 0, so when you've emptied the buffer.
since you don't do anything with b, you could just write while (Serial.read() != -1);and no need for b at all.
doing
Serial.end();
Serial.begin(115200); // whatever the baud rate was
will also empty the incoming buffer
PS: the flush term is more used to empty the outgoing buffer, not the incoming one.
It would be nice if the Processing stuff had more examples and documentation... The reference doesn't say it's looking for an address.
Agreed - the doc states
buffer: the buffer to store the bytes in. Allowed data types: array of char or byte.
which is confusing, you could get to believe a byte is fine when what they mean is array of char or array of byte
as always, the source code is the ultimate documentation ![]()
size_t Stream::readBytes(char *buffer, size_t length)
J-M-L:
indeed, this is not correctwhile (Serial.readBytes(b, 1));===> you need a pointer to the memory location, so &b
if will give you the number of bytes read, so here 0 or 1. The while() will exit if it's 0, so when you've emptied the buffer.since you don't do anything with b, you could just write
while (Serial.read() != -1);and no need for b at all.
readBytes waits for next byte until timeout and that was the point in that thread.
J-M-L:
Agreed - the doc stateswhich is confusing, you could get to believe a byte is fine when what they mean is array of char or array of byteas always, the source code is the ultimate documentation
size_t Stream::readBytes(char *buffer, size_t length)
so everything is alright with &b because it is char*
readBytes waits for next byte until timeout.
fair enough indeed. if you just read you won't wait for the timeout
so everything is alright with &b because it is char*
not in OP's post #1
J-M-L:
fair enough indeed. if you just read you won't wait for the timeout
not in OP's post #1
did you read the post linked by OP?
if the sender (Serial Monitor or other device) sends strings with pause between them, then reading with the right timeout does the trick to handle the gaps caused by slow baud rate. yes it waits after the last character some milliseconds, but it doesn't stop reading after reading out the receive buffer
you might have misread what I meant (I was unclear)
I was commenting on my own proposedwhile (Serial.read() != -1);and saying that this code won't wait for the timeout
yes. my answer #2.
indeed, is not correct, we have used readBytes because problem is described in post linked by OP...
when we dont want to to be ensure that transmission is end then yes we could use Serial.read
[Juraj] can answer more correctly ![]()
Thank you all
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.