Is a serial monitor attached?

Is it possible to detect if a serial monitor is actually attached on the computer connected to the Arduino USB? Determine if DTR control is high?

If a serial monitor is attached I want to output data to the serial port, if it is not connected I want to put the data to the SD card. The serial monitor can be detached/attached anytime whilst the Arduino program is running. I know that if the serial monitor is attached the Arduino program resets which is fine.

Using the Leonardo, yes, you can:

if (Serial) {
  // Your serial output here
}

For the Uno and other similar boards you would have to do a hardware modification - break the DTR->RESET connection and solder in a wire between the DTR output of the USB chip (before the capacitor) and an IO port of the main MCU. Of course, that then makes it harder to upload sketches as you have to manually reset the MCU at the right time to enter the bootloader.

You would need to add a wire to monitor the line - if it even changes.
DTR is programmed to only go low to create a reset when the USB is plugged in, or to start a download.
I think RTS also.
May not be able to get there.
Laptops, the monitor is always attached, yes?
Desktop, unplug yours and see what happens. I suspect nothing, pretty sure the coms from video card to the monitor is 1-way.

CrossRoads:
You would need to add a wire to monitor the line - if it even changes.
DTR is programmed to only go low to create a reset when the USB is plugged in, or to start a download.
I think RTS also.
May not be able to get there.
Laptops, the monitor is always attached, yes?
Desktop, unplug yours and see what happens. I suspect nothing, pretty sure the coms from video card to the monitor is 1-way.

I think you're getting confused. By "monitor" in this context he means the SERIAL monitor...!

And DTR (or bit 0 of the SET_LINE_STATE message) is set 1 when the serial port is opened, and RTS (or bit 1 of the SET_LINE_STATE message) is set 1 when the PC is able to receive (only meant to be actively used in half duplex communications).

Add a switch to the USB socket.

DavidOConnor:
Add a switch to the USB socket.

How would that help to know if the serial port is opened on the PC?

PEOPLE: READ THE FRELLING QUESTION, YOU’RE MAKING NO SENSE

For the Uno and other similar boards you would have to do a hardware modification - break the DTR->RESET connection and solder in a wire between the DTR output of the USB chip (before the capacitor) and an IO port of the main MCU. Of course, that then makes it harder to upload sketches as you have to manually reset the MCU at the right time to enter the bootloader.

I'm using a Mega 2560. Rather than break the DTR->RESET connection, can I just solder a wire to the USB CTS pin (PD7 pin 13 which goes to reset via c7) and connect this to an IO port of the main MCU and program the port for INPUT? Is there any need/reason to break the connection?

If you don't break the connection with reset, every time you open the serial port the board will reset. Do you really want that?

If you don't cut the connection (or somehow otherwise disable it) then you'll know that the serial port is open because the board will have just reset, so there'll be no point in the wire link.

I can live with the board being reset every time I open the serial port. Just because the board has reset doesn't mean there is a serial monitor attached. The board could have been reset from a power-on. Before I write to the serial port I want to know if there is a serial monitor connected. I use my own serial monitor I've written in c++ for Windows. I could do a software handshake between the Arduino program and my Windows monitor with a timeout for the Arduino ReadBytes. I'm not keen on using this approach as it blocks Arduino processing for the timeout delay time period if no serial monitor attached but it may be the only way as I've looked at the Arduino board and I really do not fancy soldering to the USB chip. I was hoping there was an easy way to monitor the serial handshake lines but there seems that there isn't. (Request for this feature in R4 boards please!).

Thanks for the help.

I had this problem with an application, and I didn't try to solve it by messing with the serial connection. I added a switch to my little device, connected to a digital input pin, if the switch is on, send ( or try to send ) the serial data to the computer, if the switch is off, then don't.

My understanding of the problem is that the OP wants his sketch to be able to detect whether output from the Arduino is currently visible on a PC screen, and if it is not, it will save data to an SD Card.

I suspect this is close to impossible unless you uses special software on the PC that maintains a conversation with the Arduino. (Which would be simple to do).

I don't think there is anything on the Arduino that automatically knows that anything is actively receiving data from Serial.print(). I have a project printing stuff every second for the last 10 months with nothing listening.

The business of Serial.begin() only gets the Arduino side of the conversation working - and I think that is also true of the Leonardo and Micro.

A physical switch will only be useful is the user remembers to operate it - believe me, they won't.

If you can't use a special PC program the best thing may be to write to the SD card and to the Serial Monitor all the time.

...R

I suspect this is close to impossible unless you uses special software on the PC that maintains a conversation with the Arduino. (Which would be simple to do).

This is the route that I'm now taking as I'm developing software both for the Arduino and the PC.

Thanks again.