Pages: [1] 2   Go Down
Author Topic: How to detect if the board is connected to PC?  (Read 1634 times)
0 Members and 1 Guest are viewing this topic.
USA, OH
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

USA, OH
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: July 06, 2012, 12:31:56 pm by Stan09 » Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12577
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

USA, OH
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes it is.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6137
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

USA, OH
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

USA, OH
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

USA, OH
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

« Last Edit: July 07, 2012, 01:38:30 am by Stan09 » Logged

Pages: [1] 2   Go Up
Jump to: