Arduino Due: 'class Print' has no member named 'availableForWrite'

'class Print' has no member named 'availableForWrite'

^^^ This is the error we get when trying to use 'Print::availableForWrite' from Arduino Due API headers.

By doing a search on the Net I found a couple of suggestions that this is a matter of historical API version. E.g. older version of the API did not have 'availableForWrite' in 'Print' and the newer one does. That's perfectly fine. However, if that is the case, then I'd like to know which globally defined macro does Arduino API provide to distinguish between API versions. I.e. I want to know how to do something like

#if ARDUINO_API_MAJOR_VERSION > 5
...
#endif

What is the macro name for that?

If, on the other hand, this is not a API versioning issue but simply a botched API implementation for Arduino Due, then I want it fixed immediately. In that case my question is different: where do I submit an urgent fix request? (I can't wait for a general release of the fix, so I'll want Arduino support to make an immediate fix just for me and then, at some later time, release it to the general public).

But first and foremost, again: what is root cause here? An older historical API? Or a poor quality implementation?

Thank you.

Welcome to the forum.

Most of us here are volunteers that help user that want to learn electronics.

Could you be so kind and provide a complete and short example that demonstrates the issue? Please use code tags. It should look like this.

// Your example source code

You need three ' at the beginning and end of your source code in the edit window? When you click on this icon </> you get.

```
type or paste code here
```

Maybe you bought an insane number of Arduinos and now you are a special customer. Could you provide some information that would allow us to call the Arduino team?

Most of the time, issues get fixed in the forum. When a bug is suspected we post it on GitHub. It then gets looked at by the people who maintain the code. Some are from Arduino and others are contributors from all over the world.

Well, the error pops up in a third-party library code, which assumes that public interface of Print class is supposed to have availableForWrite function. I don't see a reason to dump that library code here, but I can make an artificial example that illustrates the matter

void foo(Print *print)
{
  print->println(print->availableForWrite());
}

void setup()
{
  Serial.begin(115200);
  while (!Serial);

  foo(&Serial);
}

void loop()
{
}

The above code will compile perfectly fine for AVR-based Arduino boards as well as for Seeeduino boards. But it will fail to compile for Arduino Due with an error quoted in my original post.

Now, it might be that the third-party library is actually at fault here. I see that Arduino docs declare presence of this method in Serial, but say nothing like that about Print. The authors of that library probably looked through <Print.h>, saw availableForWrite there and assumed that it is an official part of the public interface, while in reality it is not.

So, is it supposed to be really? If not, then I apologize for a false alarm: the API is not to blame, the third-party lib is at fault here.

OK, here is what I found.

In the following file for the Arduino Nano Every (megaAVR)

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.7\cores\arduino\api\Print.h

The print class has a virtual function

// default to zero, meaning "a single write may block"
// should be overriden by subclasses with buffering
virtual int availableForWrite() { return 0; }

This is missing in

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.12\cores\arduino\Print.h

I am not a C++ expert, but if I understand this correctly the actual implementation comes from the CDC.cpp (USB sub folder) for USB Serial or UARTClass.cpp (same folder) for UART serial which overwrite the virtual function. Both classes have the availableForWrite function implemented.

So, the following code works.

void setup()
{
  Serial.begin(115200);
  while (!Serial);

  Serial.println( Serial.availableForWrite() );
}

void loop()
{
}

When I added the virtual function to the Print.h for the Arduino Due. Your code compiled without any issue and it calls the function from the CDC class because the value I get is 127.

Does that fix work for you?

If you like, I can create a GitHub issue and ask if that can be implemented as a general solution. Could you provide the name of the library that is affected? It may help convince the maintainer to apply the fix. They may have a good reason why that was removed.

@Klaus_K problem is the Arduino SAM core. Arduino should fix the core

That fix will obviously "work" for me, but modifying API library files is only an option as a temporary solution if the official fix is coming. Otherwise, it would be completely unacceptable.

So the question is, again: what is the design intent? Where exactly (how high) in the polymorphic hierarchy of Arduino API classes is availableForWriting supposed to appear for the first time? Most versions of the API make it available in Print, while the version for Due suddenly drops it much lower into Serial.

I don't seem to be able to find any documentation that would clarify this matter. All I can find is books like this one, which also seem to support the idea that availableForWriting is intended to be a polymorphic function in Print, implying that Arduino API implementation for Due is broken.

I would recommend you create an issue on GitHub and ask nicely. Let's see what the Arduino team says. Here is a link to the GitHub repository.