How to detect if the board is connected to PC?

The board is collecting data from some sensor. I want it transmit the data to PC with Serial if it is connected to PC; if there is no connection, it should switch to logging data on SD card. How I could detect programmatically if serial connection is available? I see there is if(serial) function that does the thing, but only for Leonardo. How to achieve something like that with Duemilanove? There is an obvious solution for this - like, make PC to send some response when serial bytes arrive, and detect this response wit Arduino program; I don't want to use this approach. There might be also a possible solution with detecting power voltage, it looks rather unreliable and also requires some hardware arrangements. Are there any programmatically only alternatives?

How I could detect programmatically if serial connection is available?

The data needs to be sent to some application. Make that application send a specific response to a question like "Ready?". If you get the appropriate response, within some time frame, send the data. Otherwise, log it.

There is an obvious solution for this - like, make PC to send some response when serial bytes arrive, and detect this response wit Arduino program; I don't want to use this approach.

Why?

There might be also a possible solution with detecting power voltage, it looks rather unreliable and also requires some hardware arrangements.

There is nothing in the USB connection, if that is what you are thinking of, that defines whether the USB connector is receiving power only, or power and there is something on the other end of the serial port. The USB to serial and serial to USB is handled by a separate chip that the Arduino can not access (for obvious reasons).

PaulS:

There is an obvious solution for this - like, make PC to send some response when serial bytes arrive, and detect this response with Arduino program; I don't want to use this approach.

Why?

Can you imaging a generic application that just listens to serial port and handles data regardless of what device is sending it?

PaulS: There is nothing in the USB connection, if that is what you are thinking of, that defines whether the USB connector is receiving power only, or power and there is something on the other end of the serial port.

If USB is connected this is enough for me to know the board is connected to PC and to start serial communications. http://arduino.cc/forum/index.php?topic=107860.0

Is there any way to tell whether the Serial output buffer is full? I haven't looked into how it works, but I'm guessing there's some sort of FIFO within the Arduino runtime. That might offer a way to tell whether the Serial stream is actually moving.

PeterH: Is there any way to tell whether the Serial output buffer is full? I haven't looked into how it works, but I'm guessing there's some sort of FIFO within the Arduino runtime. That might offer a way to tell whether the Serial stream is actually moving.

Since Serial doesn't have any handshaking, there's no way to tell if the usb chip is actually reading the stream.

If USB is connected this is enough for me to know the board is connected to PC and to start serial communications.

No. it isn't.

Yes it is.

I'll send you one of the "USB cables" that I have, then, that has just two wires - +V and GND. Connect that one up, and send serial data. I seriously doubt that it sill get through, although the power is there.

How do you intend to detect the presence of USB power?

Stan09: Yes it is.

So if USB is connected and your Arduino starts sending data, your PC will know what to do with it automatically?

[quote author=James C4S link=topic=113169.msg851508#msg851508 date=1341633253]

Stan09: Yes it is.

So if USB is connected and your Arduino starts sending data, your PC will know what to do with it automatically? [/quote] What is most important, in such a case my Ard program will not start logging data on SD card.

I don't care for that sort of argument; I asked a question and it looks like there is no answer to it except but 'no can do'.

Can you imaging a generic application that just listens to serial port and handles data regardless of what device is sending it?

Actually, no, I can't. Handle is such a vague term, and most likely the application would not "handle" the data in a way that I would need it to.

Is meaningless trolling how you’ve got your 23 posts?
Stop it please, I hate trolling.

You could try connecting an unused pin on the atmega328 to an unused pin on the atmega8u2 and then reprogramming the 8u2 to write it HIGH when it gets a usb connection.

The simplest solution is to get your logging program to just send a character when it starts up.

Be reasonable, he's trying to help you. Detecting the presence of power is no guarantee that the PC is running the application you want. Also the buffer doesn't fill up just because there is no program at the other end reading it.

You haven't answered the reasonable question: why not send a message to the Arduino to tell it so start transmitting? That is the reliable way. Of course you need to send another message to tell it to stop later.

PaulS has a high post count because he helps a lot of people. Some aren't very helpful back. For example, they ignore his questions.

Regarding your question, “why not send a message to the Arduino”. Honest, I was sure I said enough to explain it, well, if not, here is my more detailed explanation to you:
I have a program that receives data from COM port (without handshake) and “handles” input stream in a special way - it displays received numbers as a long diagram, that is, ECG (not necessarily, it might be EEG, or EMG, or what not), applies different filtration and peak detection algorithms and many more. ‘Handles’ means ‘it does a lot of things with’, is it more clear that way?
I can configure input port parameters - port number, speed etc, but I cannot modify program code to generate any “data transfer request”, this program acts as a passive receiver.
I am using arduino board for interfacing different sensors with that program; it is also important to provide a possibility for Ard board to accumulate data in standalone mode, without connecting to PC. So if my code could determine that there is something , anything connected to USB port, then it would transmit sensor data; if not - data is logged on SD card.
I’m afraid I can’t explain this obvious requirement more clear; besides, this is probably not that important. Point is, I am relatively new to this particular board and I had some sort of illusion there might be some hidden tricks I am not aware of.

I think there was a thread a while back about detecting if you are connected to USB or not. Something to do with checking the voltage level of Vin. That may help.

I should point out that, unless you modify things a bit, connecting to the board via serial comms tends to assert RTS which resets the board. You may need to take that into account.

Another possibility is to have a switch or two on the board. Hit a button and it sends its data.

[quote author=Nick Gammon link=topic=113169.msg851604#msg851604 date=1341644963] I think there was a thread a while back about detecting if you are connected to USB or not. Something to do with checking the voltage level of Vin. That may help. [/quote] and that was exactly the post I was referring to in my first reply ... in this thread. I can refer to it again: http://arduino.cc/forum/index.php?topic=107860.0

Moderator edit: Insult deleted.

Why are you asking then? Even so, I don't think it will be particularly reliable. Merely having power doesn't guarantee anything. I think my suggestion of a button you push when everything is connected, and the receiving program is ready, would be better.

Nick, if you could bother yourself with reading the original post, you could have noticed I was asking for solutions other then two most obvious ones. And with obvious reason as well: first one - I cannot modify program code, second one - hardly reliable.

Keep on insulting people, and see what happens.