Skip to content

Print not aborting when Write(char) returns Error #3614

Closed
@stickbreaker

Description

@stickbreaker

the Print::write(const uint8_t *buffer, size_t size) and Print::print(const __FlashStringHelper *ifsh) continue calling write(char) after write(char) has returned a 0 (zero) to mark a write failure. This creates a unexpected result when a block of characters is printed.
I implemented cts/rts handshaking in HardwareSerial, if !cts and the tx buffer is full and a timeout has occured, the HardwareSerial::write(char) returns 0(zero) to mark the failure. The Current Print object continues sending until the buffer has been send to write(char) if buffer space becomes free or cts clears, the write() failed characters are lost, but the rest of the buffer is sent. Since Print functions returns the number of characters sent, the expectation is that if len!=sent, the last (len-sent) characters were not sent. This expectation is incorrect. With the current Print library if len!=sent, there is no way to identify which character were not sent!
Included is the simple fix for this problem.

// from Arduino\hardware\avr\cores\arduino\Print.cpp 

// Public Methods //////////////////////////////////////////////////////////////

/* default implementation: may be overridden */
size_t Print::write(const uint8_t *buffer, size_t size)
{
  size_t n = 0;
  while (size--) {
// abort on write() failure, Chuck Todd
    if(write(*buffer++))n++; 
    else break;  
 //   n += write(*buffer++); original code 1.6.5
  }
  return n;
}

size_t Print::print(const __FlashStringHelper *ifsh)
{
  PGM_P p = reinterpret_cast<PGM_P>(ifsh);
  size_t n = 0;
  while (1) {
    unsigned char c = pgm_read_byte(p++);
    if (c == 0) break;
// abort on write() failure, Chuck Todd
    if(write(c)) n++; 
    else break;    
//   n += write(c); original code 1.6.5
  }
  return n;
}

Chuck Todd

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions