Skip to content

Commit b685aef

Browse files
committed
added Boolean operators to HardwareSerial and CDC to test whether the port is ready to send data.
Mostly useful for Leonardo - simple way to test whether the port is actually opened by an application and ready to receive data. For Serial objects attached to real UARTs always returns true.
1 parent 232c780 commit b685aef

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

hardware/arduino/cores/arduino/CDC.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ size_t Serial_::write(uint8_t c)
213213
return 0;
214214
}
215215

216+
Serial_::operator bool() {
217+
if (_usbLineInfo.lineState > 0)
218+
return true;
219+
return false;
220+
}
221+
216222
Serial_ Serial;
217223

218224
#endif

hardware/arduino/cores/arduino/HardwareSerial.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,10 @@ size_t HardwareSerial::write(uint8_t c)
398398
return 1;
399399
}
400400

401+
HardwareSerial::operator bool() {
402+
return true;
403+
}
404+
401405
// Preinstantiate Objects //////////////////////////////////////////////////////
402406

403407
#if defined(UBRRH) && defined(UBRRL)

hardware/arduino/cores/arduino/HardwareSerial.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class HardwareSerial : public Stream
5757
virtual void flush(void);
5858
virtual size_t write(uint8_t);
5959
using Print::write; // pull in write(str) and write(buf, size) from Print
60+
operator bool();
6061
};
6162

6263
#if defined(UBRRH) || defined(UBRR0H)

hardware/arduino/cores/arduino/USBAPI.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Serial_ : public Stream
3939
virtual int read(void);
4040
virtual void flush(void);
4141
virtual size_t write(uint8_t);
42+
operator bool();
4243
};
4344
extern Serial_ Serial;
4445

0 commit comments

Comments
 (0)