Go Down

Topic: How to detect if the board is connected to PC? (Read 2050 times) previous topic - next topic

Stan09

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?

PaulS

Quote
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.

Quote
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?

Quote
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).

Stan09

#2
Jul 06, 2012, 07:23 pm Last Edit: Jul 06, 2012, 07:31 pm by Stan09 Reason: 1

Quote
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?

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

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.
I only provide help via the forum - please do not contact me for private consultancy.

WizenedEE


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.

PaulS

Quote
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.


PaulS

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?

James C4S


Yes it is.

So if USB is connected and your Arduino starts sending data, your PC will know what to do with it automatically?
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

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?

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'.

PaulS

Quote
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.

Stan09

Is meaningless trolling how you've got your 23<something> posts?
Stop it please, I hate trolling.

WizenedEE

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.

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Stan09

#14
Jul 07, 2012, 08:30 am Last Edit: Jul 07, 2012, 08:38 am by Stan09 Reason: 1

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.


Go Up