The documentation says:
Serial.readBytes() returns the number of characters placed in the buffer. A 0 means no valid data was found.
I wrote this code:
lnLen = Serial.readBytes();
But I got this error when compiling:
no matching function for call to 'HardwareSerial::readBytes()'
Am I using the function wrong?
Thanks
Serial.available()
Description
Get the number of bytes (characters) available for reading from the serial port. This is data thatβs already arrived and stored in the serial receive buffer (which holds 64 bytes).
Read the documentation more carefully
Serial.readBytes() takes 2 parameters. The first an array into which the bytes are put and the second the number of bytes to read
Syntax
Serial.readBytes(buffer, length)
Parameters
Serial
: serial port object. See the list of available serial ports for each board on the Serial main page.
buffer
: the buffer to store the bytes in. Allowed data types: array of char
or byte
.
length
: the number of bytes to read. Allowed data types: int
.
I think it can be a little confusing that we often use the "()
" suffix on an identifier to indicate we are referring to a function, even when the function takes parameters. This is something I have often wondered about while writing this sort of thing.
It would be a lot of work to go look up the parameter names of a function every time we wanted to refer to one in casual conversation here on the forum. Trying to refer to a set of overloaded functions in this manner would not be fun, and likely to cause even more confusion in the reader. So I think the reasonable alternative is to write it like "the Serial.readBytes
function". I do sometimes employ that phrasing, but I'm not so happy with the way it looks in this particular case of a member function. I like it better with a standard function (e.g., "the digitalWrite
function").
Any thoughts on this?
When I refer to functions in replies I invariably use brackets as it looks wrong without them and the Arduino reference takes the same approach but has the luxury of going on to explain the parameters and return value
With an error message such as
no matching function for call to 'HardwareSerial::readBytes()
my first port of call would be to search for an example of its use, but If readers of the reference take no note of the parameters then there is very little to be done I am afraid,
Let us see how we can use the Serial.readBytes(arg1, arg2) function to read this string : "Forum" coming from InputBox of Serial Monitor (Fig-1) and save it in a buffer named myBuf[] under Arduino UNO Platform.
Figure-1:
1. Function prototype: Serial.readBytes(arg1, arg2);
2. Function usage: Serial.readBytes(bufName, nBytes);
where:
bufName = Name of the buffer into which received data bytes would be saved : myBuf[ ]
nBytes: Maximum number of data bytes to be read before time out occurs : 10
3. Actual function: Serial.readBytes(myBuf, 10);
4. Working priciple: Subject to the availability of data bytes in the serial buffer of the UART interface, the function keeps reading the arrived data bytes and storing them in the declared buffer until the following conditions are met: (The function also returns a value that says how many data bytes have been actually stored in the buffer.)
(1) 10 data bytes (charcaters) are read/stored.
(2) Time gap between arrivals of successive data bytes is more than 1-sec (default)
5. The Sketch
char myBuf[10];
void setup()
{
Serial.begin(9600);
}
void loop()
{
byte n = Serial.available();
if(n != 0)
{
byte m = Serial.readBytes(myBuf, 10);
myBuf[m] = '\0'; //inserting null-charcater
Serial.println(myBuf);
//-----------------------
memset(myBuf, 0x00, 10); //array elements are reset to 0s
}
}
6. Upload sketch of Step-5.
7. Open Serial Monitor with "Line ending tab" in "No line ending"option.
8. Enter Forum in the InputBox of Fig-1 and then click on Send button.
9. Check that Forum has appeared in the OutputBox of Fig-1.
10. Recheck the functionality of the sketch by enetering this string in the InputBox:
United Kingdom
The documentation says:
Serial.readBytes() returns the number of characters placed in the buffer. A 0 means no valid data was found.
Serial.readBytes() with no arguments, and "A 0 means no valid data was found." indicates to me that something ( a 0 or a higher number) will be returned.
Shouldn't the documentation be changed then to avoid this confusion?
Thanks.
GolamMostafa, thanks for your nice diagram and code.
Question, why did you put the '\0' at the end of the line?
Thanks.
You are quoting selectively and out of context
The syntax is stated quite clearly in the reference
Syntax
Serial.readBytes(buffer, length)
as are the required parameters
Parameters
Serial
: serial port object. See the list of available serial ports for each board on the Serial main page.
buffer
: the buffer to store the bytes in. Allowed data types: array of char
or byte
.
length
: the number of bytes to read. Allowed data types: int
.
and the return value
Returns
The number of bytes placed in the buffer. Data type: size_t
.
You need to read all the information and examples of this link Serial.readBytes() - Arduino Reference to realize that the text you have posted are correct. So, there is no need to chnage the documentation of the Arduino Reference Manual.
1. I have declared the following charcater type buffer (actually it is an array) which is not initialized.
char myBuf[10];
2. ASCII formatted data bytes coming from the InputBox of Serial Monitor are being saved into the buffer of Step-1.
3. If we want to print the content of a charcater type buffer, we must put (a requirement) 0x00 (the ASCII Code of NULL-character) as the last element of the array named myBuf[ ]. The C-code for the NULL-charcater is the following:
'\0'
4. m indicates the number of data bytes/characters that have been stored in the buffer. Therefore, m must the location which is empty where the NULL-character should go. Accordinngly, I have included the foollowing code in the sketch:
myBuf[m] = '\0';
OK, thanks.
I'll try to figure it all out.
BTW, I'm 78 years old and trying to keep my brain a little bit limber by programming.
I have been programming since 1976, but mostly with Assembly, Basic, Forth, and FoxPro; I've always tried to avoid any form of C, never liked the syntax.
I took a class one time for SQL Server and I think they mentioned a null at the end of some data. Completely forgot that.
Thanks again. Bye everyone until my next question.
@tonytscarpelli we use the brackets () only to indicate that it is a function. When we see that, we think "function".
@pert A member function is no problem at all. It looks fine to me.
Since new users should not be confused, I think that Ethernet.setRetransmissionTimeout
function is a good way. However the faint gray is no fun at all. I have to look a few times to see where the faint grey begins and where it ends, and there is nothing wrong with my monitor.
White = 255, 255, 255
Faint Gray
= 248, 248, 248
I like the colorful Github SVG badges. For example with a nice background color (I see "MediumAquaMarine" or "SteelBlue" when I think of a function).
βββββββββββββββ
βββββββββββββββ
What about a style for a function name with just a round-cornered border around the text ? That keeps the text as normal text, instead of a SVG embedded text.
Seeing a pair of parentheses () in front of an identified does not always meant that the identifier is a funtion. For a funtion, the necessary condition is this that the identifier must not be a keyword. For exabple:
for(); is not a function
fortyx(); is a function
for();
does not compile
I am now running 66+. I startred my hobbyist programming in 1988 on 6802, 8085, 8751 by pure hand coding as I had no access to any kind of assembler and continued for 10 years. In the meantime, I found MASM for 80x86 Microprocessor and statered MSDOS Programming where I first encountered the necessity of the insersion of 'string termination charcater' except which the the printing would never stop.
I started learning C through the conversions of the MSDOS codes into corresponding C codes for the UAPRT Port of the IBMPCAT.
C provides writing codes in understandable artificial English Language called C Programming Language. Few days attentive exercises offer more courage to learn C in better ways.
In Arduino, the IDE is very flexible to write codes in C and C++ though we need to write C++ codes rarely. The IDE gives the following four spaces:
Gloabl space to decalre global variables to be accessed by any function ofthe sketch
vois setup() //Setup Space to include codes that will be xecuted only one or for a definite number of times
{
}
void loop() //Loop space to inlude codes that will be executed repeatedly
{
}
User Space to hold user-defined functions
Example-1: Let us write codes to blink L (buit-in LED of UNO) at 1-sec inerval
1. We need to set direction of DPin-13 (L is connected with this DPin of UNO) as output using pinMode(arg1, arg2) function. This will be done only once; so, this code will go into setup() function.
2. One blink of L consists of the following 4 events:
Ignite L using this code: digitalWrite(13, HIGH);
Insert 1-sec time delay using: delay(1000);
Extinguish L using : digitalWrite(13, LOW);
Inseert time delay using: delay(1000);
Since, the L will be blinking continuously, the codes of Step-2 would find place in loop() function.
3. Here is the final sketch
//Global space
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
//User Space
Note that there no C or C++ codes in the above sketch. All functions are the built-in macros of Arduino IDE. The Example-2 shown below demonstrates the use of few C Programming syntaxes:
Example-2: Add two hex numbers 0x24 and 0x13 and show the result on the Serial Monitor.
void setup()
{
Serial.begin(9600); //this is not C code: It activates Serial Monitor
byte x1 = 0x24; //this is C code; variable or identifier x1 holds hex number 24
byte x2 = 0x13; //this C code
byte sum = x1 + x2; //this C code
Serial.print(sum, HEX); //this is not C code. It shows result on Serial Monitor
}
void loop()
{
}
Example-3: Write codes to receive two numbers interactively form the InputBox of the Serial Monitor (Fig-1), add them and show the result on the OutputBox of Serial Monior.
Figure-1:
Left as an exercise!
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.