arduino / Arduino

Arduino IDE 1.x
https://www.arduino.cc/en/software
Other
14.15k stars 7.01k forks source link

DTR Off blocks USB serial receive #9786

Open Kabron287 opened 4 years ago

Kabron287 commented 4 years ago

Board: Arduino Micro(ATMEGA32U4) Test Sketch:

void setup() {
    Serial.begin(115200);
    Serial1.begin(115200);  
}
void loop() {
    if (Serial.available()) {
        Serial1.write(Serial.read());
    }
    if (Serial1.available()) {
        Serial.write(Serial1.read());
    }
}

To reproduce the issue make LoopBack connection RX-TX open Terminal Emulator(e.g. CoolTerm) and begin to send something. Switch DTR state ON/OFF. If DTR OFF transmition stops. It does not correspond with ordinary USB to serial behavior, e.g. FT232 chip, which transmits and receves regardless DTR state.

The same test runs on Teensy 2.0 with no such issue.

Kabron287 commented 4 years ago

In fact it is done consciously. File: CDC.cpp 232

size_t Serial_::write(const uint8_t *buffer, size_t size)

{
    /* only try to send bytes if the high-level CDC connection itself 
     is open (not just the pipe) - the OS should set lineState when the port
     is opened and clear lineState when the port is closed.
     bytes sent before the user opens the connection or after
     the connection is closed are lost - just like with a UART. */

    // TODO - ZE - check behavior on different OSes and test what happens if an
    // open connection isn't broken cleanly (cable is yanked out, host dies
    // or locks up, or host virtual serial port hangs)
////    if (_usbLineInfo.lineState > 0) 
    {
        int r = USB_Send(CDC_TX,buffer,size);
        if (r > 0) {
            return r;
        } else {
            setWriteError();
            return 0;
        }
    }
    setWriteError();
    return 0;
}

Commenting if( ... solved the problem. IMHO, this functionality should be left to the user's choice.