Serial
write()
Writes binary data to the serial port. This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function instead.
// PROTOTYPES
virtual size_t write(uint8_t);
size_t write(uint16_t);
virtual size_t write(const uint8_t *buffer, size_t size);
// SYNTAX
Serial.write(val);
Serial.write(str);
Serial.write(buf, len);
// EXAMPLE USAGE
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.write(45); // send a byte with the value 45
int bytesSent = Serial.write(“hello”); //send the string “hello” and return the length of the string.
}
Parameters:
val: a value to send as a single bytestr: a string to send as a series of bytesbuf: an array to send as a series of byteslen: the length of the buffer
write() will return the number of bytes written, though reading that number is optional.
Since 6.5.0:
On hardware UART ports (Serial1, etc.) and USB serial (Serial, USBSerial1), the Serial.write(buf, len) form transfers the buffer directly instead of writing it one byte at a time, which is significantly faster for larger buffers. It honors blockOnOverrun(): with blocking enabled (the default), the call waits for room in the buffer as needed until all of len bytes have been written; with blocking disabled, it writes as many bytes as currently fit and returns immediately, so the returned count may be less than len.